InnerGraph.js 9.2 KB

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