diff options
| author | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2023-03-24 11:36:54 +0530 |
|---|---|---|
| committer | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2023-03-24 11:36:54 +0530 |
| commit | feac7acb91a78b117241abdd0a5d80f778e15f06 (patch) | |
| tree | ebee27b43105aeed1048e86afb1e6e3d2b7e1075 /config/webpack/plugins/inject-script-webpack-plugin.js | |
| parent | a9f2d88e1e5b1c7ddf524db8b5b185b8485f8d48 (diff) | |
| download | banglar-bhumi-utils-feac7acb91a78b117241abdd0a5d80f778e15f06.tar.gz banglar-bhumi-utils-feac7acb91a78b117241abdd0a5d80f778e15f06.zip | |
Overriding the javascript on the page instead of locally replacing it.
Diffstat (limited to 'config/webpack/plugins/inject-script-webpack-plugin.js')
| -rw-r--r-- | config/webpack/plugins/inject-script-webpack-plugin.js | 41 |
1 files changed, 41 insertions, 0 deletions
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; |
