InnerGraph.js 9.0 KB

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