diff options
| author | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2023-02-09 13:50:24 +0530 |
|---|---|---|
| committer | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2023-02-09 13:50:24 +0530 |
| commit | 0404234c26f6813025e9eedd9981e1536db28b2a (patch) | |
| tree | 204e2ca55540e594e4d88eba87191b90794ddaf8 /config/webpack/plugins | |
| parent | 915947e3e7ab7c1fa07596ab4b13bf6e1cc50b76 (diff) | |
| download | banglar-bhumi-utils-0404234c26f6813025e9eedd9981e1536db28b2a.tar.gz banglar-bhumi-utils-0404234c26f6813025e9eedd9981e1536db28b2a.zip | |
Combined the scripts with the same runtime into a single file to improve performance
Diffstat (limited to 'config/webpack/plugins')
| -rw-r--r-- | config/webpack/plugins/create-manifest-webpack-plugin/index.js | 36 | ||||
| -rw-r--r-- | config/webpack/plugins/inject-script-webpack-plugin.js | 71 |
2 files changed, 57 insertions, 50 deletions
diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js index 6bbb485..7748cd9 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -2,32 +2,16 @@ const { sources } = require("webpack"); const path = require("path"); const { version } = require(path.resolve(process.env.ROOT_DIR, "package.json")); - -const getRunAt = scriptType => { - switch (scriptType) { - case "before": - case "injected": - case "injected_and_loaded": - return "document_start"; - - case "rendered": - return "document_end"; - - case "loaded": - default: - return "document_idle"; - } -}; +const { getScriptRuntimeFromType } = require(path.resolve(process.env.CONFIG_DIR, "webpack/utils/script-runtime")); class CreateManifestPlugin { constructor({ scripts }) { this.scripts = {}; this.manifest = require("./manifest-template.json"); - this.scripts.before = scripts.before || {}; - this.scripts.injected = scripts.injected || {}; - this.scripts.injected_and_loaded = scripts.injected_and_loaded || {}; - this.scripts.rendered = scripts.rendered || {}; - this.scripts.loaded = scripts.loaded || {}; + for (const scriptType in scripts) { + const scriptRuntime = getScriptRuntimeFromType(scriptType); + this.scripts[scriptRuntime] = { ...this.scripts[scriptRuntime], ...scripts[scriptType] }; + } } apply(compiler) { @@ -42,9 +26,9 @@ class CreateManifestPlugin { this.manifest.version = version; this.manifest.content_scripts = [] - for (const scriptType in this.scripts) { - if (Object.keys(this.scripts[scriptType]).length > 0) { - const currentScripts = this.scripts[scriptType]; + for (const scriptRuntime in this.scripts) { + if (Object.keys(this.scripts[scriptRuntime]).length > 0) { + const currentScripts = this.scripts[scriptRuntime]; let paths = {}; for (const scriptName in currentScripts) { @@ -58,8 +42,8 @@ class CreateManifestPlugin { for (const scriptPath in paths) { this.manifest.content_scripts.push({ matches: [`*://banglarbhumi.gov.in/${scriptPath}`], - js: [`${scriptType}.js`], - run_at: getRunAt(scriptType) + js: [`${scriptRuntime}.js`], + run_at: scriptRuntime }); } } diff --git a/config/webpack/plugins/inject-script-webpack-plugin.js b/config/webpack/plugins/inject-script-webpack-plugin.js index 213e691..592751d 100644 --- a/config/webpack/plugins/inject-script-webpack-plugin.js +++ b/config/webpack/plugins/inject-script-webpack-plugin.js @@ -1,12 +1,13 @@ const { sources } = require("webpack"); const terser = require("terser"); -class InjectScriptPlugin { - constructor({ onload, isFirst }) { - this.onload = !!onload; - this.isFirst = !!isFirst; - } +const getScriptCodeScoped = (scriptCode, loaded) => scriptCode + ? (loaded + ? `document.addEventListener(\"DOMContentLoaded\", () => {${scriptCode}});` + : `(() => {${scriptCode}})();`) + : ""; +class InjectScriptPlugin { apply(compiler) { compiler.hooks.compilation.tap("InjectScriptPlugin", compilation => { compilation.hooks.processAssets.tapPromise( @@ -18,32 +19,54 @@ class InjectScriptPlugin { async assets => { for (const assetName in assets) { if (assetName.endsWith(".js")) { - let currentAssetContent = compilation.getAsset(assetName).source.source(); - let start = `(() => { - const o = new MutationObserver(() => { - const s = document.querySelector("head > script:nth-child(54)"); - if (s) { - s.innerHTML - `; - let end = ""; - if (!this.isFirst) { - start += "+"; + let scriptEnd = 0; + const currentAssetContent = compilation.getAsset(assetName).source.source(); + const scripts = { + before: "", + injected: "", + injected_and_loaded: "" + }; + while (true) { + const start = currentAssetContent.indexOf("/*<--\t", scriptEnd + 1); + if (start < 0) { + break; + } else { + const end = currentAssetContent.indexOf("\t-->*/", start + 1); + if (end < 0) { + throw Error("Failed to parse some scripts"); + } else { + const scriptType = currentAssetContent.substring(start + 6, end); + scriptEnd = currentAssetContent.indexOf("\n/*<---->*/", end + 1) + if (scriptEnd < 0) { + break; + } + scripts[scriptType.trim()] += currentAssetContent.substring(end + 6, scriptEnd); + scriptEnd += 11; + } + } } - start += "= `"; - if (this.onload) { - start += "document.addEventListener(\"DOMContentLoaded\", () => {"; - end += "});"; + for (const scriptType in scripts) { + const currentScriptCode = (await terser.minify(scripts[scriptType].replaceAll("`", "\\`"))).code; + scripts[scriptType] = currentScriptCode + ? (scriptType === "injected_and_loaded" + ? `document.addEventListener(\"DOMContentLoaded\", () => {${currentScriptCode}});` + : `(() => {${currentScriptCode}})();`) + : ""; } - end += `\`; + const injectedCode = (scripts.injected || scripts.injected_and_loaded) + ? `const o = new MutationObserver(() => { + const s = document.querySelector("head > script:nth-child(54)"); + if (s) { + s.innerHTML = \`${scripts.injected}${scripts.injected_and_loaded}\`; o.disconnect(); } }); - o.observe(document, {childList: true, subtree: true}); - })();`; - const content = (await terser.minify(currentAssetContent.replaceAll("`", "\\`"))).code; + o.observe(document, {childList: true, subtree: true});` + : ""; + const resultantCode = scripts.before + injectedCode; compilation.updateAsset( assetName, - new sources.RawSource(start + content + end) + new sources.RawSource(resultantCode) ); } } |
