aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack
diff options
context:
space:
mode:
authorArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-03-27 08:37:44 +0530
committerArpit Chakladar <54011232+arpitchakladar@users.noreply.github.com>2023-03-27 11:09:46 +0530
commit84f86da2faab3af73e3a55ab576ca14a0d38259e (patch)
treec9680f58058f6eb836362ed14f23c15d0c89e9e2 /config/webpack
parent068a14ffa464d6cb0ccc5c3cec11d10d8cceae90 (diff)
downloadbanglar-bhumi-utils-84f86da2faab3af73e3a55ab576ca14a0d38259e.tar.gz
banglar-bhumi-utils-84f86da2faab3af73e3a55ab576ca14a0d38259e.zip
Automatic conversion of import statements into dynamic imports.
Diffstat (limited to 'config/webpack')
-rw-r--r--config/webpack/loader/dynamic-imports-loader.js31
-rw-r--r--config/webpack/plugins/create-manifest-webpack-plugin/index.js8
2 files changed, 36 insertions, 3 deletions
diff --git a/config/webpack/loader/dynamic-imports-loader.js b/config/webpack/loader/dynamic-imports-loader.js
new file mode 100644
index 0000000..06f9704
--- /dev/null
+++ b/config/webpack/loader/dynamic-imports-loader.js
@@ -0,0 +1,31 @@
+const { ImportManager } = require("import-manager");
+const { getFileNameHash } = webpackRequire("utils/file-name-hash");
+
+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`;
+
+ unit.methods = {
+ makeUntraceable() {}
+ };
+ manager.remove(unit);
+ manager.insertStatement(importStatement, "bottom", "dynamic");
+ modified = true;
+ }
+ }
+
+ return modified ? manager.code.toString() + "\nexport {};" : source;
+};
diff --git a/config/webpack/plugins/create-manifest-webpack-plugin/index.js b/config/webpack/plugins/create-manifest-webpack-plugin/index.js
index 77f5959..4ab363f 100644
--- a/config/webpack/plugins/create-manifest-webpack-plugin/index.js
+++ b/config/webpack/plugins/create-manifest-webpack-plugin/index.js
@@ -38,12 +38,14 @@ class CreateManifestPlugin {
const resources = [];
for (const assetName in assets) {
- if (/^assets\/.*\.(jpg|png|gif)$/.test(assetName))
+ if (/^assets\/.*\.(jpg|png|gif)$/.test(assetName)) {
resources.push(assetName);
+ }
}
- for (const sharedModule of sharedModules)
- resources.push(`shared/${sharedModule}.js`);
+ for (const sharedModule of sharedModules) {
+ resources.push(`shared/${getFileNameHash(sharedModule)}.js`);
+ }
if (resources.length > 0) {
manifest.web_accessible_resources = [