InnerGraph.js 9.5 KB

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