aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/loader/dynamic-imports-loader.js
blob: 06f9704c74ddfdac2f442724de9d8465536be0d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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;
};