InnerGraph.js 9.3 KB

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