aboutsummaryrefslogtreecommitdiffstats
path: root/config/webpack/loader/count-imports-loader.js
blob: ef832bf4a2773b49dbf48b0b7859c5b38c131911 (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
import { ImportManager } from "import-manager";

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

/**
 * Webpack loader that counts how many times each shared module is imported.
 * This count is used later to determine which shared modules need to be
 * included in the compiled output.
 *
 * @param {string} source - The source code of the current module.
 * @returns {string} The unmodified source (this loader is side-effect only).
 */
export default function(source) {
	const { sharedModulesImportedCount } = this.getOptions();
	const manager = new ImportManager(source);

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

		if (moduleIndex >= 0) {
			sharedModulesImportedCount[sharedModules[moduleIndex]]++;
		}
	}

	return source;
};