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/inject-script-webpack-plugin.js | |
| 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/inject-script-webpack-plugin.js')
| -rw-r--r-- | config/webpack/plugins/inject-script-webpack-plugin.js | 71 |
1 files changed, 47 insertions, 24 deletions
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) ); } } |
