@wyw-in-js/transform 1.0.8 → 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 -28
  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 -79
  78. package/esm/transform/generators/workflow.js.map +1 -1
  79. package/esm/transform/helpers/loadWywOptions.js +154 -73
  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 +10 -0
  102. package/esm/utils/TransformDiagnostics.js.map +1 -0
  103. package/esm/utils/TransformMetadata.js +28 -10
  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 +7 -8
  178. package/types/index.js +24 -77
  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 +6 -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 -37
  222. package/types/transform/helpers/loadWywOptions.js +95 -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 +4 -24
  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.d.ts +9 -0
  241. package/types/utils/TransformDiagnostics.js +11 -0
  242. package/types/utils/TransformMetadata.d.ts +22 -4
  243. package/types/utils/TransformMetadata.js +23 -8
  244. package/types/utils/applyOxcProcessors.d.ts +16 -0
  245. package/types/utils/applyOxcProcessors.js +1391 -0
  246. package/types/utils/collectOxcExportsAndImports.d.ts +35 -0
  247. package/types/utils/collectOxcExportsAndImports.js +957 -0
  248. package/types/utils/collectOxcRuntime.d.ts +14 -0
  249. package/types/utils/collectOxcRuntime.js +250 -0
  250. package/types/utils/collectOxcTemplateDependencies.d.ts +38 -0
  251. package/types/utils/collectOxcTemplateDependencies.js +1580 -0
  252. package/types/utils/getFileIdx.js +1 -4
  253. package/types/utils/getPluginKey.d.ts +5 -2
  254. package/types/utils/getPluginKey.js +2 -6
  255. package/types/utils/getVisitorKeys.d.ts +4 -4
  256. package/types/utils/getVisitorKeys.js +9 -6
  257. package/types/utils/hasCachedWywPrevalExport.d.ts +14 -0
  258. package/types/utils/hasCachedWywPrevalExport.js +30 -0
  259. package/types/utils/hasWywPreval.js +1 -4
  260. package/types/utils/importOverrides.js +17 -27
  261. package/types/utils/isNode.d.ts +2 -2
  262. package/types/utils/isNode.js +2 -6
  263. package/types/utils/isNotNull.js +1 -4
  264. package/types/utils/isSerializable.js +3 -6
  265. package/types/utils/oxcAstService.d.ts +11 -0
  266. package/types/utils/oxcAstService.js +79 -0
  267. package/types/utils/oxcEmit.d.ts +19 -0
  268. package/types/utils/oxcEmit.js +506 -0
  269. package/types/utils/oxcPreevalStage.d.ts +20 -0
  270. package/types/utils/oxcPreevalStage.js +102 -0
  271. package/types/utils/oxcPreevalTransforms.d.ts +13 -0
  272. package/types/utils/oxcPreevalTransforms.js +1179 -0
  273. package/types/utils/oxcShaker.d.ts +13 -0
  274. package/types/utils/oxcShaker.js +751 -0
  275. package/types/utils/parseOxc.d.ts +11 -0
  276. package/types/utils/parseOxc.js +38 -0
  277. package/types/utils/parseRequest.js +2 -7
  278. package/types/utils/peek.js +1 -5
  279. package/types/utils/processorLookup.d.ts +8 -0
  280. package/types/utils/processorLookup.js +135 -0
  281. package/types/utils/resolveWithConditions.d.ts +12 -0
  282. package/types/utils/resolveWithConditions.js +103 -0
  283. package/types/vm/createVmContext.d.ts +2 -2
  284. package/types/vm/createVmContext.js +25 -62
  285. package/types/vm/process.js +20 -26
  286. package/esm/babel.js +0 -2
  287. package/esm/babel.js.map +0 -1
  288. package/esm/options/buildOptions.js +0 -168
  289. package/esm/options/buildOptions.js.map +0 -1
  290. package/esm/options/buildOptions.test.js +0 -138
  291. package/esm/options/buildOptions.test.js.map +0 -1
  292. package/esm/options/loadBabelOptions.js +0 -24
  293. package/esm/options/loadBabelOptions.js.map +0 -1
  294. package/esm/plugins/babel-transform.js +0 -53
  295. package/esm/plugins/babel-transform.js.map +0 -1
  296. package/esm/plugins/collector.js +0 -60
  297. package/esm/plugins/collector.js.map +0 -1
  298. package/esm/plugins/dynamic-import.js +0 -56
  299. package/esm/plugins/dynamic-import.js.map +0 -1
  300. package/esm/plugins/preeval.js +0 -73
  301. package/esm/plugins/preeval.js.map +0 -1
  302. package/esm/plugins/shaker.js +0 -680
  303. package/esm/plugins/shaker.js.map +0 -1
  304. package/esm/transform/barrelManifest.js +0 -291
  305. package/esm/transform/barrelManifest.js.map +0 -1
  306. package/esm/transform/generators/explodeReexports.js +0 -64
  307. package/esm/transform/generators/explodeReexports.js.map +0 -1
  308. package/esm/transform/generators/rewriteBarrelImports.js +0 -733
  309. package/esm/transform/generators/rewriteBarrelImports.js.map +0 -1
  310. package/esm/utils/addIdentifierToWywPreval.js +0 -68
  311. package/esm/utils/addIdentifierToWywPreval.js.map +0 -1
  312. package/esm/utils/collectExportsAndImports.js +0 -1157
  313. package/esm/utils/collectExportsAndImports.js.map +0 -1
  314. package/esm/utils/collectTemplateDependencies.js +0 -228
  315. package/esm/utils/collectTemplateDependencies.js.map +0 -1
  316. package/esm/utils/createId.js +0 -6
  317. package/esm/utils/createId.js.map +0 -1
  318. package/esm/utils/findIdentifiers.js +0 -62
  319. package/esm/utils/findIdentifiers.js.map +0 -1
  320. package/esm/utils/getConstantStringValue.js +0 -58
  321. package/esm/utils/getConstantStringValue.js.map +0 -1
  322. package/esm/utils/getMemberExpressionPropertyName.js +0 -11
  323. package/esm/utils/getMemberExpressionPropertyName.js.map +0 -1
  324. package/esm/utils/getScope.js +0 -6
  325. package/esm/utils/getScope.js.map +0 -1
  326. package/esm/utils/getSource.js +0 -15
  327. package/esm/utils/getSource.js.map +0 -1
  328. package/esm/utils/getTagProcessor.js +0 -404
  329. package/esm/utils/getTagProcessor.js.map +0 -1
  330. package/esm/utils/isExports.js +0 -22
  331. package/esm/utils/isExports.js.map +0 -1
  332. package/esm/utils/isGlobal.js +0 -6
  333. package/esm/utils/isGlobal.js.map +0 -1
  334. package/esm/utils/isNodePath.js +0 -4
  335. package/esm/utils/isNodePath.js.map +0 -1
  336. package/esm/utils/isRemoved.js +0 -46
  337. package/esm/utils/isRemoved.js.map +0 -1
  338. package/esm/utils/isRequire.js +0 -13
  339. package/esm/utils/isRequire.js.map +0 -1
  340. package/esm/utils/isTypedNode.js +0 -6
  341. package/esm/utils/isTypedNode.js.map +0 -1
  342. package/esm/utils/isUnnecessaryReactCall.js +0 -72
  343. package/esm/utils/isUnnecessaryReactCall.js.map +0 -1
  344. package/esm/utils/removeDangerousCode.js +0 -276
  345. package/esm/utils/removeDangerousCode.js.map +0 -1
  346. package/esm/utils/replaceImportMetaEnv.js +0 -44
  347. package/esm/utils/replaceImportMetaEnv.js.map +0 -1
  348. package/esm/utils/scopeHelpers.js +0 -527
  349. package/esm/utils/scopeHelpers.js.map +0 -1
  350. package/esm/utils/traversalCache.js +0 -23
  351. package/esm/utils/traversalCache.js.map +0 -1
  352. package/esm/utils/unwrapExpression.js +0 -18
  353. package/esm/utils/unwrapExpression.js.map +0 -1
  354. package/esm/utils/unwrapSequence.js +0 -14
  355. package/esm/utils/unwrapSequence.js.map +0 -1
  356. package/esm/utils/valueToLiteral.js +0 -59
  357. package/esm/utils/valueToLiteral.js.map +0 -1
  358. package/esm/utils/visitors/JSXElementsRemover.js +0 -51
  359. package/esm/utils/visitors/JSXElementsRemover.js.map +0 -1
  360. package/lib/babel.js +0 -2
  361. package/lib/babel.js.map +0 -1
  362. package/lib/cache.js +0 -308
  363. package/lib/cache.js.map +0 -1
  364. package/lib/debug/fileReporter.js +0 -153
  365. package/lib/debug/fileReporter.js.map +0 -1
  366. package/lib/evaluators/index.js +0 -20
  367. package/lib/evaluators/index.js.map +0 -1
  368. package/lib/index.js +0 -257
  369. package/lib/index.js.map +0 -1
  370. package/lib/module.js +0 -552
  371. package/lib/module.js.map +0 -1
  372. package/lib/options/buildOptions.js +0 -176
  373. package/lib/options/buildOptions.js.map +0 -1
  374. package/lib/options/buildOptions.test.js +0 -141
  375. package/lib/options/buildOptions.test.js.map +0 -1
  376. package/lib/options/loadBabelOptions.js +0 -31
  377. package/lib/options/loadBabelOptions.js.map +0 -1
  378. package/lib/plugins/babel-transform.js +0 -60
  379. package/lib/plugins/babel-transform.js.map +0 -1
  380. package/lib/plugins/collector.js +0 -70
  381. package/lib/plugins/collector.js.map +0 -1
  382. package/lib/plugins/dynamic-import.js +0 -61
  383. package/lib/plugins/dynamic-import.js.map +0 -1
  384. package/lib/plugins/preeval.js +0 -81
  385. package/lib/plugins/preeval.js.map +0 -1
  386. package/lib/plugins/shaker.js +0 -691
  387. package/lib/plugins/shaker.js.map +0 -1
  388. package/lib/shaker.js +0 -95
  389. package/lib/shaker.js.map +0 -1
  390. package/lib/transform/BaseEntrypoint.js +0 -179
  391. package/lib/transform/BaseEntrypoint.js.map +0 -1
  392. package/lib/transform/Entrypoint.helpers.js +0 -279
  393. package/lib/transform/Entrypoint.helpers.js.map +0 -1
  394. package/lib/transform/Entrypoint.js +0 -289
  395. package/lib/transform/Entrypoint.js.map +0 -1
  396. package/lib/transform/Entrypoint.types.js +0 -2
  397. package/lib/transform/Entrypoint.types.js.map +0 -1
  398. package/lib/transform/EvaluatedEntrypoint.js +0 -13
  399. package/lib/transform/EvaluatedEntrypoint.js.map +0 -1
  400. package/lib/transform/actions/AbortError.js +0 -16
  401. package/lib/transform/actions/AbortError.js.map +0 -1
  402. package/lib/transform/actions/BaseAction.js +0 -150
  403. package/lib/transform/actions/BaseAction.js.map +0 -1
  404. package/lib/transform/actions/UnprocessedEntrypointError.js +0 -16
  405. package/lib/transform/actions/UnprocessedEntrypointError.js.map +0 -1
  406. package/lib/transform/actions/actionRunner.js +0 -82
  407. package/lib/transform/actions/actionRunner.js.map +0 -1
  408. package/lib/transform/actions/types.js +0 -2
  409. package/lib/transform/actions/types.js.map +0 -1
  410. package/lib/transform/barrelManifest.js +0 -300
  411. package/lib/transform/barrelManifest.js.map +0 -1
  412. package/lib/transform/generators/baseProcessingHandlers.js +0 -27
  413. package/lib/transform/generators/baseProcessingHandlers.js.map +0 -1
  414. package/lib/transform/generators/collect.js +0 -66
  415. package/lib/transform/generators/collect.js.map +0 -1
  416. package/lib/transform/generators/createStylisPreprocessor.js +0 -372
  417. package/lib/transform/generators/createStylisPreprocessor.js.map +0 -1
  418. package/lib/transform/generators/evalFile.js +0 -57
  419. package/lib/transform/generators/evalFile.js.map +0 -1
  420. package/lib/transform/generators/explodeReexports.js +0 -71
  421. package/lib/transform/generators/explodeReexports.js.map +0 -1
  422. package/lib/transform/generators/extract.js +0 -102
  423. package/lib/transform/generators/extract.js.map +0 -1
  424. package/lib/transform/generators/getExports.js +0 -85
  425. package/lib/transform/generators/getExports.js.map +0 -1
  426. package/lib/transform/generators/index.js +0 -19
  427. package/lib/transform/generators/index.js.map +0 -1
  428. package/lib/transform/generators/processEntrypoint.js +0 -76
  429. package/lib/transform/generators/processEntrypoint.js.map +0 -1
  430. package/lib/transform/generators/processImports.js +0 -82
  431. package/lib/transform/generators/processImports.js.map +0 -1
  432. package/lib/transform/generators/resolveImports.js +0 -221
  433. package/lib/transform/generators/resolveImports.js.map +0 -1
  434. package/lib/transform/generators/rewriteBarrelImports.js +0 -743
  435. package/lib/transform/generators/rewriteBarrelImports.js.map +0 -1
  436. package/lib/transform/generators/transform.js +0 -272
  437. package/lib/transform/generators/transform.js.map +0 -1
  438. package/lib/transform/generators/workflow.js +0 -90
  439. package/lib/transform/generators/workflow.js.map +0 -1
  440. package/lib/transform/helpers/loadWywOptions.js +0 -87
  441. package/lib/transform/helpers/loadWywOptions.js.map +0 -1
  442. package/lib/transform/helpers/withDefaultServices.js +0 -31
  443. package/lib/transform/helpers/withDefaultServices.js.map +0 -1
  444. package/lib/transform/isStaticallyEvaluatableModule.js +0 -159
  445. package/lib/transform/isStaticallyEvaluatableModule.js.map +0 -1
  446. package/lib/transform/rootLog.js +0 -9
  447. package/lib/transform/rootLog.js.map +0 -1
  448. package/lib/transform/types.js +0 -8
  449. package/lib/transform/types.js.map +0 -1
  450. package/lib/transform.js +0 -160
  451. package/lib/transform.js.map +0 -1
  452. package/lib/types.js +0 -2
  453. package/lib/types.js.map +0 -1
  454. package/lib/utils/EventEmitter.js +0 -61
  455. package/lib/utils/EventEmitter.js.map +0 -1
  456. package/lib/utils/ShakerMetadata.js +0 -9
  457. package/lib/utils/ShakerMetadata.js.map +0 -1
  458. package/lib/utils/TransformMetadata.js +0 -19
  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 -20
  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,1217 @@
