diff options
| author | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2023-02-08 13:54:04 +0530 |
|---|---|---|
| committer | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2023-02-08 13:54:04 +0530 |
| commit | 4993aaf41e132dd0805bf6c487901d9f2aecfcfe (patch) | |
| tree | a51ebf7bff551e9188d68f777d100e8ffb70922d /config/webpack/plugins/inject-script-webpack-plugin.js | |
| parent | 8234acb45cd99fc7bcd99df7f71cac02845bb534 (diff) | |
| download | banglar-bhumi-utils-4993aaf41e132dd0805bf6c487901d9f2aecfcfe.tar.gz banglar-bhumi-utils-4993aaf41e132dd0805bf6c487901d9f2aecfcfe.zip | |
Combining similar scripts into 1 for improved performence and less files in build.
Diffstat (limited to 'config/webpack/plugins/inject-script-webpack-plugin.js')
| -rw-r--r-- | config/webpack/plugins/inject-script-webpack-plugin.js | 52 |
1 files changed, 32 insertions, 20 deletions
diff --git a/config/webpack/plugins/inject-script-webpack-plugin.js b/config/webpack/plugins/inject-script-webpack-plugin.js index ca75213..6568514 100644 --- a/config/webpack/plugins/inject-script-webpack-plugin.js +++ b/config/webpack/plugins/inject-script-webpack-plugin.js @@ -1,5 +1,23 @@ +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; @@ -11,45 +29,39 @@ class InjectScriptPlugin { compilation.hooks.processAssets.tapPromise( { name: "InjectScriptPlugin", - stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE, + stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE, additionalAssets: true }, async assets => { for (const assetName in assets) { if (assetName.endsWith(".js")) { - const asset = assets[assetName]; - let content = asset._source._children; - let x = -1; - for (let i = 0; i < content.length; i++) { - if (typeof content[i] === "string" && content[i].indexOf("__webpack_exports__") >= 0) { - x = i; - break; - } - } - if (x == -1) { - throw Error("Failed to inject script."); - } - content[x] += ` + 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 end = ""; if (!this.isFirst) { - content[x] += "+"; + start += "+"; } - content[x] += "= `"; + start += "= `"; if (this.onload) { - content[x] += "document.addEventListener(\"DOMContentLoaded\", () => {"; - content[content.length - 3] += "});"; + start += "document.addEventListener(\"DOMContentLoaded\", () => {"; + end += "});"; } - content[content.length - 3] += `\`; + end += `\`; observer.disconnect(); } }); observer.observe(document, {childList: true, subtree: true}); `; - content[x + 1] = (await terser.minify(content[x + 1]._source._source._valueAsString.replaceAll("`", "\\`"))).code; + const content = (await terser.minify(currentAssetContent.replaceAll("`", "\\`"))).code; + compilation.updateAsset( + assetName, + new sources.RawSource(start + content + end) + ); } } } |
