2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Sergey Melyukov @smelukov
8 const { UsageState } = require("../ExportsInfo");
10 /** @typedef {import("estree").Node} AnyNode */
11 /** @typedef {import("../Dependency")} Dependency */
12 /** @typedef {import("../ModuleGraph")} ModuleGraph */
13 /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
14 /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
15 /** @typedef {import("../Parser").ParserState} ParserState */
16 /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
17 /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
19 /** @typedef {Map<TopLevelSymbol | null, Set<string | TopLevelSymbol> | true>} InnerGraph */
20 /** @typedef {function(boolean | Set<string> | undefined): void} UsageCallback */
23 * @typedef {Object} StateObject
24 * @property {InnerGraph} innerGraph
25 * @property {TopLevelSymbol=} currentTopLevelSymbol
26 * @property {Map<TopLevelSymbol, Set<UsageCallback>>} usageCallbackMap
29 /** @typedef {false|StateObject} State */
31 /** @type {WeakMap<ParserState, State>} */
32 const parserStateMap = new WeakMap();
33 const topLevelSymbolTag = Symbol("top level symbol");
36 * @param {ParserState} parserState parser state
37 * @returns {State} state
39 function getState(parserState) {
40 return parserStateMap.get(parserState);
44 * @param {ParserState} parserState parser state
47 exports.bailout = parserState => {
48 parserStateMap.set(parserState, false);
52 * @param {ParserState} parserState parser state
55 exports.enable = parserState => {
56 const state = parserStateMap.get(parserState);
57 if (state === false) {
60 parserStateMap.set(parserState, {
61 innerGraph: new Map(),
62 currentTopLevelSymbol: undefined,
63 usageCallbackMap: new Map()
68 * @param {ParserState} parserState parser state
69 * @returns {boolean} true, when enabled
71 exports.isEnabled = parserState => {
72 const state = parserStateMap.get(parserState);
77 * @param {ParserState} state parser state
78 * @param {TopLevelSymbol | null} symbol the symbol, or null for all symbols
79 * @param {string | TopLevelSymbol | true} usage usage data
82 exports.addUsage = (state, symbol, usage) => {
83 const innerGraphState = getState(state);
85 if (innerGraphState) {
86 const { innerGraph } = innerGraphState;
87 const info = innerGraph.get(symbol);
89 innerGraph.set(symbol, true);
90 } else if (info === undefined) {
91 innerGraph.set(symbol, new Set([usage]));
92 } else if (info !== true) {
99 * @param {JavascriptParser} parser the parser
100 * @param {string} name name of variable
101 * @param {string | TopLevelSymbol | true} usage usage data
104 exports.addVariableUsage = (parser, name, usage) => {
106 /** @type {TopLevelSymbol} */ (
107 parser.getTagData(name, topLevelSymbolTag)
108 ) || exports.tagTopLevelSymbol(parser, name);
110 exports.addUsage(parser.state, symbol, usage);
115 * @param {ParserState} state parser state
118 exports.inferDependencyUsage = state => {
119 const innerGraphState = getState(state);
121 if (!innerGraphState) {
125 const { innerGraph, usageCallbackMap } = innerGraphState;
126 const processed = new Map();
127 // flatten graph to terminal nodes (string, undefined or true)
128 const nonTerminal = new Set(innerGraph.keys());
129 while (nonTerminal.size > 0) {
130 for (const key of nonTerminal) {
131 /** @type {Set<string|TopLevelSymbol> | true} */
132 let newSet = new Set();
133 let isTerminal = true;
134 const value = innerGraph.get(key);
135 let alreadyProcessed = processed.get(key);
136 if (alreadyProcessed === undefined) {
137 alreadyProcessed = new Set();
138 processed.set(key, alreadyProcessed);
140 if (value !== true && value !== undefined) {
141 for (const item of value) {
142 alreadyProcessed.add(item);
144 for (const item of value) {
145 if (typeof item === "string") {
148 const itemValue = innerGraph.get(item);
149 if (itemValue === true) {
153 if (itemValue !== undefined) {
154 for (const i of itemValue) {
155 if (i === key) continue;
156 if (alreadyProcessed.has(i)) continue;
158 if (typeof i !== "string") {
165 if (newSet === true) {
166 innerGraph.set(key, true);
167 } else if (newSet.size === 0) {
168 innerGraph.set(key, undefined);
170 innerGraph.set(key, newSet);
174 nonTerminal.delete(key);
176 // For the global key, merge with all other keys
178 const globalValue = innerGraph.get(null);
180 for (const [key, value] of innerGraph) {
181 if (key !== null && value !== true) {
182 if (globalValue === true) {
183 innerGraph.set(key, true);
185 const newSet = new Set(value);
186 for (const item of globalValue) {
189 innerGraph.set(key, newSet);
199 /** @type {Map<Dependency, true | Set<string>>} */
200 for (const [symbol, callbacks] of usageCallbackMap) {
201 const usage = /** @type {true | Set<string> | undefined} */ (
202 innerGraph.get(symbol)
204 for (const callback of callbacks) {
205 callback(usage === undefined ? false : usage);
211 * @param {ParserState} state parser state
212 * @param {UsageCallback} onUsageCallback on usage callback
214 exports.onUsage = (state, onUsageCallback) => {
215 const innerGraphState = getState(state);
217 if (innerGraphState) {
218 const { usageCallbackMap, currentTopLevelSymbol } = innerGraphState;
219 if (currentTopLevelSymbol) {
220 let callbacks = usageCallbackMap.get(currentTopLevelSymbol);
222 if (callbacks === undefined) {
223 callbacks = new Set();
224 usageCallbackMap.set(currentTopLevelSymbol, callbacks);
227 callbacks.add(onUsageCallback);
229 onUsageCallback(true);
232 onUsageCallback(undefined);
237 * @param {ParserState} state parser state
238 * @param {TopLevelSymbol} symbol the symbol
240 exports.setTopLevelSymbol = (state, symbol) => {
241 const innerGraphState = getState(state);
243 if (innerGraphState) {
244 innerGraphState.currentTopLevelSymbol = symbol;
249 * @param {ParserState} state parser state
250 * @returns {TopLevelSymbol|void} usage data
252 exports.getTopLevelSymbol = state => {
253 const innerGraphState = getState(state);
255 if (innerGraphState) {
256 return innerGraphState.currentTopLevelSymbol;
261 * @param {JavascriptParser} parser parser
262 * @param {string} name name of variable
263 * @returns {TopLevelSymbol} symbol
265 exports.tagTopLevelSymbol = (parser, name) => {
266 const innerGraphState = getState(parser.state);
267 if (!innerGraphState) return;
269 parser.defineVariable(name);
271 const existingTag = /** @type {TopLevelSymbol} */ (
272 parser.getTagData(name, topLevelSymbolTag)
278 const fn = new TopLevelSymbol(name);
279 parser.tagVariable(name, topLevelSymbolTag, fn);
284 * @param {Dependency} dependency the dependency
285 * @param {Set<string> | boolean} usedByExports usedByExports info
286 * @param {ModuleGraph} moduleGraph moduleGraph
287 * @param {RuntimeSpec} runtime runtime
288 * @returns {boolean} false, when unused. Otherwise true
290 exports.isDependencyUsedByExports = (
296 if (usedByExports === false) return false;
297 if (usedByExports !== true && usedByExports !== undefined) {
298 const selfModule = moduleGraph.getParentModule(dependency);
299 const exportsInfo = moduleGraph.getExportsInfo(selfModule);
301 for (const exportName of usedByExports) {
302 if (exportsInfo.getUsed(exportName, runtime) !== UsageState.Unused)
305 if (!used) return false;
311 * @param {Dependency} dependency the dependency
312 * @param {Set<string> | boolean} usedByExports usedByExports info
313 * @param {ModuleGraph} moduleGraph moduleGraph
314 * @returns {null | false | function(ModuleGraphConnection, RuntimeSpec): ConnectionState} function to determine if the connection is active
316 exports.getDependencyUsedByExportsCondition = (
321 if (usedByExports === false) return false;
322 if (usedByExports !== true && usedByExports !== undefined) {
323 const selfModule = moduleGraph.getParentModule(dependency);
324 const exportsInfo = moduleGraph.getExportsInfo(selfModule);
325 return (connections, runtime) => {
326 for (const exportName of usedByExports) {
327 if (exportsInfo.getUsed(exportName, runtime) !== UsageState.Unused)
336 class TopLevelSymbol {
338 * @param {string} name name of the variable
345 exports.TopLevelSymbol = TopLevelSymbol;
346 exports.topLevelSymbolTag = topLevelSymbolTag;