aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/plugins
diff options
context:
space:
mode:
authorArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-04-04 18:47:07 +0530
committerGitHub <noreply@github.com>2023-04-04 18:47:07 +0530
commit6bc8d472a556774630e4432141d5ef9f8ea68c59 (patch)
treece84736dce9c14b06f1adbda867aa6a357a4bbab /config/webpack/plugins
parentd178caee5c93ad9ed91dbd082b0f0b5a9b0507aa (diff)
parent35413607c62f2de2a90941d487b7964b5c213999 (diff)
downloadbanglar-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/plugins')
-rw-r--r--config/webpack/plugins/create-manifest-webpack-plugin/index.js20
-rw-r--r--config/webpack/plugins/create-manifest-webpack-plugin/manifest-template.json9
-rw-r--r--config/webpack/plugins/create-rules-webpack-plugin.js43
3 files changed, 65 insertions, 7 deletions
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;