diff options
| author | Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> | 2023-04-04 18:47:07 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-04 18:47:07 +0530 |
| commit | 6bc8d472a556774630e4432141d5ef9f8ea68c59 (patch) | |
| tree | ce84736dce9c14b06f1adbda867aa6a357a4bbab /config/webpack | |
| parent | d178caee5c93ad9ed91dbd082b0f0b5a9b0507aa (diff) | |
| parent | 35413607c62f2de2a90941d487b7964b5c213999 (diff) | |
| download | banglar-bhumi-utils-6bc8d472a556774630e4432141d5ef9f8ea68c59.tar.gz banglar-bhumi-utils-6bc8d472a556774630e4432141d5ef9f8ea68c59.zip | |
Merge pull request #1 from arpitchakladar/shared-modules
Merging shared modules improvements.
Diffstat (limited to 'config/webpack')
| -rw-r--r-- | config/webpack/loader/arrange-shared-module-loader.js | 26 | ||||
| -rw-r--r-- | config/webpack/loader/count-imports-loader.js | 18 | ||||
| -rw-r--r-- | config/webpack/loader/dynamic-imports-loader.js | 31 | ||||
| -rw-r--r-- | config/webpack/plugins/create-manifest-webpack-plugin/index.js | 20 | ||||
| -rw-r--r-- | config/webpack/plugins/create-manifest-webpack-plugin/manifest-template.json | 9 | ||||
| -rw-r--r-- | config/webpack/plugins/create-rules-webpack-plugin.js | 43 | ||||
| -rw-r--r-- | config/webpack/utils/filename-hash.js (renamed from config/webpack/utils/file-name-hash.js) | 3 | ||||
| -rw-r--r-- | config/webpack/utils/shared-modules.js | 4 |
8 files changed, 111 insertions, 43 deletions
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/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/loader/dynamic-imports-loader.js b/config/webpack/loader/dynamic-imports-loader.js deleted file mode 100644 index 12eb5b7..0000000 --- a/config/webpack/loader/dynamic-imports-loader.js +++ /dev/null @@ -1,31 +0,0 @@ -const { ImportManager } = require("import-manager"); -const { getFileNameHash } = webpackRequire("utils/file-name-hash"); - -const sharedModules = webpackRequire("utils/shared-modules.js"); - -module.exports = source => { - const manager = new ImportManager(source); - let modified = false; - - for (const unit of manager.imports.es6.units) { - const moduleIndex = sharedModules.indexOf(unit.module.name); - - if (moduleIndex >= 0 && unit.type === "es6") { - const importedModuleNames = unit.members.entities - .map(entity => `\t${entity.name}`) - .concat(unit.defaultMembers.entities.map(entity => `\tdefault: ${entity.name}`)) - .join(",\n"); - - const importStatement = `const {\n${importedModuleNames}\n} = await import(/* webpackIgnore: true */ "/shared/${getFileNameHash(sharedModules[moduleIndex], "shared")}.js");\n`; - - unit.methods = { - makeUntraceable() {} - }; - manager.remove(unit); - manager.insertStatement(importStatement, "bottom", "dynamic"); - modified = true; - } - } - - return modified ? manager.code.toString() + "\nexport {};" : source; -}; diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js index 785d604..b4a29a3 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -2,12 +2,16 @@ 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"); class CreateManifestPlugin { + constructor({ sharedModulesImportedCount, sortedSharedModules }) { + this.sharedModulesImportedCount = sharedModulesImportedCount; + this.sortedSharedModules = sortedSharedModules; + } + 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: this.sortedSharedModules + .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}`]; @@ -43,10 +55,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" + }] } } diff --git a/config/webpack/plugins/create-rules-webpack-plugin.js b/config/webpack/plugins/create-rules-webpack-plugin.js new file mode 100644 index 0000000..4a02e2f --- /dev/null +++ b/config/webpack/plugins/create-rules-webpack-plugin.js @@ -0,0 +1,43 @@ +const path = require("path"); +const fs = require("fs"); +const { sources } = require("webpack"); + +class CreateRulesPlugin { + apply(compiler) { + compiler.hooks.compilation.tap("CreateRulesPlugin", compilation => { + compilation.hooks.processAssets.tap( + { + name: "CreateRulesPlugin", + stage: compiler.webpack.Compilation.PROCESS_ASSETS_ADDITIONAL, + additionalAssets: true + }, + assets => { + const ruleFileNames = fs.readdirSync(path.resolve(SOURCE_DIR, "rules")); + const rules = []; + let ruleId = 1; + + for (const ruleFileName of ruleFileNames) { + const rule = require(path.resolve(SOURCE_DIR, "rules", ruleFileName)); + + if (rule instanceof Array) { + for (const rule of rule) { + rule.id = ruleId++; + rules.push(rule); + } + } else { + rule.id = ruleId++; + rules.push(rule); + } + } + + compilation.emitAsset( + "rules.json", + new sources.RawSource(JSON.stringify(rules, undefined, process.env.NODE_ENV === "production" ? undefined : "\t")) + ); + } + ); + }); + } +} + +module.exports = CreateRulesPlugin; diff --git a/config/webpack/utils/file-name-hash.js b/config/webpack/utils/filename-hash.js index 889e9a4..bd66647 100644 --- a/config/webpack/utils/file-name-hash.js +++ b/config/webpack/utils/filename-hash.js @@ -1,10 +1,9 @@ const crypto = require("crypto"); module.exports.getFileNameHash = (fileName, prefix) => { - const finalFileName = `${prefix}-${fileName}`; const hash = crypto .createHash("md5") - .update(finalFileName) + .update(`${prefix}-${fileName}`) .digest("hex"); return production ? hash : `${fileName}-${hash.substring(16)}`; 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; |
