UNPKG

1.37 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 } = require("./util/runtime");
9
10/** @typedef {import("./Compiler")} Compiler */
11
12class FlagEntryExportAsUsedPlugin {
13 constructor(nsObjectUsed, explanation) {
14 this.nsObjectUsed = nsObjectUsed;
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.thisCompilation.tap(
25 "FlagEntryExportAsUsedPlugin",
26 compilation => {
27 const moduleGraph = compilation.moduleGraph;
28 compilation.hooks.seal.tap("FlagEntryExportAsUsedPlugin", () => {
29 for (const [
30 entryName,
31 { dependencies: deps, options }
32 ] of compilation.entries) {
33 const runtime = getEntryRuntime(compilation, entryName, options);
34 for (const dep of deps) {
35 const module = moduleGraph.getModule(dep);
36 if (module) {
37 const exportsInfo = moduleGraph.getExportsInfo(module);
38 if (this.nsObjectUsed) {
39 exportsInfo.setUsedInUnknownWay(runtime);
40 } else {
41 exportsInfo.setAllKnownExportsUsed(runtime);
42 }
43 moduleGraph.addExtraReason(module, this.explanation);
44 }
45 }
46 }
47 });
48 }
49 );
50 }
51}
52
53module.exports = FlagEntryExportAsUsedPlugin;