forked from webpack/webpack.js.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProdAssetsManifest.mjs
50 lines (49 loc) · 1.59 KB
/
ProdAssetsManifest.mjs
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
import webpack from 'webpack';
const { Compilation, sources } = webpack;
// collect assets data (vendor.[contenthash].js and index.[contenthash].js) for ssg
class ProdAssetsManifest {
apply(compiler) {
let js = [];
let css = [];
compiler.hooks.thisCompilation.tap('ProdAssetsManifest', (compilation) => {
compilation.hooks.processAssets.tap(
{
name: 'ProdAssetsManifest',
stage: Compilation.PROCESS_ASSETS_STAGE_ANALYSE,
},
() => {
for (const [, entrypoint] of compilation.entrypoints) {
const files = entrypoint.getFiles();
js = js.concat(
files
.filter((file) => file.endsWith('.js')) // two js files
.sort((a) => {
// vendor first
if (a.startsWith('vendor')) return -1;
return 1;
})
.map((file) => {
return compilation.outputOptions.publicPath + file;
})
);
css = css.concat(
files
.filter((file) => file.endsWith('.css'))
.sort((a) => {
// index first
if (a.startsWith('index')) return -1;
return 1;
})
.map((file) => compilation.outputOptions.publicPath + file)
);
}
compilation.emitAsset(
'prod-assets-manifest.json',
new sources.RawSource(JSON.stringify({ js, css }))
);
}
);
});
}
}
export default ProdAssetsManifest;