InnerGraph.js 9.3 KB

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