From 4993aaf41e132dd0805bf6c487901d9f2aecfcfe Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Wed, 8 Feb 2023 13:54:04 +0530 Subject: Combining similar scripts into 1 for improved performence and less files in build. --- .../create-manifest-webpack-plugin/index.js | 34 +++++++++++++++------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'config/webpack/plugins/create-manifest-webpack-plugin') diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js index 5dac1b2..4a07380 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -2,17 +2,31 @@ const path = require("path"); const { version } = require(path.resolve(process.env.ROOT_DIR, "package.json")); +const getRunAt = scriptType => { + switch (scriptType) { + case "before": + case "injected": + case "injected_and_loaded": + return "document_start"; + + case "rendered": + return "document_end"; + + case "loaded": + default: + return "document_idle"; + } +}; + class CreateManifestPlugin { - constructor(scripts) { + constructor({ scripts }) { this.scripts = {}; this.manifest = require("./manifest-template.json"); - this.scripts.document_start = { - ...(scripts.before || {}), - ...(scripts.injected || {}), - ...(scripts.injected_and_loaded || {}) - }; - this.scripts.document_end = scripts.rendered || {}; - this.scripts.document_idle = scripts.loaded || {}; + this.scripts.before = scripts.before || {}; + this.scripts.injected = scripts.injected || {}; + this.scripts.injected_and_loaded = scripts.injected_and_loaded || {}; + this.scripts.rendered = scripts.rendered || {}; + this.scripts.loaded = scripts.loaded || {}; } apply(compiler) { @@ -43,8 +57,8 @@ class CreateManifestPlugin { for (const scriptPath in paths) { this.manifest.content_scripts.push({ matches: [`*://banglarbhumi.gov.in/${scriptPath}`], - js: paths[scriptPath].map(scriptName => `${scriptName}.js`), - run_at: scriptType + js: [`${scriptType}.js`], + run_at: getRunAt(scriptType) }); } } -- cgit v1.2.3