1
+ /* eslint-disable no-restricted-syntax */
2
+ import { basename, dirname } from "path";
3
+ import { BaseProcessor, expressionToCode } from "@wyw-in-js/processor-utils";
4
+ import { ValueType } from "@wyw-in-js/shared";
5
+ import { collectOxcProcessorImportsFromProgram } from "./collectOxcExportsAndImports.js";
6
+ import { EventEmitter } from "./EventEmitter.js";
7
+ import { collectOxcExpressionDependencies } from "./collectOxcTemplateDependencies.js";
8
+ import { isNotNull } from "./isNotNull.js";
9
+ import { createOxcAstService, printOxcAstServiceImport } from "./oxcAstService.js";
10
+ import { parseOxcProgramCached } from "./parseOxc.js";
11
+ import { getProcessorForImport } from "./processorLookup.js";
12
+ let didWarnSkipSymbolMismatch = false;
13
+ const GENERATED_HELPER_NAME_RE = /^_exp\d*$/;
14
+ const isNode = (value) => !!value && typeof value === "object" && "type" in value && typeof value.type === "string";
15
+ const getChildren = (node) => {
16
+ const result = [];
17
+ const record = node;
18
+ Object.keys(record).forEach((key) => {
19
+ if (key === "type" || key === "start" || key === "end" || key === "range") {
20
+ return;
21
+ }
22
+ const value = record[key];
23
+ if (isNode(value)) {
24
+ result.push(value);
25
+ return;
26
+ }
27
+ if (Array.isArray(value)) {
28
+ value.forEach((item) => {
29
+ if (isNode(item)) {
30
+ result.push(item);
31
+ }
32
+ });
33
+ }
34
+ });
35
+ return result;
36
+ };
37
+ const parseOxc = (code, filename) => {
38
+ return parseOxcProgramCached(filename, code, "module");
39
+ };
40
+ const visit = (node, enter, parent = null) => {
41
+ enter(node, parent);
42
+ getChildren(node).forEach((child) => visit(child, enter, node));
43
+ };
44
+ const applyReplacements = (code, replacements) => {
45
+ let result = code;
46
+ replacements.sort((a, b) => b.start - a.start).forEach((replacement) => {
47
+ result = result.slice(0, replacement.start) + replacement.value + result.slice(replacement.end);
48
+ });
49
+ return result;
50
+ };
51
+ const insertAddedImports = (code, program, addedImports) => {
52
+ if (addedImports.length === 0) {
53
+ return code;
54
+ }
55
+ const uniqueImports = [...new Map(addedImports.map((item) => [`${item.source}\0${item.imported}\0${item.local}`, item])).values()];
56
+ const importBlock = uniqueImports.map(printOxcAstServiceImport).join("\n");
57
+ const lastImport = [...program.body].reverse().find((statement) => statement.type === "ImportDeclaration");
58
+ const hashbangEnd = code.startsWith("#!") ? (() => {
59
+ const newline = code.indexOf("\n");
60
+ return newline === -1 ? code.length : newline + 1;
61
+ })() : 0;
62
+ const insertionPoint = lastImport?.end ?? hashbangEnd;
63
+ const prefix = code.slice(0, insertionPoint);
64
+ const suffix = code.slice(insertionPoint);
65
+ const leadingBreak = prefix.length > 0 && !prefix.endsWith("\n") ? "\n" : "";
66
+ const trailingBreak = suffix.length > 0 && !suffix.startsWith("\n") ? "\n" : "";
67
+ return `${prefix}${leadingBreak}${importBlock}${trailingBreak}${suffix}`;
68
+ };
69
+ const createLocationLookup = (code) => {
70
+ const lineStarts = [0];
71
+ for (let idx = 0; idx < code.length; idx += 1) {
72
+ if (code[idx] === "\n") {
73
+ lineStarts.push(idx + 1);
74
+ }
75
+ }
76
+ return (offset) => {
77
+ let low = 0;
78
+ let high = lineStarts.length - 1;
79
+ while (low <= high) {
80
+ const mid = Math.floor((low + high) / 2);
81
+ const next = lineStarts[mid + 1] ?? Infinity;
82
+ if (lineStarts[mid] <= offset && offset < next) {
83
+ return {
84
+ column: offset - lineStarts[mid],
85
+ line: mid + 1
86
+ };
87
+ }
88
+ if (offset < lineStarts[mid]) {
89
+ high = mid - 1;
90
+ } else {
91
+ low = mid + 1;
92
+ }
93
+ }
94
+ const lastLine = lineStarts.length - 1;
95
+ return {
96
+ column: Math.max(0, offset - lineStarts[lastLine]),
97
+ line: lastLine + 1
98
+ };
99
+ };
100
+ };
101
+ const getSourceLocation = (start, end, loc, filename) => ({
102
+ end: loc(end),
103
+ filename: filename ?? undefined,
104
+ start: loc(start)
105
+ });
106
+ const buildCodeFrameError = (code, location, message) => {
107
+ const lines = code.split("\n");
108
+ const startLine = location.start.line;
109
+ const endLine = location.end.line;
110
+ const frameStart = Math.max(1, startLine - 2);
111
+ const frameEnd = Math.min(lines.length, endLine + 2);
112
+ const lineNoWidth = String(frameEnd).length;
113
+ const frame = [];
114
+ for (let lineNo = frameStart; lineNo <= frameEnd; lineNo += 1) {
115
+ const marker = lineNo === startLine ? ">" : " ";
116
+ const line = lines[lineNo - 1] ?? "";
117
+ frame.push(line.length > 0 ? `${marker} ${String(lineNo).padStart(lineNoWidth)} | ${line}` : `${marker} ${String(lineNo).padStart(lineNoWidth)} |`);
118
+ if (lineNo === startLine) {
119
+ const pointerLength = startLine === endLine ? Math.max(1, location.end.column - location.start.column) : 1;
120
+ frame.push(` ${" ".repeat(lineNoWidth)} | ${" ".repeat(location.start.column)}${"^".repeat(pointerLength)}`);
121
+ }
122
+ }
123
+ const prefix = location.filename ? `${location.filename}: ` : "";
124
+ return new Error(`${prefix}${message}\n${frame.join("\n")}`);
125
+ };
126
+ const collectUsedNames = (program) => {
127
+ const names = new Set();
128
+ visit(program, (node) => {
129
+ if (node.type === "Identifier") {
130
+ names.add(node.name);
131
+ }
132
+ });
133
+ return names;
134
+ };
135
+ const isNodeReference = (node, parent) => {
136
+ if (node.type === "Identifier") {
137
+ const parentRecord = parent;
138
+ if (!parentRecord) {
139
+ return true;
140
+ }
141
+ if (parent?.type === "ImportDeclaration" || parent?.type === "ImportSpecifier" || parent?.type === "ImportDefaultSpecifier" || parent?.type === "ImportNamespaceSpecifier") {
142
+ return false;
143
+ }
144
+ if (parent?.type === "MemberExpression" && parentRecord.property === node && !parentRecord.computed) {
145
+ return false;
146
+ }
147
+ if ((parent?.type === "VariableDeclarator" || parent?.type === "FunctionDeclaration" || parent?.type === "ClassDeclaration" || parent?.type === "ClassExpression") && parentRecord.id === node) {
148
+ return false;
149
+ }
150
+ if ((parent?.type === "PropertyDefinition" || parent?.type === "MethodDefinition") && parentRecord.key === node && !parentRecord.computed) {
151
+ return false;
152
+ }
153
+ if (parent?.type === "Property" && parentRecord.key === node && parentRecord.value !== node && !parentRecord.computed) {
154
+ return false;
155
+ }
156
+ return true;
157
+ }
158
+ if (node.type === "JSXIdentifier") {
159
+ const parentRecord = parent;
160
+ if (parent?.type === "JSXAttribute" && parentRecord?.name === node) {
161
+ return false;
162
+ }
163
+ return true;
164
+ }
165
+ return false;
166
+ };
167
+ const collectReferencedNames = (root) => {
168
+ const names = new Set();
169
+ const walk = (node, parent = null) => {
170
+ if (node.type === "ImportDeclaration") {
171
+ return;
172
+ }
173
+ if (isNodeReference(node, parent) && "name" in node && typeof node.name === "string") {
174
+ names.add(node.name);
175
+ }
176
+ getChildren(node).forEach((child) => walk(child, node));
177
+ };
178
+ walk(root);
179
+ return names;
180
+ };
181
+ const collectImportLocalNames = (node) => {
182
+ if (node.type !== "ImportDeclaration") {
183
+ return [];
184
+ }
185
+ const { specifiers } = node;
186
+ if (!Array.isArray(specifiers)) {
187
+ return [];
188
+ }
189
+ return specifiers.map((specifier) => {
190
+ const { local } = specifier;
191
+ return isNode(local) && "name" in local && typeof local.name === "string" ? local.name : null;
192
+ }).filter(isNotNull);
193
+ };
194
+ const getImportSpecifierLocalName = (node) => {
195
+ const { local } = node;
196
+ return isNode(local) && "name" in local && typeof local.name === "string" ? local.name : null;
197
+ };
198
+ const collectDeclaredNames = (node) => {
199
+ if (node.type === "Identifier") {
200
+ return [node.name];
201
+ }
202
+ if (node.type === "RestElement") {
203
+ return collectDeclaredNames(node.argument);
204
+ }
205
+ if (node.type === "AssignmentPattern") {
206
+ return collectDeclaredNames(node.left);
207
+ }
208
+ if (node.type === "ObjectPattern") {
209
+ return node.properties.flatMap((property) => property.type === "RestElement" ? collectDeclaredNames(property.argument) : collectDeclaredNames(property.value));
210
+ }
211
+ if (node.type === "ArrayPattern") {
212
+ return node.elements.flatMap((element) => element ? collectDeclaredNames(element) : []);
213
+ }
214
+ if (node.type === "TSParameterProperty") {
215
+ return collectDeclaredNames(node.parameter);
216
+ }
217
+ return [];
218
+ };
219
+ const collectTopLevelBindings = (statement) => {
220
+ const bindings = new Set();
221
+ if (statement.type === "ImportDeclaration") {
222
+ collectImportLocalNames(statement).forEach((name) => bindings.add(name));
223
+ return bindings;
224
+ }
225
+ if (statement.type === "VariableDeclaration") {
226
+ const { declarations } = statement;
227
+ if (!Array.isArray(declarations)) {
228
+ return bindings;
229
+ }
230
+ declarations.forEach((declarator) => {
231
+ const { id } = declarator;
232
+ if (isNode(id)) {
233
+ collectDeclaredNames(id).forEach((name) => bindings.add(name));
234
+ }
235
+ });
236
+ return bindings;
237
+ }
238
+ if ((statement.type === "FunctionDeclaration" || statement.type === "ClassDeclaration" || statement.type === "TSEnumDeclaration") && "id" in statement) {
239
+ const { id } = statement;
240
+ if (isNode(id) && id.type === "Identifier") {
241
+ bindings.add(id.name);
242
+ }
243
+ return bindings;
244
+ }
245
+ if (statement.type === "ExportNamedDeclaration") {
246
+ const { declaration } = statement;
247
+ return isNode(declaration) ? collectTopLevelBindings(declaration) : bindings;
248
+ }
249
+ return bindings;
250
+ };
251
+ const collectTopLevelStatementInfos = (program) => program.body.map((statement) => ({
252
+ bindings: collectTopLevelBindings(statement),
253
+ node: statement,
254
+ references: collectReferencedNames(statement)
255
+ }));
256
+ const collectTopLevelBindingsFromStatements = (statements) => new Set(statements.flatMap((statement) => [...statement.bindings]));
257
+ const collectRemovableNamesFromStatements = (statements, initialNames) => {
258
+ const removable = new Set(initialNames);
259
+ const bindingToStatement = new Map();
260
+ statements.forEach((statement) => {
261
+ statement.bindings.forEach((name) => {
262
+ bindingToStatement.set(name, statement);
263
+ });
264
+ });
265
+ const queue = [...removable];
266
+ while (queue.length > 0) {
267
+ const name = queue.shift();
268
+ const statement = bindingToStatement.get(name);
269
+ if (statement) {
270
+ statement.references.forEach((reference) => {
271
+ if (bindingToStatement.has(reference) && !removable.has(reference)) {
272
+ removable.add(reference);
273
+ queue.push(reference);
274
+ }
275
+ });
276
+ }
277
+ }
278
+ return removable;
279
+ };
280
+ const createScopedCleanupScope = (parent) => ({
281
+ bindings: new Map(),
282
+ parent
283
+ });
284
+ const resolveScopedBinding = (scope, name) => {
285
+ let current = scope;
286
+ while (current) {
287
+ const bindingId = current.bindings.get(name);
288
+ if (bindingId) {
289
+ return bindingId;
290
+ }
291
+ current = current.parent;
292
+ }
293
+ return null;
294
+ };
295
+ const collectScopedBindingInfos = (program) => {
296
+ const bindings = new Map();
297
+ let sequence = 0;
298
+ const addBinding = (scope, name, kind, declaration) => {
299
+ const id = `${kind}:${name}:${declaration.start}:${sequence}`;
300
+ sequence += 1;
301
+ bindings.set(id, {
302
+ declaration,
303
+ dependencies: new Set(),
304
+ externalReferences: 0,
305
+ id,
306
+ incomingFromBindings: new Set(),
307
+ kind,
308
+ name
309
+ });
310
+ scope.bindings.set(name, id);
311
+ return id;
312
+ };
313
+ const addPatternBindings = (scope, pattern, kind, declaration) => {
314
+ collectDeclaredNames(pattern).forEach((name) => {
315
+ addBinding(scope, name, kind, declaration);
316
+ });
317
+ };
318
+ const recordReference = (scope, name, ownerBindingId) => {
319
+ const targetId = resolveScopedBinding(scope, name);
320
+ if (!targetId) {
321
+ return;
322
+ }
323
+ const target = bindings.get(targetId);
324
+ if (!target) {
325
+ return;
326
+ }
327
+ if (ownerBindingId && ownerBindingId !== targetId) {
328
+ target.incomingFromBindings.add(ownerBindingId);
329
+ bindings.get(ownerBindingId)?.dependencies.add(targetId);
330
+ return;
331
+ }
332
+ target.externalReferences += 1;
333
+ };
334
+ let walk;
335
+ function walkPatternReferenceSubexpressions(node, scope, ownerBindingId) {
336
+ if (node.type === "Identifier") {
337
+ return;
338
+ }
339
+ if (node.type === "TSParameterProperty") {
340
+ walkPatternReferenceSubexpressions(node.parameter, scope, ownerBindingId);
341
+ return;
342
+ }
343
+ if (node.type === "RestElement") {
344
+ walkPatternReferenceSubexpressions(node.argument, scope, ownerBindingId);
345
+ return;
346
+ }
347
+ if (node.type === "AssignmentPattern") {
348
+ walkPatternReferenceSubexpressions(node.left, scope, ownerBindingId);
349
+ walk(node.right, scope, node, ownerBindingId);
350
+ return;
351
+ }
352
+ if (node.type === "ObjectPattern") {
353
+ node.properties.forEach((property) => {
354
+ if (property.type === "RestElement") {
355
+ walkPatternReferenceSubexpressions(property.argument, scope, ownerBindingId);
356
+ return;
357
+ }
358
+ if (property.computed && isNode(property.key)) {
359
+ walk(property.key, scope, property, ownerBindingId);
360
+ }
361
+ walkPatternReferenceSubexpressions(property.value, scope, ownerBindingId);
362
+ });
363
+ return;
364
+ }
365
+ if (node.type === "ArrayPattern") {
366
+ node.elements.forEach((element) => {
367
+ if (element && isNode(element)) {
368
+ walkPatternReferenceSubexpressions(element, scope, ownerBindingId);
369
+ }
370
+ });
371
+ }
372
+ }
373
+ walk = (node, scope, parent = null, ownerBindingId = null) => {
374
+ if (node.type === "ImportDeclaration") {
375
+ const { specifiers } = node;
376
+ if (Array.isArray(specifiers)) {
377
+ specifiers.forEach((specifier) => {
378
+ const { local } = specifier;
379
+ if (isNode(local) && local.type === "Identifier" && typeof local.name === "string") {
380
+ addBinding(scope, local.name, "import", node);
381
+ }
382
+ });
383
+ }
384
+ return;
385
+ }
386
+ if (node.type === "ExportNamedDeclaration" && node.declaration) {
387
+ walk(node.declaration, scope, node, ownerBindingId);
388
+ collectTopLevelBindings(node).forEach((name) => {
389
+ recordReference(scope, name, ownerBindingId);
390
+ });
391
+ return;
392
+ }
393
+ if (node.type === "ExportDefaultDeclaration") {
394
+ const { declaration } = node;
395
+ if (isNode(declaration)) {
396
+ walk(declaration, scope, node, ownerBindingId);
397
+ if ((declaration.type === "FunctionDeclaration" || declaration.type === "ClassDeclaration") && declaration.id) {
398
+ recordReference(scope, declaration.id.name, ownerBindingId);
399
+ }
400
+ }
401
+ return;
402
+ }
403
+ if (node.type === "VariableDeclaration") {
404
+ const { declarations } = node;
405
+ if (!Array.isArray(declarations)) {
406
+ return;
407
+ }
408
+ declarations.forEach((declarator) => {
409
+ const { id } = declarator;
410
+ if (isNode(id)) {
411
+ addPatternBindings(scope, id, "variable", node);
412
+ }
413
+ });
414
+ declarations.forEach((declarator) => {
415
+ const { id } = declarator;
416
+ const { init } = declarator;
417
+ if (!isNode(id) || !isNode(init)) {
418
+ return;
419
+ }
420
+ const ownerName = collectDeclaredNames(id)[0] ?? null;
421
+ const nextOwner = ownerName !== null ? resolveScopedBinding(scope, ownerName) : null;
422
+ walk(init, scope, declarator, nextOwner);
423
+ });
424
+ return;
425
+ }
426
+ if (node.type === "FunctionDeclaration" && node.id) {
427
+ const functionBindingId = addBinding(scope, node.id.name, "function", node);
428
+ const fnScope = createScopedCleanupScope(scope);
429
+ node.params.forEach((param) => {
430
+ addPatternBindings(fnScope, param, "param", param);
431
+ walkPatternReferenceSubexpressions(param, fnScope, functionBindingId);
432
+ });
433
+ if (node.body) {
434
+ walk(node.body, fnScope, node, functionBindingId);
435
+ }
436
+ return;
437
+ }
438
+ if (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
439
+ const fnScope = createScopedCleanupScope(scope);
440
+ if (node.type === "FunctionExpression" && node.id) {
441
+ addBinding(fnScope, node.id.name, "function", node);
442
+ }
443
+ node.params.forEach((param) => {
444
+ addPatternBindings(fnScope, param, "param", param);
445
+ walkPatternReferenceSubexpressions(param, fnScope, ownerBindingId);
446
+ });
447
+ if (node.body) {
448
+ walk(node.body, fnScope, node, ownerBindingId);
449
+ }
450
+ return;
451
+ }
452
+ if (node.type === "BlockStatement") {
453
+ const blockScope = createScopedCleanupScope(scope);
454
+ getChildren(node).forEach((child) => walk(child, blockScope, node, ownerBindingId));
455
+ return;
456
+ }
457
+ if (isNodeReference(node, parent) && "name" in node && typeof node.name === "string") {
458
+ recordReference(scope, node.name, ownerBindingId);
459
+ }
460
+ getChildren(node).forEach((child) => walk(child, scope, node, ownerBindingId));
461
+ };
462
+ walk(program, createScopedCleanupScope(null));
463
+ return bindings;
464
+ };
465
+ const collectScopedRemovableBindingIds = (bindings, initialNames) => {
466
+ const removable = new Set();
467
+ let changed = true;
468
+ while (changed) {
469
+ changed = false;
470
+ for (const binding of bindings.values()) {
471
+ if (!removable.has(binding.id) && binding.kind !== "import" && binding.kind !== "param" && binding.externalReferences === 0) {
472
+ const seededByName = initialNames.has(binding.name) || GENERATED_HELPER_NAME_RE.test(binding.name) && binding.incomingFromBindings.size === 0;
473
+ const allIncomingRemovable = binding.incomingFromBindings.size > 0 && [...binding.incomingFromBindings].every((sourceId) => removable.has(sourceId));
474
+ if (seededByName && binding.incomingFromBindings.size === 0 || allIncomingRemovable) {
475
+ removable.add(binding.id);
476
+ changed = true;
477
+ }
478
+ }
479
+ }
480
+ }
481
+ return removable;
482
+ };
483
+ function expandImportRemovalRange(code, start, end) {
484
+ let removalStart = start;
485
+ while (removalStart > 0 && (code[removalStart - 1] === " " || code[removalStart - 1] === " ")) {
486
+ removalStart -= 1;
487
+ }
488
+ let removalEnd = end;
489
+ if (code[removalEnd] === ";") {
490
+ removalEnd += 1;
491
+ }
492
+ while (removalEnd < code.length && (code[removalEnd] === " " || code[removalEnd] === " ")) {
493
+ removalEnd += 1;
494
+ }
495
+ if (code[removalEnd] === "\r" && code[removalEnd + 1] === "\n") {
496
+ removalEnd += 2;
497
+ } else if (code[removalEnd] === "\n") {
498
+ removalEnd += 1;
499
+ }
500
+ return {
501
+ end: removalEnd,
502
+ start: removalStart,
503
+ value: ""
504
+ };
505
+ }
506
+ const collectUnusedScopedDeclarationRemovals = (code, bindings, initialRemovableNames) => {
507
+ const removableBindingIds = collectScopedRemovableBindingIds(bindings, initialRemovableNames);
508
+ const removals = new Map();
509
+ bindings.forEach((binding) => {
510
+ if (!removableBindingIds.has(binding.id) || binding.kind === "import" || binding.kind === "param" || binding.externalReferences > 0 || [...binding.incomingFromBindings].some((sourceId) => !removableBindingIds.has(sourceId))) {
511
+ return;
512
+ }
513
+ if (binding.kind === "function" && binding.declaration.type === "FunctionDeclaration") {
514
+ const range = expandImportRemovalRange(code, binding.declaration.start, binding.declaration.end);
515
+ removals.set(`${range.start}:${range.end}`, range);
516
+ return;
517
+ }
518
+ if (binding.declaration.type !== "VariableDeclaration") {
519
+ return;
520
+ }
521
+ const { declarations } = binding.declaration;
522
+ if (!Array.isArray(declarations) || declarations.length !== 1) {
523
+ return;
524
+ }
525
+ const range = expandImportRemovalRange(code, binding.declaration.start, binding.declaration.end);
526
+ removals.set(`${range.start}:${range.end}`, range);
527
+ });
528
+ return [...removals.values()];
529
+ };
530
+ const expandImportSpecifierRemovalRange = (code, start, end) => {
531
+ let removalStart = start;
532
+ let removalEnd = end;
533
+ let whitespaceStart = removalStart;
534
+ while (whitespaceStart > 0 && (code[whitespaceStart - 1] === " " || code[whitespaceStart - 1] === " ")) {
535
+ whitespaceStart -= 1;
536
+ }
537
+ if (code[whitespaceStart - 1] !== "{") {
538
+ removalStart = whitespaceStart;
539
+ }
540
+ while (removalEnd < code.length && (code[removalEnd] === " " || code[removalEnd] === " ")) {
541
+ removalEnd += 1;
542
+ }
543
+ if (code[removalEnd] === ",") {
544
+ removalEnd += 1;
545
+ while (removalEnd < code.length && (code[removalEnd] === " " || code[removalEnd] === " ")) {
546
+ removalEnd += 1;
547
+ }
548
+ } else {
549
+ while (removalStart > 0 && (code[removalStart - 1] === " " || code[removalStart - 1] === " ")) {
550
+ removalStart -= 1;
551
+ }
552
+ if (code[removalStart - 1] === ",") {
553
+ removalStart -= 1;
554
+ while (removalStart > 0 && (code[removalStart - 1] === " " || code[removalStart - 1] === " ")) {
555
+ removalStart -= 1;
556
+ }
557
+ }
558
+ }
559
+ return {
560
+ end: removalEnd,
561
+ start: removalStart,
562
+ value: ""
563
+ };
564
+ };
565
+ const mergeEmptyRemovalRanges = (removals) => {
566
+ if (removals.length <= 1) {
567
+ return removals;
568
+ }
569
+ const sorted = [...removals].sort((a, b) => a.start - b.start);
570
+ const merged = [];
571
+ sorted.forEach((removal) => {
572
+ const previous = merged[merged.length - 1];
573
+ if (previous && previous.value === "" && removal.value === "" && removal.start <= previous.end) {
574
+ previous.end = Math.max(previous.end, removal.end);
575
+ return;
576
+ }
577
+ merged.push({ ...removal });
578
+ });
579
+ return merged;
580
+ };
581
+ const collectUnusedImportRemovals = (code, program, referencedNames, removableNames, preserveSideEffectImportLocals) => {
582
+ const removals = [];
583
+ program.body.forEach((statement) => {
584
+ if (statement.type !== "ImportDeclaration") {
585
+ return;
586
+ }
587
+ const localNames = collectImportLocalNames(statement);
588
+ const removableLocalNames = localNames.filter((localName) => removableNames.has(localName));
589
+ if (removableLocalNames.length > 0 && removableLocalNames.length === localNames.length && removableLocalNames.every((localName) => !referencedNames.has(localName))) {
590
+ if (removableLocalNames.some((localName) => preserveSideEffectImportLocals.has(localName))) {
591
+ removals.push({
592
+ end: statement.end,
593
+ start: statement.start,
594
+ value: `import ${code.slice(statement.source.start, statement.source.end)};`
595
+ });
596
+ return;
597
+ }
598
+ removals.push(expandImportRemovalRange(code, statement.start, statement.end));
599
+ return;
600
+ }
601
+ const { specifiers } = statement;
602
+ if (!Array.isArray(specifiers) || specifiers.length <= 1) {
603
+ return;
604
+ }
605
+ specifiers.forEach((specifier) => {
606
+ if (!isNode(specifier)) {
607
+ return;
608
+ }
609
+ const localName = getImportSpecifierLocalName(specifier);
610
+ if (localName && removableNames.has(localName) && !referencedNames.has(localName)) {
611
+ removals.push(expandImportSpecifierRemovalRange(code, specifier.start, specifier.end));
612
+ }
613
+ });
614
+ });
615
+ return removals;
616
+ };
617
+ const collectUnusedTopLevelDeclarationRemovals = (code, program, referencedNames, removableNames) => {
618
+ const removals = [];
619
+ program.body.forEach((statement) => {
620
+ if (statement.type !== "VariableDeclaration") {
621
+ return;
622
+ }
623
+ const localNames = [...collectTopLevelBindings(statement)];
624
+ if (localNames.length > 0 && localNames.every((localName) => removableNames.has(localName)) && localNames.every((localName) => !referencedNames.has(localName))) {
625
+ removals.push(expandImportRemovalRange(code, statement.start, statement.end));
626
+ }
627
+ });
628
+ return removals;
629
+ };
630
+ const collectUnusedGeneratedHelperDeclarationRemovals = (code, program, referencedNames) => {
631
+ const removals = [];
632
+ program.body.forEach((statement) => {
633
+ if (statement.type !== "VariableDeclaration") {
634
+ return;
635
+ }
636
+ const localNames = [...collectTopLevelBindings(statement)];
637
+ if (localNames.length > 0 && localNames.every((localName) => GENERATED_HELPER_NAME_RE.test(localName)) && localNames.every((localName) => !referencedNames.has(localName))) {
638
+ removals.push(expandImportRemovalRange(code, statement.start, statement.end));
639
+ }
640
+ });
641
+ return removals;
642
+ };
643
+ const collectTopLevelExpressionStatementRemovals = (code, statements, topLevelBindings, removableExpressionRefs) => {
644
+ const removals = [];
645
+ statements.forEach((statement) => {
646
+ if (statement.node.type !== "ExpressionStatement") {
647
+ return;
648
+ }
649
+ const { expression } = statement.node;
650
+ const isPureExpression = expression.type === "Identifier" || expression.type === "Literal" || expression.type === "ObjectExpression" || expression.type === "ArrayExpression" || expression.type === "ArrowFunctionExpression" || expression.type === "FunctionExpression" || expression.type === "TemplateLiteral" && expression.expressions.length === 0;
651
+ if (!isPureExpression) {
652
+ return;
653
+ }
654
+ const localReferences = [...statement.references].filter((name) => topLevelBindings.has(name));
655
+ if (localReferences.length > 0 && localReferences.every((name) => removableExpressionRefs.has(name))) {
656
+ removals.push(expandImportRemovalRange(code, statement.node.start, statement.node.end));
657
+ }
658
+ });
659
+ return removals;
660
+ };
661
+ const collectEmptyTopLevelBlockRemovals = (code, program) => {
662
+ const removals = [];
663
+ program.body.forEach((statement) => {
664
+ if (statement.type !== "BlockStatement" || statement.body.length > 0) {
665
+ return;
666
+ }
667
+ removals.push(expandImportRemovalRange(code, statement.start, statement.end));
668
+ });
669
+ return removals;
670
+ };
671
+ const removeUnusedAfterReplacement = (code, filename, initialRemovableNames, removableExpressionRefs, preserveSideEffectImportLocals) => {
672
+ let current = code;
673
+ const cumulativeRemovableNames = new Set(initialRemovableNames);
674
+ const applyIfParsable = (next) => {
675
+ try {
676
+ parseOxc(next, filename);
677
+ return next;
678
+ } catch {
679
+ return current;
680
+ }
681
+ };
682
+ for (let idx = 0; idx < 5; idx += 1) {
683
+ const previous = current;
684
+ const program = parseOxc(current, filename);
685
+ const statements = collectTopLevelStatementInfos(program);
686
+ const removableNames = collectRemovableNamesFromStatements(statements, cumulativeRemovableNames);
687
+ removableNames.forEach((name) => cumulativeRemovableNames.add(name));
688
+ const referencedNames = collectReferencedNames(program);
689
+ const topLevelBindings = collectTopLevelBindingsFromStatements(statements);
690
+ const scopedBindings = collectScopedBindingInfos(program);
691
+ const removals = mergeEmptyRemovalRanges([
692
+ ...collectUnusedScopedDeclarationRemovals(current, scopedBindings, cumulativeRemovableNames),
693
+ ...collectUnusedTopLevelDeclarationRemovals(current, program, referencedNames, cumulativeRemovableNames),
694
+ ...collectUnusedGeneratedHelperDeclarationRemovals(current, program, referencedNames),
695
+ ...collectUnusedImportRemovals(current, program, referencedNames, cumulativeRemovableNames, preserveSideEffectImportLocals),
696
+ ...collectTopLevelExpressionStatementRemovals(current, statements, topLevelBindings, removableExpressionRefs),
697
+ ...collectEmptyTopLevelBlockRemovals(current, program)
698
+ ]);
699
+ current = removals.length > 0 ? applyIfParsable(applyReplacements(current, removals)) : current;
700
+ if (current === previous) {
701
+ return current;
702
+ }
703
+ }
704
+ return current;
705
+ };
706
+ const getMemberName = (node) => {
707
+ if (node.computed) {
708
+ return node.property.type === "Literal" && typeof node.property.value === "string" ? node.property.value : null;
709
+ }
710
+ return node.property.type === "Identifier" ? node.property.name : null;
711
+ };
712
+ const unwrapQualifiedExpression = (node) => {
713
+ if (node.type === "TSAsExpression" || node.type === "TSSatisfiesExpression" || node.type === "TSNonNullExpression" || node.type === "TSTypeAssertion" || node.type === "ParenthesizedExpression") {
714
+ return unwrapQualifiedExpression(node.expression);
715
+ }
716
+ if (node.type === "SequenceExpression") {
717
+ const sequence = node;
718
+ return unwrapQualifiedExpression(sequence.expressions[sequence.expressions.length - 1] ?? node);
719
+ }
720
+ return node;
721
+ };
722
+ const getRootIdentifier = (node) => {
723
+ const expression = unwrapQualifiedExpression(node);
724
+ if (expression.type === "Identifier") {
725
+ return expression;
726
+ }
727
+ if (expression.type === "MemberExpression") {
728
+ return getRootIdentifier(expression.object);
729
+ }
730
+ if (expression.type === "CallExpression") {
731
+ return getRootIdentifier(expression.callee);
732
+ }
733
+ return null;
734
+ };
735
+ const getQualifiedName = (node) => {
736
+ const expression = unwrapQualifiedExpression(node);
737
+ if (expression.type === "Identifier") {
738
+ return expression.name;
739
+ }
740
+ if (expression.type === "MemberExpression") {
741
+ const object = getQualifiedName(expression.object);
742
+ const member = getMemberName(expression);
743
+ return object && member ? `${object}.${member}` : null;
744
+ }
745
+ if (expression.type === "CallExpression") {
746
+ return getQualifiedName(expression.callee);
747
+ }
748
+ return null;
749
+ };
750
+ const resolveDefinedProcessor = (callee, definedProcessors) => {
751
+ const qualified = getQualifiedName(callee);
752
+ if (qualified) {
753
+ const definedProcessor = definedProcessors.get(qualified);
754
+ if (definedProcessor) {
755
+ return {
756
+ collapseQualifiedCallee: qualified.includes("."),
757
+ definedProcessor
758
+ };
759
+ }
760
+ }
761
+ const root = getRootIdentifier(callee);
762
+ if (!root) {
763
+ return null;
764
+ }
765
+ const definedProcessor = definedProcessors.get(root.name);
766
+ return definedProcessor ? {
767
+ collapseQualifiedCallee: false,
768
+ definedProcessor
769
+ } : null;
770
+ };
771
+ const isCallTagOfTaggedTemplate = (node, parent) => parent?.type === "TaggedTemplateExpression" && parent.tag === node;
772
+ const expandReplacementTarget = (target, ancestors) => {
773
+ let current = target;
774
+ for (let idx = ancestors.length - 1; idx >= 0; idx -= 1) {
775
+ const ancestor = ancestors[idx];
776
+ if (ancestor.type === "SequenceExpression" && ancestor.expressions[ancestor.expressions.length - 1] === current) {
777
+ current = ancestor;
778
+ } else if (ancestor.type === "ParenthesizedExpression" && ancestor.expression === current) {
779
+ current = ancestor;
780
+ } else {
781
+ break;
782
+ }
783
+ }
784
+ return current;
785
+ };
786
+ const collectProcessorUsages = (program, definedProcessors) => {
787
+ const usages = [];
788
+ const walk = (node, ancestors, parent = null) => {
789
+ if (node.type === "TaggedTemplateExpression") {
790
+ const callee = node.tag;
791
+ const resolvedProcessor = resolveDefinedProcessor(callee, definedProcessors);
792
+ if (resolvedProcessor) {
793
+ usages.push({
794
+ ancestors,
795
+ callee,
796
+ collapseQualifiedCallee: resolvedProcessor.collapseQualifiedCallee,
797
+ definedProcessor: resolvedProcessor.definedProcessor,
798
+ kind: "template",
799
+ replacementTarget: expandReplacementTarget(node, ancestors),
800
+ target: node
801
+ });
802
+ }
803
+ } else if (node.type === "CallExpression" && !isCallTagOfTaggedTemplate(node, parent)) {
804
+ const { callee } = node;
805
+ const resolvedProcessor = resolveDefinedProcessor(callee, definedProcessors);
806
+ if (resolvedProcessor) {
807
+ usages.push({
808
+ ancestors,
809
+ callee,
810
+ collapseQualifiedCallee: resolvedProcessor.collapseQualifiedCallee,
811
+ definedProcessor: resolvedProcessor.definedProcessor,
812
+ kind: "call",
813
+ replacementTarget: expandReplacementTarget(node, ancestors),
814
+ target: node
815
+ });
816
+ }
817
+ }
818
+ getChildren(node).forEach((child) => walk(child, [...ancestors, node], node));
819
+ };
820
+ walk(program, []);
821
+ return usages.sort((a, b) => a.target.start - b.target.start);
822
+ };
823
+ const expressionSpan = (expression) => ({
824
+ end: expression.end,
825
+ start: expression.start
826
+ });
827
+ const collectCallArgumentSpans = (node) => {
828
+ const expression = unwrapQualifiedExpression(node);
829
+ if (expression.type === "CallExpression") {
830
+ const call = expression;
831
+ const calleeSpans = collectCallArgumentSpans(call.callee);
832
+ const argumentSpans = call.arguments.flatMap((arg) => arg.type === "SpreadElement" ? [] : [expressionSpan(arg)]);
833
+ return [...calleeSpans, ...argumentSpans];
834
+ }
835
+ if (expression.type === "MemberExpression") {
836
+ return collectCallArgumentSpans(expression.object);
837
+ }
838
+ return [];
839
+ };
840
+ const collectUsageExpressionSpans = (usage) => {
841
+ const calleeSpans = collectCallArgumentSpans(usage.callee);
842
+ if (usage.kind === "template") {
843
+ return [...calleeSpans, ...usage.target.quasi.expressions.map((expression) => expressionSpan(expression))];
844
+ }
845
+ return [...calleeSpans, ...usage.target.arguments.flatMap((arg) => arg.type === "SpreadElement" ? [] : [expressionSpan(arg)])];
846
+ };
847
+ const literalExpressionValue = (expression, code, source, location) => {
848
+ if (expression.type !== "Literal") {
849
+ return null;
850
+ }
851
+ if (expression.value === null || typeof expression.value === "string" || typeof expression.value === "number" || typeof expression.value === "boolean") {
852
+ let type;
853
+ if (expression.value === null) {
854
+ type = "NullLiteral";
855
+ } else if (typeof expression.value === "string") {
856
+ type = "StringLiteral";
857
+ } else if (typeof expression.value === "number") {
858
+ type = "NumericLiteral";
859
+ } else {
860
+ type = "BooleanLiteral";
861
+ }
862
+ const ex = expression.value === null ? {
863
+ loc: location,
864
+ type
865
+ } : {
866
+ loc: location,
867
+ type,
868
+ value: expression.value
869
+ };
870
+ return {
871
+ buildCodeFrameError: (message) => buildCodeFrameError(code, location, message),
872
+ ex,
873
+ kind: ValueType.CONST,
874
+ source,
875
+ value: expression.value
876
+ };
877
+ }
878
+ return null;
879
+ };
880
+ const expressionValue = (expression, code, loc, filename) => {
881
+ const source = code.slice(expression.start, expression.end);
882
+ const location = getSourceLocation(expression.start, expression.end, loc, filename);
883
+ const literal = literalExpressionValue(expression, code, source, location);
884
+ if (literal) {
885
+ return literal;
886
+ }
887
+ const helperCallName = expression.type === "CallExpression" && expression.arguments.length === 0 && expression.callee.type === "Identifier" && GENERATED_HELPER_NAME_RE.test(expression.callee.name) ? expression.callee.name : null;
888
+ let ex;
889
+ if (expression.type === "Identifier") {
890
+ ex = {
891
+ loc: location,
892
+ name: expression.name,
893
+ type: "Identifier"
894
+ };
895
+ } else if (helperCallName) {
896
+ ex = {
897
+ loc: location,
898
+ name: helperCallName,
899
+ type: "Identifier"
900
+ };
901
+ } else {
902
+ ex = {
903
+ loc: location,
904
+ name: code.slice(expression.start, expression.end),
905
+ type: "Identifier"
906
+ };
907
+ }
908
+ return {
909
+ buildCodeFrameError: (message) => buildCodeFrameError(code, location, message),
910
+ ex,
911
+ kind: expression.type === "ArrowFunctionExpression" || expression.type === "FunctionExpression" ? ValueType.FUNCTION : ValueType.LAZY,
912
+ source
913
+ };
914
+ };
915
+ const withCurrentExpressionLocation = (value, expression, loc, filename) => {
916
+ const location = getSourceLocation(expression.start, expression.end, loc, filename);
917
+ if (value.kind === ValueType.CONST) {
918
+ return {
919
+ ...value,
920
+ ex: {
921
+ ...value.ex,
922
+ loc: location
923
+ }
924
+ };
925
+ }
926
+ if (value.kind === ValueType.FUNCTION) {
927
+ return {
928
+ ...value,
929
+ ex: {
930
+ ...value.ex,
931
+ loc: location
932
+ }
933
+ };
934
+ }
935
+ return {
936
+ ...value,
937
+ ex: {
938
+ ...value.ex,
939
+ loc: location
940
+ }
941
+ };
942
+ };
943
+ const shiftExpressionValue = (expressionValues, expression, code, loc, filename) => expressionValues.length > 0 ? withCurrentExpressionLocation(expressionValues.shift(), expression, loc, filename) : expressionValue(expression, code, loc, filename);
944
+ const zipTemplate = (template, code, loc, filename, expressionValues) => {
945
+ const parts = template.quasi.quasis.flatMap((quasi, idx) => {
946
+ const expression = template.quasi.expressions[idx];
947
+ const templateElement = {
948
+ ...quasi,
949
+ loc: getSourceLocation(quasi.start, quasi.end, loc, filename)
950
+ };
951
+ return [templateElement, expression ? shiftExpressionValue(expressionValues, expression, code, loc, filename) : null].filter(isNotNull);
952
+ });
953
+ return ["template", parts];
954
+ };
955
+ const buildCalleeParams = (node, code, loc, filename, expressionValues, collapseQualifiedCallee = false) => {
956
+ const expression = unwrapQualifiedExpression(node);
957
+ if (collapseQualifiedCallee && (expression.type === "Identifier" || expression.type === "MemberExpression")) {
958
+ return [["callee", expression]];
959
+ }
960
+ if (expression.type === "Identifier") {
961
+ return [["callee", {
962
+ name: expression.name,
963
+ type: "Identifier"
964
+ }]];
965
+ }
966
+ if (expression.type === "MemberExpression") {
967
+ const params = buildCalleeParams(expression.object, code, loc, filename, expressionValues, collapseQualifiedCallee);
968
+ const member = getMemberName(expression);
969
+ return params && member ? [...params, ["member", member]] : null;
970
+ }
971
+ if (expression.type === "CallExpression") {
972
+ const call = expression;
973
+ const params = buildCalleeParams(call.callee, code, loc, filename, expressionValues, collapseQualifiedCallee);
974
+ if (!params) {
975
+ return null;
976
+ }
977
+ const callValues = call.arguments.filter((arg) => arg.type !== "SpreadElement").map((arg) => shiftExpressionValue(expressionValues, arg, code, loc, filename));
978
+ return [...params, ["call", ...callValues]];
979
+ }
980
+ return null;
981
+ };
982
+ const buildParams = (usage, code, loc, filename, expressionValues, collapseQualifiedCallee) => {
983
+ const params = buildCalleeParams(usage.callee, code, loc, filename, expressionValues, collapseQualifiedCallee);
984
+ if (!params) {
985
+ return null;
986
+ }
987
+ if (usage.kind === "template") {
988
+ return [...params, zipTemplate(usage.target, code, loc, filename, expressionValues)];
989
+ }
990
+ const callValues = usage.target.arguments.filter((arg) => arg.type !== "SpreadElement").map((arg) => shiftExpressionValue(expressionValues, arg, code, loc, filename));
991
+ return [...params, ["call", ...callValues]];
992
+ };
993
+ const getPropertyKeyName = (property, code) => {
994
+ const { key } = property;
995
+ if (!isNode(key)) {
996
+ return null;
997
+ }
998
+ if (key.type === "Identifier") {
999
+ return key.name;
1000
+ }
1001
+ if (key.type === "Literal") {
1002
+ return String(key.value);
1003
+ }
1004
+ return typeof key.start === "number" && typeof key.end === "number" ? code.slice(key.start, key.end) : null;
1005
+ };
1006
+ const getDisplayName = (ancestors, idx, code, filename) => {
1007
+ const owner = [...ancestors].reverse().find((node) => {
1008
+ return node.type === "Property" || node.type === "JSXOpeningElement" || node.type === "VariableDeclarator";
1009
+ });
1010
+ if (owner?.type === "Property") {
1011
+ const keyName = getPropertyKeyName(owner, code);
1012
+ if (keyName) {
1013
+ return keyName;
1014
+ }
1015
+ } else if (owner?.type === "JSXOpeningElement") {
1016
+ const { name } = owner;
1017
+ if (isNode(name) && name.type === "JSXIdentifier") {
1018
+ return name.name;
1019
+ }
1020
+ } else if (owner?.type === "VariableDeclarator") {
1021
+ const { id } = owner;
1022
+ if (isNode(id) && id.type === "Identifier") {
1023
+ return id.name;
1024
+ }
1025
+ }
1026
+ let displayName = basename(filename ?? "unknown").replace(/\.[a-z\d]+$/, "");
1027
+ if (filename && /^index\.[a-z\d]+$/.test(basename(filename))) {
1028
+ displayName = basename(dirname(filename));
1029
+ }
1030
+ if (!displayName) {
1031
+ throw new Error("Couldn't determine a name for the component. Ensure that it's either:\n" + "- Assigned to a variable\n" + "- Is an object property\n" + "- Is a prop in a JSX element\n");
1032
+ }
1033
+ return `${displayName}${idx}`;
1034
+ };
1035
+ const getTagOwner = (ancestors) => {
1036
+ const owner = [...ancestors].reverse().find((node) => node.type === "Property" || node.type === "JSXOpeningElement" || node.type === "VariableDeclarator");
1037
+ return owner ?? null;
1038
+ };
1039
+ const isTagReferenced = (program, ancestors) => {
1040
+ const owner = getTagOwner(ancestors);
1041
+ if (owner?.type !== "VariableDeclarator") {
1042
+ return true;
1043
+ }
1044
+ const { id } = owner;
1045
+ if (!isNode(id) || id.type !== "Identifier") {
1046
+ return true;
1047
+ }
1048
+ if (ancestors.some((node) => node.type === "ExportNamedDeclaration")) {
1049
+ return true;
1050
+ }
1051
+ let referenced = false;
1052
+ visit(program, (node, parent) => {
1053
+ const referenceName = node.type === "Identifier" || node.type === "JSXIdentifier" ? node.name : null;
1054
+ if (referenced || referenceName !== id.name || node.type === "Identifier" && node.start === id.start && node.end === id.end) {
1055
+ return;
1056
+ }
1057
+ referenced = isNodeReference(node, parent);
1058
+ });
1059
+ return referenced;
1060
+ };
1061
+ const isReplacementPure = (replacement) => replacement.type === "CallExpression";
1062
+ const createProcessor = (definedProcessor, params, target, replacementTarget, ancestors, fileContext, options, code, loc, idx, isReferenced, usedNames, replacements) => {
1063
+ const [Processor, tagSource] = definedProcessor;
1064
+ const astService = createOxcAstService(usedNames);
1065
+ const replacer = (replacement, isPure) => {
1066
+ const next = typeof replacement === "function" ? replacement(target) : replacement;
1067
+ const replacementCode = expressionToCode(next);
1068
+ replacements.push({
1069
+ start: replacementTarget.start,
1070
+ end: replacementTarget.end,
1071
+ value: isPure && isReplacementPure(next) ? `/*#__PURE__*/${replacementCode}` : replacementCode
1072
+ });
1073
+ };
1074
+ try {
1075
+ let displayName;
1076
+ try {
1077
+ displayName = getDisplayName(ancestors, idx, code, fileContext.filename);
1078
+ } catch (error) {
1079
+ if (error instanceof Error && error.message.startsWith("Couldn't determine a name for the component")) {
1080
+ let displayNameNode = target;
1081
+ if (target.type === "TaggedTemplateExpression") {
1082
+ displayNameNode = target.tag;
1083
+ } else if (target.type === "CallExpression") {
1084
+ displayNameNode = target.callee;
1085
+ }
1086
+ const pointerNode = displayNameNode.type === "MemberExpression" ? getRootIdentifier(displayNameNode) ?? displayNameNode : displayNameNode;
1087
+ throw buildCodeFrameError(code, getSourceLocation(pointerNode.start, pointerNode.end, loc, fileContext.filename), error.message);
1088
+ }
1089
+ throw error;
1090
+ }
1091
+ return {
1092
+ astService,
1093
+ processor: new Processor(params, tagSource, astService, getSourceLocation(target.start, target.end, loc, fileContext.filename), replacer, displayName, isReferenced, idx, options, fileContext)
1094
+ };
1095
+ } catch (e) {
1096
+ if (e === BaseProcessor.SKIP) {
1097
+ return null;
1098
+ }
1099
+ if (typeof e === "symbol" && e.description === BaseProcessor.SKIP.description) {
1100
+ if (!didWarnSkipSymbolMismatch) {
1101
+ didWarnSkipSymbolMismatch = true;
1102
+ // eslint-disable-next-line no-console
1103
+ console.warn([
1104
+ "[wyw-in-js] Processor threw Symbol('skip') that does not match BaseProcessor.SKIP identity.",
1105
+ "This usually means duplicate copies of @wyw-in-js/processor-utils (or the processor) are bundled/installed.",
1106
+ "Consider deduping dependencies to avoid subtle issues (instanceof checks, sentinels, etc)."
1107
+ ].join("\n"));
1108
+ }
1109
+ return null;
1110
+ }
1111
+ throw e;
1112
+ }
1113
+ };
1114
+ export const applyOxcProcessors = (code, fileContext, options, callback, cleanupUnused = false) => {
1115
+ const filename = fileContext.filename ?? "unknown.js";
1116
+ const eventEmitter = options.eventEmitter ?? EventEmitter.dummy;
1117
+ let workingCode = code;
1118
+ let program = parseOxc(workingCode, filename);
1119
+ const definedProcessors = new Map();
1120
+ const removableImportLocals = new Set();
1121
+ const removableExpressionRefs = new Set();
1122
+ eventEmitter.perf("transform:preeval:processTemplate:imports", () => {
1123
+ const imports = eventEmitter.perf("transform:preeval:processTemplate:imports:analysis", () => collectOxcProcessorImportsFromProgram(program, workingCode));
1124
+ eventEmitter.perf("transform:preeval:processTemplate:imports:lookup", () => {
1125
+ imports.forEach((item) => {
1126
+ const localName = item.local.name ?? item.local.code;
1127
+ if (item.imported === "side-effect" || !localName) {
1128
+ return;
1129
+ }
1130
+ const [processor, tagSource] = getProcessorForImport({
1131
+ imported: item.imported,
1132
+ source: item.source
1133
+ }, filename, options);
1134
+ if (processor) {
1135
+ definedProcessors.set(localName, [processor, tagSource]);
1136
+ removableImportLocals.add(localName);
1137
+ const rootLocalName = localName.split(".")[0];
1138
+ if (rootLocalName) {
1139
+ removableImportLocals.add(rootLocalName);
1140
+ }
1141
+ }
1142
+ });
1143
+ });
1144
+ });
1145
+ if (definedProcessors.size === 0) {
1146
+ return {
1147
+ code: workingCode,
1148
+ processors: [],
1149
+ staticValueCandidates: [],
1150
+ staticValues: []
1151
+ };
1152
+ }
1153
+ let processorUsages = eventEmitter.perf("transform:preeval:processTemplate:usages", () => collectProcessorUsages(program, definedProcessors));
1154
+ if (processorUsages.length === 0) {
1155
+ return {
1156
+ code: workingCode,
1157
+ processors: [],
1158
+ staticValueCandidates: [],
1159
+ staticValues: []
1160
+ };
1161
+ }
1162
+ const targetExpressionSpans = processorUsages.flatMap(collectUsageExpressionSpans);
1163
+ const extracted = targetExpressionSpans.length > 0 ? eventEmitter.perf("transform:preeval:processTemplate:deps", () => collectOxcExpressionDependencies(workingCode, filename, options.evaluate, targetExpressionSpans)) : {
1164
+ code: workingCode,
1165
+ dependencyNames: [],
1166
+ expressionValues: [],
1167
+ staticValueCandidates: [],
1168
+ staticValues: []
1169
+ };
1170
+ if (extracted.code !== workingCode) {
1171
+ workingCode = extracted.code;
1172
+ program = eventEmitter.perf("transform:preeval:processTemplate:reparse", () => parseOxc(workingCode, filename));
1173
+ processorUsages = eventEmitter.perf("transform:preeval:processTemplate:usages", () => collectProcessorUsages(program, definedProcessors));
1174
+ }
1175
+ const templateExpressionValues = extracted.expressionValues.map((value) => ({
1176
+ ...value,
1177
+ buildCodeFrameError: (message) => buildCodeFrameError(code, value.ex.loc, message)
1178
+ }));
1179
+ const loc = createLocationLookup(workingCode);
1180
+ const usedNames = eventEmitter.perf("transform:preeval:processTemplate:usedNames", () => collectUsedNames(program));
1181
+ const addedImports = [];
1182
+ const replacements = [];
1183
+ const processors = [];
1184
+ extracted.dependencyNames.forEach((name) => removableImportLocals.add(name));
1185
+ eventEmitter.perf("transform:preeval:processTemplate:processors", () => {
1186
+ processorUsages.forEach((usage, idx) => {
1187
+ const params = buildParams(usage, workingCode, loc, filename, templateExpressionValues, usage.collapseQualifiedCallee);
1188
+ if (!params) {
1189
+ return;
1190
+ }
1191
+ const created = createProcessor(usage.definedProcessor, params, usage.target, usage.replacementTarget, usage.ancestors, fileContext, options, workingCode, loc, idx, isTagReferenced(program, usage.ancestors), usedNames, replacements);
1192
+ if (!created) {
1193
+ return;
1194
+ }
1195
+ const { astService, processor } = created;
1196
+ const owner = getTagOwner(usage.ancestors);
1197
+ if (owner?.type === "VariableDeclarator") {
1198
+ const { id } = owner;
1199
+ if (isNode(id) && id.type === "Identifier") {
1200
+ removableExpressionRefs.add(id.name);
1201
+ }
1202
+ }
1203
+ processors.push(processor);
1204
+ callback(processor);
1205
+ addedImports.push(...astService.getAddedImports());
1206
+ });
1207
+ });
1208
+ const replacedCode = applyReplacements(workingCode, replacements);
1209
+ const codeWithAddedImports = insertAddedImports(replacedCode, program, addedImports);
1210
+ return {
1211
+ code: cleanupUnused ? eventEmitter.perf("transform:preeval:processTemplate:cleanup", () => removeUnusedAfterReplacement(codeWithAddedImports, filename, removableImportLocals, new Set([...removableExpressionRefs, ...extracted.dependencyNames]), options.preserveSideEffectImportLocals ?? new Set())) : codeWithAddedImports,
1212
+ processors,
1213
+ staticValueCandidates: extracted.staticValueCandidates,
1214
+ staticValues: extracted.staticValues
1215
+ };
1216
+ };
1217
+ //# sourceMappingURL=applyOxcProcessors.js.map