InnerGraph.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Sergey Melyukov @smelukov
  4. */
  5. "use strict";
  6. const { UsageState } = require("../ExportsInfo");
  7. const JavascriptParser = require("../javascript/JavascriptParser");
  8. /** @typedef {import("../Dependency")} Dependency */
  9. /** @typedef {import("../Dependency").GetConditionFn} GetConditionFn */
  10. /** @typedef {import("../Module")} Module */
  11. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  12. /** @typedef {import("../Parser").ParserState} ParserState */
  13. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  14. /** @typedef {Set<string | TopLevelSymbol>} InnerGraphValueSet */
  15. /** @typedef {InnerGraphValueSet | true} InnerGraphValue */
  16. /** @typedef {TopLevelSymbol | null} InnerGraphKey */
  17. /** @typedef {Map<InnerGraphKey, InnerGraphValue | undefined>} InnerGraph */
  18. /** @typedef {(value: boolean | Set<string> | undefined) => void} UsageCallback */
  19. /**
  20. * Defines the state object type used by this module.
  21. * @typedef {object} StateObject
  22. * @property {InnerGraph} innerGraph
  23. * @property {TopLevelSymbol=} currentTopLevelSymbol
  24. * @property {Map<TopLevelSymbol, Set<UsageCallback>>} usageCallbackMap
  25. */
  26. /** @typedef {false | StateObject} State */
  27. class TopLevelSymbol {
  28. /**
  29. * Creates an instance of TopLevelSymbol.
  30. * @param {string} name name of the variable
  31. */
  32. constructor(name) {
  33. /** @type {string} */
  34. this.name = name;
  35. }
  36. }
  37. module.exports.TopLevelSymbol = TopLevelSymbol;
  38. /** @type {WeakMap<ParserState, State>} */
  39. const parserStateMap = new WeakMap();
  40. const topLevelSymbolTag = Symbol("top level symbol");
  41. /**
  42. * Returns state.
  43. * @param {ParserState} parserState parser state
  44. * @returns {State | undefined} state
  45. */
  46. function getState(parserState) {
  47. return parserStateMap.get(parserState);
  48. }
  49. /**
  50. * Processes the provided state.
  51. * @param {ParserState} state parser state
  52. * @param {TopLevelSymbol | null} symbol the symbol, or null for all symbols
  53. * @param {Usage} usage usage data
  54. * @returns {void}
  55. */
  56. module.exports.addUsage = (state, symbol, usage) => {
  57. const innerGraphState = getState(state);
  58. if (innerGraphState) {
  59. const { innerGraph } = innerGraphState;
  60. const info = innerGraph.get(symbol);
  61. if (usage === true) {
  62. innerGraph.set(symbol, true);
  63. } else if (info === undefined) {
  64. innerGraph.set(symbol, new Set([usage]));
  65. } else if (info !== true) {
  66. info.add(usage);
  67. }
  68. }
  69. };
  70. /** @typedef {string | TopLevelSymbol | true} Usage */
  71. /**
  72. * Processes the provided parser.
  73. * @param {JavascriptParser} parser the parser
  74. * @param {string} name name of variable
  75. * @param {Usage} usage usage data
  76. * @returns {void}
  77. */
  78. module.exports.addVariableUsage = (parser, name, usage) => {
  79. const symbol =
  80. /** @type {TopLevelSymbol} */ (
  81. parser.getTagData(name, topLevelSymbolTag)
  82. ) || module.exports.tagTopLevelSymbol(parser, name);
  83. if (symbol) {
  84. module.exports.addUsage(parser.state, symbol, usage);
  85. }
  86. };
  87. /**
  88. * Processes the provided parser state.
  89. * @param {ParserState} parserState parser state
  90. * @returns {void}
  91. */
  92. module.exports.bailout = (parserState) => {
  93. parserStateMap.set(parserState, false);
  94. };
  95. /**
  96. * Processes the provided parser state.
  97. * @param {ParserState} parserState parser state
  98. * @returns {void}
  99. */
  100. module.exports.enable = (parserState) => {
  101. const state = parserStateMap.get(parserState);
  102. if (state === false) {
  103. return;
  104. }
  105. parserStateMap.set(parserState, {
  106. innerGraph: new Map(),
  107. currentTopLevelSymbol: undefined,
  108. usageCallbackMap: new Map()
  109. });
  110. };
  111. /** @typedef {Set<string> | boolean} UsedByExports */
  112. /**
  113. * Usage callback map.
  114. * @param {Dependency} dependency the dependency
  115. * @param {UsedByExports | undefined} usedByExports usedByExports info
  116. * @param {ModuleGraph} moduleGraph moduleGraph
  117. * @returns {null | false | GetConditionFn} function to determine if the connection is active
  118. */
  119. module.exports.getDependencyUsedByExportsCondition = (
  120. dependency,
  121. usedByExports,
  122. moduleGraph
  123. ) => {
  124. if (usedByExports === false) return false;
  125. if (usedByExports !== true && usedByExports !== undefined) {
  126. const selfModule =
  127. /** @type {Module} */
  128. (moduleGraph.getParentModule(dependency));
  129. const exportsInfo = moduleGraph.getExportsInfo(selfModule);
  130. return (connections, runtime) => {
  131. for (const exportName of usedByExports) {
  132. if (exportsInfo.getUsed(exportName, runtime) !== UsageState.Unused) {
  133. return true;
  134. }
  135. }
  136. return false;
  137. };
  138. }
  139. return null;
  140. };
  141. /**
  142. * Returns usage data.
  143. * @param {ParserState} state parser state
  144. * @returns {TopLevelSymbol | void} usage data
  145. */
  146. module.exports.getTopLevelSymbol = (state) => {
  147. const innerGraphState = getState(state);
  148. if (innerGraphState) {
  149. return innerGraphState.currentTopLevelSymbol;
  150. }
  151. };
  152. /**
  153. * Processes the provided state.
  154. * @param {ParserState} state parser state
  155. * @returns {void}
  156. */
  157. module.exports.inferDependencyUsage = (state) => {
  158. const innerGraphState = getState(state);
  159. if (!innerGraphState) {
  160. return;
  161. }
  162. const { innerGraph, usageCallbackMap } = innerGraphState;
  163. /** @type {Map<InnerGraphKey, InnerGraphValueSet | undefined>} */
  164. const processed = new Map();
  165. // flatten graph to terminal nodes (string, undefined or true)
  166. const nonTerminal = new Set(innerGraph.keys());
  167. while (nonTerminal.size > 0) {
  168. for (const key of nonTerminal) {
  169. /** @type {InnerGraphValue} */
  170. let newSet = new Set();
  171. let isTerminal = true;
  172. const value = innerGraph.get(key);
  173. let alreadyProcessed = processed.get(key);
  174. if (alreadyProcessed === undefined) {
  175. /** @type {InnerGraphValueSet} */
  176. alreadyProcessed = new Set();
  177. processed.set(key, alreadyProcessed);
  178. }
  179. if (value !== true && value !== undefined) {
  180. for (const item of value) {
  181. alreadyProcessed.add(item);
  182. }
  183. for (const item of value) {
  184. if (typeof item === "string") {
  185. newSet.add(item);
  186. } else {
  187. const itemValue = innerGraph.get(item);
  188. if (itemValue === true) {
  189. newSet = true;
  190. break;
  191. }
  192. if (itemValue !== undefined) {
  193. for (const i of itemValue) {
  194. if (i === key) continue;
  195. if (alreadyProcessed.has(i)) continue;
  196. newSet.add(i);
  197. if (typeof i !== "string") {
  198. isTerminal = false;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. if (newSet === true) {
  205. innerGraph.set(key, true);
  206. } else if (newSet.size === 0) {
  207. innerGraph.set(key, undefined);
  208. } else {
  209. innerGraph.set(key, newSet);
  210. }
  211. }
  212. if (isTerminal) {
  213. nonTerminal.delete(key);
  214. // For the global key, merge with all other keys
  215. if (key === null) {
  216. const globalValue = innerGraph.get(null);
  217. if (globalValue) {
  218. for (const [key, value] of innerGraph) {
  219. if (key !== null && value !== true) {
  220. if (globalValue === true) {
  221. innerGraph.set(key, true);
  222. } else {
  223. const newSet = new Set(value);
  224. for (const item of globalValue) {
  225. newSet.add(item);
  226. }
  227. innerGraph.set(key, newSet);
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. /** @type {Map<Dependency, true | Set<string>>} */
  237. for (const [symbol, callbacks] of usageCallbackMap) {
  238. const usage = /** @type {true | Set<string> | undefined} */ (
  239. innerGraph.get(symbol)
  240. );
  241. for (const callback of callbacks) {
  242. callback(usage === undefined ? false : usage);
  243. }
  244. }
  245. };
  246. /**
  247. * Returns false, when unused. Otherwise true.
  248. * @param {Dependency} dependency the dependency
  249. * @param {UsedByExports | undefined} usedByExports usedByExports info
  250. * @param {ModuleGraph} moduleGraph moduleGraph
  251. * @param {RuntimeSpec} runtime runtime
  252. * @returns {boolean} false, when unused. Otherwise true
  253. */
  254. module.exports.isDependencyUsedByExports = (
  255. dependency,
  256. usedByExports,
  257. moduleGraph,
  258. runtime
  259. ) => {
  260. if (usedByExports === false) return false;
  261. if (usedByExports !== true && usedByExports !== undefined) {
  262. const selfModule =
  263. /** @type {Module} */
  264. (moduleGraph.getParentModule(dependency));
  265. const exportsInfo = moduleGraph.getExportsInfo(selfModule);
  266. let used = false;
  267. for (const exportName of usedByExports) {
  268. if (exportsInfo.getUsed(exportName, runtime) !== UsageState.Unused) {
  269. used = true;
  270. }
  271. }
  272. if (!used) return false;
  273. }
  274. return true;
  275. };
  276. /**
  277. * Returns true, when enabled.
  278. * @param {ParserState} parserState parser state
  279. * @returns {boolean} true, when enabled
  280. */
  281. module.exports.isEnabled = (parserState) => {
  282. const state = parserStateMap.get(parserState);
  283. return Boolean(state);
  284. };
  285. /**
  286. * Processes the provided state.
  287. * @param {ParserState} state parser state
  288. * @param {UsageCallback} onUsageCallback on usage callback
  289. */
  290. module.exports.onUsage = (state, onUsageCallback) => {
  291. const innerGraphState = getState(state);
  292. if (innerGraphState) {
  293. const { usageCallbackMap, currentTopLevelSymbol } = innerGraphState;
  294. if (currentTopLevelSymbol) {
  295. let callbacks = usageCallbackMap.get(currentTopLevelSymbol);
  296. if (callbacks === undefined) {
  297. /** @type {Set<UsageCallback>} */
  298. callbacks = new Set();
  299. usageCallbackMap.set(currentTopLevelSymbol, callbacks);
  300. }
  301. callbacks.add(onUsageCallback);
  302. } else {
  303. onUsageCallback(true);
  304. }
  305. } else {
  306. onUsageCallback(undefined);
  307. }
  308. };
  309. /**
  310. * Processes the provided state.
  311. * @param {ParserState} state parser state
  312. * @param {TopLevelSymbol | undefined} symbol the symbol
  313. */
  314. module.exports.setTopLevelSymbol = (state, symbol) => {
  315. const innerGraphState = getState(state);
  316. if (innerGraphState) {
  317. innerGraphState.currentTopLevelSymbol = symbol;
  318. }
  319. };
  320. /**
  321. * Returns symbol.
  322. * @param {JavascriptParser} parser parser
  323. * @param {string} name name of variable
  324. * @returns {TopLevelSymbol | undefined} symbol
  325. */
  326. module.exports.tagTopLevelSymbol = (parser, name) => {
  327. const innerGraphState = getState(parser.state);
  328. if (!innerGraphState) return;
  329. parser.defineVariable(name);
  330. const existingTag = /** @type {TopLevelSymbol} */ (
  331. parser.getTagData(name, topLevelSymbolTag)
  332. );
  333. if (existingTag) {
  334. return existingTag;
  335. }
  336. const symbol = new TopLevelSymbol(name);
  337. parser.tagVariable(
  338. name,
  339. topLevelSymbolTag,
  340. symbol,
  341. JavascriptParser.VariableInfoFlags.Normal
  342. );
  343. return symbol;
  344. };
  345. module.exports.topLevelSymbolTag = topLevelSymbolTag;