-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
47 lines (44 loc) · 1.83 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
class HtmlWebpackCustomInject {
constructor(options) {
this.options = options;
}
apply(compiler) {
if (compiler.hooks) {
compiler.hooks.compilation.tap('htmlWebpackCustomInject', (compilation) => {
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync(
'htmlWebpackCustomInject',
(data, cb) => {
if (this.options.inject) {
data.html = this.options.inject(data.outputName, data.html);
cb(null, data);
} else if (this.options.asyncInject) {
this.options.asyncInject(data.outputName, data.html, (err, res) => {
if (err) {
return cb(err);
}
cb(null, data.html = res)
})
}
})
})
} else {
compiler.plugin('compilation', (compilation) => {
compilation.plugin('html-webpack-plugin-after-html-processing', (data, cb) => {
if (this.options.inject) {
data.html = this.options.inject(data.outputName, data.html)
cb(null, data);
} else if (this.options.asyncInject) {
this.options.asyncInject(data.outputName, data.html, (err, res) => {
if (err) {
return cb(err);
}
data.html = res
cb(null, data)
})
}
})
})
}
}
}
module.exports = HtmlWebpackCustomInject;