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 { UsageState } = require("../ExportsInfo");
|
---|
9 | const makeSerializable = require("../util/makeSerializable");
|
---|
10 | const NullDependency = require("./NullDependency");
|
---|
11 |
|
---|
12 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
13 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
14 | /** @typedef {import("../Dependency")} Dependency */
|
---|
15 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
---|
16 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
17 | /** @typedef {import("../Module")} Module */
|
---|
18 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
19 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
20 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
21 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
22 | /** @typedef {import("../util/Hash")} Hash */
|
---|
23 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * @param {ModuleGraph} moduleGraph the module graph
|
---|
27 | * @param {Module} module the module
|
---|
28 | * @param {string[] | null} _exportName name of the export if any
|
---|
29 | * @param {string | null} property name of the requested property
|
---|
30 | * @param {RuntimeSpec} runtime for which runtime
|
---|
31 | * @returns {any} value of the property
|
---|
32 | */
|
---|
33 | const getProperty = (moduleGraph, module, _exportName, property, runtime) => {
|
---|
34 | if (!_exportName) {
|
---|
35 | switch (property) {
|
---|
36 | case "usedExports": {
|
---|
37 | const usedExports = moduleGraph
|
---|
38 | .getExportsInfo(module)
|
---|
39 | .getUsedExports(runtime);
|
---|
40 | if (
|
---|
41 | typeof usedExports === "boolean" ||
|
---|
42 | usedExports === undefined ||
|
---|
43 | usedExports === null
|
---|
44 | ) {
|
---|
45 | return usedExports;
|
---|
46 | }
|
---|
47 | return Array.from(usedExports).sort();
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 | const exportName = /** @type {string[]} */ (_exportName);
|
---|
52 | switch (property) {
|
---|
53 | case "canMangle": {
|
---|
54 | const exportsInfo = moduleGraph.getExportsInfo(module);
|
---|
55 | const exportInfo = exportsInfo.getReadOnlyExportInfoRecursive(exportName);
|
---|
56 | if (exportInfo) return exportInfo.canMangle;
|
---|
57 | return exportsInfo.otherExportsInfo.canMangle;
|
---|
58 | }
|
---|
59 | case "used":
|
---|
60 | return (
|
---|
61 | moduleGraph.getExportsInfo(module).getUsed(exportName, runtime) !==
|
---|
62 | UsageState.Unused
|
---|
63 | );
|
---|
64 | case "useInfo": {
|
---|
65 | const state = moduleGraph
|
---|
66 | .getExportsInfo(module)
|
---|
67 | .getUsed(exportName, runtime);
|
---|
68 | switch (state) {
|
---|
69 | case UsageState.Used:
|
---|
70 | case UsageState.OnlyPropertiesUsed:
|
---|
71 | return true;
|
---|
72 | case UsageState.Unused:
|
---|
73 | return false;
|
---|
74 | case UsageState.NoInfo:
|
---|
75 | return;
|
---|
76 | case UsageState.Unknown:
|
---|
77 | return null;
|
---|
78 | default:
|
---|
79 | throw new Error(`Unexpected UsageState ${state}`);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | case "provideInfo":
|
---|
83 | return moduleGraph.getExportsInfo(module).isExportProvided(exportName);
|
---|
84 | }
|
---|
85 | };
|
---|
86 |
|
---|
87 | class ExportsInfoDependency extends NullDependency {
|
---|
88 | /**
|
---|
89 | * @param {Range} range range
|
---|
90 | * @param {string[] | null} exportName export name
|
---|
91 | * @param {string | null} property property
|
---|
92 | */
|
---|
93 | constructor(range, exportName, property) {
|
---|
94 | super();
|
---|
95 | this.range = range;
|
---|
96 | this.exportName = exportName;
|
---|
97 | this.property = property;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * @param {ObjectSerializerContext} context context
|
---|
102 | */
|
---|
103 | serialize(context) {
|
---|
104 | const { write } = context;
|
---|
105 | write(this.range);
|
---|
106 | write(this.exportName);
|
---|
107 | write(this.property);
|
---|
108 | super.serialize(context);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * @param {ObjectDeserializerContext} context context
|
---|
113 | * @returns {ExportsInfoDependency} ExportsInfoDependency
|
---|
114 | */
|
---|
115 | static deserialize(context) {
|
---|
116 | const obj = new ExportsInfoDependency(
|
---|
117 | context.read(),
|
---|
118 | context.read(),
|
---|
119 | context.read()
|
---|
120 | );
|
---|
121 | obj.deserialize(context);
|
---|
122 | return obj;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | makeSerializable(
|
---|
127 | ExportsInfoDependency,
|
---|
128 | "webpack/lib/dependencies/ExportsInfoDependency"
|
---|
129 | );
|
---|
130 |
|
---|
131 | ExportsInfoDependency.Template = class ExportsInfoDependencyTemplate extends (
|
---|
132 | NullDependency.Template
|
---|
133 | ) {
|
---|
134 | /**
|
---|
135 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
136 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
137 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
138 | * @returns {void}
|
---|
139 | */
|
---|
140 | apply(dependency, source, { module, moduleGraph, runtime }) {
|
---|
141 | const dep = /** @type {ExportsInfoDependency} */ (dependency);
|
---|
142 |
|
---|
143 | const value = getProperty(
|
---|
144 | moduleGraph,
|
---|
145 | module,
|
---|
146 | dep.exportName,
|
---|
147 | dep.property,
|
---|
148 | runtime
|
---|
149 | );
|
---|
150 | source.replace(
|
---|
151 | dep.range[0],
|
---|
152 | dep.range[1] - 1,
|
---|
153 | value === undefined ? "undefined" : JSON.stringify(value)
|
---|
154 | );
|
---|
155 | }
|
---|
156 | };
|
---|
157 |
|
---|
158 | module.exports = ExportsInfoDependency;
|
---|