From 5f23f07d04073c06af530332c2d75b48d6c023a0 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Tue, 4 Apr 2023 14:53:04 +0530 Subject: Using different global variables for shared modules. --- .../plugins/create-manifest-webpack-plugin/index.js | 14 +++++++++----- .../create-manifest-webpack-plugin/manifest-template.json | 9 ++++++++- 2 files changed, 17 insertions(+), 6 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 785d604..a2040da 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -2,7 +2,7 @@ const { sources } = require("webpack"); const path = require("path"); const { getScriptRuntimeFromType } = webpackRequire("utils/script-runtime"); -const { getFileNameHash } = webpackRequire("utils/file-name-hash"); +const { getFileNameHash } = webpackRequire("utils/filename-hash"); const scripts = webpackRequire("utils/scripts"); const sharedModules = webpackRequire("utils/shared-modules"); const manifest = webpackRequire("plugins/create-manifest-webpack-plugin/manifest-template.json"); @@ -35,6 +35,14 @@ class CreateManifestPlugin { } } + for (const sharedModule of sharedModules) { + manifest.content_scripts.push({ + matches: [`*://banglarbhumi.gov.in/BanglarBhumi/*`], + js: [`shared/${getFileNameHash(sharedModule, "shared")}.js`], + run_at: "document_start" + }); + } + const resources = []; for (const assetName in assets) { @@ -43,10 +51,6 @@ class CreateManifestPlugin { } } - for (const sharedModule of sharedModules) { - resources.push(`shared/${getFileNameHash(sharedModule, "shared")}.js`); - } - if (resources.length > 0) { manifest.web_accessible_resources = [ { diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/manifest-template.json b/config/webpack/plugins/create-manifest-webpack-plugin/manifest-template.json index 0c59ef2..d1d29f4 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/manifest-template.json +++ b/config/webpack/plugins/create-manifest-webpack-plugin/manifest-template.json @@ -1,9 +1,16 @@ { "manifest_version": 3, "name": "Banglar Bhumi Utils", - "permissions": ["scripting"], + "permissions": ["scripting", "declarativeNetRequest"], "description": "A chrome extension to improve functionality of the banglarbhumi.gov.in website.", "icons": { "512": "icon512.png" + }, + "declarative_net_request" : { + "rule_resources" : [{ + "id": "rules", + "enabled": true, + "path": "rules.json" + }] } } -- cgit v1.2.3 From 7455c1e81dd18a898004c398a16e9f0b6f7c19ae Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Tue, 4 Apr 2023 16:05:48 +0530 Subject: Keeping track of the number of times that a shared module has been used and dertermining if it is to be included along with the other scripts. --- config/webpack.config.js | 32 +++++++++++++++------- config/webpack/loader/count-imports-loader.js | 18 ++++++++++++ .../create-manifest-webpack-plugin/index.js | 20 ++++++++------ config/webpack/utils/shared-modules.js | 4 +-- 4 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 config/webpack/loader/count-imports-loader.js (limited to 'config/webpack/plugins/create-manifest-webpack-plugin') diff --git a/config/webpack.config.js b/config/webpack.config.js index 22544b9..98315b0 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -22,6 +22,12 @@ const scripts = webpackRequire("utils/scripts"); const sharedModules = webpackRequire("utils/shared-modules"); +const sharedModulesImportedCount = {}; + +for (const sharedModule of sharedModules) { + sharedModulesImportedCount[sharedModule] = 0; +} + const uninjectedScriptEntries = {}; const injectedAfterScriptEntries = {}; const injectedBeforeScriptEntries = {}; @@ -94,20 +100,25 @@ const commonOptions = { } }; -const ecmaScriptModuleOptions = { - experiments: { - outputModule: true - } -}; - const sharedModuleOptions = { externals: sharedModuleExternals, - externalsType: "var" + externalsType: "var", + module: { + rules: [ + { + test: /\.(j|t)s$/, + loader: path.resolve(CONFIG_DIR, "webpack/loader/count-imports-loader.js"), + options: { + sharedModulesImportedCount + }, + enforce: "post" + } + ] + }, }; const uninjectedScriptConfiguration = merge( commonOptions, - ecmaScriptModuleOptions, sharedModuleOptions, { entry: uninjectedScriptEntries, @@ -144,12 +155,13 @@ const injectedBeforeScriptConfiguration = merge( const sharedModulesConfiguration = merge( commonOptions, - ecmaScriptModuleOptions, sharedModuleOptions, { entry: sharedModuleEntries, plugins: [ - new CreateManifestPlugin(), + new CreateManifestPlugin({ + sharedModulesImportedCount + }), new CreateRulesPlugin() ] } diff --git a/config/webpack/loader/count-imports-loader.js b/config/webpack/loader/count-imports-loader.js new file mode 100644 index 0000000..4c3faa4 --- /dev/null +++ b/config/webpack/loader/count-imports-loader.js @@ -0,0 +1,18 @@ +const { ImportManager } = require("import-manager"); + +const sharedModules = webpackRequire("utils/shared-modules.js"); + +module.exports = function(source) { + const { sharedModulesImportedCount } = this.getOptions(); + const manager = new ImportManager(source); + + for (const unit of manager.imports.es6.units) { + const moduleIndex = sharedModules.indexOf(unit.module.name); + + if (moduleIndex >= 0) { + sharedModulesImportedCount[sharedModules[moduleIndex]]++; + } + } + + return source; +}; diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js index a2040da..103fb3c 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -8,6 +8,10 @@ const sharedModules = webpackRequire("utils/shared-modules"); const manifest = webpackRequire("plugins/create-manifest-webpack-plugin/manifest-template.json"); class CreateManifestPlugin { + constructor({ sharedModulesImportedCount }) { + this.sharedModulesImportedCount = sharedModulesImportedCount; + } + apply(compiler) { compiler.hooks.compilation.tap("CreateManifestPlugin", compilation => { compilation.hooks.processAssets.tap( @@ -20,6 +24,14 @@ class CreateManifestPlugin { manifest.version = require(path.resolve(ROOT_DIR, "package.json")).version; manifest.content_scripts = []; + manifest.content_scripts.push({ + matches: [`*://banglarbhumi.gov.in/BanglarBhumi/*`], + js: sharedModules + .filter(sharedModule => this.sharedModulesImportedCount[sharedModule] > 0) + .map(sharedModule => `shared/${getFileNameHash(sharedModule, "shared")}.js`), + run_at: "document_start" + }); + for (const scriptPath in scripts) { for (const scriptType in scripts[scriptPath]) { const matches = [`*://banglarbhumi.gov.in/BanglarBhumi/${scriptPath}`]; @@ -35,14 +47,6 @@ class CreateManifestPlugin { } } - for (const sharedModule of sharedModules) { - manifest.content_scripts.push({ - matches: [`*://banglarbhumi.gov.in/BanglarBhumi/*`], - js: [`shared/${getFileNameHash(sharedModule, "shared")}.js`], - run_at: "document_start" - }); - } - const resources = []; for (const assetName in assets) { diff --git a/config/webpack/utils/shared-modules.js b/config/webpack/utils/shared-modules.js index 974145f..1584f0f 100644 --- a/config/webpack/utils/shared-modules.js +++ b/config/webpack/utils/shared-modules.js @@ -1,8 +1,6 @@ const path = require("path"); const fs = require("fs"); -const sharedModules = fs.readdirSync(path.resolve(SOURCE_DIR, "shared")) +module.exports = fs.readdirSync(path.resolve(SOURCE_DIR, "shared")) .filter(sharedModule => !sharedModule.endsWith("import-shared.js")) .map(sharedModule => sharedModule.substring(0, sharedModule.length - 3)); - -module.exports = sharedModules; -- cgit v1.2.3 From 35413607c62f2de2a90941d487b7964b5c213999 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Tue, 4 Apr 2023 18:45:18 +0530 Subject: Arranging shared modules in the scripts in order to make sure that the dependencies are available when shared modules references each other. --- config/webpack.config.js | 19 ++++++++++++++-- .../webpack/loader/arrange-shared-module-loader.js | 26 ++++++++++++++++++++++ .../create-manifest-webpack-plugin/index.js | 6 ++--- 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 config/webpack/loader/arrange-shared-module-loader.js (limited to 'config/webpack/plugins/create-manifest-webpack-plugin') diff --git a/config/webpack.config.js b/config/webpack.config.js index 98315b0..15cae74 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -153,6 +153,8 @@ const injectedBeforeScriptConfiguration = merge( } ); +const sortedSharedModules = [] + const sharedModulesConfiguration = merge( commonOptions, sharedModuleOptions, @@ -160,10 +162,23 @@ const sharedModulesConfiguration = merge( entry: sharedModuleEntries, plugins: [ new CreateManifestPlugin({ - sharedModulesImportedCount + sharedModulesImportedCount, + sortedSharedModules }), new CreateRulesPlugin() - ] + ], + module: { + rules: [ + { + test: /\.(j|t)s$/, + loader: path.resolve(CONFIG_DIR, "webpack/loader/arrange-shared-module-loader.js"), + options: { + sortedSharedModules + }, + enforce: "post" + } + ] + } } ); diff --git a/config/webpack/loader/arrange-shared-module-loader.js b/config/webpack/loader/arrange-shared-module-loader.js new file mode 100644 index 0000000..9e21b23 --- /dev/null +++ b/config/webpack/loader/arrange-shared-module-loader.js @@ -0,0 +1,26 @@ +const path = require("path"); + +const { ImportManager } = require("import-manager"); + +const sharedModules = webpackRequire("utils/shared-modules.js"); + +module.exports = function(source) { + const { sortedSharedModules } = this.getOptions(); + const manager = new ImportManager(source); + + for (const unit of manager.imports.es6.units) { + const moduleIndex = sharedModules.indexOf(unit.module.name); + + if (moduleIndex >= 0) { + const currentSharedModuleName = sharedModules[moduleIndex]; + + if (!sortedSharedModules.includes(currentSharedModuleName)) { + sortedSharedModules.push(currentSharedModuleName); + } + } + } + + sortedSharedModules.push(path.basename(this.resourcePath.substring(0, this.resourcePath.length - 3))); + + return source; +}; diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js index 103fb3c..b4a29a3 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -4,12 +4,12 @@ const path = require("path"); const { getScriptRuntimeFromType } = webpackRequire("utils/script-runtime"); const { getFileNameHash } = webpackRequire("utils/filename-hash"); const scripts = webpackRequire("utils/scripts"); -const sharedModules = webpackRequire("utils/shared-modules"); const manifest = webpackRequire("plugins/create-manifest-webpack-plugin/manifest-template.json"); class CreateManifestPlugin { - constructor({ sharedModulesImportedCount }) { + constructor({ sharedModulesImportedCount, sortedSharedModules }) { this.sharedModulesImportedCount = sharedModulesImportedCount; + this.sortedSharedModules = sortedSharedModules; } apply(compiler) { @@ -26,7 +26,7 @@ class CreateManifestPlugin { manifest.content_scripts.push({ matches: [`*://banglarbhumi.gov.in/BanglarBhumi/*`], - js: sharedModules + js: this.sortedSharedModules .filter(sharedModule => this.sharedModulesImportedCount[sharedModule] > 0) .map(sharedModule => `shared/${getFileNameHash(sharedModule, "shared")}.js`), run_at: "document_start" -- cgit v1.2.3