From 915947e3e7ab7c1fa07596ab4b13bf6e1cc50b76 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Wed, 8 Feb 2023 18:51:39 +0530 Subject: Cleaned code. --- .../create-manifest-webpack-plugin/index.js | 11 +++++--- .../plugins/inject-script-webpack-plugin.js | 33 ++++++---------------- .../sanitize-scripts-import-webpack-plugin.js | 10 +++---- 3 files changed, 19 insertions(+), 35 deletions(-) (limited to 'config/webpack/plugins') diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js index 4a07380..6bbb485 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -1,3 +1,4 @@ +const { sources } = require("webpack"); const path = require("path"); const { version } = require(path.resolve(process.env.ROOT_DIR, "package.json")); @@ -34,10 +35,10 @@ class CreateManifestPlugin { compilation.hooks.processAssets.tap( { name: "CreateManifestPlugin", - stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE, + stage: compiler.webpack.Compilation.PROCESS_ASSETS_ADDITIONAL, additionalAssets: true }, - assets => { + () => { this.manifest.version = version; this.manifest.content_scripts = [] @@ -64,8 +65,10 @@ class CreateManifestPlugin { } } - const { RawSource } = compiler.webpack.sources; - assets["manifest.json"] = new RawSource(Buffer.from(JSON.stringify(this.manifest))); + compilation.updateAsset( + "manifest.json", + new sources.RawSource(JSON.stringify(this.manifest)) + ); } ); }); diff --git a/config/webpack/plugins/inject-script-webpack-plugin.js b/config/webpack/plugins/inject-script-webpack-plugin.js index 6568514..213e691 100644 --- a/config/webpack/plugins/inject-script-webpack-plugin.js +++ b/config/webpack/plugins/inject-script-webpack-plugin.js @@ -1,23 +1,6 @@ const { sources } = require("webpack"); const terser = require("terser"); -const propectualIndexOf = (string, characterList, start) => { - if (start > 0) { - let i = 0; - let x = start; - while (i < characterList.length) { - x = string.indexOf(characterList[i], x + 1); - if (x < 0) { - return -1; - } - i++; - } - return x; - } else { - return -1; - } -}; - class InjectScriptPlugin { constructor({ onload, isFirst }) { this.onload = !!onload; @@ -36,11 +19,11 @@ class InjectScriptPlugin { for (const assetName in assets) { if (assetName.endsWith(".js")) { let currentAssetContent = compilation.getAsset(assetName).source.source(); - let start = ` - const observer = new MutationObserver(mutations => { - const script = document.querySelector("head > script:nth-child(54)"); - if (script) { - script.innerHTML + let start = `(() => { + const o = new MutationObserver(() => { + const s = document.querySelector("head > script:nth-child(54)"); + if (s) { + s.innerHTML `; let end = ""; if (!this.isFirst) { @@ -52,11 +35,11 @@ class InjectScriptPlugin { end += "});"; } end += `\`; - observer.disconnect(); + o.disconnect(); } }); - observer.observe(document, {childList: true, subtree: true}); - `; + o.observe(document, {childList: true, subtree: true}); + })();`; const content = (await terser.minify(currentAssetContent.replaceAll("`", "\\`"))).code; compilation.updateAsset( assetName, diff --git a/config/webpack/plugins/sanitize-scripts-import-webpack-plugin.js b/config/webpack/plugins/sanitize-scripts-import-webpack-plugin.js index 61ce804..37c2cf4 100644 --- a/config/webpack/plugins/sanitize-scripts-import-webpack-plugin.js +++ b/config/webpack/plugins/sanitize-scripts-import-webpack-plugin.js @@ -1,15 +1,13 @@ const { sources } = require("webpack"); -const propectualIndexOf = (string, characterList, start) => { +const perpectualIndexOf = (string, characterList, start) => { if (start > 0) { - let i = 0; let x = start; - while (i < characterList.length) { + for (let i = 0; i < characterList.length; i++) { x = string.indexOf(characterList[i], x + 1); if (x < 0) { return -1; } - i++; } return x; } else { @@ -33,8 +31,8 @@ class SanitizeScriptsImportPlugin { let previouslyEnded = 0; let code = ""; while (true) { - const start = propectualIndexOf(currentAssetContent, ["\n/***/", "\n/***/", "\n"], previouslyEnded + 1); - previouslyEnded = propectualIndexOf(currentAssetContent, ["\n/***/"], start + 1); + const start = perpectualIndexOf(currentAssetContent, ["\n/***/", "\n/***/", "\n"], previouslyEnded + 1); + previouslyEnded = perpectualIndexOf(currentAssetContent, ["\n/***/"], start + 1); if (previouslyEnded < 0) { break; } else { -- cgit v1.2.3