-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
56 lines (47 loc) · 1.78 KB
/
index.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
const path = require('path');
module.exports = class EmitAllPlugin {
constructor(opts = {}) {
this.ignorePattern = opts.ignorePattern || /node_modules/;
this.ignoreExternals = !!opts.ignoreExternals;
this.path = opts.path;
}
shouldIgnore(path) {
return this.ignorePattern.test(path);
}
apply(compiler) {
compiler.hooks.afterCompile.tapAsync(
'EmitAllPlugin',
(compilation, cb) => {
const { modules } = compilation;
modules.forEach(mod => {
const absolutePath = mod.resource;
if (this.ignoreExternals && mod.external) return;
if (this.shouldIgnore(absolutePath)) return;
// Used for vendor chunk
if (mod.constructor.name === 'MultiModule') return;
const source = mod._source._value;
const projectRoot = compiler.context;
const out = this.path || compiler.options.output.path;
const dest = path.join(
out,
absolutePath.replace(projectRoot, '')
);
compiler.outputFileSystem.mkdirp(
path.dirname(dest),
err => {
if (err) throw err;
compiler.outputFileSystem.writeFile(
dest,
source,
err => {
if (err) throw err;
}
);
}
);
});
cb();
}
);
}
};