-
Notifications
You must be signed in to change notification settings - Fork 1
/
InlineAssetsPlugin.js
48 lines (40 loc) · 1.33 KB
/
InlineAssetsPlugin.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
class InlineAssetsPlugin {
constructor(options = {}) {
const {patterns = []} = options;
this.patterns = patterns;
}
apply(compiler) {
compiler.hooks.emit.tapAsync(
'InlineAssetsPlugin',
(compilation, callback) => {
for (const pattern of this.patterns) {
const fromSource = this.getSource(compilation.getAsset(pattern.from));
const toSource = this.getSource(compilation.getAsset(pattern.to));
const resultSource = toSource.replace(pattern.pattern, fromSource)
compilation.updateAsset(pattern.to, {
source() {
return resultSource;
},
size() {
return resultSource.length;
}
})
}
callback();
}
);
}
getSource(asset) {
let source = asset.source
if (typeof source === 'function') {
source = source()
} else {
source = source.source()
}
if (source instanceof Buffer) {
return source.toString('utf8')
}
return source;
}
}
module.exports.InlineAssetsPlugin = InlineAssetsPlugin