UNPKG

1.45 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { getEntryRuntime, mergeRuntimeOwned } = require("./util/runtime");
9
10/** @typedef {import("./Compiler")} Compiler */
11/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
12
13class FlagAllModulesAsUsedPlugin {
14 constructor(explanation) {
15 this.explanation = explanation;
16 }
17
18 /**
19 * Apply the plugin
20 * @param {Compiler} compiler the compiler instance
21 * @returns {void}
22 */
23 apply(compiler) {
24 compiler.hooks.compilation.tap(
25 "FlagAllModulesAsUsedPlugin",
26 compilation => {
27 const moduleGraph = compilation.moduleGraph;
28 compilation.hooks.optimizeDependencies.tap(
29 "FlagAllModulesAsUsedPlugin",
30 modules => {
31 /** @type {RuntimeSpec} */
32 let runtime = undefined;
33 for (const [name, { options }] of compilation.entries) {
34 runtime = mergeRuntimeOwned(
35 runtime,
36 getEntryRuntime(compilation, name, options)
37 );
38 }
39 for (const module of modules) {
40 const exportsInfo = moduleGraph.getExportsInfo(module);
41 exportsInfo.setUsedInUnknownWay(runtime);
42 moduleGraph.addExtraReason(module, this.explanation);
43 if (module.factoryMeta === undefined) {
44 module.factoryMeta = {};
45 }
46 module.factoryMeta.sideEffectFree = false;
47 }
48 }
49 );
50 }
51 );
52 }
53}
54
55module.exports = FlagAllModulesAsUsedPlugin;