aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js
diff options
context:
space:
mode:
authorArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-04-08 11:46:35 +0530
committerArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-04-08 11:46:35 +0530
commitccf73485cfba952451144d7841cbb3fac8368867 (patch)
tree642b374684afa6ad4446af556fd98f118609d5c7 /config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js
parentc59c25bff510166011471b136d12c4217f844eb9 (diff)
downloadbanglar-bhumi-utils-ccf73485cfba952451144d7841cbb3fac8368867.tar.gz
banglar-bhumi-utils-ccf73485cfba952451144d7841cbb3fac8368867.zip
Injecting shared modules for the injected scripts, further reusing code.
Diffstat (limited to 'config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js')
-rw-r--r--config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js38
1 files changed, 38 insertions, 0 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
new file mode 100644
index 0000000..88959c5
--- /dev/null
+++ b/config/webpack/plugins/create-injected-shared-modules-webpack-plugin.js
@@ -0,0 +1,38 @@
+const path = require("path");
+const fs = require("fs");
+const { sources } = require("webpack");
+
+const { getFileNameHash } = webpackRequire("utils/filename-hash");
+
+class CreateInjectedSharedModulesPlugin {
+ constructor({ injectedSharedModulesImportedCount, sortedSharedModules }) {
+ this.injectedSharedModulesImportedCount = injectedSharedModulesImportedCount;
+ this.sortedSharedModules = sortedSharedModules;
+ }
+
+ apply(compiler) {
+ compiler.hooks.compilation.tap("CreateInjectedSharedModulesPlugin", compilation => {
+ compilation.hooks.processAssets.tap(
+ {
+ name: "CreateInjectedSharedModulesPlugin",
+ stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
+ },
+ assets => {
+ const injectedSharedScripts = this.sortedSharedModules
+ .filter(sharedModule => this.injectedSharedModulesImportedCount[sharedModule] > 0)
+ .map(sharedModule => `"/shared/${getFileNameHash(sharedModule, "shared")}.js"`);
+
+ if (injectedSharedScripts.length > 0) {
+ const injectionCode = `(async()=>{let e=(await Promise.all([${injectedSharedScripts.join(", ")}].map(async e=>(await fetch(chrome.runtime.getURL(e))).text()))).join(""),t=new MutationObserver(()=>{let n=document.querySelector("head > script:nth-child(54)");n&&(n.innerHTML=e+n.innerHTML,t.disconnect())});t.observe(document,{childList:!0,subtree:!0})})();`;
+ compilation.emitAsset(
+ `shared/${getFileNameHash("injected-shared-modules", "shared")}.js`,
+ new sources.RawSource(injectionCode)
+ );
+ }
+ }
+ );
+ });
+ }
+}
+
+module.exports = CreateInjectedSharedModulesPlugin;