@wyw-in-js/transform 1.1.0 → 2.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (603) hide show
  1. package/esm/cache.js +426 -289
  2. package/esm/cache.js.map +1 -1
  3. package/esm/debug/fileReporter.js +137 -134
  4. package/esm/debug/fileReporter.js.map +1 -1
  5. package/esm/eval/broker.js +2134 -0
  6. package/esm/eval/broker.js.map +1 -0
  7. package/esm/eval/lru.js +36 -0
  8. package/esm/eval/lru.js.map +1 -0
  9. package/esm/eval/prepareModuleOnDemand.js +21 -0
  10. package/esm/eval/prepareModuleOnDemand.js.map +1 -0
  11. package/esm/eval/protocol.js +2 -0
  12. package/esm/eval/protocol.js.map +1 -0
  13. package/esm/eval/resolverStrategy.js +51 -0
  14. package/esm/eval/resolverStrategy.js.map +1 -0
  15. package/esm/eval/runner.js +1759 -0
  16. package/esm/eval/runner.js.map +1 -0
  17. package/esm/eval/serialize.js +333 -0
  18. package/esm/eval/serialize.js.map +1 -0
  19. package/esm/eval/writeQueue.js +81 -0
  20. package/esm/eval/writeQueue.js.map +1 -0
  21. package/esm/evaluators/index.js +11 -12
  22. package/esm/evaluators/index.js.map +1 -1
  23. package/esm/index.js +25 -29
  24. package/esm/index.js.map +1 -1
  25. package/esm/module.js +896 -520
  26. package/esm/module.js.map +1 -1
  27. package/esm/shaker.js +14 -80
  28. package/esm/shaker.js.map +1 -1
  29. package/esm/transform/BaseEntrypoint.js +162 -164
  30. package/esm/transform/BaseEntrypoint.js.map +1 -1
  31. package/esm/transform/Entrypoint.helpers.js +96 -253
  32. package/esm/transform/Entrypoint.helpers.js.map +1 -1
  33. package/esm/transform/Entrypoint.js +336 -270
  34. package/esm/transform/Entrypoint.js.map +1 -1
  35. package/esm/transform/Entrypoint.types.js +1 -1
  36. package/esm/transform/Entrypoint.types.js.map +1 -1
  37. package/esm/transform/EvaluatedEntrypoint.js +10 -4
  38. package/esm/transform/EvaluatedEntrypoint.js.map +1 -1
  39. package/esm/transform/actions/AbortError.js +6 -6
  40. package/esm/transform/actions/AbortError.js.map +1 -1
  41. package/esm/transform/actions/BaseAction.js +140 -136
  42. package/esm/transform/actions/BaseAction.js.map +1 -1
  43. package/esm/transform/actions/UnprocessedEntrypointError.js +6 -6
  44. package/esm/transform/actions/UnprocessedEntrypointError.js.map +1 -1
  45. package/esm/transform/actions/actionRunner.js +63 -66
  46. package/esm/transform/actions/actionRunner.js.map +1 -1
  47. package/esm/transform/actions/types.js +1 -1
  48. package/esm/transform/actions/types.js.map +1 -1
  49. package/esm/transform/barrelManifest.types.js +2 -0
  50. package/esm/transform/barrelManifest.types.js.map +1 -0
  51. package/esm/transform/generators/baseProcessingHandlers.js +15 -17
  52. package/esm/transform/generators/baseProcessingHandlers.js.map +1 -1
  53. package/esm/transform/generators/collect.js +27 -55
  54. package/esm/transform/generators/collect.js.map +1 -1
  55. package/esm/transform/generators/createStylisPreprocessor.js +353 -321
  56. package/esm/transform/generators/createStylisPreprocessor.js.map +1 -1
  57. package/esm/transform/generators/evalFile.js +48 -47
  58. package/esm/transform/generators/evalFile.js.map +1 -1
  59. package/esm/transform/generators/extract.js +78 -90
  60. package/esm/transform/generators/extract.js.map +1 -1
  61. package/esm/transform/generators/getExports.js +57 -74
  62. package/esm/transform/generators/getExports.js.map +1 -1
  63. package/esm/transform/generators/index.js +11 -11
  64. package/esm/transform/generators/index.js.map +1 -1
  65. package/esm/transform/generators/processEntrypoint.js +78 -67
  66. package/esm/transform/generators/processEntrypoint.js.map +1 -1
  67. package/esm/transform/generators/processImports.js +102 -67
  68. package/esm/transform/generators/processImports.js.map +1 -1
  69. package/esm/transform/generators/resolveImports.js +165 -196
  70. package/esm/transform/generators/resolveImports.js.map +1 -1
  71. package/esm/transform/generators/resolveStaticOxcValues.js +2911 -0
  72. package/esm/transform/generators/resolveStaticOxcValues.js.map +1 -0
  73. package/esm/transform/generators/rewriteOxcBarrelImports.js +822 -0
  74. package/esm/transform/generators/rewriteOxcBarrelImports.js.map +1 -0
  75. package/esm/transform/generators/transform.js +239 -248
  76. package/esm/transform/generators/transform.js.map +1 -1
  77. package/esm/transform/generators/workflow.js +87 -90
  78. package/esm/transform/generators/workflow.js.map +1 -1
  79. package/esm/transform/helpers/loadWywOptions.js +154 -74
  80. package/esm/transform/helpers/loadWywOptions.js.map +1 -1
  81. package/esm/transform/helpers/withDefaultServices.js +13 -22
  82. package/esm/transform/helpers/withDefaultServices.js.map +1 -1
  83. package/esm/transform/isStaticallyEvaluatableModule.js +140 -152
  84. package/esm/transform/isStaticallyEvaluatableModule.js.map +1 -1
  85. package/esm/transform/oxcBarrelManifest.js +349 -0
  86. package/esm/transform/oxcBarrelManifest.js.map +1 -0
  87. package/esm/transform/rootLog.js +3 -3
  88. package/esm/transform/rootLog.js.map +1 -1
  89. package/esm/transform/syntax.js +2 -0
  90. package/esm/transform/syntax.js.map +1 -0
  91. package/esm/transform/types.js +2 -2
  92. package/esm/transform/types.js.map +1 -1
  93. package/esm/transform.js +123 -147
  94. package/esm/transform.js.map +1 -1
  95. package/esm/types.js +4 -1
  96. package/esm/types.js.map +1 -1
  97. package/esm/utils/EventEmitter.js +46 -48
  98. package/esm/utils/EventEmitter.js.map +1 -1
  99. package/esm/utils/ShakerMetadata.js +2 -2
  100. package/esm/utils/ShakerMetadata.js.map +1 -1
  101. package/esm/utils/TransformDiagnostics.js +9 -9
  102. package/esm/utils/TransformDiagnostics.js.map +1 -1
  103. package/esm/utils/TransformMetadata.js +24 -26
  104. package/esm/utils/TransformMetadata.js.map +1 -1
  105. package/esm/utils/applyOxcProcessors.js +1217 -0
  106. package/esm/utils/applyOxcProcessors.js.map +1 -0
  107. package/esm/utils/collectOxcExportsAndImports.js +934 -0
  108. package/esm/utils/collectOxcExportsAndImports.js.map +1 -0
  109. package/esm/utils/collectOxcRuntime.js +220 -0
  110. package/esm/utils/collectOxcRuntime.js.map +1 -0
  111. package/esm/utils/collectOxcTemplateDependencies.js +1398 -0
  112. package/esm/utils/collectOxcTemplateDependencies.js.map +1 -0
  113. package/esm/utils/dispose-polyfill.js +3 -4
  114. package/esm/utils/dispose-polyfill.js.map +1 -1
  115. package/esm/utils/getFileIdx.js +6 -6
  116. package/esm/utils/getFileIdx.js.map +1 -1
  117. package/esm/utils/getPluginKey.js +12 -12
  118. package/esm/utils/getPluginKey.js.map +1 -1
  119. package/esm/utils/getVisitorKeys.js +9 -3
  120. package/esm/utils/getVisitorKeys.js.map +1 -1
  121. package/esm/utils/hasCachedWywPrevalExport.js +23 -0
  122. package/esm/utils/hasCachedWywPrevalExport.js.map +1 -0
  123. package/esm/utils/hasWywPreval.js +5 -5
  124. package/esm/utils/hasWywPreval.js.map +1 -1
  125. package/esm/utils/importOverrides.js +75 -90
  126. package/esm/utils/importOverrides.js.map +1 -1
  127. package/esm/utils/isNode.js +2 -2
  128. package/esm/utils/isNode.js.map +1 -1
  129. package/esm/utils/isNotNull.js +2 -2
  130. package/esm/utils/isNotNull.js.map +1 -1
  131. package/esm/utils/isSerializable.js +11 -11
  132. package/esm/utils/isSerializable.js.map +1 -1
  133. package/esm/utils/oxcAstService.js +121 -0
  134. package/esm/utils/oxcAstService.js.map +1 -0
  135. package/esm/utils/oxcEmit.js +447 -0
  136. package/esm/utils/oxcEmit.js.map +1 -0
  137. package/esm/utils/oxcPreevalStage.js +97 -0
  138. package/esm/utils/oxcPreevalStage.js.map +1 -0
  139. package/esm/utils/oxcPreevalTransforms.js +1054 -0
  140. package/esm/utils/oxcPreevalTransforms.js.map +1 -0
  141. package/esm/utils/oxcShaker.js +662 -0
  142. package/esm/utils/oxcShaker.js.map +1 -0
  143. package/esm/utils/parseOxc.js +37 -0
  144. package/esm/utils/parseOxc.js.map +1 -0
  145. package/esm/utils/parseRequest.js +27 -27
  146. package/esm/utils/parseRequest.js.map +1 -1
  147. package/esm/utils/peek.js +1 -1
  148. package/esm/utils/peek.js.map +1 -1
  149. package/esm/utils/processorLookup.js +125 -0
  150. package/esm/utils/processorLookup.js.map +1 -0
  151. package/esm/utils/resolveWithConditions.js +99 -0
  152. package/esm/utils/resolveWithConditions.js.map +1 -0
  153. package/esm/vm/createVmContext.js +140 -141
  154. package/esm/vm/createVmContext.js.map +1 -1
  155. package/esm/vm/process.js +11 -13
  156. package/esm/vm/process.js.map +1 -1
  157. package/package.json +18 -26
  158. package/types/cache.d.ts +17 -8
  159. package/types/cache.js +237 -95
  160. package/types/debug/fileReporter.js +22 -22
  161. package/types/eval/broker.d.ts +88 -0
  162. package/types/eval/broker.js +2262 -0
  163. package/types/eval/lru.d.ts +10 -0
  164. package/types/eval/lru.js +36 -0
  165. package/types/eval/prepareModuleOnDemand.d.ts +7 -0
  166. package/types/eval/prepareModuleOnDemand.js +24 -0
  167. package/types/eval/protocol.d.ts +97 -0
  168. package/types/eval/protocol.js +1 -0
  169. package/types/eval/resolverStrategy.d.ts +13 -0
  170. package/types/eval/resolverStrategy.js +46 -0
  171. package/types/eval/serialize.d.ts +78 -0
  172. package/types/eval/serialize.js +357 -0
  173. package/types/eval/writeQueue.d.ts +13 -0
  174. package/types/eval/writeQueue.js +80 -0
  175. package/types/evaluators/index.d.ts +2 -2
  176. package/types/evaluators/index.js +6 -9
  177. package/types/index.d.ts +3 -6
  178. package/types/index.js +24 -82
  179. package/types/module.d.ts +35 -7
  180. package/types/module.js +535 -163
  181. package/types/shaker.d.ts +2 -10
  182. package/types/shaker.js +10 -100
  183. package/types/transform/BaseEntrypoint.js +6 -11
  184. package/types/transform/Entrypoint.d.ts +15 -15
  185. package/types/transform/Entrypoint.helpers.d.ts +2 -5
  186. package/types/transform/Entrypoint.helpers.js +43 -203
  187. package/types/transform/Entrypoint.js +130 -53
  188. package/types/transform/Entrypoint.types.d.ts +28 -6
  189. package/types/transform/Entrypoint.types.js +1 -2
  190. package/types/transform/EvaluatedEntrypoint.d.ts +13 -2
  191. package/types/transform/EvaluatedEntrypoint.js +7 -6
  192. package/types/transform/actions/AbortError.js +2 -7
  193. package/types/transform/actions/BaseAction.js +4 -8
  194. package/types/transform/actions/UnprocessedEntrypointError.js +2 -7
  195. package/types/transform/actions/actionRunner.js +8 -12
  196. package/types/transform/actions/types.d.ts +2 -2
  197. package/types/transform/actions/types.js +1 -2
  198. package/types/transform/{barrelManifest.d.ts → barrelManifest.types.d.ts} +0 -2
  199. package/types/transform/barrelManifest.types.js +1 -0
  200. package/types/transform/generators/baseProcessingHandlers.d.ts +4 -5
  201. package/types/transform/generators/baseProcessingHandlers.js +10 -14
  202. package/types/transform/generators/collect.js +13 -39
  203. package/types/transform/generators/createStylisPreprocessor.js +19 -60
  204. package/types/transform/generators/evalFile.d.ts +2 -2
  205. package/types/transform/generators/evalFile.js +26 -28
  206. package/types/transform/generators/extract.js +5 -8
  207. package/types/transform/generators/getExports.js +23 -30
  208. package/types/transform/generators/index.d.ts +2 -2
  209. package/types/transform/generators/index.js +11 -14
  210. package/types/transform/generators/processEntrypoint.d.ts +2 -2
  211. package/types/transform/generators/processEntrypoint.js +60 -26
  212. package/types/transform/generators/processImports.d.ts +0 -3
  213. package/types/transform/generators/processImports.js +60 -20
  214. package/types/transform/generators/resolveImports.js +18 -22
  215. package/types/transform/generators/resolveStaticOxcValues.d.ts +2 -0
  216. package/types/transform/generators/resolveStaticOxcValues.js +3235 -0
  217. package/types/transform/generators/{rewriteBarrelImports.d.ts → rewriteOxcBarrelImports.d.ts} +2 -3
  218. package/types/transform/generators/{rewriteBarrelImports.js → rewriteOxcBarrelImports.js} +282 -225
  219. package/types/transform/generators/transform.d.ts +3 -7
  220. package/types/transform/generators/transform.js +203 -199
  221. package/types/transform/generators/workflow.js +62 -45
  222. package/types/transform/helpers/loadWywOptions.js +94 -20
  223. package/types/transform/helpers/withDefaultServices.d.ts +1 -1
  224. package/types/transform/helpers/withDefaultServices.js +6 -44
  225. package/types/transform/isStaticallyEvaluatableModule.d.ts +1 -2
  226. package/types/transform/isStaticallyEvaluatableModule.js +125 -126
  227. package/types/transform/oxcBarrelManifest.d.ts +2 -0
  228. package/types/transform/{barrelManifest.js → oxcBarrelManifest.js} +156 -97
  229. package/types/transform/rootLog.js +2 -5
  230. package/types/transform/syntax.d.ts +38 -0
  231. package/types/transform/syntax.js +1 -0
  232. package/types/transform/types.d.ts +9 -6
  233. package/types/transform/types.js +1 -4
  234. package/types/transform.d.ts +2 -2
  235. package/types/transform.js +88 -101
  236. package/types/types.d.ts +0 -23
  237. package/types/types.js +1 -2
  238. package/types/utils/EventEmitter.js +3 -9
  239. package/types/utils/ShakerMetadata.js +1 -5
  240. package/types/utils/TransformDiagnostics.js +3 -7
  241. package/types/utils/TransformMetadata.js +8 -16
  242. package/types/utils/applyOxcProcessors.d.ts +16 -0
  243. package/types/utils/applyOxcProcessors.js +1391 -0
  244. package/types/utils/collectOxcExportsAndImports.d.ts +35 -0
  245. package/types/utils/collectOxcExportsAndImports.js +957 -0
  246. package/types/utils/collectOxcRuntime.d.ts +14 -0
  247. package/types/utils/collectOxcRuntime.js +250 -0
  248. package/types/utils/collectOxcTemplateDependencies.d.ts +38 -0
  249. package/types/utils/collectOxcTemplateDependencies.js +1580 -0
  250. package/types/utils/getFileIdx.js +1 -4
  251. package/types/utils/getPluginKey.d.ts +5 -2
  252. package/types/utils/getPluginKey.js +2 -6
  253. package/types/utils/getVisitorKeys.d.ts +4 -4
  254. package/types/utils/getVisitorKeys.js +9 -6
  255. package/types/utils/hasCachedWywPrevalExport.d.ts +14 -0
  256. package/types/utils/hasCachedWywPrevalExport.js +30 -0
  257. package/types/utils/hasWywPreval.js +1 -4
  258. package/types/utils/importOverrides.js +17 -27
  259. package/types/utils/isNode.d.ts +2 -2
  260. package/types/utils/isNode.js +2 -6
  261. package/types/utils/isNotNull.js +1 -4
  262. package/types/utils/isSerializable.js +3 -6
  263. package/types/utils/oxcAstService.d.ts +11 -0
  264. package/types/utils/oxcAstService.js +79 -0
  265. package/types/utils/oxcEmit.d.ts +19 -0
  266. package/types/utils/oxcEmit.js +506 -0
  267. package/types/utils/oxcPreevalStage.d.ts +20 -0
  268. package/types/utils/oxcPreevalStage.js +102 -0
  269. package/types/utils/oxcPreevalTransforms.d.ts +13 -0
  270. package/types/utils/oxcPreevalTransforms.js +1179 -0
  271. package/types/utils/oxcShaker.d.ts +13 -0
  272. package/types/utils/oxcShaker.js +751 -0
  273. package/types/utils/parseOxc.d.ts +11 -0
  274. package/types/utils/parseOxc.js +38 -0
  275. package/types/utils/parseRequest.js +2 -7
  276. package/types/utils/peek.js +1 -5
  277. package/types/utils/processorLookup.d.ts +8 -0
  278. package/types/utils/processorLookup.js +135 -0
  279. package/types/utils/resolveWithConditions.d.ts +12 -0
  280. package/types/utils/resolveWithConditions.js +103 -0
  281. package/types/vm/createVmContext.d.ts +2 -2
  282. package/types/vm/createVmContext.js +25 -62
  283. package/types/vm/process.js +20 -26
  284. package/esm/babel.js +0 -2
  285. package/esm/babel.js.map +0 -1
  286. package/esm/options/buildOptions.js +0 -168
  287. package/esm/options/buildOptions.js.map +0 -1
  288. package/esm/options/buildOptions.test.js +0 -138
  289. package/esm/options/buildOptions.test.js.map +0 -1
  290. package/esm/options/loadBabelOptions.js +0 -24
  291. package/esm/options/loadBabelOptions.js.map +0 -1
  292. package/esm/plugins/babel-transform.js +0 -53
  293. package/esm/plugins/babel-transform.js.map +0 -1
  294. package/esm/plugins/collector.js +0 -60
  295. package/esm/plugins/collector.js.map +0 -1
  296. package/esm/plugins/dynamic-import.js +0 -56
  297. package/esm/plugins/dynamic-import.js.map +0 -1
  298. package/esm/plugins/preeval.js +0 -73
  299. package/esm/plugins/preeval.js.map +0 -1
  300. package/esm/plugins/shaker.js +0 -680
  301. package/esm/plugins/shaker.js.map +0 -1
  302. package/esm/transform/barrelManifest.js +0 -291
  303. package/esm/transform/barrelManifest.js.map +0 -1
  304. package/esm/transform/generators/explodeReexports.js +0 -64
  305. package/esm/transform/generators/explodeReexports.js.map +0 -1
  306. package/esm/transform/generators/rewriteBarrelImports.js +0 -733
  307. package/esm/transform/generators/rewriteBarrelImports.js.map +0 -1
  308. package/esm/utils/addIdentifierToWywPreval.js +0 -68
  309. package/esm/utils/addIdentifierToWywPreval.js.map +0 -1
  310. package/esm/utils/collectExportsAndImports.js +0 -1157
  311. package/esm/utils/collectExportsAndImports.js.map +0 -1
  312. package/esm/utils/collectTemplateDependencies.js +0 -228
  313. package/esm/utils/collectTemplateDependencies.js.map +0 -1
  314. package/esm/utils/createId.js +0 -6
  315. package/esm/utils/createId.js.map +0 -1
  316. package/esm/utils/findIdentifiers.js +0 -62
  317. package/esm/utils/findIdentifiers.js.map +0 -1
  318. package/esm/utils/getConstantStringValue.js +0 -58
  319. package/esm/utils/getConstantStringValue.js.map +0 -1
  320. package/esm/utils/getMemberExpressionPropertyName.js +0 -11
  321. package/esm/utils/getMemberExpressionPropertyName.js.map +0 -1
  322. package/esm/utils/getScope.js +0 -6
  323. package/esm/utils/getScope.js.map +0 -1
  324. package/esm/utils/getSource.js +0 -15
  325. package/esm/utils/getSource.js.map +0 -1
  326. package/esm/utils/getTagProcessor.js +0 -404
  327. package/esm/utils/getTagProcessor.js.map +0 -1
  328. package/esm/utils/isExports.js +0 -22
  329. package/esm/utils/isExports.js.map +0 -1
  330. package/esm/utils/isGlobal.js +0 -6
  331. package/esm/utils/isGlobal.js.map +0 -1
  332. package/esm/utils/isNodePath.js +0 -4
  333. package/esm/utils/isNodePath.js.map +0 -1
  334. package/esm/utils/isRemoved.js +0 -46
  335. package/esm/utils/isRemoved.js.map +0 -1
  336. package/esm/utils/isRequire.js +0 -13
  337. package/esm/utils/isRequire.js.map +0 -1
  338. package/esm/utils/isTypedNode.js +0 -6
  339. package/esm/utils/isTypedNode.js.map +0 -1
  340. package/esm/utils/isUnnecessaryReactCall.js +0 -72
  341. package/esm/utils/isUnnecessaryReactCall.js.map +0 -1
  342. package/esm/utils/removeDangerousCode.js +0 -276
  343. package/esm/utils/removeDangerousCode.js.map +0 -1
  344. package/esm/utils/replaceImportMetaEnv.js +0 -44
  345. package/esm/utils/replaceImportMetaEnv.js.map +0 -1
  346. package/esm/utils/scopeHelpers.js +0 -527
  347. package/esm/utils/scopeHelpers.js.map +0 -1
  348. package/esm/utils/traversalCache.js +0 -23
  349. package/esm/utils/traversalCache.js.map +0 -1
  350. package/esm/utils/unwrapExpression.js +0 -18
  351. package/esm/utils/unwrapExpression.js.map +0 -1
  352. package/esm/utils/unwrapSequence.js +0 -14
  353. package/esm/utils/unwrapSequence.js.map +0 -1
  354. package/esm/utils/valueToLiteral.js +0 -59
  355. package/esm/utils/valueToLiteral.js.map +0 -1
  356. package/esm/utils/visitors/JSXElementsRemover.js +0 -51
  357. package/esm/utils/visitors/JSXElementsRemover.js.map +0 -1
  358. package/lib/babel.js +0 -2
  359. package/lib/babel.js.map +0 -1
  360. package/lib/cache.js +0 -308
  361. package/lib/cache.js.map +0 -1
  362. package/lib/debug/fileReporter.js +0 -153
  363. package/lib/debug/fileReporter.js.map +0 -1
  364. package/lib/evaluators/index.js +0 -20
  365. package/lib/evaluators/index.js.map +0 -1
  366. package/lib/index.js +0 -286
  367. package/lib/index.js.map +0 -1
  368. package/lib/module.js +0 -552
  369. package/lib/module.js.map +0 -1
  370. package/lib/options/buildOptions.js +0 -176
  371. package/lib/options/buildOptions.js.map +0 -1
  372. package/lib/options/buildOptions.test.js +0 -141
  373. package/lib/options/buildOptions.test.js.map +0 -1
  374. package/lib/options/loadBabelOptions.js +0 -31
  375. package/lib/options/loadBabelOptions.js.map +0 -1
  376. package/lib/plugins/babel-transform.js +0 -60
  377. package/lib/plugins/babel-transform.js.map +0 -1
  378. package/lib/plugins/collector.js +0 -70
  379. package/lib/plugins/collector.js.map +0 -1
  380. package/lib/plugins/dynamic-import.js +0 -61
  381. package/lib/plugins/dynamic-import.js.map +0 -1
  382. package/lib/plugins/preeval.js +0 -81
  383. package/lib/plugins/preeval.js.map +0 -1
  384. package/lib/plugins/shaker.js +0 -691
  385. package/lib/plugins/shaker.js.map +0 -1
  386. package/lib/shaker.js +0 -95
  387. package/lib/shaker.js.map +0 -1
  388. package/lib/transform/BaseEntrypoint.js +0 -179
  389. package/lib/transform/BaseEntrypoint.js.map +0 -1
  390. package/lib/transform/Entrypoint.helpers.js +0 -279
  391. package/lib/transform/Entrypoint.helpers.js.map +0 -1
  392. package/lib/transform/Entrypoint.js +0 -289
  393. package/lib/transform/Entrypoint.js.map +0 -1
  394. package/lib/transform/Entrypoint.types.js +0 -2
  395. package/lib/transform/Entrypoint.types.js.map +0 -1
  396. package/lib/transform/EvaluatedEntrypoint.js +0 -13
  397. package/lib/transform/EvaluatedEntrypoint.js.map +0 -1
  398. package/lib/transform/actions/AbortError.js +0 -16
  399. package/lib/transform/actions/AbortError.js.map +0 -1
  400. package/lib/transform/actions/BaseAction.js +0 -150
  401. package/lib/transform/actions/BaseAction.js.map +0 -1
  402. package/lib/transform/actions/UnprocessedEntrypointError.js +0 -16
  403. package/lib/transform/actions/UnprocessedEntrypointError.js.map +0 -1
  404. package/lib/transform/actions/actionRunner.js +0 -82
  405. package/lib/transform/actions/actionRunner.js.map +0 -1
  406. package/lib/transform/actions/types.js +0 -2
  407. package/lib/transform/actions/types.js.map +0 -1
  408. package/lib/transform/barrelManifest.js +0 -300
  409. package/lib/transform/barrelManifest.js.map +0 -1
  410. package/lib/transform/generators/baseProcessingHandlers.js +0 -27
  411. package/lib/transform/generators/baseProcessingHandlers.js.map +0 -1
  412. package/lib/transform/generators/collect.js +0 -66
  413. package/lib/transform/generators/collect.js.map +0 -1
  414. package/lib/transform/generators/createStylisPreprocessor.js +0 -372
  415. package/lib/transform/generators/createStylisPreprocessor.js.map +0 -1
  416. package/lib/transform/generators/evalFile.js +0 -57
  417. package/lib/transform/generators/evalFile.js.map +0 -1
  418. package/lib/transform/generators/explodeReexports.js +0 -71
  419. package/lib/transform/generators/explodeReexports.js.map +0 -1
  420. package/lib/transform/generators/extract.js +0 -102
  421. package/lib/transform/generators/extract.js.map +0 -1
  422. package/lib/transform/generators/getExports.js +0 -85
  423. package/lib/transform/generators/getExports.js.map +0 -1
  424. package/lib/transform/generators/index.js +0 -19
  425. package/lib/transform/generators/index.js.map +0 -1
  426. package/lib/transform/generators/processEntrypoint.js +0 -76
  427. package/lib/transform/generators/processEntrypoint.js.map +0 -1
  428. package/lib/transform/generators/processImports.js +0 -82
  429. package/lib/transform/generators/processImports.js.map +0 -1
  430. package/lib/transform/generators/resolveImports.js +0 -221
  431. package/lib/transform/generators/resolveImports.js.map +0 -1
  432. package/lib/transform/generators/rewriteBarrelImports.js +0 -743
  433. package/lib/transform/generators/rewriteBarrelImports.js.map +0 -1
  434. package/lib/transform/generators/transform.js +0 -272
  435. package/lib/transform/generators/transform.js.map +0 -1
  436. package/lib/transform/generators/workflow.js +0 -100
  437. package/lib/transform/generators/workflow.js.map +0 -1
  438. package/lib/transform/helpers/loadWywOptions.js +0 -88
  439. package/lib/transform/helpers/loadWywOptions.js.map +0 -1
  440. package/lib/transform/helpers/withDefaultServices.js +0 -31
  441. package/lib/transform/helpers/withDefaultServices.js.map +0 -1
  442. package/lib/transform/isStaticallyEvaluatableModule.js +0 -159
  443. package/lib/transform/isStaticallyEvaluatableModule.js.map +0 -1
  444. package/lib/transform/rootLog.js +0 -9
  445. package/lib/transform/rootLog.js.map +0 -1
  446. package/lib/transform/types.js +0 -8
  447. package/lib/transform/types.js.map +0 -1
  448. package/lib/transform.js +0 -160
  449. package/lib/transform.js.map +0 -1
  450. package/lib/types.js +0 -2
  451. package/lib/types.js.map +0 -1
  452. package/lib/utils/EventEmitter.js +0 -61
  453. package/lib/utils/EventEmitter.js.map +0 -1
  454. package/lib/utils/ShakerMetadata.js +0 -9
  455. package/lib/utils/ShakerMetadata.js.map +0 -1
  456. package/lib/utils/TransformDiagnostics.js +0 -20
  457. package/lib/utils/TransformDiagnostics.js.map +0 -1
  458. package/lib/utils/TransformMetadata.js +0 -45
  459. package/lib/utils/TransformMetadata.js.map +0 -1
  460. package/lib/utils/addIdentifierToWywPreval.js +0 -75
  461. package/lib/utils/addIdentifierToWywPreval.js.map +0 -1
  462. package/lib/utils/collectExportsAndImports.js +0 -1173
  463. package/lib/utils/collectExportsAndImports.js.map +0 -1
  464. package/lib/utils/collectTemplateDependencies.js +0 -242
  465. package/lib/utils/collectTemplateDependencies.js.map +0 -1
  466. package/lib/utils/createId.js +0 -13
  467. package/lib/utils/createId.js.map +0 -1
  468. package/lib/utils/dispose-polyfill.js +0 -9
  469. package/lib/utils/dispose-polyfill.js.map +0 -1
  470. package/lib/utils/findIdentifiers.js +0 -73
  471. package/lib/utils/findIdentifiers.js.map +0 -1
  472. package/lib/utils/getConstantStringValue.js +0 -66
  473. package/lib/utils/getConstantStringValue.js.map +0 -1
  474. package/lib/utils/getFileIdx.js +0 -16
  475. package/lib/utils/getFileIdx.js.map +0 -1
  476. package/lib/utils/getMemberExpressionPropertyName.js +0 -18
  477. package/lib/utils/getMemberExpressionPropertyName.js.map +0 -1
  478. package/lib/utils/getPluginKey.js +0 -21
  479. package/lib/utils/getPluginKey.js.map +0 -1
  480. package/lib/utils/getScope.js +0 -12
  481. package/lib/utils/getScope.js.map +0 -1
  482. package/lib/utils/getSource.js +0 -24
  483. package/lib/utils/getSource.js.map +0 -1
  484. package/lib/utils/getTagProcessor.js +0 -424
  485. package/lib/utils/getTagProcessor.js.map +0 -1
  486. package/lib/utils/getVisitorKeys.js +0 -11
  487. package/lib/utils/getVisitorKeys.js.map +0 -1
  488. package/lib/utils/hasWywPreval.js +0 -13
  489. package/lib/utils/hasWywPreval.js.map +0 -1
  490. package/lib/utils/importOverrides.js +0 -119
  491. package/lib/utils/importOverrides.js.map +0 -1
  492. package/lib/utils/isExports.js +0 -27
  493. package/lib/utils/isExports.js.map +0 -1
  494. package/lib/utils/isGlobal.js +0 -13
  495. package/lib/utils/isGlobal.js.map +0 -1
  496. package/lib/utils/isNode.js +0 -9
  497. package/lib/utils/isNode.js.map +0 -1
  498. package/lib/utils/isNodePath.js +0 -10
  499. package/lib/utils/isNodePath.js.map +0 -1
  500. package/lib/utils/isNotNull.js +0 -10
  501. package/lib/utils/isNotNull.js.map +0 -1
  502. package/lib/utils/isRemoved.js +0 -52
  503. package/lib/utils/isRemoved.js.map +0 -1
  504. package/lib/utils/isRequire.js +0 -18
  505. package/lib/utils/isRequire.js.map +0 -1
  506. package/lib/utils/isSerializable.js +0 -19
  507. package/lib/utils/isSerializable.js.map +0 -1
  508. package/lib/utils/isTypedNode.js +0 -13
  509. package/lib/utils/isTypedNode.js.map +0 -1
  510. package/lib/utils/isUnnecessaryReactCall.js +0 -81
  511. package/lib/utils/isUnnecessaryReactCall.js.map +0 -1
  512. package/lib/utils/parseRequest.js +0 -37
  513. package/lib/utils/parseRequest.js.map +0 -1
  514. package/lib/utils/peek.js +0 -9
  515. package/lib/utils/peek.js.map +0 -1
  516. package/lib/utils/removeDangerousCode.js +0 -284
  517. package/lib/utils/removeDangerousCode.js.map +0 -1
  518. package/lib/utils/replaceImportMetaEnv.js +0 -50
  519. package/lib/utils/replaceImportMetaEnv.js.map +0 -1
  520. package/lib/utils/scopeHelpers.js +0 -557
  521. package/lib/utils/scopeHelpers.js.map +0 -1
  522. package/lib/utils/traversalCache.js +0 -31
  523. package/lib/utils/traversalCache.js.map +0 -1
  524. package/lib/utils/unwrapExpression.js +0 -25
  525. package/lib/utils/unwrapExpression.js.map +0 -1
  526. package/lib/utils/unwrapSequence.js +0 -20
  527. package/lib/utils/unwrapSequence.js.map +0 -1
  528. package/lib/utils/valueToLiteral.js +0 -65
  529. package/lib/utils/valueToLiteral.js.map +0 -1
  530. package/lib/utils/visitors/JSXElementsRemover.js +0 -57
  531. package/lib/utils/visitors/JSXElementsRemover.js.map +0 -1
  532. package/lib/vm/createVmContext.js +0 -166
  533. package/lib/vm/createVmContext.js.map +0 -1
  534. package/lib/vm/process.js +0 -38
  535. package/lib/vm/process.js.map +0 -1
  536. package/types/babel.d.ts +0 -2
  537. package/types/babel.js +0 -2
  538. package/types/options/buildOptions.d.ts +0 -6
  539. package/types/options/buildOptions.js +0 -178
  540. package/types/options/loadBabelOptions.d.ts +0 -3
  541. package/types/options/loadBabelOptions.js +0 -26
  542. package/types/plugins/babel-transform.d.ts +0 -4
  543. package/types/plugins/babel-transform.js +0 -49
  544. package/types/plugins/collector.d.ts +0 -23
  545. package/types/plugins/collector.js +0 -62
  546. package/types/plugins/dynamic-import.d.ts +0 -6
  547. package/types/plugins/dynamic-import.js +0 -60
  548. package/types/plugins/preeval.d.ts +0 -16
  549. package/types/plugins/preeval.js +0 -69
  550. package/types/plugins/shaker.d.ts +0 -14
  551. package/types/plugins/shaker.js +0 -724
  552. package/types/transform/generators/explodeReexports.d.ts +0 -7
  553. package/types/transform/generators/explodeReexports.js +0 -65
  554. package/types/utils/addIdentifierToWywPreval.d.ts +0 -4
  555. package/types/utils/addIdentifierToWywPreval.js +0 -74
  556. package/types/utils/collectExportsAndImports.d.ts +0 -31
  557. package/types/utils/collectExportsAndImports.js +0 -1147
  558. package/types/utils/collectTemplateDependencies.d.ts +0 -17
  559. package/types/utils/collectTemplateDependencies.js +0 -220
  560. package/types/utils/createId.d.ts +0 -2
  561. package/types/utils/createId.js +0 -9
  562. package/types/utils/findIdentifiers.d.ts +0 -6
  563. package/types/utils/findIdentifiers.js +0 -67
  564. package/types/utils/getConstantStringValue.d.ts +0 -2
  565. package/types/utils/getConstantStringValue.js +0 -94
  566. package/types/utils/getMemberExpressionPropertyName.d.ts +0 -2
  567. package/types/utils/getMemberExpressionPropertyName.js +0 -46
  568. package/types/utils/getScope.d.ts +0 -2
  569. package/types/utils/getScope.js +0 -10
  570. package/types/utils/getSource.d.ts +0 -2
  571. package/types/utils/getSource.js +0 -22
  572. package/types/utils/getTagProcessor.d.ts +0 -13
  573. package/types/utils/getTagProcessor.js +0 -411
  574. package/types/utils/isExports.d.ts +0 -6
  575. package/types/utils/isExports.js +0 -19
  576. package/types/utils/isGlobal.d.ts +0 -2
  577. package/types/utils/isGlobal.js +0 -9
  578. package/types/utils/isNodePath.d.ts +0 -3
  579. package/types/utils/isNodePath.js +0 -6
  580. package/types/utils/isRemoved.d.ts +0 -5
  581. package/types/utils/isRemoved.js +0 -41
  582. package/types/utils/isRequire.d.ts +0 -6
  583. package/types/utils/isRequire.js +0 -14
  584. package/types/utils/isTypedNode.d.ts +0 -5
  585. package/types/utils/isTypedNode.js +0 -9
  586. package/types/utils/isUnnecessaryReactCall.d.ts +0 -3
  587. package/types/utils/isUnnecessaryReactCall.js +0 -75
  588. package/types/utils/removeDangerousCode.d.ts +0 -4
  589. package/types/utils/removeDangerousCode.js +0 -326
  590. package/types/utils/replaceImportMetaEnv.d.ts +0 -3
  591. package/types/utils/replaceImportMetaEnv.js +0 -39
  592. package/types/utils/scopeHelpers.d.ts +0 -12
  593. package/types/utils/scopeHelpers.js +0 -580
  594. package/types/utils/traversalCache.d.ts +0 -4
  595. package/types/utils/traversalCache.js +0 -27
  596. package/types/utils/unwrapExpression.d.ts +0 -2
  597. package/types/utils/unwrapExpression.js +0 -57
  598. package/types/utils/unwrapSequence.d.ts +0 -8
  599. package/types/utils/unwrapSequence.js +0 -16
  600. package/types/utils/valueToLiteral.d.ts +0 -3
  601. package/types/utils/valueToLiteral.js +0 -63
  602. package/types/utils/visitors/JSXElementsRemover.d.ts +0 -3
  603. package/types/utils/visitors/JSXElementsRemover.js +0 -51
