aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/loader/arrange-shared-module-loader.js
blob: 22651854ebdeb6886ee40c8ea79be793516de7a2 (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
32
33
34
35
36
37
38
39
40
41
42
import path from "path";
import { ImportManager } from "import-manager";

import sharedModules from "../utils/shared-modules.js";

export default function(source) {
	const currentSharedModuleName = path.basename(this.resourcePath.substring(0, this.resourcePath.length - 3));

	if (sharedModules.includes(currentSharedModuleName)) {
		const { sortedSharedModules } = this.getOptions();
		const manager = new ImportManager(source);
		const modulesToBeIncluded = [];

		for (const unit of manager.imports.es6.units) {
			const moduleIndex = sharedModules.indexOf(unit.module.name);

			if (moduleIndex >= 0) {
				const currentSharedModuleName = sharedModules[moduleIndex];

				if (!(sortedSharedModules.includes(currentSharedModuleName) || modulesToBeIncluded.includes(currentSharedModuleName))) {
					modulesToBeIncluded.push(currentSharedModuleName);
				}
			}
		}

		const currentModuleIndex = sortedSharedModules.indexOf(currentSharedModuleName);

		if (currentModuleIndex >= 0) {
			for (const sharedModule of modulesToBeIncluded) {
				sortedSharedModules.splice(currentModuleIndex, 0, sharedModule);
			}
		} else {
			for (const sharedModule of modulesToBeIncluded) {
				sortedSharedModules.push(sharedModule);
			}

			sortedSharedModules.push(currentSharedModuleName);
		}
	}

	return source;
};