/
create-load-manifest.js
68 lines (60 loc) 路 1.79 KB
/
create-load-manifest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module.exports = (assets, namedChunkGroups, isProd) => {
if (!isProd) return {};
/**
* This is a mapping of generic/pre-build filenames to their postbuild output
*
* bundle.js -> bundle.29bec.esm.js
* route-home.css -> styles/route-home.chunk.8aeee.css
*
* Even if a user alters the output name, we still have keys we can expect & rely on
*/
assets = JSON.parse(assets['asset-manifest.json']._value);
const mainJs = assets['bundle.js'];
const mainCss = assets['bundle.css'];
const defaults = {
...(mainCss && {
[mainCss]: {
type: 'style',
weight: 1,
},
}),
...(mainJs && {
[mainJs]: {
type: 'script',
weight: 1,
},
}),
},
manifest = {
'/': defaults,
};
Object.keys(assets)
.filter(asset => /^route-.*\.js$/.test(asset))
.map(asset => asset.replace(/\.js$/, ''))
.forEach(route => {
const routeManifest = Object.assign({}, defaults);
const routeCss = assets[`${route}.css`];
const routeJs = assets[`${route}.js`];
routeManifest[routeJs] = { type: 'script', weight: 0.9 };
if (routeCss) routeManifest[routeCss] = { type: 'script', weight: 0.9 };
const path = route.replace(/^route-/, '/').replace(/^\/home/, '/');
if (namedChunkGroups) {
// async files to be loaded, generated by splitChunksPlugin
const asyncFiles = namedChunkGroups.get(route) || {};
if (asyncFiles && asyncFiles.chunks) {
asyncFiles.chunks.forEach(asset => {
asset.files = asset.files || [];
asset.files.forEach(file => {
if (/\.css$/.test(file)) {
routeManifest[file] = { type: 'style', weight: 0.9 };
} else if (/\.js$/.test(file)) {
routeManifest[file] = { type: 'script', weight: 0.9 };
}
});
});
}
}
manifest[path] = routeManifest;
});
return manifest;
};