From feac7acb91a78b117241abdd0a5d80f778e15f06 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Fri, 24 Mar 2023 11:36:54 +0530 Subject: Overriding the javascript on the page instead of locally replacing it. --- .../plugins/inject-script-webpack-plugin.js | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 config/webpack/plugins/inject-script-webpack-plugin.js (limited to 'config/webpack/plugins/inject-script-webpack-plugin.js') diff --git a/config/webpack/plugins/inject-script-webpack-plugin.js b/config/webpack/plugins/inject-script-webpack-plugin.js new file mode 100644 index 0000000..8d72ea2 --- /dev/null +++ b/config/webpack/plugins/inject-script-webpack-plugin.js @@ -0,0 +1,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; -- cgit v1.2.3