diff options
| author | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2023-02-06 19:30:21 +0530 |
|---|---|---|
| committer | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2023-02-06 19:30:21 +0530 |
| commit | c57a9e3d9e304bd360437f8f732dbc0479376cd7 (patch) | |
| tree | ad9e31238aab26fd7a8ceb54d1424daa79a237c9 /config | |
| parent | 1a301791bb621fa01fa76e9403b23fe1f4a120af (diff) | |
| download | banglar-bhumi-utils-c57a9e3d9e304bd360437f8f732dbc0479376cd7.tar.gz banglar-bhumi-utils-c57a9e3d9e304bd360437f8f732dbc0479376cd7.zip | |
Fixed some bugs... Updated some code...
Diffstat (limited to 'config')
| -rw-r--r-- | config/webpack.config.js | 45 | ||||
| -rw-r--r-- | config/webpack/plugins/create-manifest-webpack-plugin/index.js | 30 | ||||
| -rw-r--r-- | config/webpack/plugins/inject-script-webpack-plugin.js | 28 | ||||
| -rw-r--r-- | config/webpack/utils/generate-script-build-config.js | 51 |
4 files changed, 83 insertions, 71 deletions
diff --git a/config/webpack.config.js b/config/webpack.config.js index 1796aa8..f21d38f 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -1,55 +1,14 @@ -const fs = require("fs"); const path = require("path"); const CopyPlugin = require("copy-webpack-plugin"); process.env.ROOT_DIR = path.resolve(__dirname, ".."); process.env.SOURCE_DIR = path.resolve(process.env.ROOT_DIR, "src"); -process.env.STATIC_DIR = path.resolve(process.env.ROOT_DIR, "static"); -const InjectScriptPlugin = require("./webpack/plugins/inject-script-webpack-plugin"); const CreateManifestPlugin = require("./webpack/plugins/create-manifest-webpack-plugin"); +const generateScriptBuildConfig = require("./webpack/utils/generate-script-build-config"); const manifest = require(path.resolve(process.env.SOURCE_DIR, "manifest.json")); -const generateScriptBuildConfig = (script, scriptType) => { - const scriptName = script.substring(0, script.length - 3); - let entry = path.resolve(process.env.SOURCE_DIR, `scripts/${scriptName}.ts`); - if (!fs.existsSync(entry)) { - entry = path.resolve(process.env.SOURCE_DIR, `scripts/${scriptName}/index.ts`); - if (!fs.existsSync(entry)) { - entry = path.resolve(process.env.SOURCE_DIR, `scripts/${scriptName}.js`); - } - } - - return { - entry, - mode: "production", - output: { - filename: `${scriptName}.js` - }, - resolve: { - extensions: ["", ".ts", ".js"], - alias: { - "@": process.env.SOURCE_DIR - } - }, - module: { - rules: [ - { - test: /\.tsx?$/, - loader: "ts-loader", - options: { - configFile: "config/tsconfig.json" - } - } - ] - }, - plugins: scriptType.startsWith("injected") - ? [new InjectScriptPlugin({ onload: scriptType === "injected_and_loaded" })] - : undefined - }; -}; - let scripts = []; for (const scriptType in manifest) { @@ -61,7 +20,7 @@ for (const scriptType in manifest) { scripts[scripts.length - 1].plugins = (scripts[scripts.length - 1].plugins || []).concat([ new CopyPlugin({ patterns: [{ - from: process.env.STATIC_DIR, + from: path.resolve(process.env.ROOT_DIR, "static"), to: "./" }] }), diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js index 2ab2721..fade66d 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -2,20 +2,17 @@ const path = require("path"); const { version } = require(path.resolve(process.env.ROOT_DIR, "package.json")); -const scriptTypeRunAtMap = { - injected: "document_start", - rendered: "document_end", - loaded: "document_idle" -}; - class CreateManifestPlugin { constructor(scripts) { - this.scripts = scripts; + this.scripts = {}; this.manifest = require("./manifest-template.json"); - this.scripts.injected = this.scripts.injected - ? { ...this.scripts.injected, ...(this.scripts.injected_and_loaded || {}) } - : this.scripts.injected_and_loaded; - this.scripts.injected_and_loaded = undefined; + this.scripts.document_start = { + ...(scripts.before || {}), + ...(scripts.injected || {}), + ...(scripts.injected_and_loaded || {}) + }; + this.scripts.document_end = scripts.rendered || {}; + this.scripts.document_idle = scripts.loaded || {}; } apply(compiler) { @@ -30,19 +27,18 @@ class CreateManifestPlugin { this.manifest.version = version; this.manifest.content_scripts = [] - for (const scriptType in scriptTypeRunAtMap) { - if (this.scripts[scriptType]) { + for (const scriptType in this.scripts) { + if (Object.keys(this.scripts[scriptType]).length > 0) { this.manifest.content_scripts.push({ matches: ["https://banglarbhumi.gov.in/*", "http://banglarbhumi.gov.in/*"], - js: Object.keys(this.scripts[scriptType]), - run_at: scriptTypeRunAtMap[scriptType] + js: Object.keys(this.scripts[scriptType]).map(scriptName => `${scriptName}.js`), + run_at: scriptType }); } } - const encoder = new TextEncoder("UTF-8"); const { RawSource } = compiler.webpack.sources; - assets["manifest.json"] = new RawSource(Buffer.from(encoder.encode(JSON.stringify(this.manifest).buffer))); + assets["manifest.json"] = new RawSource(Buffer.from(JSON.stringify(this.manifest))); } ); }); diff --git a/config/webpack/plugins/inject-script-webpack-plugin.js b/config/webpack/plugins/inject-script-webpack-plugin.js index 63bab27..ca75213 100644 --- a/config/webpack/plugins/inject-script-webpack-plugin.js +++ b/config/webpack/plugins/inject-script-webpack-plugin.js @@ -1,10 +1,9 @@ const terser = require("terser"); -let count = 0; - class InjectScriptPlugin { - constructor({ onload }) { + constructor({ onload, isFirst }) { this.onload = !!onload; + this.isFirst = !!isFirst; } apply(compiler) { @@ -20,30 +19,37 @@ class InjectScriptPlugin { if (assetName.endsWith(".js")) { const asset = assets[assetName]; let content = asset._source._children; - let x = 2; + 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] += ` const observer = new MutationObserver(mutations => { const script = document.querySelector("head > script:nth-child(54)"); if (script) { - ` - if (count > 0) { - content[x] += "script.innerHTML += `"; - } else { - content[x] += "script.innerHTML = `"; + script.innerHTML + `; + if (!this.isFirst) { + content[x] += "+"; } + content[x] += "= `"; if (this.onload) { content[x] += "document.addEventListener(\"DOMContentLoaded\", () => {"; content[content.length - 3] += "});"; } - content[content.length - 3] += "`;observer.disconnect();}});observer.observe(document, {childList: true, subtree: true});"; + content[content.length - 3] += `\`; + observer.disconnect(); + } + }); + observer.observe(document, {childList: true, subtree: true}); + `; content[x + 1] = (await terser.minify(content[x + 1]._source._source._valueAsString.replaceAll("`", "\\`"))).code; - count++; } } } diff --git a/config/webpack/utils/generate-script-build-config.js b/config/webpack/utils/generate-script-build-config.js new file mode 100644 index 0000000..2485e85 --- /dev/null +++ b/config/webpack/utils/generate-script-build-config.js @@ -0,0 +1,51 @@ +const fs = require("fs"); +const path = require("path"); + +const InjectScriptPlugin = require("../plugins/inject-script-webpack-plugin"); + +const manifest = require(path.resolve(process.env.SOURCE_DIR, "manifest.json")); + +let count = 0; + +const generateScriptBuildConfig = (scriptName, scriptType) => { + let entry = path.resolve(process.env.SOURCE_DIR, `scripts/${scriptName}.ts`); + if (!fs.existsSync(entry)) { + entry = path.resolve(process.env.SOURCE_DIR, `scripts/${scriptName}/index.ts`); + if (!fs.existsSync(entry)) { + entry = path.resolve(process.env.SOURCE_DIR, `scripts/${scriptName}.js`); + } + } + + return { + entry, + mode: "production", + output: { + filename: `${scriptName}.js` + }, + resolve: { + extensions: ["", ".ts", ".js"], + alias: { + "@": process.env.SOURCE_DIR + } + }, + module: { + rules: [ + { + test: /\.tsx?$/, + loader: "ts-loader", + options: { + configFile: "config/tsconfig.json" + } + } + ] + }, + plugins: scriptType.startsWith("injected") + ? [new InjectScriptPlugin({ + onload: scriptType === "injected_and_loaded", + isFirst: count++ <= 0 + })] + : undefined + }; +}; + +module.exports = generateScriptBuildConfig; |
