aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack
diff options
context:
space:
mode:
authorArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-03-27 21:38:43 +0530
committerArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-03-27 21:38:43 +0530
commitd178caee5c93ad9ed91dbd082b0f0b5a9b0507aa (patch)
treeccfdf4c6992a86955c80780de0518a2fea10ac39 /config/webpack
parent5955bd821303cffa35263c41286339fb28e7536b (diff)
downloadbanglar-bhumi-utils-d178caee5c93ad9ed91dbd082b0f0b5a9b0507aa.tar.gz
banglar-bhumi-utils-d178caee5c93ad9ed91dbd082b0f0b5a9b0507aa.zip
Refactoring code.
Only using hashes for output file names. Using singleton pattern to store the callbacks for observeDOM, and only one MutationObserver.
Diffstat (limited to 'config/webpack')
-rw-r--r--config/webpack/loader/dynamic-imports-loader.js4
-rw-r--r--config/webpack/plugins/create-manifest-webpack-plugin/index.js4
-rw-r--r--config/webpack/plugins/inject-script-webpack-plugin.js24
-rw-r--r--config/webpack/utils/file-name-hash.js10
-rw-r--r--config/webpack/utils/scripts.js1
-rw-r--r--config/webpack/utils/shared-modules.js2
6 files changed, 26 insertions, 19 deletions
diff --git a/config/webpack/loader/dynamic-imports-loader.js b/config/webpack/loader/dynamic-imports-loader.js
index 06f9704..12eb5b7 100644
--- a/config/webpack/loader/dynamic-imports-loader.js
+++ b/config/webpack/loader/dynamic-imports-loader.js
@@ -5,18 +5,18 @@ 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])}.js");\n`;
+ const importStatement = `const {\n${importedModuleNames}\n} = await import(/* webpackIgnore: true */ "/shared/${getFileNameHash(sharedModules[moduleIndex], "shared")}.js");\n`;
unit.methods = {
makeUntraceable() {}
diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js
index 4ab363f..785d604 100644
--- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js
+++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js
@@ -29,7 +29,7 @@ class CreateManifestPlugin {
manifest.content_scripts.push({
matches,
- js: [`scripts/${getFileNameHash(scriptPath)}-${scriptType}.js`],
+ js: [`scripts/${getFileNameHash(scriptType, scriptPath)}.js`],
run_at: getScriptRuntimeFromType(scriptType)
});
}
@@ -44,7 +44,7 @@ class CreateManifestPlugin {
}
for (const sharedModule of sharedModules) {
- resources.push(`shared/${getFileNameHash(sharedModule)}.js`);
+ resources.push(`shared/${getFileNameHash(sharedModule, "shared")}.js`);
}
if (resources.length > 0) {
diff --git a/config/webpack/plugins/inject-script-webpack-plugin.js b/config/webpack/plugins/inject-script-webpack-plugin.js
index 862b3f0..c597e9a 100644
--- a/config/webpack/plugins/inject-script-webpack-plugin.js
+++ b/config/webpack/plugins/inject-script-webpack-plugin.js
@@ -3,6 +3,10 @@ const { sources } = require("webpack");
const { getInjectionCode } = webpackRequire("utils/get-injection-code");
class InjectScriptPlugin {
+ constructor(insertedBefore = false) {
+ this.insertedBefore = insertedBefore;
+ }
+
apply(compiler) {
compiler.hooks.compilation.tap("InjectScriptPlugin", compilation => {
compilation.hooks.processAssets.tapPromise(
@@ -13,21 +17,15 @@ class InjectScriptPlugin {
},
async assets => {
for (const assetName in assets) {
- let injected = false;
- let injected_before = false;
-
- if (assetName.endsWith("injected.js"))
- injected = true;
-
- else if (assetName.endsWith("injected-before.js")) {
- injected = true;
- injected_before = true;
- }
-
- if (injected) {
+ if (/\.js$/.test(assetName)) {
compilation.updateAsset(
assetName,
- new sources.RawSource(getInjectionCode(compilation.getAsset(assetName).source.source(), injected_before))
+ new sources.RawSource(
+ getInjectionCode(
+ compilation.getAsset(assetName).source.source(),
+ this.insertedBefore
+ )
+ )
);
}
}
diff --git a/config/webpack/utils/file-name-hash.js b/config/webpack/utils/file-name-hash.js
index e3988a9..889e9a4 100644
--- a/config/webpack/utils/file-name-hash.js
+++ b/config/webpack/utils/file-name-hash.js
@@ -1,3 +1,11 @@
const crypto = require("crypto");
-module.exports.getFileNameHash = fileName => crypto.createHash("md5").update(fileName).digest("hex").substring(16);
+module.exports.getFileNameHash = (fileName, prefix) => {
+ const finalFileName = `${prefix}-${fileName}`;
+ const hash = crypto
+ .createHash("md5")
+ .update(finalFileName)
+ .digest("hex");
+
+ return production ? hash : `${fileName}-${hash.substring(16)}`;
+};
diff --git a/config/webpack/utils/scripts.js b/config/webpack/utils/scripts.js
index 2196737..427aaab 100644
--- a/config/webpack/utils/scripts.js
+++ b/config/webpack/utils/scripts.js
@@ -5,6 +5,7 @@ const formattedScripts = {};
for (const scriptType in scripts) {
const currentScripts = scripts[scriptType];
+
for (const scriptName in currentScripts) {
const scriptPath = currentScripts[scriptName];
formattedScripts[scriptPath] = formattedScripts[scriptPath] || {};
diff --git a/config/webpack/utils/shared-modules.js b/config/webpack/utils/shared-modules.js
index f10d4a1..974145f 100644
--- a/config/webpack/utils/shared-modules.js
+++ b/config/webpack/utils/shared-modules.js
@@ -3,6 +3,6 @@ const fs = require("fs");
const sharedModules = fs.readdirSync(path.resolve(SOURCE_DIR, "shared"))
.filter(sharedModule => !sharedModule.endsWith("import-shared.js"))
- .map(sharedModule => sharedModule.substring(0, sharedModule.length - 3));4
+ .map(sharedModule => sharedModule.substring(0, sharedModule.length - 3));
module.exports = sharedModules;