-
Notifications
You must be signed in to change notification settings - Fork 762
Expand file tree
/
Copy pathwebpack.js
More file actions
64 lines (56 loc) · 1.75 KB
/
webpack.js
File metadata and controls
64 lines (56 loc) · 1.75 KB
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
'use strict';
const fs = require('fs');
const path = require('path');
const url = require('url');
function buildManifest(compiler, compilation) {
let context = compiler.options.context;
let manifest = {};
compilation.chunks.forEach(chunk => {
chunk.files.forEach(file => {
chunk.forEachModule(module => {
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 (currentModule.rawRequest){
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);
const outputDirectory = path.dirname(this.filename);
try {
fs.mkdirSync(outputDirectory);
} catch (err) {
if (err.code !== 'EEXIST') {
throw err;
}
}
fs.writeFileSync(this.filename, json);
callback();
});
}
}
function getBundles(manifest, moduleIds) {
return moduleIds.reduce((bundles, moduleId) => {
return bundles.concat(manifest[moduleId]);
}, []);
}
exports.ReactLoadablePlugin = ReactLoadablePlugin;
exports.getBundles = getBundles;