@@ -0,0 +1,1580 @@
1
+ /* eslint-disable no-restricted-syntax,no-continue,@typescript-eslint/no-use-before-define */
2
+ import { ValueType } from '@wyw-in-js/shared';
3
+ import { parseOxcProgramCached } from './parseOxc';
4
+ const isNode = (value) => !!value &&
5
+ typeof value === 'object' &&
6
+ 'type' in value &&
7
+ typeof value.type === 'string';
8
+ const getChildren = (node) => {
9
+ const result = [];
10
+ const record = node;
11
+ Object.keys(record).forEach((key) => {
12
+ if (key === 'type' || key === 'start' || key === 'end' || key === 'range') {
13
+ return;
14
+ }
15
+ const value = record[key];
16
+ if (isNode(value)) {
17
+ result.push(value);
18
+ return;
19
+ }
20
+ if (Array.isArray(value)) {
21
+ value.forEach((item) => {
22
+ if (isNode(item)) {
23
+ result.push(item);
24
+ }
25
+ });
26
+ }
27
+ });
28
+ return result;
29
+ };
30
+ const parseOxc = (code, filename) => {
31
+ return parseOxcProgramCached(filename, code, 'unambiguous');
32
+ };
33
+ const toSpanKey = (start, end) => `${start}:${end}`;
34
+ const createSpanLookup = (spans) => {
35
+ if (!spans || spans.length === 0) {
36
+ return null;
37
+ }
38
+ return new Set(spans.map((span) => toSpanKey(span.start, span.end)));
39
+ };
40
+ const matchesSpanLookup = (node, spanLookup) => !spanLookup || spanLookup.has(toSpanKey(node.start, node.end));
41
+ const createLocationLookup = (code) => {
42
+ const lineStarts = [0];
43
+ for (let idx = 0; idx < code.length; idx += 1) {
44
+ if (code[idx] === '\n') {
45
+ lineStarts.push(idx + 1);
46
+ }
47
+ }
48
+ return (offset) => {
49
+ let low = 0;
50
+ let high = lineStarts.length - 1;
51
+ while (low <= high) {
52
+ const mid = Math.floor((low + high) / 2);
53
+ const next = lineStarts[mid + 1] ?? Infinity;
54
+ if (lineStarts[mid] <= offset && offset < next) {
55
+ return {
56
+ column: offset - lineStarts[mid],
57
+ line: mid + 1,
58
+ };
59
+ }
60
+ if (offset < lineStarts[mid]) {
61
+ high = mid - 1;
62
+ }
63
+ else {
64
+ low = mid + 1;
65
+ }
66
+ }
67
+ const lastLine = lineStarts.length - 1;
68
+ return {
69
+ column: Math.max(0, offset - lineStarts[lastLine]),
70
+ line: lastLine + 1,
71
+ };
72
+ };
73
+ };
74
+ const getSourceLocation = (start, end, ctx) => ({
75
+ end: ctx.loc(end),
76
+ filename: ctx.filename,
77
+ identifierName: undefined,
78
+ start: ctx.loc(start),
79
+ });
80
+ const createScope = (parent, node, root = false, functionBoundary = false) => ({
81
+ bindings: new Map(),
82
+ depth: parent ? parent.depth + 1 : 0,
83
+ end: node.end,
84
+ functionBoundary,
85
+ params: new Set(),
86
+ parent,
87
+ root,
88
+ start: node.start,
89
+ });
90
+ const normalizeDeclarationKind = (declarationKind) => {
91
+ if (declarationKind === 'var') {
92
+ return 'var';
93
+ }
94
+ if (declarationKind === 'let') {
95
+ return 'let';
96
+ }
97
+ return 'const';
98
+ };
99
+ const moduleExportName = (node) => node.type === 'Literal' ? String(node.value) : node.name;
100
+ const getImportSpecifierInfo = (statement, specifier) => {
101
+ const local = specifier.local?.name;
102
+ if (!local) {
103
+ return null;
104
+ }
105
+ if (specifier.type === 'ImportDefaultSpecifier') {
106
+ return {
107
+ imported: 'default',
108
+ local,
109
+ };
110
+ }
111
+ if (specifier.type === 'ImportNamespaceSpecifier') {
112
+ return {
113
+ imported: '*',
114
+ local,
115
+ };
116
+ }
117
+ if (statement.importKind === 'type' ||
118
+ specifier.importKind === 'type') {
119
+ return null;
120
+ }
121
+ return {
122
+ imported: moduleExportName(specifier.imported),
123
+ local,
124
+ };
125
+ };
126
+ const getDeclarationScope = (scope, declarationKind) => {
127
+ if (declarationKind !== 'var') {
128
+ return scope;
129
+ }
130
+ let current = scope;
131
+ while (current && !current.functionBoundary) {
132
+ current = current.parent;
133
+ }
134
+ return current ?? scope;
135
+ };
136
+ const collectBindingNames = (node) => {
137
+ if (node.type === 'Identifier') {
138
+ return [node.name];
139
+ }
140
+ if (node.type === 'RestElement') {
141
+ return collectBindingNames(node.argument);
142
+ }
143
+ if (node.type === 'AssignmentPattern') {
144
+ return collectBindingNames(node.left);
145
+ }
146
+ if (node.type === 'ObjectPattern') {
147
+ return node.properties.flatMap((property) => property.type === 'RestElement'
148
+ ? collectBindingNames(property.argument)
149
+ : collectBindingNames(property.value));
150
+ }
151
+ if (node.type === 'ArrayPattern') {
152
+ return node.elements.flatMap((element) => element ? collectBindingNames(element) : []);
153
+ }
154
+ if (node.type === 'TSParameterProperty') {
155
+ return collectBindingNames(node.parameter);
156
+ }
157
+ return [];
158
+ };
159
+ const isInTypeContext = (ancestors) => ancestors.some((ancestor) => ancestor.type.startsWith('TS') || ancestor.type.startsWith('JSDoc'));
160
+ const isPropertyOnlyIdentifier = (node, parent) => !!parent &&
161
+ parent.type === 'MemberExpression' &&
162
+ parent.property === node &&
163
+ !parent.computed;
164
+ const isObjectPropertyKey = (node, parent) => !!parent &&
165
+ parent.type === 'Property' &&
166
+ parent.key === node &&
167
+ !parent.computed &&
168
+ parent.value !== node;
169
+ const isBindingPosition = (node, parent) => {
170
+ if (!parent) {
171
+ return false;
172
+ }
173
+ if (parent.type === 'VariableDeclarator' && parent.id === node) {
174
+ return true;
175
+ }
176
+ if ((parent.type === 'FunctionDeclaration' ||
177
+ parent.type === 'FunctionExpression' ||
178
+ parent.type === 'ClassDeclaration' ||
179
+ parent.type === 'ClassExpression') &&
180
+ parent.id === node) {
181
+ return true;
182
+ }
183
+ if ((parent.type === 'ImportSpecifier' ||
184
+ parent.type === 'ImportDefaultSpecifier' ||
185
+ parent.type === 'ImportNamespaceSpecifier') &&
186
+ 'local' in parent &&
187
+ parent.local === node) {
188
+ return true;
189
+ }
190
+ return false;
191
+ };
192
+ const visit = (node, scope, enter, parent = null, ancestors = []) => {
193
+ const visitNode = (currentNode, currentScope, currentParent) => {
194
+ let nextScope;
195
+ if (currentNode.type === 'Program') {
196
+ nextScope = createScope(null, currentNode, true, true);
197
+ }
198
+ else if (currentNode.type === 'BlockStatement' ||
199
+ currentNode.type === 'FunctionDeclaration' ||
200
+ currentNode.type === 'FunctionExpression' ||
201
+ currentNode.type === 'ArrowFunctionExpression') {
202
+ nextScope = createScope(currentScope, currentNode, false, currentNode.type !== 'BlockStatement');
203
+ }
204
+ else if (currentScope) {
205
+ nextScope = currentScope;
206
+ }
207
+ else {
208
+ nextScope = createScope(null, currentNode, false, true);
209
+ }
210
+ if (currentNode.type === 'FunctionDeclaration' ||
211
+ currentNode.type === 'FunctionExpression' ||
212
+ currentNode.type === 'ArrowFunctionExpression') {
213
+ currentNode.params.forEach((param) => {
214
+ collectBindingNames(param).forEach((name) => {
215
+ nextScope.params.add(name);
216
+ nextScope.bindings.set(name, {
217
+ declaredAt: param.start,
218
+ declaration: null,
219
+ declarator: null,
220
+ functionNode: null,
221
+ isRoot: false,
222
+ kind: 'param',
223
+ name,
224
+ scope: nextScope,
225
+ });
226
+ });
227
+ });
228
+ }
229
+ enter(currentNode, nextScope, currentParent, ancestors);
230
+ ancestors.push(currentNode);
231
+ getChildren(currentNode).forEach((child) => visitNode(child, nextScope, currentNode));
232
+ ancestors.pop();
233
+ };
234
+ visitNode(node, scope, parent);
235
+ };
236
+ const analyzeProgram = (program, { collectTargetExpressions = false, collectTemplateLiterals = false, expressionSpanLookup = null, templateSpanLookup = null, } = {}) => {
237
+ const bindings = new Map();
238
+ const usedNames = new Set();
239
+ const templateLiterals = [];
240
+ const targetExpressions = [];
241
+ const addBinding = (scope, binding) => {
242
+ scope.bindings.set(binding.name, binding);
243
+ const existing = bindings.get(binding.name) ?? [];
244
+ existing.push(binding);
245
+ bindings.set(binding.name, existing);
246
+ };
247
+ const collectTargets = (node, ancestors) => {
248
+ if (collectTemplateLiterals &&
249
+ node.type === 'TemplateLiteral' &&
250
+ node.expressions.length > 0 &&
251
+ !ancestors.some((ancestor) => ancestor.type === 'TemplateLiteral') &&
252
+ matchesSpanLookup(node, templateSpanLookup)) {
253
+ templateLiterals.push(node);
254
+ }
255
+ if (collectTargetExpressions &&
256
+ expressionSpanLookup &&
257
+ matchesSpanLookup(node, expressionSpanLookup)) {
258
+ targetExpressions.push(node);
259
+ }
260
+ };
261
+ visit(program, null, (node, scope, _parent, ancestors) => {
262
+ collectTargets(node, ancestors);
263
+ if (node.type === 'Identifier') {
264
+ usedNames.add(node.name);
265
+ }
266
+ if (node.type === 'FunctionDeclaration' ||
267
+ node.type === 'FunctionExpression' ||
268
+ node.type === 'ArrowFunctionExpression') {
269
+ node.params.forEach((param) => {
270
+ collectBindingNames(param).forEach((name) => {
271
+ const binding = scope.bindings.get(name);
272
+ if (binding) {
273
+ addBinding(scope, binding);
274
+ }
275
+ });
276
+ });
277
+ if (node.type !== 'FunctionDeclaration') {
278
+ return;
279
+ }
280
+ }
281
+ if (node.type === 'ImportDeclaration') {
282
+ const source = node.source.value;
283
+ node.specifiers.forEach((specifier) => {
284
+ const importInfo = getImportSpecifierInfo(node, specifier);
285
+ if (!importInfo) {
286
+ return;
287
+ }
288
+ addBinding(scope, {
289
+ declaredAt: specifier.start,
290
+ declaration: null,
291
+ declarator: null,
292
+ functionNode: null,
293
+ imported: importInfo.imported,
294
+ importedFrom: source,
295
+ isRoot: scope.root,
296
+ kind: 'import',
297
+ name: importInfo.local,
298
+ scope,
299
+ });
300
+ });
301
+ return;
302
+ }
303
+ if (node.type !== 'VariableDeclaration') {
304
+ if (node.type === 'FunctionDeclaration' && node.id) {
305
+ const declarationScope = scope.parent ?? scope;
306
+ const binding = {
307
+ declaredAt: node.start,
308
+ declaration: null,
309
+ declarator: null,
310
+ functionNode: node,
311
+ isRoot: declarationScope.root,
312
+ kind: 'function',
313
+ name: node.id.name,
314
+ scope: declarationScope,
315
+ };
316
+ addBinding(declarationScope, binding);
317
+ }
318
+ return;
319
+ }
320
+ node.declarations.forEach((declarator) => {
321
+ collectBindingNames(declarator.id).forEach((name) => {
322
+ const declarationKind = normalizeDeclarationKind(node.kind);
323
+ const declarationScope = getDeclarationScope(scope, declarationKind);
324
+ const binding = {
325
+ declarationKind,
326
+ declaredAt: declarator.start,
327
+ declaration: node,
328
+ declarator,
329
+ functionNode: null,
330
+ isRoot: declarationScope.root,
331
+ kind: 'variable',
332
+ name,
333
+ scope: declarationScope,
334
+ };
335
+ addBinding(declarationScope, binding);
336
+ });
337
+ });
338
+ });
339
+ return {
340
+ bindingsByName: bindings,
341
+ rootMutationsByBinding: collectRootMutations(program),
342
+ targetExpressions: targetExpressions.sort((a, b) => a.start - b.start),
343
+ templateLiterals,
344
+ usedNames,
345
+ };
346
+ };
347
+ const resolveBindingAt = (ctx, name, referenceStart) => {
348
+ const cachedBindings = ctx.bindingResolutionCache.get(name);
349
+ if (cachedBindings?.has(referenceStart)) {
350
+ return cachedBindings.get(referenceStart) ?? undefined;
351
+ }
352
+ const bindings = ctx.bindingsByName.get(name);
353
+ const bindingCache = cachedBindings ?? new Map();
354
+ if (!cachedBindings) {
355
+ ctx.bindingResolutionCache.set(name, bindingCache);
356
+ }
357
+ if (!bindings || bindings.length === 0) {
358
+ bindingCache.set(referenceStart, null);
359
+ return undefined;
360
+ }
361
+ let binding;
362
+ bindings.forEach((candidate) => {
363
+ if (candidate.scope.start > referenceStart ||
364
+ referenceStart >= candidate.scope.end) {
365
+ return;
366
+ }
367
+ if (!binding ||
368
+ candidate.scope.depth > binding.scope.depth ||
369
+ (candidate.scope.depth === binding.scope.depth &&
370
+ candidate.declaredAt > binding.declaredAt)) {
371
+ binding = candidate;
372
+ }
373
+ });
374
+ bindingCache.set(referenceStart, binding ?? null);
375
+ return binding;
376
+ };
377
+ const collectRootMutations = (program) => {
378
+ const mutations = new Map();
379
+ const getRootMutationTarget = (node) => {
380
+ if (node.type === 'Identifier') {
381
+ return {
382
+ binding: node.name,
383
+ path: [],
384
+ };
385
+ }
386
+ if (node.type !== 'MemberExpression') {
387
+ return null;
388
+ }
389
+ const parent = getRootMutationTarget(node.object);
390
+ if (!parent) {
391
+ return null;
392
+ }
393
+ let key = null;
394
+ if (node.computed &&
395
+ node.property.type === 'Literal' &&
396
+ (typeof node.property.value === 'string' ||
397
+ typeof node.property.value === 'number')) {
398
+ key = node.property.value;
399
+ }
400
+ else if (!node.computed && node.property.type === 'Identifier') {
401
+ key = node.property.name;
402
+ }
403
+ if (key === null) {
404
+ return null;
405
+ }
406
+ return {
407
+ binding: parent.binding,
408
+ path: [...parent.path, key],
409
+ };
410
+ };
411
+ program.body.forEach((statement) => {
412
+ if (statement.type !== 'ExpressionStatement') {
413
+ return;
414
+ }
415
+ const { expression } = statement;
416
+ if (expression.type === 'AssignmentExpression') {
417
+ const target = getRootMutationTarget(expression.left);
418
+ if (!target || target.path.length === 0) {
419
+ return;
420
+ }
421
+ const bucket = mutations.get(target.binding) ?? [];
422
+ bucket.push(expression);
423
+ mutations.set(target.binding, bucket);
424
+ return;
425
+ }
426
+ if (expression.type === 'UpdateExpression') {
427
+ const target = getRootMutationTarget(expression.argument);
428
+ if (!target || target.path.length === 0) {
429
+ return;
430
+ }
431
+ const bucket = mutations.get(target.binding) ?? [];
432
+ bucket.push(expression);
433
+ mutations.set(target.binding, bucket);
434
+ }
435
+ });
436
+ return mutations;
437
+ };
438
+ const hasLocalBinding = (scope, name) => {
439
+ let current = scope;
440
+ while (current) {
441
+ if (current.bindings.has(name)) {
442
+ return true;
443
+ }
444
+ current = current.parent;
445
+ }
446
+ return false;
447
+ };
448
+ const hasLocalBindingCached = (scope, name, cache) => {
449
+ const scopeCache = cache.get(scope);
450
+ if (scopeCache?.has(name)) {
451
+ return scopeCache.get(name);
452
+ }
453
+ const result = hasLocalBinding(scope, name);
454
+ const nextScopeCache = scopeCache ?? new Map();
455
+ nextScopeCache.set(name, result);
456
+ if (!scopeCache) {
457
+ cache.set(scope, nextScopeCache);
458
+ }
459
+ return result;
460
+ };
461
+ const findReferences = (node, referenceCache) => {
462
+ const cachedReferences = referenceCache?.get(node);
463
+ if (cachedReferences) {
464
+ return cachedReferences;
465
+ }
466
+ const refs = new Map();
467
+ const localBindingCache = new WeakMap();
468
+ visit(node, null, (current, scope, parent, ancestors) => {
469
+ if (current.type !== 'Identifier' ||
470
+ isInTypeContext(ancestors) ||
471
+ isBindingPosition(current, parent) ||
472
+ isPropertyOnlyIdentifier(current, parent) ||
473
+ isObjectPropertyKey(current, parent) ||
474
+ hasLocalBindingCached(scope, current.name, localBindingCache)) {
475
+ return;
476
+ }
477
+ const key = `${current.start}:${current.end}:${current.name}`;
478
+ refs.set(key, {
479
+ end: current.end,
480
+ name: current.name,
481
+ start: current.start,
482
+ });
483
+ });
484
+ const resolvedReferences = [...refs.values()];
485
+ referenceCache?.set(node, resolvedReferences);
486
+ return resolvedReferences;
487
+ };
488
+ const isBindingDeclaredWithin = (binding, container) => container.start <= binding.declaredAt && binding.declaredAt < container.end;
489
+ const literalCode = (value) => {
490
+ if (typeof value === 'number') {
491
+ return Number.isFinite(value) ? JSON.stringify(value) : null;
492
+ }
493
+ if (value === null ||
494
+ typeof value === 'string' ||
495
+ typeof value === 'boolean') {
496
+ return JSON.stringify(value);
497
+ }
498
+ if (Array.isArray(value)) {
499
+ return JSON.stringify(value);
500
+ }
501
+ if (typeof value === 'object' && value !== null) {
502
+ return `(${JSON.stringify(value)})`;
503
+ }
504
+ return null;
505
+ };
506
+ const isStaticSerializableValue = (value) => literalCode(value) !== null;
507
+ const cloneStaticValue = (value) => {
508
+ if (Array.isArray(value)) {
509
+ return value.map((item) => cloneStaticValue(item));
510
+ }
511
+ if (typeof value === 'object' && value !== null) {
512
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, cloneStaticValue(item)]));
513
+ }
514
+ return value;
515
+ };
516
+ const getObjectMember = (objectValue, property) => {
517
+ if (objectValue === null ||
518
+ objectValue === undefined ||
519
+ (typeof objectValue !== 'object' &&
520
+ typeof objectValue !== 'string' &&
521
+ typeof objectValue !== 'number' &&
522
+ typeof objectValue !== 'boolean')) {
523
+ return undefined;
524
+ }
525
+ return objectValue[property];
526
+ };
527
+ const oxcStaticCallableValue = Symbol('wyw.oxc.staticCallableValue');
528
+ const isOxcStaticCallableValue = (value) => typeof value === 'object' &&
529
+ value !== null &&
530
+ oxcStaticCallableValue in value;
531
+ const unwrapOxcStaticCallableValue = (value) => isOxcStaticCallableValue(value) ? value[oxcStaticCallableValue] : value;
532
+ export const createOxcStaticCallableValue = (value) => ({
533
+ [oxcStaticCallableValue]: value,
534
+ });
535
+ const assignPatternValue = (pattern, value, ctx, env, stack) => {
536
+ if (pattern.type === 'Identifier') {
537
+ env.set(pattern.name, value);
538
+ return true;
539
+ }
540
+ if (pattern.type === 'AssignmentPattern') {
541
+ return assignPatternValue(pattern.left, value === undefined
542
+ ? evaluateStatic(pattern.right, ctx, env, stack)
543
+ : value, ctx, env, stack);
544
+ }
545
+ if (pattern.type === 'ObjectPattern') {
546
+ if (typeof value !== 'object' || value === null) {
547
+ return false;
548
+ }
549
+ return pattern.properties.every((property) => {
550
+ if (property.type === 'RestElement') {
551
+ return false;
552
+ }
553
+ let key;
554
+ if (property.computed) {
555
+ key = evaluateStatic(property.key, ctx, env, stack);
556
+ }
557
+ else if (property.key.type === 'Identifier') {
558
+ key = property.key.name;
559
+ }
560
+ else if (property.key.type === 'Literal') {
561
+ key = property.key.value;
562
+ }
563
+ if (key === undefined || key === null) {
564
+ return false;
565
+ }
566
+ return assignPatternValue(property.value, getObjectMember(value, key), ctx, env, stack);
567
+ });
568
+ }
569
+ if (pattern.type === 'ArrayPattern') {
570
+ if (!Array.isArray(value)) {
571
+ return false;
572
+ }
573
+ return pattern.elements.every((element, index) => element
574
+ ? assignPatternValue(element, value[index], ctx, env, stack)
575
+ : true);
576
+ }
577
+ return false;
578
+ };
579
+ const applyRootMutation = (bindingName, baseValue, mutation, ctx, env, stack) => {
580
+ const resolvePath = (node) => {
581
+ if (node.type === 'Identifier') {
582
+ return node.name === bindingName ? { path: [] } : null;
583
+ }
584
+ if (node.type !== 'MemberExpression') {
585
+ return null;
586
+ }
587
+ const parent = resolvePath(node.object);
588
+ if (!parent) {
589
+ return null;
590
+ }
591
+ let key;
592
+ if (node.computed) {
593
+ key = evaluateStatic(node.property, ctx, env, stack);
594
+ }
595
+ else if (node.property.type === 'Identifier') {
596
+ key = node.property.name;
597
+ }
598
+ if (key === undefined ||
599
+ key === null ||
600
+ (typeof key !== 'string' && typeof key !== 'number')) {
601
+ return null;
602
+ }
603
+ return {
604
+ path: [...parent.path, key],
605
+ };
606
+ };
607
+ const pathInfo = resolvePath(mutation.type === 'AssignmentExpression' ? mutation.left : mutation.argument);
608
+ if (!pathInfo) {
609
+ return undefined;
610
+ }
611
+ const cloned = cloneStaticValue(baseValue);
612
+ if (pathInfo.path.length === 0) {
613
+ if (mutation.type !== 'AssignmentExpression') {
614
+ return undefined;
615
+ }
616
+ return evaluateStatic(mutation.right, ctx, env, stack);
617
+ }
618
+ let target = cloned;
619
+ for (let idx = 0; idx < pathInfo.path.length - 1; idx += 1) {
620
+ const key = pathInfo.path[idx];
621
+ const next = target?.[key];
622
+ if (typeof next !== 'object' || next === null) {
623
+ return undefined;
624
+ }
625
+ target = next;
626
+ }
627
+ const lastKey = pathInfo.path[pathInfo.path.length - 1];
628
+ if (mutation.type === 'AssignmentExpression') {
629
+ const nextValue = evaluateStatic(mutation.right, ctx, env, stack);
630
+ if (nextValue === undefined) {
631
+ return undefined;
632
+ }
633
+ target[lastKey] = nextValue;
634
+ return cloned;
635
+ }
636
+ const currentValue = target[lastKey];
637
+ if (typeof currentValue !== 'number') {
638
+ return undefined;
639
+ }
640
+ target[lastKey] =
641
+ mutation.operator === '++' ? currentValue + 1 : currentValue - 1;
642
+ return cloned;
643
+ };
644
+ const evaluateFunctionCall = (fn, args, ctx, env, stack) => {
645
+ if (fn.async || !fn.body) {
646
+ return undefined;
647
+ }
648
+ const localEnv = new Map(env);
649
+ for (let idx = 0; idx < fn.params.length; idx += 1) {
650
+ if (!assignPatternValue(fn.params[idx], args[idx], ctx, localEnv, stack)) {
651
+ return undefined;
652
+ }
653
+ }
654
+ if (fn.body.type !== 'BlockStatement') {
655
+ return evaluateStatic(fn.body, ctx, localEnv, stack);
656
+ }
657
+ for (const statement of fn.body.body) {
658
+ if (statement.type === 'VariableDeclaration') {
659
+ for (const declarator of statement.declarations) {
660
+ const value = declarator.init
661
+ ? evaluateStatic(declarator.init, ctx, localEnv, stack)
662
+ : undefined;
663
+ if (!assignPatternValue(declarator.id, value, ctx, localEnv, stack)) {
664
+ return undefined;
665
+ }
666
+ }
667
+ continue;
668
+ }
669
+ if (statement.type === 'ReturnStatement') {
670
+ if (!statement.argument) {
671
+ return undefined;
672
+ }
673
+ return evaluateStatic(statement.argument, ctx, localEnv, stack);
674
+ }
675
+ return undefined;
676
+ }
677
+ return undefined;
678
+ };
679
+ const getConstantReplacement = (binding, ctx) => {
680
+ const init = binding?.declarator?.init;
681
+ if (!init) {
682
+ return null;
683
+ }
684
+ if (init.type === 'Literal') {
685
+ return literalCode(init.value);
686
+ }
687
+ if (init.type === 'ObjectExpression' &&
688
+ binding?.isRoot &&
689
+ binding.declarator?.id.type === 'Identifier') {
690
+ const evaluated = evaluateStatic(binding.declarator.id, ctx);
691
+ return literalCode(evaluated);
692
+ }
693
+ return null;
694
+ };
695
+ const replaceIdentifierReferences = (expression, replacements, code) => {
696
+ const localReplacements = [];
697
+ const ancestors = [];
698
+ const walk = (current, parent) => {
699
+ if (current.type === 'Identifier' &&
700
+ replacements.has(current.name) &&
701
+ !isInTypeContext(ancestors) &&
702
+ !isBindingPosition(current, parent) &&
703
+ !isPropertyOnlyIdentifier(current, parent) &&
704
+ !isObjectPropertyKey(current, parent)) {
705
+ const replacement = replacements.get(current.name);
706
+ // Shorthand property `{ width }` → `{ width: 500 }` when the
707
+ // identifier is the value side of a shorthand ObjectProperty.
708
+ const isShorthandValue = !!parent &&
709
+ parent.type === 'Property' &&
710
+ parent.shorthand &&
711
+ parent.value === current;
712
+ localReplacements.push({
713
+ start: isShorthandValue ? parent.start : current.start,
714
+ end: current.end,
715
+ value: isShorthandValue
716
+ ? `${current.name}: ${replacement}`
717
+ : replacement,
718
+ });
719
+ }
720
+ ancestors.push(current);
721
+ getChildren(current).forEach((child) => walk(child, current));
722
+ ancestors.pop();
723
+ };
724
+ walk(expression, null);
725
+ let result = code.slice(expression.start, expression.end);
726
+ localReplacements
727
+ .sort((a, b) => b.start - a.start)
728
+ .forEach((replacement) => {
729
+ const start = replacement.start - expression.start;
730
+ const end = replacement.end - expression.start;
731
+ result = result.slice(0, start) + replacement.value + result.slice(end);
732
+ });
733
+ return result;
734
+ };
735
+ const applyExpressionReplacements = (expression, replacements, code) => {
736
+ let result = code.slice(expression.start, expression.end);
737
+ replacements
738
+ .sort((a, b) => b.start - a.start)
739
+ .forEach((replacement) => {
740
+ const start = replacement.start - expression.start;
741
+ const end = replacement.end - expression.start;
742
+ result = result.slice(0, start) + replacement.value + result.slice(end);
743
+ });
744
+ return result;
745
+ };
746
+ const staticImportAliasPart = (value) => value.replace(/[^A-Za-z0-9_$]/g, '_') || 'value';
747
+ const allocateStaticImportAlias = (binding, imported, ctx) => {
748
+ const key = `${binding.importedFrom ?? ''}\0${binding.name}\0${imported}`;
749
+ const existing = ctx.staticImportAliases.get(key);
750
+ if (existing) {
751
+ return existing;
752
+ }
753
+ const namespacePart = staticImportAliasPart(binding.name);
754
+ const importedPart = staticImportAliasPart(imported);
755
+ let alias = `_wyw_static_${namespacePart}_${importedPart}`;
756
+ let idx = 1;
757
+ while (ctx.usedNames.has(alias)) {
758
+ idx += 1;
759
+ alias = `_wyw_static_${namespacePart}_${importedPart}_${idx}`;
760
+ }
761
+ ctx.usedNames.add(alias);
762
+ ctx.staticImportAliases.set(key, alias);
763
+ return alias;
764
+ };
765
+ const staticMemberPropertyName = (expression) => {
766
+ if (!expression.computed && expression.property.type === 'Identifier') {
767
+ return expression.property.name;
768
+ }
769
+ if (expression.computed &&
770
+ expression.property.type === 'Literal' &&
771
+ typeof expression.property.value === 'string') {
772
+ return expression.property.value;
773
+ }
774
+ return null;
775
+ };
776
+ const collectStaticNamespaceMemberReferences = (expression, ctx) => {
777
+ const coveredReferenceStarts = new Set();
778
+ const imports = new Map();
779
+ const replacements = [];
780
+ const walk = (node) => {
781
+ if (node.type === 'MemberExpression' && node.object.type === 'Identifier') {
782
+ const binding = resolveBindingAt(ctx, node.object.name, node.object.start);
783
+ const imported = staticMemberPropertyName(node);
784
+ if (binding?.importedFrom &&
785
+ binding.imported === '*' &&
786
+ imported !== null) {
787
+ const alias = allocateStaticImportAlias(binding, imported, ctx);
788
+ imports.set(`${binding.importedFrom}\0${imported}\0${alias}`, {
789
+ imported,
790
+ importLocal: binding.name,
791
+ local: alias,
792
+ source: binding.importedFrom,
793
+ });
794
+ replacements.push({
795
+ end: node.end,
796
+ start: node.start,
797
+ value: alias,
798
+ });
799
+ coveredReferenceStarts.add(node.object.start);
800
+ }
801
+ }
802
+ getChildren(node).forEach(walk);
803
+ };
804
+ walk(expression);
805
+ return {
806
+ coveredReferenceStarts,
807
+ imports: [...imports.values()],
808
+ replacements,
809
+ };
810
+ };
811
+ const isProcessEnvMember = (node) => {
812
+ if (node.type !== 'MemberExpression' || node.computed) {
813
+ return false;
814
+ }
815
+ if (node.property.type !== 'Identifier' ||
816
+ node.property.name !== 'env') {
817
+ return false;
818
+ }
819
+ return node.object.type === 'Identifier' && node.object.name === 'process';
820
+ };
821
+ const evaluateBinary = (expression, ctx, env = new Map(), stack = []) => {
822
+ if (expression.type !== 'BinaryExpression') {
823
+ return undefined;
824
+ }
825
+ const left = evaluateStatic(expression.left, ctx, env, stack);
826
+ const right = evaluateStatic(expression.right, ctx, env, stack);
827
+ if (left === undefined || right === undefined) {
828
+ return undefined;
829
+ }
830
+ if (expression.operator === '+') {
831
+ if (typeof left === 'number' && typeof right === 'number') {
832
+ return left + right;
833
+ }
834
+ if ((typeof left === 'string' || typeof left === 'number') &&
835
+ (typeof right === 'string' || typeof right === 'number')) {
836
+ return `${left}${right}`;
837
+ }
838
+ }
839
+ if (typeof left === 'number' && typeof right === 'number') {
840
+ switch (expression.operator) {
841
+ case '-':
842
+ return left - right;
843
+ case '*':
844
+ return left * right;
845
+ case '/':
846
+ return left / right;
847
+ case '%':
848
+ return left % right;
849
+ case '**':
850
+ return left ** right;
851
+ default:
852
+ break;
853
+ }
854
+ }
855
+ return undefined;
856
+ };
857
+ const evaluateStatic = (expression, ctx, env = new Map(), stack = []) => {
858
+ if (expression.type === 'TSAsExpression' ||
859
+ expression.type === 'TSSatisfiesExpression' ||
860
+ expression.type === 'TSNonNullExpression' ||
861
+ expression.type === 'TSInstantiationExpression' ||
862
+ expression.type === 'TSTypeAssertion' ||
863
+ expression.type === 'ParenthesizedExpression') {
864
+ return evaluateStatic(expression.expression, ctx, env, stack);
865
+ }
866
+ if (expression.type === 'Literal') {
867
+ return expression.value;
868
+ }
869
+ if (expression.type === 'UnaryExpression') {
870
+ if (expression.operator === 'typeof') {
871
+ const argIsProcessEnvAccess = expression.argument.type === 'MemberExpression' &&
872
+ isProcessEnvMember(expression.argument.object);
873
+ const arg = evaluateStatic(expression.argument, ctx, env, stack);
874
+ if (arg === undefined) {
875
+ return argIsProcessEnvAccess ? 'undefined' : undefined;
876
+ }
877
+ return typeof arg;
878
+ }
879
+ const arg = evaluateStatic(expression.argument, ctx, env, stack);
880
+ if (arg === undefined) {
881
+ return undefined;
882
+ }
883
+ switch (expression.operator) {
884
+ case '-':
885
+ return typeof arg === 'number' ? -arg : undefined;
886
+ case '+':
887
+ return typeof arg === 'number' ? +arg : undefined;
888
+ case '!':
889
+ return !arg;
890
+ case '~':
891
+ return typeof arg === 'number' ? ~arg : undefined;
892
+ case 'void':
893
+ return undefined;
894
+ default:
895
+ return undefined;
896
+ }
897
+ }
898
+ if (expression.type === 'LogicalExpression') {
899
+ const left = evaluateStatic(expression.left, ctx, env, stack);
900
+ // process.env.X access is the only source we trust as "deterministically
901
+ // undefined" — it's a build-time lookup we control. For everything else,
902
+ // undefined means "couldn't evaluate" and we must bail to avoid inlining
903
+ // a wrong fallback when the runtime value isn't actually nullish.
904
+ const leftIsProcessEnvAccess = expression.left.type === 'MemberExpression' &&
905
+ isProcessEnvMember(expression.left.object);
906
+ if (left === undefined && !leftIsProcessEnvAccess) {
907
+ return undefined;
908
+ }
909
+ if (expression.operator === '||') {
910
+ return left || evaluateStatic(expression.right, ctx, env, stack);
911
+ }
912
+ if (expression.operator === '??') {
913
+ return left ?? evaluateStatic(expression.right, ctx, env, stack);
914
+ }
915
+ if (expression.operator === '&&') {
916
+ return left && evaluateStatic(expression.right, ctx, env, stack);
917
+ }
918
+ return undefined;
919
+ }
920
+ if (expression.type === 'ConditionalExpression') {
921
+ const test = evaluateStatic(expression.test, ctx, env, stack);
922
+ if (test === undefined) {
923
+ return undefined;
924
+ }
925
+ return evaluateStatic(test ? expression.consequent : expression.alternate, ctx, env, stack);
926
+ }
927
+ if (expression.type === 'TemplateLiteral') {
928
+ let result = '';
929
+ for (let idx = 0; idx < expression.quasis.length; idx += 1) {
930
+ result += expression.quasis[idx]?.value.cooked ?? '';
931
+ const nextExpression = expression.expressions[idx];
932
+ if (!nextExpression) {
933
+ continue;
934
+ }
935
+ const value = evaluateStatic(nextExpression, ctx, env, stack);
936
+ if (value === undefined ||
937
+ (typeof value !== 'string' && typeof value !== 'number')) {
938
+ return undefined;
939
+ }
940
+ result += String(value);
941
+ }
942
+ return result;
943
+ }
944
+ if (expression.type === 'Identifier') {
945
+ if (env.has(expression.name)) {
946
+ return unwrapOxcStaticCallableValue(env.get(expression.name));
947
+ }
948
+ const binding = resolveBindingAt(ctx, expression.name, expression.start);
949
+ if (!binding || binding.importedFrom) {
950
+ return undefined;
951
+ }
952
+ if (binding.kind === 'param') {
953
+ return undefined;
954
+ }
955
+ if (stack.includes(binding.name)) {
956
+ return undefined;
957
+ }
958
+ let value;
959
+ const { declarator } = binding;
960
+ const init = declarator?.init;
961
+ if (init) {
962
+ if (declarator.id.type !== 'Identifier') {
963
+ return undefined;
964
+ }
965
+ value = evaluateStatic(init, ctx, env, [...stack, binding.name]);
966
+ }
967
+ else if (binding.functionNode) {
968
+ value = binding.functionNode;
969
+ }
970
+ if (value !== undefined &&
971
+ binding.isRoot &&
972
+ typeof value === 'object' &&
973
+ value !== null &&
974
+ !Array.isArray(value)) {
975
+ const mutations = ctx.rootMutationsByBinding.get(binding.name) ?? [];
976
+ let nextValue = cloneStaticValue(value);
977
+ for (const mutation of mutations) {
978
+ if (mutation.start >= ctx.currentExpressionStart) {
979
+ break;
980
+ }
981
+ const applied = applyRootMutation(binding.name, nextValue, mutation, ctx, env, [...stack, binding.name]);
982
+ if (applied === undefined) {
983
+ return undefined;
984
+ }
985
+ nextValue = applied;
986
+ }
987
+ return nextValue;
988
+ }
989
+ return value;
990
+ }
991
+ if (expression.type === 'ObjectExpression') {
992
+ const result = {};
993
+ for (const property of expression.properties) {
994
+ if (property.type === 'SpreadElement') {
995
+ const spreadValue = evaluateStatic(property.argument, ctx, env, stack);
996
+ if (typeof spreadValue !== 'object' || spreadValue === null) {
997
+ return undefined;
998
+ }
999
+ Object.assign(result, spreadValue);
1000
+ continue;
1001
+ }
1002
+ let key;
1003
+ if (property.computed) {
1004
+ key = evaluateStatic(property.key, ctx, env, stack);
1005
+ }
1006
+ else if (property.key.type === 'Identifier') {
1007
+ key = property.key.name;
1008
+ }
1009
+ else if (property.key.type === 'Literal') {
1010
+ key = property.key.value;
1011
+ }
1012
+ if (key === undefined ||
1013
+ key === null ||
1014
+ (typeof key !== 'string' && typeof key !== 'number')) {
1015
+ return undefined;
1016
+ }
1017
+ const value = evaluateStatic(property.value, ctx, env, stack);
1018
+ if (value === undefined) {
1019
+ return undefined;
1020
+ }
1021
+ result[key] = value;
1022
+ }
1023
+ return result;
1024
+ }
1025
+ if (expression.type === 'ArrayExpression') {
1026
+ const result = [];
1027
+ for (const element of expression.elements) {
1028
+ if (!element) {
1029
+ return undefined;
1030
+ }
1031
+ if (element.type === 'SpreadElement') {
1032
+ const spreadValue = evaluateStatic(element.argument, ctx, env, stack);
1033
+ if (!Array.isArray(spreadValue)) {
1034
+ return undefined;
1035
+ }
1036
+ result.push(...spreadValue);
1037
+ continue;
1038
+ }
1039
+ const value = evaluateStatic(element, ctx, env, stack);
1040
+ if (value === undefined) {
1041
+ return undefined;
1042
+ }
1043
+ result.push(value);
1044
+ }
1045
+ return result;
1046
+ }
1047
+ if (expression.type === 'MemberExpression') {
1048
+ let key;
1049
+ if (expression.computed) {
1050
+ key = evaluateStatic(expression.property, ctx, env, stack);
1051
+ }
1052
+ else if (expression.property.type === 'Identifier') {
1053
+ key = expression.property.name;
1054
+ }
1055
+ if (key === undefined ||
1056
+ key === null ||
1057
+ (typeof key !== 'string' && typeof key !== 'number')) {
1058
+ return undefined;
1059
+ }
1060
+ if (isProcessEnvMember(expression.object) &&
1061
+ typeof key === 'string' &&
1062
+ !env.has('process')) {
1063
+ // Treat process.env.X as deterministically undefined at build time.
1064
+ // Reading from real process.env would couple the bundle to whatever
1065
+ // happens to be set on the build machine; falling back to the
1066
+ // ?? / || branch (or a runtime read) is more predictable.
1067
+ return undefined;
1068
+ }
1069
+ const objectValue = evaluateStatic(expression.object, ctx, env, stack);
1070
+ if (objectValue === undefined) {
1071
+ return undefined;
1072
+ }
1073
+ return getObjectMember(objectValue, key);
1074
+ }
1075
+ if (expression.type === 'NewExpression') {
1076
+ if (expression.callee.type !== 'Identifier' ||
1077
+ expression.arguments.length !== 1) {
1078
+ return undefined;
1079
+ }
1080
+ const [argument] = expression.arguments;
1081
+ if (!argument || argument.type === 'SpreadElement') {
1082
+ return undefined;
1083
+ }
1084
+ const value = evaluateStatic(argument, ctx, env, stack);
1085
+ if (value === undefined) {
1086
+ return undefined;
1087
+ }
1088
+ if (expression.callee.name === 'String') {
1089
+ return String(value);
1090
+ }
1091
+ if (expression.callee.name === 'Number') {
1092
+ return Number(value);
1093
+ }
1094
+ if (expression.callee.name === 'Boolean') {
1095
+ return Boolean(value);
1096
+ }
1097
+ return undefined;
1098
+ }
1099
+ if (expression.type === 'CallExpression') {
1100
+ if (expression.callee.type === 'Identifier') {
1101
+ const args = expression.arguments.map((arg) => arg.type === 'SpreadElement'
1102
+ ? undefined
1103
+ : evaluateStatic(arg, ctx, env, stack));
1104
+ if (args.some((value) => value === undefined)) {
1105
+ return undefined;
1106
+ }
1107
+ const staticCallable = env.get(expression.callee.name);
1108
+ if (isOxcStaticCallableValue(staticCallable) &&
1109
+ expression.arguments.length === 0) {
1110
+ return unwrapOxcStaticCallableValue(staticCallable);
1111
+ }
1112
+ if (expression.callee.name === 'String' && args.length === 1) {
1113
+ return String(args[0]);
1114
+ }
1115
+ if (expression.callee.name === 'Number' && args.length === 1) {
1116
+ return Number(args[0]);
1117
+ }
1118
+ if (expression.callee.name === 'Boolean' && args.length === 1) {
1119
+ return Boolean(args[0]);
1120
+ }
1121
+ const binding = resolveBindingAt(ctx, expression.callee.name, expression.callee.start);
1122
+ const fn = binding?.functionNode ?? binding?.declarator?.init;
1123
+ if (fn &&
1124
+ (fn.type === 'ArrowFunctionExpression' ||
1125
+ fn.type === 'FunctionDeclaration' ||
1126
+ fn.type === 'FunctionExpression')) {
1127
+ return evaluateFunctionCall(fn, args, ctx, env, [
1128
+ ...stack,
1129
+ expression.callee.name,
1130
+ ]);
1131
+ }
1132
+ }
1133
+ if (expression.callee.type === 'MemberExpression') {
1134
+ const objectValue = evaluateStatic(expression.callee.object, ctx, env, stack);
1135
+ let key;
1136
+ if (expression.callee.computed) {
1137
+ key = evaluateStatic(expression.callee.property, ctx, env, stack);
1138
+ }
1139
+ else if (expression.callee.property.type === 'Identifier') {
1140
+ key = expression.callee.property.name;
1141
+ }
1142
+ if (typeof objectValue === 'string') {
1143
+ if (key === 'toLowerCase' && expression.arguments.length === 0) {
1144
+ return objectValue.toLowerCase();
1145
+ }
1146
+ if (key === 'toUpperCase' && expression.arguments.length === 0) {
1147
+ return objectValue.toUpperCase();
1148
+ }
1149
+ }
1150
+ }
1151
+ }
1152
+ return evaluateBinary(expression, ctx, env, stack);
1153
+ };
1154
+ const allocateExpressionName = (ctx) => {
1155
+ let base = '_exp';
1156
+ let idx = 1;
1157
+ while (ctx.usedNames.has(base)) {
1158
+ idx += 1;
1159
+ base = `_exp${idx}`;
1160
+ }
1161
+ ctx.usedNames.add(base);
1162
+ return base;
1163
+ };
1164
+ const hoistedBindingKey = (binding) => `${binding.scope.start}:${binding.scope.end}:${binding.declaredAt}:${binding.name}`;
1165
+ const allocateHoistedBindingName = (originalName, ctx) => {
1166
+ const sanitized = originalName.replace(/[^A-Za-z0-9_$]/g, '_') || 'hoisted';
1167
+ const base = /^[A-Za-z_$]/.test(sanitized) ? `_${sanitized}` : '_hoisted';
1168
+ let candidate = base;
1169
+ let idx = 2;
1170
+ while (ctx.usedNames.has(candidate)) {
1171
+ candidate = `${base}${idx}`;
1172
+ idx += 1;
1173
+ }
1174
+ ctx.usedNames.add(candidate);
1175
+ return candidate;
1176
+ };
1177
+ const getHoistedBindingName = (binding, ctx) => {
1178
+ const key = hoistedBindingKey(binding);
1179
+ const existing = ctx.hoistedBindingNames.get(key);
1180
+ if (existing) {
1181
+ return existing;
1182
+ }
1183
+ const next = allocateHoistedBindingName(binding.name, ctx);
1184
+ ctx.hoistedBindingNames.set(key, next);
1185
+ return next;
1186
+ };
1187
+ const declarationInitCode = (init, ctx) => {
1188
+ const renamedDependencies = new Map();
1189
+ findReferences(init, ctx.referencesByNode).forEach(({ name, start }) => {
1190
+ const dependency = resolveBindingAt(ctx, name, start);
1191
+ if (!dependency ||
1192
+ dependency.importedFrom ||
1193
+ dependency.isRoot ||
1194
+ dependency.declarator?.id.type !== 'Identifier') {
1195
+ return;
1196
+ }
1197
+ renamedDependencies.set(name, getHoistedBindingName(dependency, ctx));
1198
+ });
1199
+ return renamedDependencies.size > 0
1200
+ ? replaceIdentifierReferences(init, renamedDependencies, ctx.code)
1201
+ : ctx.code.slice(init.start, init.end);
1202
+ };
1203
+ const addHoistedCode = (key, code, ctx) => {
1204
+ if (ctx.hoistedDeclarations.has(key)) {
1205
+ return;
1206
+ }
1207
+ ctx.hoistedDeclarations.set(key, code);
1208
+ const declarations = ctx.hoistedDeclarationsByInsertionPoint.get(ctx.currentInsertionPoint) ??
1209
+ [];
1210
+ declarations.push(code);
1211
+ ctx.hoistedDeclarationsByInsertionPoint.set(ctx.currentInsertionPoint, declarations);
1212
+ };
1213
+ const declarationCode = (binding, ctx) => {
1214
+ const { declarator } = binding;
1215
+ if (!declarator) {
1216
+ return '';
1217
+ }
1218
+ const { id } = declarator;
1219
+ if (id.type !== 'Identifier') {
1220
+ const idCode = ctx.code.slice(id.start, id.end);
1221
+ if (!declarator.init) {
1222
+ return `let ${idCode};`;
1223
+ }
1224
+ return `let ${idCode} = ${declarationInitCode(declarator.init, ctx)};`;
1225
+ }
1226
+ const hoistedName = getHoistedBindingName(binding, ctx);
1227
+ if (!declarator.init) {
1228
+ return `let ${hoistedName};`;
1229
+ }
1230
+ return `let ${hoistedName} = ${declarationInitCode(declarator.init, ctx)};`;
1231
+ };
1232
+ const assertHoistable = (binding, ctx, stack = []) => {
1233
+ if (!binding.declarator?.init || binding.importedFrom || binding.isRoot) {
1234
+ return;
1235
+ }
1236
+ if (stack.includes(binding.name)) {
1237
+ return;
1238
+ }
1239
+ const refs = findReferences(binding.declarator.init, ctx.referencesByNode);
1240
+ refs.forEach(({ name, start }) => {
1241
+ const nextBinding = resolveBindingAt(ctx, name, start);
1242
+ if (!nextBinding) {
1243
+ return;
1244
+ }
1245
+ if (nextBinding.kind === 'param') {
1246
+ throw new Error(`This identifier cannot be used in the template, because it is a function parameter.`);
1247
+ }
1248
+ assertHoistable(nextBinding, ctx, [...stack, binding.name]);
1249
+ });
1250
+ };
1251
+ const addHoistedDeclaration = (binding, ctx, stack = []) => {
1252
+ if (!binding.declaration ||
1253
+ !binding.declarator ||
1254
+ binding.importedFrom ||
1255
+ binding.isRoot ||
1256
+ stack.includes(binding.name)) {
1257
+ return;
1258
+ }
1259
+ const hoistSource = binding.declarator.init ?? binding.declarator;
1260
+ findReferences(hoistSource, ctx.referencesByNode).forEach(({ name, start }) => {
1261
+ const dependency = resolveBindingAt(ctx, name, start);
1262
+ if (dependency) {
1263
+ addHoistedDeclaration(dependency, ctx, [...stack, binding.name]);
1264
+ }
1265
+ });
1266
+ if (!ctx.hoistedDeclarations.has(binding.name)) {
1267
+ addHoistedCode(binding.name, declarationCode(binding, ctx), ctx);
1268
+ }
1269
+ };
1270
+ const literalExpressionValue = (expression, ctx) => {
1271
+ if (expression.type !== 'Literal') {
1272
+ return null;
1273
+ }
1274
+ if (expression.value !== null &&
1275
+ typeof expression.value !== 'string' &&
1276
+ typeof expression.value !== 'number' &&
1277
+ typeof expression.value !== 'boolean') {
1278
+ return null;
1279
+ }
1280
+ let type;
1281
+ if (expression.value === null) {
1282
+ type = 'NullLiteral';
1283
+ }
1284
+ else if (typeof expression.value === 'string') {
1285
+ type = 'StringLiteral';
1286
+ }
1287
+ else if (typeof expression.value === 'number') {
1288
+ type = 'NumericLiteral';
1289
+ }
1290
+ else {
1291
+ type = 'BooleanLiteral';
1292
+ }
1293
+ const loc = getSourceLocation(expression.start, expression.end, ctx);
1294
+ const ex = expression.value === null
1295
+ ? { loc, type }
1296
+ : {
1297
+ loc,
1298
+ type,
1299
+ value: expression.value,
1300
+ };
1301
+ return {
1302
+ ex,
1303
+ kind: ValueType.CONST,
1304
+ source: ctx.code.slice(expression.start, expression.end),
1305
+ value: expression.value,
1306
+ };
1307
+ };
1308
+ const extractExpression = (expression, ctx, evaluate) => {
1309
+ const source = ctx.code.slice(expression.start, expression.end);
1310
+ const isFunction = expression.type === 'FunctionExpression' ||
1311
+ expression.type === 'ArrowFunctionExpression';
1312
+ if (evaluate) {
1313
+ const evaluated = evaluateStatic(expression, ctx);
1314
+ const literal = literalCode(evaluated);
1315
+ if (literal) {
1316
+ findReferences(expression, ctx.referencesByNode).forEach(({ name }) => ctx.dependencyNames.add(name));
1317
+ return {
1318
+ expressionCode: literal,
1319
+ importedFrom: [],
1320
+ kind: isFunction ? ValueType.FUNCTION : ValueType.LAZY,
1321
+ staticImports: [],
1322
+ staticValue: isStaticSerializableValue(evaluated)
1323
+ ? cloneStaticValue(evaluated)
1324
+ : undefined,
1325
+ };
1326
+ }
1327
+ }
1328
+ const identifierReplacements = new Map();
1329
+ const importedFrom = [];
1330
+ const namespaceStatic = collectStaticNamespaceMemberReferences(expression, ctx);
1331
+ const staticImports = [
1332
+ ...namespaceStatic.imports,
1333
+ ];
1334
+ let hasNonStaticLocalReference = false;
1335
+ findReferences(expression, ctx.referencesByNode).forEach(({ name, start }) => {
1336
+ const binding = resolveBindingAt(ctx, name, start);
1337
+ if (!binding) {
1338
+ return;
1339
+ }
1340
+ if (isFunction && isBindingDeclaredWithin(binding, expression)) {
1341
+ return;
1342
+ }
1343
+ ctx.dependencyNames.add(name);
1344
+ if (binding.importedFrom) {
1345
+ importedFrom.push(binding.importedFrom);
1346
+ if (binding.imported && binding.imported !== '*') {
1347
+ staticImports.push({
1348
+ imported: binding.imported,
1349
+ local: binding.name,
1350
+ source: binding.importedFrom,
1351
+ });
1352
+ }
1353
+ else if (binding.imported === '*' &&
1354
+ namespaceStatic.coveredReferenceStarts.has(start)) {
1355
+ // The static candidate source gets a synthetic named import alias,
1356
+ // while the eval fallback keeps the original namespace expression.
1357
+ }
1358
+ else {
1359
+ hasNonStaticLocalReference = true;
1360
+ }
1361
+ return;
1362
+ }
1363
+ const replacement = getConstantReplacement(binding, ctx);
1364
+ if (evaluate && replacement) {
1365
+ identifierReplacements.set(name, replacement);
1366
+ return;
1367
+ }
1368
+ hasNonStaticLocalReference = true;
1369
+ assertHoistable(binding, ctx);
1370
+ addHoistedDeclaration(binding, ctx);
1371
+ if (!binding.isRoot && binding.declarator?.id.type === 'Identifier') {
1372
+ identifierReplacements.set(name, getHoistedBindingName(binding, ctx));
1373
+ }
1374
+ });
1375
+ return {
1376
+ expressionCode: identifierReplacements.size > 0
1377
+ ? replaceIdentifierReferences(expression, identifierReplacements, ctx.code)
1378
+ : source,
1379
+ importedFrom,
1380
+ kind: isFunction ? ValueType.FUNCTION : ValueType.LAZY,
1381
+ staticExpressionCode: namespaceStatic.replacements.length > 0
1382
+ ? applyExpressionReplacements(expression, namespaceStatic.replacements, ctx.code)
1383
+ : undefined,
1384
+ staticImports: hasNonStaticLocalReference ? [] : staticImports,
1385
+ };
1386
+ };
1387
+ const getInsertionPoints = (program, expressions) => {
1388
+ if (expressions.length === 0) {
1389
+ return [];
1390
+ }
1391
+ if (program.body.length === 0) {
1392
+ return expressions.map(() => 0);
1393
+ }
1394
+ const insertionPoints = [];
1395
+ let ownerIndex = 0;
1396
+ expressions.forEach((expression) => {
1397
+ while (ownerIndex < program.body.length - 1 &&
1398
+ program.body[ownerIndex].end < expression.start) {
1399
+ ownerIndex += 1;
1400
+ }
1401
+ let owner = program.body[ownerIndex];
1402
+ if (!owner ||
1403
+ owner.start > expression.start ||
1404
+ owner.end < expression.end) {
1405
+ owner = program.body.find((statement) => statement.start <= expression.start && statement.end >= expression.end);
1406
+ }
1407
+ insertionPoints.push(owner?.start ?? 0);
1408
+ });
1409
+ return insertionPoints;
1410
+ };
1411
+ const applyReplacements = (code, replacements) => {
1412
+ let result = code;
1413
+ replacements
1414
+ .sort((a, b) => b.start - a.start)
1415
+ .forEach((replacement) => {
1416
+ result =
1417
+ result.slice(0, replacement.start) +
1418
+ replacement.value +
1419
+ result.slice(replacement.end);
1420
+ });
1421
+ return result;
1422
+ };
1423
+ const extractExpressions = (code, filename, evaluate, program, analysis, expressions) => {
1424
+ if (expressions.length === 0) {
1425
+ return {
1426
+ code,
1427
+ dependencyNames: [],
1428
+ expressionValues: [],
1429
+ staticValueCandidates: [],
1430
+ staticValues: [],
1431
+ };
1432
+ }
1433
+ const insertionPoints = getInsertionPoints(program, expressions);
1434
+ const ctx = {
1435
+ bindingResolutionCache: new Map(),
1436
+ bindingsByName: analysis.bindingsByName,
1437
+ code,
1438
+ currentInsertionPoint: insertionPoints[0] ?? 0,
1439
+ currentExpressionStart: expressions[0].start,
1440
+ dependencyNames: new Set(),
1441
+ expressionValues: [],
1442
+ filename,
1443
+ hoistedBindingNames: new Map(),
1444
+ hoistedDeclarations: new Map(),
1445
+ hoistedDeclarationsByInsertionPoint: new Map(),
1446
+ loc: createLocationLookup(code),
1447
+ referencesByNode: new WeakMap(),
1448
+ replacements: [],
1449
+ rootMutationsByBinding: analysis.rootMutationsByBinding,
1450
+ staticImportAliases: new Map(),
1451
+ staticValueCandidates: [],
1452
+ staticValues: [],
1453
+ usedNames: new Set(analysis.usedNames),
1454
+ };
1455
+ expressions.forEach((expression, index) => {
1456
+ ctx.currentInsertionPoint = insertionPoints[index] ?? 0;
1457
+ ctx.currentExpressionStart = expression.start;
1458
+ const literal = literalExpressionValue(expression, ctx);
1459
+ if (literal) {
1460
+ ctx.expressionValues.push(literal);
1461
+ return;
1462
+ }
1463
+ const { expressionCode, importedFrom, kind, staticExpressionCode, staticImports, staticValue, } = extractExpression(expression, ctx, evaluate);
1464
+ const expName = allocateExpressionName(ctx);
1465
+ addHoistedCode(expName, `const ${expName} = () => (${expressionCode});`, ctx);
1466
+ if (staticValue !== undefined && kind !== ValueType.FUNCTION) {
1467
+ ctx.staticValues.push({
1468
+ name: expName,
1469
+ value: staticValue,
1470
+ });
1471
+ }
1472
+ else if (staticImports.length > 0 && kind !== ValueType.FUNCTION) {
1473
+ const uniqueImports = new Map();
1474
+ staticImports.forEach((item) => {
1475
+ uniqueImports.set(`${item.local}\0${item.importLocal ?? ''}\0${item.source}\0${item.imported}`, item);
1476
+ });
1477
+ ctx.staticValueCandidates.push({
1478
+ imports: [...uniqueImports.values()],
1479
+ name: expName,
1480
+ source: staticExpressionCode ?? expressionCode,
1481
+ });
1482
+ }
1483
+ ctx.replacements.push({
1484
+ start: expression.start,
1485
+ end: expression.end,
1486
+ value: `${expName}()`,
1487
+ });
1488
+ ctx.expressionValues.push({
1489
+ ex: {
1490
+ loc: getSourceLocation(expression.start, expression.end, ctx),
1491
+ name: expName,
1492
+ type: 'Identifier',
1493
+ },
1494
+ importedFrom,
1495
+ kind,
1496
+ source: ctx.code.slice(expression.start, expression.end),
1497
+ });
1498
+ });
1499
+ ctx.hoistedDeclarationsByInsertionPoint.forEach((declarations, point) => {
1500
+ ctx.replacements.push({
1501
+ start: point,
1502
+ end: point,
1503
+ value: `${declarations.join('\n')}\n`,
1504
+ });
1505
+ });
1506
+ return {
1507
+ code: applyReplacements(code, ctx.replacements),
1508
+ dependencyNames: [...ctx.dependencyNames],
1509
+ expressionValues: ctx.expressionValues,
1510
+ staticValueCandidates: ctx.staticValueCandidates,
1511
+ staticValues: ctx.staticValues,
1512
+ };
1513
+ };
1514
+ export const isOxcStaticSerializableValue = (value) => isStaticSerializableValue(value);
1515
+ export const evaluateOxcStaticExpressionAt = (code, filename, expressionSpan, staticBindings = new Map()) => {
1516
+ const program = parseOxc(code, filename);
1517
+ const analysis = analyzeProgram(program, {
1518
+ collectTargetExpressions: true,
1519
+ expressionSpanLookup: createSpanLookup([expressionSpan]),
1520
+ });
1521
+ const [expression] = analysis.targetExpressions;
1522
+ if (!expression) {
1523
+ return undefined;
1524
+ }
1525
+ const ctx = {
1526
+ bindingResolutionCache: new Map(),
1527
+ bindingsByName: analysis.bindingsByName,
1528
+ code,
1529
+ currentInsertionPoint: 0,
1530
+ currentExpressionStart: expression.start,
1531
+ dependencyNames: new Set(),
1532
+ expressionValues: [],
1533
+ filename,
1534
+ hoistedBindingNames: new Map(),
1535
+ hoistedDeclarations: new Map(),
1536
+ hoistedDeclarationsByInsertionPoint: new Map(),
1537
+ loc: createLocationLookup(code),
1538
+ referencesByNode: new WeakMap(),
1539
+ replacements: [],
1540
+ rootMutationsByBinding: analysis.rootMutationsByBinding,
1541
+ staticImportAliases: new Map(),
1542
+ staticValueCandidates: [],
1543
+ staticValues: [],
1544
+ usedNames: new Set(analysis.usedNames),
1545
+ };
1546
+ return evaluateStatic(expression, ctx, new Map(staticBindings));
1547
+ };
1548
+ export const evaluateOxcStaticExpression = (source, filename, staticBindings = new Map()) => {
1549
+ const code = `const __wyw_static_value = ${source};`;
1550
+ const program = parseOxc(code, filename);
1551
+ const declaration = program.body[0];
1552
+ if (declaration?.type !== 'VariableDeclaration') {
1553
+ return undefined;
1554
+ }
1555
+ const [declarator] = declaration.declarations;
1556
+ if (!declarator?.init) {
1557
+ return undefined;
1558
+ }
1559
+ return evaluateOxcStaticExpressionAt(code, filename, {
1560
+ end: declarator.init.end,
1561
+ start: declarator.init.start,
1562
+ }, staticBindings);
1563
+ };
1564
+ export const collectOxcExpressionDependencies = (code, filename, evaluate = false, targetExpressionSpans) => {
1565
+ const program = parseOxc(code, filename);
1566
+ const analysis = analyzeProgram(program, {
1567
+ collectTargetExpressions: true,
1568
+ expressionSpanLookup: createSpanLookup(targetExpressionSpans),
1569
+ });
1570
+ return extractExpressions(code, filename, evaluate, program, analysis, analysis.targetExpressions);
1571
+ };
1572
+ export const collectOxcTemplateDependencies = (code, filename, evaluate = false, targetTemplateSpans) => {
1573
+ const program = parseOxc(code, filename);
1574
+ const analysis = analyzeProgram(program, {
1575
+ collectTemplateLiterals: true,
1576
+ templateSpanLookup: createSpanLookup(targetTemplateSpans),
1577
+ });
1578
+ const expressions = analysis.templateLiterals.flatMap((template) => template.expressions);
1579
+ return extractExpressions(code, filename, evaluate, program, analysis, expressions);
1580
+ };