From d178caee5c93ad9ed91dbd082b0f0b5a9b0507aa Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Mon, 27 Mar 2023 21:38:43 +0530 Subject: Refactoring code. Only using hashes for output file names. Using singleton pattern to store the callbacks for observeDOM, and only one MutationObserver. --- .../plugins/inject-script-webpack-plugin.js | 24 ++++++++++------------ 1 file changed, 11 insertions(+), 13 deletions(-) (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 index 862b3f0..c597e9a 100644 --- a/config/webpack/plugins/inject-script-webpack-plugin.js +++ b/config/webpack/plugins/inject-script-webpack-plugin.js @@ -3,6 +3,10 @@ const { sources } = require("webpack"); const { getInjectionCode } = webpackRequire("utils/get-injection-code"); class InjectScriptPlugin { + constructor(insertedBefore = false) { + this.insertedBefore = insertedBefore; + } + apply(compiler) { compiler.hooks.compilation.tap("InjectScriptPlugin", compilation => { compilation.hooks.processAssets.tapPromise( @@ -13,21 +17,15 @@ class InjectScriptPlugin { }, 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) { + if (/\.js$/.test(assetName)) { compilation.updateAsset( assetName, - new sources.RawSource(getInjectionCode(compilation.getAsset(assetName).source.source(), injected_before)) + new sources.RawSource( + getInjectionCode( + compilation.getAsset(assetName).source.source(), + this.insertedBefore + ) + ) ); } } -- cgit v1.2.3