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 | |
| 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')
8 files changed, 62 insertions, 188 deletions
diff --git a/config/webpack/loaders/scoped-loader.js b/config/webpack/loaders/scoped-loader.js deleted file mode 100644 index ec7b7eb..0000000 --- a/config/webpack/loaders/scoped-loader.js +++ /dev/null @@ -1,18 +0,0 @@ -const path = require("path"); - -const scripts = require(path.resolve(SOURCE_DIR, "scripts.json")); - -const getScriptType = scriptName => { - for (const scriptType in scripts) { - if (scripts[scriptType][scriptName]) { - return scriptType; - } - } - return "undefined_script"; -}; - -module.exports = function (source) { - return `/*<--\t${getScriptType(this.resourcePath.replace(path.resolve(SOURCE_DIR, "scripts") + "/", ""))}\t-->*/ -(() => {\n${source}\n})(); -/*<---->*/` -}; diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js index 7852d62..3a16770 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -1,22 +1,11 @@ const { sources } = require("webpack"); const path = require("path"); -const scripts = require(path.resolve(SOURCE_DIR, "scripts.json")); - -const { version } = require(path.resolve(ROOT_DIR, "package.json")); const { getScriptRuntimeFromType } = webpackRequire("utils/script-runtime"); const { getFileNameHash } = webpackRequire("utils/file-name-hash"); +const { formatScripts } = webpackRequire("utils/format-scripts"); class CreateManifestPlugin { - constructor() { - this.scripts = {}; - this.manifest = webpackRequire("plugins/create-manifest-webpack-plugin/manifest-template.json"); - for (const scriptType in scripts) { - const scriptRuntime = getScriptRuntimeFromType(scriptType); - this.scripts[scriptRuntime] = { ...this.scripts[scriptRuntime], ...scripts[scriptType] }; - } - } - apply(compiler) { compiler.hooks.compilation.tap("CreateManifestPlugin", compilation => { compilation.hooks.processAssets.tap( @@ -26,35 +15,24 @@ class CreateManifestPlugin { additionalAssets: true }, () => { - this.manifest.version = version; - this.manifest.content_scripts = [] - - 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) { - const scriptPath = currentScripts[scriptName]; - if (!path[scriptPath]) { - paths[scriptPath] = []; - } - paths[scriptPath].push(scriptName); - } - - for (const scriptPath in paths) { - this.manifest.content_scripts.push({ - matches: [`*://banglarbhumi.gov.in/${scriptPath}`], - js: [`scripts/${getFileNameHash(scriptPath)}/${scriptRuntime}.js`], - run_at: scriptRuntime - }); - } + const scripts = formatScripts(require(path.resolve(SOURCE_DIR, "scripts.json"))); + const manifest = webpackRequire("plugins/create-manifest-webpack-plugin/manifest-template.json"); + manifest.version = require(path.resolve(ROOT_DIR, "package.json")).version; + manifest.content_scripts = [] + + for (const scriptPath in scripts) { + for (const scriptType in scripts[scriptPath]) { + manifest.content_scripts.push({ + matches: [`*://banglarbhumi.gov.in/BanglarBhumi/${scriptPath}`], + js: [`scripts/${getFileNameHash(scriptPath)}/${scriptType}.js`], + run_at: getScriptRuntimeFromType(scriptType) + }); } } compilation.emitAsset( "manifest.json", - new sources.RawSource(JSON.stringify(this.manifest)) + new sources.RawSource(JSON.stringify(manifest)) ); } ); 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; diff --git a/config/webpack/plugins/inject-script-webpack-plugin/get-injection-code.js b/config/webpack/plugins/inject-script-webpack-plugin/get-injection-code.js new file mode 100644 index 0000000..90a1cb7 --- /dev/null +++ b/config/webpack/plugins/inject-script-webpack-plugin/get-injection-code.js @@ -0,0 +1 @@ +module.exports = code => `const o=new MutationObserver(()=>{let t=document.querySelector("head > script:nth-child(54)");if(t){let e=\`(()=>{${code.replaceAll("`","\\`")}})();\`;null==t.getAttribute("data-replaced")?(t.innerHTML=e,t.setAttribute("data-replaced","")):t.innerHTML+=e,o.disconnect()}});o.observe(document,{childList:!0,subtree:!0});`; diff --git a/config/webpack/plugins/inject-script-webpack-plugin/index.js b/config/webpack/plugins/inject-script-webpack-plugin/index.js new file mode 100644 index 0000000..b7b5940 --- /dev/null +++ b/config/webpack/plugins/inject-script-webpack-plugin/index.js @@ -0,0 +1,30 @@ +const { sources } = require("webpack"); +const terser = require("terser"); + +const getInjectionCode = webpackRequire("plugins/inject-script-webpack-plugin/get-injection-code"); + +class InjectScriptPlugin { + apply(compiler) { + compiler.hooks.compilation.tap("InjectScriptPlugin", compilation => { + compilation.hooks.processAssets.tapPromise( + { + name: "InjectScriptPlugin", + stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ANALYSE, + additionalAssets: true + }, + async assets => { + for (const assetName in assets) { + if (assetName.endsWith("injected.js")) { + compilation.updateAsset( + assetName, + new sources.RawSource(getInjectionCode(compilation.getAsset(assetName).source.source())) + ); + } + } + } + ); + }); + } +} + +module.exports = InjectScriptPlugin; diff --git a/config/webpack/plugins/sanitize-scripts-import-webpack-plugin.js b/config/webpack/plugins/sanitize-scripts-import-webpack-plugin.js deleted file mode 100644 index 37c2cf4..0000000 --- a/config/webpack/plugins/sanitize-scripts-import-webpack-plugin.js +++ /dev/null @@ -1,54 +0,0 @@ -const { sources } = require("webpack"); - -const perpectualIndexOf = (string, characterList, start) => { - if (start > 0) { - let x = start; - for (let i = 0; i < characterList.length; i++) { - x = string.indexOf(characterList[i], x + 1); - if (x < 0) { - return -1; - } - } - return x; - } else { - return -1; - } -}; - -class SanitizeScriptsImportPlugin { - apply(compiler) { - compiler.hooks.compilation.tap("SanitizeScriptsImportPlugin", compilation => { - compilation.hooks.processAssets.tap( - { - name: "SanitizeScriptsImportPlugin", - stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE, - additionalAssets: true - }, - assets => { - for (const assetName in assets) { - if (assetName.endsWith(".js")) { - const currentAssetContent = compilation.getAsset(assetName).source.source(); - let previouslyEnded = 0; - let code = ""; - while (true) { - const start = perpectualIndexOf(currentAssetContent, ["\n/***/", "\n/***/", "\n"], previouslyEnded + 1); - previouslyEnded = perpectualIndexOf(currentAssetContent, ["\n/***/"], start + 1); - if (previouslyEnded < 0) { - break; - } else { - code += currentAssetContent.substring(start, previouslyEnded); - } - } - compilation.updateAsset( - assetName, - new sources.RawSource(code) - ); - } - } - } - ); - }); - } -} - -module.exports = SanitizeScriptsImportPlugin; diff --git a/config/webpack/utils/format-scripts.js b/config/webpack/utils/format-scripts.js new file mode 100644 index 0000000..cfa11bc --- /dev/null +++ b/config/webpack/utils/format-scripts.js @@ -0,0 +1,17 @@ +module.exports.formatScripts = scripts => { + const _scripts = {}; + + for (const scriptType in scripts) { + const currentScripts = scripts[scriptType]; + for (const scriptName in currentScripts) { + const scriptPath = currentScripts[scriptName]; + _scripts[scriptPath] = _scripts[scriptPath] || {}; + _scripts[scriptPath][scriptType] = _scripts[scriptPath][scriptType] || []; + _scripts[scriptPath][scriptType].push(scriptName); + } + } + + + + return _scripts; +}; diff --git a/config/webpack/utils/script-runtime.js b/config/webpack/utils/script-runtime.js index 394e87e..8215ba2 100644 --- a/config/webpack/utils/script-runtime.js +++ b/config/webpack/utils/script-runtime.js @@ -2,7 +2,6 @@ module.exports.getScriptRuntimeFromType = scriptType => { switch (scriptType) { case "before": case "injected": - case "injected_and_loaded": return "document_start"; case "rendered": |
