blob: 862b3f0372b5584abfc8015abb2c9cba97f8fe1e (
plain)
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
|
const { sources } = require("webpack");
const { getInjectionCode } = webpackRequire("utils/get-injection-code");
class InjectScriptPlugin {
apply(compiler) {
compiler.hooks.compilation.tap("InjectScriptPlugin", compilation => {
compilation.hooks.processAssets.tapPromise(
{
name: "InjectScriptPlugin",
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ANALYSE,
additionalAssets: true
},
async assets => {
for (const assetName in assets) {
let injected = false;
let injected_before = false;
if (assetName.endsWith("injected.js"))
injected = true;
else if (assetName.endsWith("injected-before.js")) {
injected = true;
injected_before = true;
}
if (injected) {
compilation.updateAsset(
assetName,
new sources.RawSource(getInjectionCode(compilation.getAsset(assetName).source.source(), injected_before))
);
}
}
}
);
});
}
}
module.exports = InjectScriptPlugin;
|