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