aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/plugins
diff options
context:
space:
mode:
authorArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-02-06 19:30:21 +0530
committerArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-02-06 19:30:21 +0530
commitc57a9e3d9e304bd360437f8f732dbc0479376cd7 (patch)
treead9e31238aab26fd7a8ceb54d1424daa79a237c9 /config/webpack/plugins
parent1a301791bb621fa01fa76e9403b23fe1f4a120af (diff)
downloadbanglar-bhumi-utils-c57a9e3d9e304bd360437f8f732dbc0479376cd7.tar.gz
banglar-bhumi-utils-c57a9e3d9e304bd360437f8f732dbc0479376cd7.zip
Fixed some bugs... Updated some code...
Diffstat (limited to 'config/webpack/plugins')
-rw-r--r--config/webpack/plugins/create-manifest-webpack-plugin/index.js30
-rw-r--r--config/webpack/plugins/inject-script-webpack-plugin.js28
2 files changed, 30 insertions, 28 deletions
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++;
}
}
}