aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/plugins
diff options
context:
space:
mode:
authorArpit Chakladar <arpitchakladar+git@gmail.com>2026-07-13 23:32:26 +0530
committerGitHub <noreply@github.com>2026-07-13 23:32:26 +0530
commitce401d03e75bcbfd1cdfa963954e0807219fb80d (patch)
tree52a813c1705a952312c20c328b00fa845a3453f9 /config/webpack/plugins
parent98e7019c61ea0ffe344ef7ce809d38d62254a9fc (diff)
parentdfba7ff6fff7bde33b40a343a2318b35c429b40f (diff)
downloadbanglar-bhumi-utils-ce401d03e75bcbfd1cdfa963954e0807219fb80d.tar.gz
banglar-bhumi-utils-ce401d03e75bcbfd1cdfa963954e0807219fb80d.zip
Merge pull request #6 from arpitchakladar/adding-documentation-comments
Adding documentation comments
Diffstat (limited to 'config/webpack/plugins')
-rw-r--r--config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js25
-rw-r--r--config/webpack/plugins/create-manifest-webpack-plugin/index.js48
-rw-r--r--config/webpack/plugins/create-rules-webpack-plugin.js16
-rw-r--r--config/webpack/plugins/inject-script-webpack-plugin.js23
4 files changed, 85 insertions, 27 deletions
diff --git a/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js b/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js
index 695d4ef..c94ae86 100644
--- a/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js
+++ b/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js
@@ -1,31 +1,44 @@
import path from "path";
-import fs from "fs";
+
import webpack from "webpack";
import { getFileName } from "../utils/build-file.js";
+/**
+ * Webpack plugin that generates an injected shared-modules bundle.
+ * After all assets are processed, it creates a small JS file that
+ * calls the script injector for each shared module that was imported
+ * by injected scripts.
+ */
class CreateInjectedSharedModulesPlugin {
+ /**
+ * @param {{ injectedSharedModulesImportedCount: Record<string, number>, sortedSharedModules: string[] }} options
+ */
constructor({ injectedSharedModulesImportedCount, sortedSharedModules }) {
this.injectedSharedModulesImportedCount = injectedSharedModulesImportedCount;
this.sortedSharedModules = sortedSharedModules;
}
+ /**
+ * @param {import("webpack").Compiler} compiler
+ * @returns {void}
+ */
apply(compiler) {
- compiler.hooks.compilation.tap("CreateInjectedSharedModulesPlugin", compilation => {
+ compiler.hooks.compilation.tap("CreateInjectedSharedModulesPlugin", (compilation) => {
compilation.hooks.processAssets.tap(
{
name: "CreateInjectedSharedModulesPlugin",
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
},
- assets => {
+ (_assets) => {
const injectedSharedScripts = this.sortedSharedModules
- .filter(sharedModule => this.injectedSharedModulesImportedCount[sharedModule] > 0)
- .map(sharedModule => `shared/${getFileName(sharedModule, "shared")}.js`);
+ .filter((sharedModule) => this.injectedSharedModulesImportedCount[sharedModule] > 0)
+ .map((sharedModule) => `shared/${getFileName(sharedModule, "shared")}.js`);
if (injectedSharedScripts.length > 0) {
const scriptInjectorModuleName = getFileName("script-injector", "shared", true);
const injectionCode = injectedSharedScripts
- .map(injectedScript => `$${scriptInjectorModuleName}.injectScriptHead("shared/${path.basename(injectedScript)}");`)
+ .map((injectedScript) => `$${scriptInjectorModuleName}.injectScriptHead("shared/${path.basename(injectedScript)}");`)
.join("");
compilation.emitAsset(
`shared/${getFileName("injected-shared-modules", "shared")}.js`,
diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js
index 6f1e2be..95d0be8 100644
--- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js
+++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js
@@ -1,31 +1,53 @@
-import webpack from "webpack";
-import path from "path";
import fs from "fs";
+import path from "path";
+
+import webpack from "webpack";
-import { getScriptRuntimeFromType } from "../../utils/script-runtime.js";
import { getFileName } from "../../utils/build-file.js";
+import { getScriptRuntimeFromType } from "../../utils/script-runtime.js";
import scripts from "../../utils/scripts.js";
+
const manifest = JSON.parse(
fs.readFileSync(
- path.resolve("config/webpack/plugins/create-manifest-webpack-plugin/manifest-template.json"),
- ),
+ path.resolve("config/webpack/plugins/create-manifest-webpack-plugin/manifest-template.json")
+ )
);
+/**
+ * Webpack plugin that dynamically generates the extension's
+ * `manifest.json` asset. It reads the template, resolves the
+ * current version from `package.json`, and builds `content_scripts`
+ * entries by matching script definitions from `scripts.json` against
+ * page URL patterns. Shared modules, injected scripts, and
+ * web-accessible resources are added automatically based on import
+ * counts collected during the build.
+ */
class CreateManifestPlugin {
+ /**
+ * @param {{
+ * sharedModulesImportedCount: Record<string, number>,
+ * injectedSharedModulesImportedCount: Record<string, number>,
+ * sortedSharedModules: string[]
+ * }} options
+ */
constructor({ sharedModulesImportedCount, injectedSharedModulesImportedCount, sortedSharedModules }) {
this.sharedModulesImportedCount = sharedModulesImportedCount;
this.injectedSharedModulesImportedCount = injectedSharedModulesImportedCount;
this.sortedSharedModules = sortedSharedModules;
}
+ /**
+ * @param {import("webpack").Compiler} compiler
+ * @returns {void}
+ */
apply(compiler) {
- compiler.hooks.compilation.tap("CreateManifestPlugin", compilation => {
+ compiler.hooks.compilation.tap("CreateManifestPlugin", (compilation) => {
compilation.hooks.processAssets.tap(
{
name: "CreateManifestPlugin",
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
},
- assets => {
+ (assets) => {
manifest.version = JSON.parse(fs.readFileSync((path.resolve(ROOT_DIR, "package.json")))).version;
manifest.content_scripts = [];
let resources = [];
@@ -50,12 +72,12 @@ class CreateManifestPlugin {
}
manifest.content_scripts.push({
- matches: [`*://banglarbhumi.gov.in/BanglarBhumi/*`],
+ matches: ["*://banglarbhumi.gov.in/BanglarBhumi/*"],
js: this.sortedSharedModules
- .filter(sharedModule => this.sharedModulesImportedCount[sharedModule] > 0)
- .map(sharedModule => `shared/${getFileName(sharedModule, "shared")}.js`)
+ .filter((sharedModule) => this.sharedModulesImportedCount[sharedModule] > 0)
+ .map((sharedModule) => `shared/${getFileName(sharedModule, "shared")}.js`)
.concat(arrangedScripts["*"]["document_start"])
- .filter(x => x != null),
+ .filter((x) => x != null),
run_at: "document_start"
});
@@ -83,8 +105,8 @@ class CreateManifestPlugin {
}
const injectedSharedModules = this.sortedSharedModules
- .filter(sharedModule => this.injectedSharedModulesImportedCount[sharedModule] > 0)
- .map(sharedModule => `shared/${getFileName(sharedModule, "shared")}.js`);
+ .filter((sharedModule) => this.injectedSharedModulesImportedCount[sharedModule] > 0)
+ .map((sharedModule) => `shared/${getFileName(sharedModule, "shared")}.js`);
if (injectedSharedModules.length > 0) {
resources = resources.concat(injectedSharedModules);
diff --git a/config/webpack/plugins/create-rules-webpack-plugin.js b/config/webpack/plugins/create-rules-webpack-plugin.js
index 5573ddf..ce2fbef 100644
--- a/config/webpack/plugins/create-rules-webpack-plugin.js
+++ b/config/webpack/plugins/create-rules-webpack-plugin.js
@@ -1,16 +1,26 @@
-import path from "path";
import fs from "fs";
+import path from "path";
+
import webpack from "webpack";
+/**
+ * Webpack plugin that reads all JSON rule files from `src/rules/`,
+ * assigns sequential IDs to each rule, and emits a single `rules.json`
+ * asset used by `declarativeNetRequest`.
+ */
class CreateRulesPlugin {
+ /**
+ * @param {import("webpack").Compiler} compiler
+ * @returns {void}
+ */
apply(compiler) {
- compiler.hooks.compilation.tap("CreateRulesPlugin", compilation => {
+ compiler.hooks.compilation.tap("CreateRulesPlugin", (compilation) => {
compilation.hooks.processAssets.tap(
{
name: "CreateRulesPlugin",
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
},
- assets => {
+ (_assets) => {
const ruleFileNames = fs.readdirSync(path.resolve(SOURCE_DIR, "rules"));
const rules = [];
let ruleId = 1;
diff --git a/config/webpack/plugins/inject-script-webpack-plugin.js b/config/webpack/plugins/inject-script-webpack-plugin.js
index 2b60022..16b2e98 100644
--- a/config/webpack/plugins/inject-script-webpack-plugin.js
+++ b/config/webpack/plugins/inject-script-webpack-plugin.js
@@ -1,29 +1,42 @@
-import webpack from "webpack";
import path from "path";
+import webpack from "webpack";
+
import { getFileName } from "../utils/build-file.js";
import { getInjectedCode } from "../utils/injected-code.js";
+/**
+ * Webpack plugin that post-processes every `.js` asset to replace
+ * extension-asset URL placeholders with runtime lookup code, and
+ * emits an additional "injected" script that the content script
+ * injects into the page via the script-injector module.
+ */
class InjectScriptPlugin {
+ /**
+ * @param {import("webpack").Compiler} compiler
+ * @returns {void}
+ */
apply(compiler) {
- compiler.hooks.compilation.tap("InjectScriptPlugin", compilation => {
+ compiler.hooks.compilation.tap("InjectScriptPlugin", (compilation) => {
compilation.hooks.processAssets.tapPromise(
{
name: "InjectScriptPlugin",
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
additionalAssets: true
},
- async assets => {
+ async(assets) => {
for (const assetName in assets) {
if (/\.js$/.test(assetName)) {
- const injectedCodeResponse = getInjectedCode(compilation.getAsset(assetName).source.source())
+ const assetSource = compilation.getAsset(assetName).source.source();
+ const injectedCodeResponse = getInjectedCode(assetSource);
const scriptInjectorModuleName = getFileName("script-injector", "shared", true);
compilation.updateAsset(
assetName,
new webpack.sources.RawSource(injectedCodeResponse[0])
);
+ const injectedFileName = `scripts/injected/${path.basename(assetName)}`;
assets["scripts/" + path.basename(assetName)] = new webpack.sources.RawSource(
- `$${scriptInjectorModuleName}.injectScriptHead("scripts/injected/${path.basename(assetName)}", ${injectedCodeResponse[1]});`
+ `$${scriptInjectorModuleName}.injectScriptHead("${injectedFileName}", ${injectedCodeResponse[1]});`
);
}
}