aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/plugins/create-manifest-webpack-plugin/index.js
diff options
context:
space:
mode:
authorArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-02-05 22:57:54 +0530
committerArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-02-05 22:57:54 +0530
commit1a301791bb621fa01fa76e9403b23fe1f4a120af (patch)
treeebfff3e1b7c366c738750644f4fdefaa4fef21b7 /config/webpack/plugins/create-manifest-webpack-plugin/index.js
parentc690f47a786eef27900873ac9fe75134995f156a (diff)
downloadbanglar-bhumi-utils-1a301791bb621fa01fa76e9403b23fe1f4a120af.tar.gz
banglar-bhumi-utils-1a301791bb621fa01fa76e9403b23fe1f4a120af.zip
Added a more extensive way to automate script development.
Diffstat (limited to 'config/webpack/plugins/create-manifest-webpack-plugin/index.js')
-rw-r--r--config/webpack/plugins/create-manifest-webpack-plugin/index.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js
new file mode 100644
index 0000000..2ab2721
--- /dev/null
+++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js
@@ -0,0 +1,52 @@
+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.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;
+ }
+
+ apply(compiler) {
+ compiler.hooks.compilation.tap("CreateManifestPlugin", compilation => {
+ compilation.hooks.processAssets.tap(
+ {
+ name: "CreateManifestPlugin",
+ stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
+ additionalAssets: true
+ },
+ assets => {
+ this.manifest.version = version;
+ this.manifest.content_scripts = []
+
+ for (const scriptType in scriptTypeRunAtMap) {
+ if (this.scripts[scriptType]) {
+ this.manifest.content_scripts.push({
+ matches: ["https://banglarbhumi.gov.in/*", "http://banglarbhumi.gov.in/*"],
+ js: Object.keys(this.scripts[scriptType]),
+ run_at: scriptTypeRunAtMap[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)));
+ }
+ );
+ });
+ }
+}
+
+module.exports = CreateManifestPlugin;