diff options
| author | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2023-02-13 22:49:42 +0530 |
|---|---|---|
| committer | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2023-02-13 22:49:42 +0530 |
| commit | 34ad953c953a395ba3e51c8125034a6bb65b2a79 (patch) | |
| tree | 253cc30047bb3963f387bd340d50938b3b727053 /config/webpack/plugins/inject-script-webpack-plugin.js | |
| parent | aeeabaa0050ecb9e43b1a3fb309f314d92d3ca97 (diff) | |
| download | banglar-bhumi-utils-34ad953c953a395ba3e51c8125034a6bb65b2a79.tar.gz banglar-bhumi-utils-34ad953c953a395ba3e51c8125034a6bb65b2a79.zip | |
Fixed not being able to import modules in scripts.
Diffstat (limited to 'config/webpack/plugins/inject-script-webpack-plugin.js')
| -rw-r--r-- | config/webpack/plugins/inject-script-webpack-plugin.js | 79 |
1 files changed, 0 insertions, 79 deletions
diff --git a/config/webpack/plugins/inject-script-webpack-plugin.js b/config/webpack/plugins/inject-script-webpack-plugin.js deleted file mode 100644 index b6525f6..0000000 --- a/config/webpack/plugins/inject-script-webpack-plugin.js +++ /dev/null @@ -1,79 +0,0 @@ -const { sources } = require("webpack"); -const terser = require("terser"); - -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( - { - name: "InjectScriptPlugin", - stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE, - additionalAssets: true - }, - async assets => { - for (const assetName in assets) { - if (assetName.endsWith("document_start.js")) { - const currentAssetContent = compilation.getAsset(assetName).source.source(); - const scripts = { - before: "", - injected: "", - injected_and_loaded: "" - }; - let scriptEnd = 0; - 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 scripts with document_start runtime"); - } else { - scriptEnd = currentAssetContent.indexOf("\n/*<---->*/", end + 1); - if (scriptEnd < 0) { - throw Error("Failed to parse scripts with document_start runtime"); - } - const scriptType = currentAssetContent.substring(start + 6, end); - scripts[scriptType.trim()] += currentAssetContent.substring(end + 6, scriptEnd); - scriptEnd += 11; - } - } - } - 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}})();`) - : ""; - } - 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 resultantCode = scripts.before + injectedCode; - compilation.updateAsset( - assetName, - new sources.RawSource(resultantCode) - ); - } - } - } - ); - }); - } -} - -module.exports = InjectScriptPlugin; |
