From 600c337b1cdc884aebf59083eb57b178b43441ce Mon Sep 17 00:00:00 2001 From: Arpit Chakladar Date: Mon, 13 Jul 2026 11:35:08 +0530 Subject: docs: added some documentation comments to all functions --- config/webpack.config.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'config/webpack.config.js') diff --git a/config/webpack.config.js b/config/webpack.config.js index 8fd3f15..347d204 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -1,3 +1,17 @@ +/** + * Webpack configuration for the Banglar Bhumi Utils Chrome extension. + * + * Produces four separate bundles: + * 1. **Background script** – service worker entry point. + * 2. **Uninjected scripts** – content scripts that run as-is. + * 3. **Injected scripts** – content scripts that are further processed + * so they can be injected into the page DOM. + * 4. **Shared modules** – libraries compiled as standalone files and + * referenced via global variables (`$`). + * + * Custom loaders and plugins handle dependency ordering, import counting, + * asset-URL rewriting, manifest generation, and declarative-net-rule creation. + */ import path from "path"; import fs from "fs"; import { fileURLToPath } from "url"; -- cgit v1.2.3 From 51a443e5d027ced43a6d7c8759e7aae6fd90839f Mon Sep 17 00:00:00 2001 From: Arpit Chakladar Date: Mon, 13 Jul 2026 18:44:47 +0530 Subject: refactor: updated flake.nix, tsconfig.json and added .editorconfig made some changes to the environment to make the development environment more reproduceable. --- .editorconfig | 47 ++++++++++++++++++ config/tsconfig.json | 26 ---------- config/webpack.config.js | 2 +- flake.nix | 32 +++++++------ package-lock.json | 121 +---------------------------------------------- tsconfig.json | 26 ++++++++++ 6 files changed, 93 insertions(+), 161 deletions(-) create mode 100644 .editorconfig delete mode 100644 config/tsconfig.json create mode 100644 tsconfig.json (limited to 'config/webpack.config.js') diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..fde1ee4 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,47 @@ +root = true + +[*] +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.ts] +indent_style = tab +indent_size = 2 +tab_width = 2 + +[*.js] +indent_style = tab +indent_size = 2 +tab_width = 2 + +[*.html] +indent_style = tab +indent_size = 2 +tab_width = 2 + +[*.css] +indent_style = tab +indent_size = 2 +tab_width = 2 + +[*.json] +indent_style = tab +indent_size = 2 +tab_width = 2 + +[*.{yml,yaml}] +indent_style = tab +indent_size = 2 +tab_width = 2 + +[*.md] +indent_style = tab +indent_size = 2 +tab_width = 2 + +[*.nix] +indent_style = space +indent_size = 2 +tab_width = 2 diff --git a/config/tsconfig.json b/config/tsconfig.json deleted file mode 100644 index ec61595..0000000 --- a/config/tsconfig.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "files": [ - "../src/html.d.ts" - ], - "compilerOptions": { - "outDir": "dist", - "sourceMap": true, - "strict": true, - "noImplicitReturns": true, - "noImplicitAny": true, - "module": "es2022", - "moduleResolution": "node", - "target": "es2021", - "allowJs": true, - "allowSyntheticDefaultImports": true, - "skipLibCheck": true, - "moduleDetection": "force", - "baseUrl": "../", - "paths": { - "@/*": ["src/*"] - } - }, - "exclude": [ - "../node_modules/**/*" - ] -} diff --git a/config/webpack.config.js b/config/webpack.config.js index 347d204..b0c1641 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -118,7 +118,7 @@ const commonOptions = { test: /\.ts$/, loader: "ts-loader", options: { - configFile: "config/tsconfig.json", + configFile: "tsconfig.json", } }, { diff --git a/flake.nix b/flake.nix index a3ce275..27215c7 100644 --- a/flake.nix +++ b/flake.nix @@ -1,19 +1,21 @@ { - description = "Flake for development environment for better-containers."; + description = "Flake for development environment for better-containers."; - inputs = { - nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; - }; + inputs = { + nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; + }; - outputs = { self, nixpkgs }: - let - pkgs = nixpkgs.legacyPackages."x86_64-linux"; - in { - devShells."x86_64-linux".default = pkgs.mkShell { - packages = with pkgs; [ - nodejs_22 - nodePackages.typescript-language-server - ]; - }; - }; + outputs = { self, nixpkgs }: + let + pkgs = nixpkgs.legacyPackages."x86_64-linux"; + in { + devShells."x86_64-linux".default = pkgs.mkShell { + packages = with pkgs; [ + nodejs_22 + vscode-langservers-extracted + tailwindcss-language-server + vtsls + ]; + }; + }; } diff --git a/package-lock.json b/package-lock.json index 26574cd..d413d16 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,13 @@ { "name": "banglar-bhumi-utils", - "version": "1.1.0", + "version": "2.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "banglar-bhumi-utils", - "version": "1.1.0", + "version": "2.0.0", "license": "MIT", - "dependencies": { - "tesseract.js": "^6.0.1" - }, "devDependencies": { "@types/chrome": "^0.0.213", "@types/jquery": "^3.5.16", @@ -520,12 +517,6 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, - "node_modules/bmp-js": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/bmp-js/-/bmp-js-0.1.0.tgz", - "integrity": "sha512-vHdS19CnY3hwiNdkaqk93DvjVLfbEcI8mys4UjuWrlX1haDmroo8o4xCzh4wD6DGV6HxRCyauwhHRqMTfERtjw==", - "license": "MIT" - }, "node_modules/braces": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", @@ -958,12 +949,6 @@ "node": ">=8" } }, - "node_modules/idb-keyval": { - "version": "6.2.2", - "resolved": "https://registry.npmjs.org/idb-keyval/-/idb-keyval-6.2.2.tgz", - "integrity": "sha512-yjD9nARJ/jb1g+CvD0tlhUHOrJ9Sy0P8T9MF3YaLlHnSRpwPfpTX0XIvpmw3gAJUmEu3FiICLBDPXVwyEvrleg==", - "license": "Apache-2.0" - }, "node_modules/ignore": { "version": "5.2.4", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.2.4.tgz", @@ -1067,12 +1052,6 @@ "node": ">=0.10.0" } }, - "node_modules/is-url": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/is-url/-/is-url-1.2.4.tgz", - "integrity": "sha512-ITvGim8FhRiYe4IQ5uHSkj7pVaPDrCTkNd3yq3cV7iZAcJdHTUMPMEHcqSOy9xZ9qFenQCvi+2wjH9a1nXqHww==", - "license": "MIT" - }, "node_modules/isexe": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", @@ -1238,26 +1217,6 @@ "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==", "dev": true }, - "node_modules/node-fetch": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", - "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", - "license": "MIT", - "dependencies": { - "whatwg-url": "^5.0.0" - }, - "engines": { - "node": "4.x || >=6.0.0" - }, - "peerDependencies": { - "encoding": "^0.1.0" - }, - "peerDependenciesMeta": { - "encoding": { - "optional": true - } - } - }, "node_modules/node-releases": { "version": "2.0.9", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.9.tgz", @@ -1273,15 +1232,6 @@ "node": ">=0.10.0" } }, - "node_modules/opencollective-postinstall": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/opencollective-postinstall/-/opencollective-postinstall-2.0.3.tgz", - "integrity": "sha512-8AV/sCtuzUeTo8gQK5qDZzARrulB3egtLzFgteqB2tcT4Mw7B8Kt7JcDHmltjz6FOAHsvTevk70gZEbhM4ZS9Q==", - "license": "MIT", - "bin": { - "opencollective-postinstall": "index.js" - } - }, "node_modules/p-limit": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz", @@ -1449,12 +1399,6 @@ "node": ">= 10.13.0" } }, - "node_modules/regenerator-runtime": { - "version": "0.13.11", - "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz", - "integrity": "sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==", - "license": "MIT" - }, "node_modules/require-from-string": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", @@ -1796,30 +1740,6 @@ "url": "https://opencollective.com/webpack" } }, - "node_modules/tesseract.js": { - "version": "6.0.1", - "resolved": "https://registry.npmjs.org/tesseract.js/-/tesseract.js-6.0.1.tgz", - "integrity": "sha512-/sPvMvrCtgxnNRCjbTYbr7BRu0yfWDsMZQ2a/T5aN/L1t8wUQN6tTWv6p6FwzpoEBA0jrN2UD2SX4QQFRdoDbA==", - "hasInstallScript": true, - "license": "Apache-2.0", - "dependencies": { - "bmp-js": "^0.1.0", - "idb-keyval": "^6.2.0", - "is-url": "^1.2.4", - "node-fetch": "^2.6.9", - "opencollective-postinstall": "^2.0.3", - "regenerator-runtime": "^0.13.3", - "tesseract.js-core": "^6.0.0", - "wasm-feature-detect": "^1.2.11", - "zlibjs": "^0.3.1" - } - }, - "node_modules/tesseract.js-core": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/tesseract.js-core/-/tesseract.js-core-6.0.0.tgz", - "integrity": "sha512-1Qncm/9oKM7xgrQXZXNB+NRh19qiXGhxlrR8EwFbK5SaUbPZnS5OMtP/ghtqfd23hsr1ZvZbZjeuAGcMxd/ooA==", - "license": "Apache-2.0" - }, "node_modules/to-regex-range": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", @@ -1832,12 +1752,6 @@ "node": ">=8.0" } }, - "node_modules/tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==", - "license": "MIT" - }, "node_modules/ts-loader": { "version": "9.4.2", "resolved": "https://registry.npmjs.org/ts-loader/-/ts-loader-9.4.2.tgz", @@ -1911,12 +1825,6 @@ "punycode": "^2.1.0" } }, - "node_modules/wasm-feature-detect": { - "version": "1.8.0", - "resolved": "https://registry.npmjs.org/wasm-feature-detect/-/wasm-feature-detect-1.8.0.tgz", - "integrity": "sha512-zksaLKM2fVlnB5jQQDqKXXwYHLQUVH9es+5TOOHwGOVJOCeRBCiPjwSg+3tN2AdTCzjgli4jijCH290kXb/zWQ==", - "license": "Apache-2.0" - }, "node_modules/watchpack": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.4.0.tgz", @@ -1930,12 +1838,6 @@ "node": ">=10.13.0" } }, - "node_modules/webidl-conversions": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", - "license": "BSD-2-Clause" - }, "node_modules/webpack": { "version": "5.75.0", "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.75.0.tgz", @@ -2108,16 +2010,6 @@ "url": "https://opencollective.com/webpack" } }, - "node_modules/whatwg-url": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", - "license": "MIT", - "dependencies": { - "tr46": "~0.0.3", - "webidl-conversions": "^3.0.0" - } - }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", @@ -2144,15 +2036,6 @@ "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", "dev": true - }, - "node_modules/zlibjs": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/zlibjs/-/zlibjs-0.3.1.tgz", - "integrity": "sha512-+J9RrgTKOmlxFSDHo0pI1xM6BLVUv+o0ZT9ANtCxGkjIVCCUdx9alUF8Gm+dGLKbkkkidWIHFDZHDMpfITt4+w==", - "license": "MIT", - "engines": { - "node": "*" - } } } } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..4e9177d --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "outDir": "dist", + "sourceMap": true, + "strict": true, + "noImplicitReturns": true, + "noImplicitAny": true, + "module": "es2022", + "moduleResolution": "node", + "target": "es2021", + "allowJs": true, + "allowSyntheticDefaultImports": true, + "skipLibCheck": true, + "moduleDetection": "force", + "baseUrl": ".", + "paths": { + "@/*": ["src/*"] + } + }, + "include": [ + "src/**/*" + ], + "exclude": [ + "./node_modules/**/*" + ] +} -- cgit v1.2.3 From 20c3b38f24d79df8843f17985a7d13533d4e0d10 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar Date: Mon, 13 Jul 2026 20:56:46 +0530 Subject: refactor: some minor refactoring to config files --- config/webpack.config.js | 252 --------------------- ...reate-injected-shared-modules-webpack-plugin.js | 2 +- .../create-manifest-webpack-plugin/index.js | 2 +- .../webpack/plugins/create-rules-webpack-plugin.js | 2 +- .../plugins/inject-script-webpack-plugin.js | 2 +- config/webpack/utils/build-file.js | 2 +- config/webpack/utils/injected-code.js | 2 +- config/webpack/utils/inline-javascript.js | 4 +- config/webpack/utils/script-runtime.js | 2 +- package.json | 6 +- webpack.config.js | 250 ++++++++++++++++++++ 11 files changed, 263 insertions(+), 263 deletions(-) delete mode 100644 config/webpack.config.js create mode 100644 webpack.config.js (limited to 'config/webpack.config.js') diff --git a/config/webpack.config.js b/config/webpack.config.js deleted file mode 100644 index b0c1641..0000000 --- a/config/webpack.config.js +++ /dev/null @@ -1,252 +0,0 @@ -/** - * Webpack configuration for the Banglar Bhumi Utils Chrome extension. - * - * Produces four separate bundles: - * 1. **Background script** – service worker entry point. - * 2. **Uninjected scripts** – content scripts that run as-is. - * 3. **Injected scripts** – content scripts that are further processed - * so they can be injected into the page DOM. - * 4. **Shared modules** – libraries compiled as standalone files and - * referenced via global variables (`$`). - * - * Custom loaders and plugins handle dependency ordering, import counting, - * asset-URL rewriting, manifest generation, and declarative-net-rule creation. - */ -import path from "path"; -import fs from "fs"; -import { fileURLToPath } from "url"; -import webpack from "webpack"; -import { merge } from "webpack-merge"; -import CopyPlugin from "copy-webpack-plugin"; - -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); - -global.ROOT_DIR = path.resolve(__dirname, ".."); -global.CONFIG_DIR = path.resolve(ROOT_DIR, "config"); -global.SOURCE_DIR = path.resolve(ROOT_DIR, "src"); -global.production = process.env.NODE_ENV === "production"; - -import CreateManifestPlugin from "./webpack/plugins/create-manifest-webpack-plugin/index.js"; -import CreateRulesPlugin from "./webpack/plugins/create-rules-webpack-plugin.js"; -import InjectScriptPlugin from "./webpack/plugins/inject-script-webpack-plugin.js"; -import CreateInjectedSharedModulesPlugin from "./webpack/plugins/create-injected-shared-modules-webpack-plugin.js"; - -import { inlineJavascript } from "./webpack/utils/inline-javascript.js"; -import { getScriptRuntimeFromType } from "./webpack/utils/script-runtime.js"; -import { getFileName } from "./webpack/utils/build-file.js"; - -import scripts from "./webpack/utils/scripts.js"; -import sharedModules from "./webpack/utils/shared-modules.js"; - -const sharedModulesImportedCount = {}; - -for (const sharedModule of sharedModules) { - sharedModulesImportedCount[sharedModule] = 0; -} - -const injectedSharedModulesImportedCount = {}; - -for (const sharedModule of sharedModules) { - injectedSharedModulesImportedCount[sharedModule] = 0; -} - -const backgroundScriptEntries = {}; -const uninjectedScriptEntries = {}; -const injectedAfterScriptEntries = {}; -const injectedBeforeScriptEntries = {}; -const sharedModuleEntries = {}; -const sharedModuleExternals = {}; - -const backgroundScriptImports = fs.readdirSync(path.resolve(SOURCE_DIR, "background")) - .map(scriptName => `import "${path.resolve(SOURCE_DIR, "background", scriptName)}";`) - .join("\n"); -backgroundScriptEntries["background script"] = { - import: inlineJavascript(backgroundScriptImports), - filename: "background.js" -}; - -for (const scriptPath in scripts) { - for (const scriptType in scripts[scriptPath]) { - const currentScripts = scripts[scriptPath][scriptType]; - let scriptEntries; - let destinationFolder = "scripts"; - - switch (scriptType) { - case "injected": - scriptEntries = injectedAfterScriptEntries; - destinationFolder = "scripts/injected"; - break; - - default: - scriptEntries = uninjectedScriptEntries; - } - - scriptEntries[`${scriptType} - "${scriptPath}"`] = { - import: inlineJavascript(currentScripts.map(script => `import "${path.resolve(SOURCE_DIR, "scripts", script)}";`).join("\n")), - filename: `${destinationFolder}/${getFileName(scriptType, scriptPath)}.js` - }; - } -} - -for (const sharedModule of sharedModules) { - const sharedModuleNameHash = getFileName(sharedModule, "shared"); - const sharedModuleGlobalVariableName = `$${sharedModuleNameHash.substring(sharedModuleNameHash.length - 16)}`; - sharedModuleEntries[sharedModule] = { - import: path.resolve(SOURCE_DIR, "shared", sharedModule), - filename: `shared/${sharedModuleNameHash}.js`, - library: { - name: sharedModuleGlobalVariableName, - type: "var" - } - }; - sharedModuleExternals[`@/shared/${sharedModule}`] = sharedModuleGlobalVariableName; -} - -const commonOptions = { - mode: "production", - target: "web", - resolve: { - extensions: [".ts", ".js", ".html"], - alias: { - "@": global.SOURCE_DIR, - }, - }, - module: { - rules: [ - { - test: /\.ts$/, - loader: "ts-loader", - options: { - configFile: "tsconfig.json", - } - }, - { - test: /\.html$/, - loader: path.resolve(CONFIG_DIR, "webpack/loader/to-string-loader.js"), - }, - ], - }, - optimization: { - minimize: process.env.NODE_ENV === "production", - runtimeChunk: false, - }, - output: { - iife: true, - }, - plugins: [ - new webpack.optimize.LimitChunkCountPlugin({ - maxChunks: 1, - }), - ], -}; - -const sharedModuleOptions = { - externals: sharedModuleExternals, - externalsType: "var", - module: { - rules: [ - { - test: /banglar-bhumi-utils\/src\/.*.(^.?|\.[^d]|[^.]d|[^.][^d])\.(t|j)s$/, - loader: path.resolve(CONFIG_DIR, "webpack/loader/count-imports-loader.js"), - options: { - sharedModulesImportedCount, - }, - enforce: "post", - } - ] - } -}; - -const backgroundScriptConfiguration = merge( - commonOptions, - sharedModuleOptions, - { - entry: backgroundScriptEntries, - }, -); - -const uninjectedScriptConfiguration = merge( - commonOptions, - sharedModuleOptions, - { - entry: uninjectedScriptEntries - } -); - -const injectedScriptConfiguration = merge( - commonOptions, - { - externals: sharedModuleExternals, - externalsType: "var", - module: { - rules: [ - { - test: /banglar-bhumi-utils\/src\/.*.(^.?|\.[^d]|[^.]d|[^.][^d])\.(t|j)s$/, - loader: path.resolve(CONFIG_DIR, "webpack/loader/count-imports-loader.js"), - options: { - sharedModulesImportedCount: injectedSharedModulesImportedCount - }, - enforce: "post" - } - ] - }, - entry: injectedAfterScriptEntries, - plugins: [ - new InjectScriptPlugin() - ] - } -); - -const sortedSharedModules = [] - -const sharedModulesConfiguration = merge( - commonOptions, - sharedModuleOptions, - { - entry: sharedModuleEntries, - plugins: [ - new CopyPlugin({ - patterns: [ - { - from: path.resolve("static"), - to: "./", - }, - { - from: path.resolve("src/offscreen"), - to: "./offscreen", - }, - ], - }), - new CreateInjectedSharedModulesPlugin({ - injectedSharedModulesImportedCount, - sortedSharedModules - }), - new CreateRulesPlugin(), - new CreateManifestPlugin({ - sharedModulesImportedCount, - injectedSharedModulesImportedCount, - sortedSharedModules - }) - ], - module: { - rules: [ - { - test: /banglar-bhumi-utils\/src\/.*.(^.?|\.[^d]|[^.]d|[^.][^d])\.(t|j)s$/, - loader: path.resolve(CONFIG_DIR, "webpack/loader/arrange-shared-module-loader.js"), - options: { - sortedSharedModules - }, - enforce: "post" - } - ] - } - } -); - -export default [ - backgroundScriptConfiguration, - uninjectedScriptConfiguration, - injectedScriptConfiguration, - sharedModulesConfiguration -]; 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 6201a01..21cff34 100644 --- a/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js +++ b/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js @@ -28,7 +28,7 @@ class CreateInjectedSharedModulesPlugin { 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`); diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js index f012f1e..7328d5d 100644 --- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js +++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js @@ -38,7 +38,7 @@ class CreateManifestPlugin { * @param {import("webpack").Compiler} compiler */ apply(compiler) { - compiler.hooks.compilation.tap("CreateManifestPlugin", compilation => { + compiler.hooks.compilation.tap("CreateManifestPlugin", (compilation) => { compilation.hooks.processAssets.tap( { name: "CreateManifestPlugin", diff --git a/config/webpack/plugins/create-rules-webpack-plugin.js b/config/webpack/plugins/create-rules-webpack-plugin.js index 55b60f7..993ac31 100644 --- a/config/webpack/plugins/create-rules-webpack-plugin.js +++ b/config/webpack/plugins/create-rules-webpack-plugin.js @@ -18,7 +18,7 @@ class CreateRulesPlugin { 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 43c5d9f..4f76ca8 100644 --- a/config/webpack/plugins/inject-script-webpack-plugin.js +++ b/config/webpack/plugins/inject-script-webpack-plugin.js @@ -22,7 +22,7 @@ class 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()) diff --git a/config/webpack/utils/build-file.js b/config/webpack/utils/build-file.js index 17f0b03..e09747d 100644 --- a/config/webpack/utils/build-file.js +++ b/config/webpack/utils/build-file.js @@ -10,7 +10,7 @@ import crypto from "crypto"; * @param {boolean} justHash - When true, always return only the hash part. * @returns {string} The transformed filename. */ -export const getFileName = (fileName, prefix, justHash = false) => { +export function getFileName(fileName, prefix, justHash = false) { let hash = crypto .createHash("md5") .update(`${prefix}-${fileName}`) diff --git a/config/webpack/utils/injected-code.js b/config/webpack/utils/injected-code.js index 2b8f750..f91fd4a 100644 --- a/config/webpack/utils/injected-code.js +++ b/config/webpack/utils/injected-code.js @@ -23,7 +23,7 @@ const getHash = url => * @param {string} code - The compiled JavaScript bundle. * @returns {[string, string]} A tuple of [transformedCode, extensionAssetsJSON]. */ -export const getInjectedCode = code => { +export function getInjectedCode(code) { const extensionAssets = {}; let i = 0; diff --git a/config/webpack/utils/inline-javascript.js b/config/webpack/utils/inline-javascript.js index 205bda4..501f852 100644 --- a/config/webpack/utils/inline-javascript.js +++ b/config/webpack/utils/inline-javascript.js @@ -5,4 +5,6 @@ * @param {string} code - The JavaScript source to inline. * @returns {string} A `data:text/javascript;base64,…` URI. */ -export const inlineJavascript = code => `data:text/javascript;base64,${Buffer.from(code).toString("base64")}`; +export function inlineJavascript(code) { + return `data:text/javascript;base64,${Buffer.from(code).toString("base64")}`; +} diff --git a/config/webpack/utils/script-runtime.js b/config/webpack/utils/script-runtime.js index 1c23922..2745d54 100644 --- a/config/webpack/utils/script-runtime.js +++ b/config/webpack/utils/script-runtime.js @@ -9,7 +9,7 @@ * @param {string} scriptType - The script type from scripts.json. * @returns {string} The corresponding `run_at` value. */ -export const getScriptRuntimeFromType = scriptType => { +export function getScriptRuntimeFromType(scriptType) { switch (scriptType) { case "before": case "injected-after": diff --git a/package.json b/package.json index 82601a2..af070d9 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,9 @@ "author": "Arpit Chakladar", "license": "MIT", "scripts": { - "watch": "rm -rf dist && NODE_ENV=development webpack watch --config config/webpack.config.js", - "build": "rm -rf dist && NODE_ENV=production webpack build --config config/webpack.config.js", - "build:dev": "rm -rf dist && NODE_ENV=development webpack build --config config/webpack.config.js" + "watch": "rm -rf dist && NODE_ENV=development webpack watch", + "build": "rm -rf dist && NODE_ENV=production webpack build", + "build:dev": "rm -rf dist && NODE_ENV=development webpack build" }, "devDependencies": { "@types/chrome": "^0.0.213", diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..0d1c864 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,250 @@ +/** + * Webpack configuration for the Banglar Bhumi Utils Chrome extension. + * + * Produces four separate bundles: + * 1. **Background script** – service worker entry point. + * 2. **Uninjected scripts** – content scripts that run as-is. + * 3. **Injected scripts** – content scripts that are further processed + * so they can be injected into the page DOM. + * 4. **Shared modules** – libraries compiled as standalone files and + * referenced via global variables (`$`). + * + * Custom loaders and plugins handle dependency ordering, import counting, + * asset-URL rewriting, manifest generation, and declarative-net-rule creation. + */ +import path from "path"; +import fs from "fs"; +import { fileURLToPath } from "url"; +import webpack from "webpack"; +import { merge } from "webpack-merge"; +import CopyPlugin from "copy-webpack-plugin"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +global.ROOT_DIR = path.resolve(__dirname); +global.CONFIG_DIR = path.resolve(ROOT_DIR, "config"); +global.SOURCE_DIR = path.resolve(ROOT_DIR, "src"); +global.production = process.env.NODE_ENV === "production"; + +import CreateManifestPlugin from "./config/webpack/plugins/create-manifest-webpack-plugin/index.js"; +import CreateRulesPlugin from "./config/webpack/plugins/create-rules-webpack-plugin.js"; +import InjectScriptPlugin from "./config/webpack/plugins/inject-script-webpack-plugin.js"; +import CreateInjectedSharedModulesPlugin from "./config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js"; + +import { inlineJavascript } from "./config/webpack/utils/inline-javascript.js"; +import { getFileName } from "./config/webpack/utils/build-file.js"; + +import scripts from "./config/webpack/utils/scripts.js"; +import sharedModules from "./config/webpack/utils/shared-modules.js"; + +const sharedModulesImportedCount = {}; + +for (const sharedModule of sharedModules) { + sharedModulesImportedCount[sharedModule] = 0; +} + +const injectedSharedModulesImportedCount = {}; + +for (const sharedModule of sharedModules) { + injectedSharedModulesImportedCount[sharedModule] = 0; +} + +const backgroundScriptEntries = {}; +const uninjectedScriptEntries = {}; +const injectedAfterScriptEntries = {}; +const sharedModuleEntries = {}; +const sharedModuleExternals = {}; + +const backgroundScriptImports = fs.readdirSync(path.resolve(SOURCE_DIR, "background")) + .map(scriptName => `import "${path.resolve(SOURCE_DIR, "background", scriptName)}";`) + .join("\n"); +backgroundScriptEntries["background script"] = { + import: inlineJavascript(backgroundScriptImports), + filename: "background.js" +}; + +for (const scriptPath in scripts) { + for (const scriptType in scripts[scriptPath]) { + const currentScripts = scripts[scriptPath][scriptType]; + let scriptEntries; + let destinationFolder = "scripts"; + + switch (scriptType) { + case "injected": + scriptEntries = injectedAfterScriptEntries; + destinationFolder = "scripts/injected"; + break; + + default: + scriptEntries = uninjectedScriptEntries; + } + + scriptEntries[`${scriptType} - "${scriptPath}"`] = { + import: inlineJavascript(currentScripts.map(script => `import "${path.resolve(SOURCE_DIR, "scripts", script)}";`).join("\n")), + filename: `${destinationFolder}/${getFileName(scriptType, scriptPath)}.js` + }; + } +} + +for (const sharedModule of sharedModules) { + const sharedModuleNameHash = getFileName(sharedModule, "shared"); + const sharedModuleGlobalVariableName = `$${sharedModuleNameHash.substring(sharedModuleNameHash.length - 16)}`; + sharedModuleEntries[sharedModule] = { + import: path.resolve(SOURCE_DIR, "shared", sharedModule), + filename: `shared/${sharedModuleNameHash}.js`, + library: { + name: sharedModuleGlobalVariableName, + type: "var" + } + }; + sharedModuleExternals[`@/shared/${sharedModule}`] = sharedModuleGlobalVariableName; +} + +const commonOptions = { + mode: "production", + target: "web", + resolve: { + extensions: [".ts", ".js", ".html"], + alias: { + "@": global.SOURCE_DIR, + }, + }, + module: { + rules: [ + { + test: /\.ts$/, + loader: "ts-loader", + options: { + configFile: "tsconfig.json", + } + }, + { + test: /\.html$/, + loader: path.resolve(CONFIG_DIR, "webpack/loader/to-string-loader.js"), + }, + ], + }, + optimization: { + minimize: process.env.NODE_ENV === "production", + runtimeChunk: false, + }, + output: { + iife: true, + }, + plugins: [ + new webpack.optimize.LimitChunkCountPlugin({ + maxChunks: 1, + }), + ], +}; + +const sharedModuleOptions = { + externals: sharedModuleExternals, + externalsType: "var", + module: { + rules: [ + { + test: /banglar-bhumi-utils\/src\/.*.(^.?|\.[^d]|[^.]d|[^.][^d])\.(t|j)s$/, + loader: path.resolve(CONFIG_DIR, "webpack/loader/count-imports-loader.js"), + options: { + sharedModulesImportedCount, + }, + enforce: "post", + } + ] + } +}; + +const backgroundScriptConfiguration = merge( + commonOptions, + sharedModuleOptions, + { + entry: backgroundScriptEntries, + }, +); + +const uninjectedScriptConfiguration = merge( + commonOptions, + sharedModuleOptions, + { + entry: uninjectedScriptEntries + } +); + +const injectedScriptConfiguration = merge( + commonOptions, + { + externals: sharedModuleExternals, + externalsType: "var", + module: { + rules: [ + { + test: /banglar-bhumi-utils\/src\/.*.(^.?|\.[^d]|[^.]d|[^.][^d])\.(t|j)s$/, + loader: path.resolve(CONFIG_DIR, "webpack/loader/count-imports-loader.js"), + options: { + sharedModulesImportedCount: injectedSharedModulesImportedCount + }, + enforce: "post" + } + ] + }, + entry: injectedAfterScriptEntries, + plugins: [ + new InjectScriptPlugin() + ] + } +); + +const sortedSharedModules = [] + +const sharedModulesConfiguration = merge( + commonOptions, + sharedModuleOptions, + { + entry: sharedModuleEntries, + plugins: [ + new CopyPlugin({ + patterns: [ + { + from: path.resolve("static"), + to: "./", + }, + { + from: path.resolve("src/offscreen"), + to: "./offscreen", + }, + ], + }), + new CreateInjectedSharedModulesPlugin({ + injectedSharedModulesImportedCount, + sortedSharedModules + }), + new CreateRulesPlugin(), + new CreateManifestPlugin({ + sharedModulesImportedCount, + injectedSharedModulesImportedCount, + sortedSharedModules + }) + ], + module: { + rules: [ + { + test: /banglar-bhumi-utils\/src\/.*.(^.?|\.[^d]|[^.]d|[^.][^d])\.(t|j)s$/, + loader: path.resolve(CONFIG_DIR, "webpack/loader/arrange-shared-module-loader.js"), + options: { + sortedSharedModules + }, + enforce: "post" + } + ] + } + } +); + +export default [ + backgroundScriptConfiguration, + uninjectedScriptConfiguration, + injectedScriptConfiguration, + sharedModulesConfiguration +]; -- cgit v1.2.3