aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/plugins/inject-script-webpack-plugin.js
blob: 8d72ea23d77d678cc449788682f9b899ca4517ab (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
41
const { sources } = require("webpack");
const terser = require("terser");

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;