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

const { getInjectionCode } = webpackRequire("plugins/inject-script-webpack-plugin/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) {
						if (assetName.endsWith("injected.js")) {
							compilation.updateAsset(
								assetName,
								new sources.RawSource(getInjectionCode(compilation.getAsset(assetName).source.source()))
							);
						}
					}
				}
			);
		});
	}
}

module.exports = InjectScriptPlugin;