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