-
Notifications
You must be signed in to change notification settings - Fork 780
/
Copy pathwebpack.js
60 lines (51 loc) · 1.58 KB
/
webpack.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
'use strict';
const url = require('url');
function buildManifest(compiler, compilation) {
let context = compiler.options.context;
let manifest = {};
compilation.chunks.forEach(chunk => {
chunk.files.forEach(file => {
for(const module of chunk.modulesIterable) {
let id = module.id;
let name = typeof module.libIdent === 'function' ? module.libIdent({ context }) : null;
let publicPath = url.resolve(compilation.outputOptions.publicPath || '', file);
let currentModule = module;
if (module.constructor.name === 'ConcatenatedModule') {
currentModule = module.rootModule;
}
if (!manifest[currentModule.rawRequest]) {
manifest[currentModule.rawRequest] = [];
}
manifest[currentModule.rawRequest].push({ id, name, file, publicPath });
}
});
});
return manifest;
}
class ReactLoadablePlugin {
constructor(opts = {}) {
this.filename = opts.filename;
}
apply(compiler) {
compiler.plugin('emit', (compilation, callback) => {
const manifest = buildManifest(compiler, compilation);
var json = JSON.stringify(manifest, null, 2);
compilation.assets[this.filename] = {
source() {
return json;
},
size() {
return json.length
}
}
callback();
});
}
}
function getBundles(manifest, moduleIds) {
return moduleIds.reduce((bundles, moduleId) => {
return bundles.concat(manifest[moduleId]);
}, []);
}
exports.ReactLoadablePlugin = ReactLoadablePlugin;
exports.getBundles = getBundles;