@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,2262 @@
1
+ /* eslint-disable no-continue, no-plusplus, no-nested-ternary, no-void, no-await-in-loop, @typescript-eslint/no-use-before-define */
2
+ import { createHash } from 'crypto';
3
+ import fs from 'fs';
4
+ import NativeModule from 'module';
5
+ import path from 'path';
6
+ import { fileURLToPath } from 'url';
7
+ import { spawn } from 'child_process';
8
+ import { invariant } from 'ts-invariant';
9
+ import { isFeatureEnabled } from '@wyw-in-js/shared';
10
+ import { isStaticallyEvaluatableModule } from '../transform/isStaticallyEvaluatableModule';
11
+ import { applyImportOverrideToOnly, getImportOverride, resolveMockSpecifier, toImportKey, } from '../utils/importOverrides';
12
+ import { getFileIdx } from '../utils/getFileIdx';
13
+ import { collectOxcExportsAndImports } from '../utils/collectOxcExportsAndImports';
14
+ import { parseRequest, stripQueryAndHash } from '../utils/parseRequest';
15
+ import { resolveFilenameWithConditions } from '../utils/resolveWithConditions';
16
+ import { hasCachedWywPrevalExport, } from '../utils/hasCachedWywPrevalExport';
17
+ import { isSuperSet, mergeOnly } from '../transform/Entrypoint.helpers';
18
+ import { oxcShaker } from '../shaker';
19
+ import { analyzeOxcBarrelFile } from '../transform/oxcBarrelManifest';
20
+ import { LruCache } from './lru';
21
+ import { prepareModuleOnDemand, } from './prepareModuleOnDemand';
22
+ import { deserializeValue, encodeGlobals, serializeValue, } from './serialize';
23
+ import { createWriteQueue, writeToStream } from './writeQueue';
24
+ const DefaultModuleImplementation = NativeModule;
25
+ const CJS_DEFAULT_CONDITIONS = ['require', 'node', 'default'];
26
+ const expandConditions = (conditionNames) => {
27
+ const result = new Set();
28
+ conditionNames.forEach((name) => {
29
+ if (name === '...') {
30
+ CJS_DEFAULT_CONDITIONS.forEach((condition) => result.add(condition));
31
+ return;
32
+ }
33
+ result.add(name);
34
+ });
35
+ return result;
36
+ };
37
+ const NOOP = () => { };
38
+ const isBuiltinSpecifier = (specifier) => {
39
+ const normalized = specifier.startsWith('node:')
40
+ ? specifier.slice(5)
41
+ : specifier;
42
+ return (DefaultModuleImplementation.builtinModules?.includes(normalized) ||
43
+ DefaultModuleImplementation.builtinModules?.includes(`node:${normalized}`));
44
+ };
45
+ const isVirtualSpecifier = (specifier) => specifier.startsWith('/@') ||
46
+ specifier.startsWith('virtual:') ||
47
+ specifier.startsWith('\0');
48
+ const isEvalOnlyKey = (key) => key === '__wywPreval' || key === 'side-effect';
49
+ const isPreparedOnlySuperSet = (currentOnly, requestedOnly) => {
50
+ if (requestedOnly.includes('__wywPreval') &&
51
+ !currentOnly.includes('__wywPreval')) {
52
+ return false;
53
+ }
54
+ return isSuperSet(currentOnly, requestedOnly);
55
+ };
56
+ const hasPreparedExportKeys = (prepared, requestedOnly) => {
57
+ const requestedKeys = requestedOnly.filter((key) => !isEvalOnlyKey(key) && key !== '*');
58
+ if (requestedKeys.length === 0) {
59
+ return true;
60
+ }
61
+ if (!prepared.exports) {
62
+ if (!prepared.code) {
63
+ return false;
64
+ }
65
+ try {
66
+ const collected = collectOxcExportsAndImports(prepared.code, 'prepared-module.js');
67
+ if (collected.reexports.some((reexport) => reexport.exported === '*')) {
68
+ return true;
69
+ }
70
+ const exportNames = new Set([
71
+ ...Object.keys(collected.exports),
72
+ ...collected.reexports
73
+ .filter((reexport) => reexport.exported !== '*')
74
+ .map((reexport) => reexport.exported),
75
+ ]);
76
+ return requestedKeys.every((key) => exportNames.has(key));
77
+ }
78
+ catch {
79
+ return false;
80
+ }
81
+ }
82
+ return requestedKeys.every((key) => Object.prototype.hasOwnProperty.call(prepared.exports, key));
83
+ };
84
+ const isPreparedCacheHit = (prepared, requestedOnly) => isPreparedOnlySuperSet(prepared.only, requestedOnly) &&
85
+ hasPreparedExportKeys(prepared, requestedOnly);
86
+ const isExportContainer = (value) => value !== null && (typeof value === 'object' || typeof value === 'function');
87
+ const hasCachedExport = (source, key) => {
88
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
89
+ return true;
90
+ }
91
+ if (key === 'default') {
92
+ return false;
93
+ }
94
+ const fallback = source.default;
95
+ return (isExportContainer(fallback) &&
96
+ Object.prototype.hasOwnProperty.call(fallback, key));
97
+ };
98
+ const resolveCachedExport = (source, key) => {
99
+ if (key === 'default') {
100
+ return Object.prototype.hasOwnProperty.call(source, 'default')
101
+ ? source.default
102
+ : undefined;
103
+ }
104
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
105
+ return source[key];
106
+ }
107
+ const fallback = source.default;
108
+ if (isExportContainer(fallback) &&
109
+ Object.prototype.hasOwnProperty.call(fallback, key)) {
110
+ return fallback[key];
111
+ }
112
+ return undefined;
113
+ };
114
+ const serializeCachedExports = (exportsValue, requiredOnly) => {
115
+ if (requiredOnly.some(isEvalOnlyKey)) {
116
+ return null;
117
+ }
118
+ const keys = requiredOnly.includes('*')
119
+ ? Object.keys(exportsValue).filter((key) => !isEvalOnlyKey(key))
120
+ : requiredOnly.filter((key) => !isEvalOnlyKey(key));
121
+ if (keys.length === 0) {
122
+ return null;
123
+ }
124
+ const serialized = {};
125
+ for (const key of keys) {
126
+ if (!hasCachedExport(exportsValue, key)) {
127
+ return null;
128
+ }
129
+ try {
130
+ const encoded = serializeValue(resolveCachedExport(exportsValue, key));
131
+ if (encoded.kind === 'function') {
132
+ return null;
133
+ }
134
+ serialized[key] = encoded;
135
+ }
136
+ catch {
137
+ return null;
138
+ }
139
+ }
140
+ return serialized;
141
+ };
142
+ const collectKnownExportNames = (services, id, cachedEntrypoint) => {
143
+ let knownExports = services.cache.get('exports', id);
144
+ if (knownExports || !cachedEntrypoint) {
145
+ return knownExports;
146
+ }
147
+ const { loadedAndParsed } = cachedEntrypoint;
148
+ if (loadedAndParsed?.evaluator !== oxcShaker || !loadedAndParsed.code) {
149
+ return undefined;
150
+ }
151
+ const analyzed = collectOxcExportsAndImports(loadedAndParsed.code, loadedAndParsed.evalConfig?.filename ?? id);
152
+ if (analyzed.reexports.some((reexport) => reexport.exported === '*')) {
153
+ return undefined;
154
+ }
155
+ knownExports = Array.from(new Set([
156
+ ...Object.keys(analyzed.exports),
157
+ ...analyzed.reexports.map((reexport) => reexport.exported),
158
+ ]));
159
+ services.cache.add('exports', id, knownExports);
160
+ return knownExports;
161
+ };
162
+ const getSerializableStaticImportKeys = (services, id, cachedEntrypoint, requiredOnly, request, importerId) => {
163
+ const isStaticImportLoad = Boolean(request && importerId);
164
+ const requestedExports = requiredOnly.includes('*')
165
+ ? null
166
+ : requiredOnly.filter((key) => !isEvalOnlyKey(key) && key !== '*');
167
+ const knownExports = collectKnownExportNames(services, id, cachedEntrypoint)?.filter((key) => !isEvalOnlyKey(key) && key !== '*');
168
+ if (isStaticImportLoad) {
169
+ if (!requestedExports?.length ||
170
+ !knownExports?.length ||
171
+ !isSuperSet(cachedEntrypoint.evaluatedOnly ?? [], knownExports)) {
172
+ return null;
173
+ }
174
+ if (!requestedExports.every((key) => knownExports.includes(key))) {
175
+ return null;
176
+ }
177
+ return isSuperSet(cachedEntrypoint.evaluatedOnly ?? [], requestedExports)
178
+ ? requestedExports
179
+ : null;
180
+ }
181
+ if (knownExports?.length) {
182
+ return isSuperSet(cachedEntrypoint.evaluatedOnly ?? [], knownExports)
183
+ ? knownExports
184
+ : null;
185
+ }
186
+ const evaluatedOnly = cachedEntrypoint.evaluatedOnly ?? requiredOnly;
187
+ return requiredOnly.includes('*') ? evaluatedOnly : requiredOnly;
188
+ };
189
+ const DEFAULT_EVAL_OPTIONS = {
190
+ mode: 'strict',
191
+ require: 'warn-and-run',
192
+ resolver: 'bundler',
193
+ };
194
+ const MAX_MESSAGE_SIZE = 10 * 1024 * 1024;
195
+ const MAX_CHUNK_SIZE = 512 * 1024;
196
+ const RESOLVE_CACHE_SIZE = 5000;
197
+ const LOAD_CACHE_SIZE = 1000;
198
+ const IDENTIFIER_RE = /^[A-Za-z_$][\w$]*$/u;
199
+ const REQUEST_TIMEOUT_MS = 30_000;
200
+ const EVAL_TIMEOUT_MS = Number(process.env.WYW_EVAL_TIMEOUT_MS ?? 300_000);
201
+ const INIT_TIMEOUT_MS = 120_000;
202
+ const HAPPYDOM_INIT_TIMEOUT_MS = Number(process.env.WYW_EVAL_HAPPYDOM_INIT_TIMEOUT_MS ??
203
+ process.env.WYW_HAPPYDOM_TIMEOUT_MS ??
204
+ 15_000);
205
+ // Mirrors runner.js `isFullModuleLoad`: wildcard `['*']` (or empty) is the
206
+ // only shape stored in the runner's moduleCache; everything else lands in
207
+ // moduleVariants. The shipped-code dedup must respect this shape because the
208
+ // runner picks its lookup map based on the LoadResult's `only`.
209
+ const isWildcardOnly = (only) => !only || only.length === 0 || (only.length === 1 && only[0] === '*');
210
+ const isEvalTimeoutError = (error) => {
211
+ if (!error || typeof error !== 'object')
212
+ return false;
213
+ if ('code' in error && error.code) {
214
+ return error.code === 'WYW_EVAL_TIMEOUT';
215
+ }
216
+ return false;
217
+ };
218
+ // ---------------------------------------------------------------------------
219
+ // WYW_DEBUG eval dump
220
+ // ---------------------------------------------------------------------------
221
+ const resolveDebugEvalDir = () => {
222
+ const override = process.env.WYW_DUMP_EVALS_DIR;
223
+ if (override) {
224
+ return path.resolve(override);
225
+ }
226
+ const base = process.env.WYW_DUMP_EVALS;
227
+ if (!base) {
228
+ return undefined;
229
+ }
230
+ const ts = new Date()
231
+ .toISOString()
232
+ .slice(0, 19)
233
+ .replace(/[-:T]/g, (c) => (c === 'T' ? '-' : ''));
234
+ const root = base === '1' || base === 'true' ? './tmp' : base;
235
+ return path.resolve(root, `wyw-dump-evals-${ts}`);
236
+ };
237
+ const debugEvalDir = resolveDebugEvalDir();
238
+ let debugEvalDirReady = false;
239
+ const ensureDebugEvalDir = () => {
240
+ if (!debugEvalDir || debugEvalDirReady) {
241
+ return;
242
+ }
243
+ fs.mkdirSync(debugEvalDir, { recursive: true });
244
+ debugEvalDirReady = true;
245
+ };
246
+ let debugEvalSeq = 0;
247
+ const dumpEvalCode = (id, code, only, source, evalSeq) => {
248
+ if (!debugEvalDir) {
249
+ return;
250
+ }
251
+ ensureDebugEvalDir();
252
+ const seq = String(++debugEvalSeq).padStart(5, '0');
253
+ const eSeq = String(evalSeq).padStart(5, '0');
254
+ const relId = path.relative(process.cwd(), stripQueryAndHash(id));
255
+ const safeName = relId.replace(/[/\\]/g, '__').replace(/^__/, '');
256
+ const filename = `seq${seq}_eval${eSeq}_${safeName}.js`;
257
+ const header = [
258
+ `// id: ${id}`,
259
+ `// only: ${JSON.stringify(only)}`,
260
+ `// source: ${source}`,
261
+ `// seq: ${seq}`,
262
+ `// eval: #${eSeq}`,
263
+ '',
264
+ ].join('\n');
265
+ fs.writeFileSync(path.join(debugEvalDir, filename), header + code);
266
+ };
267
+ let debugActionStream = null;
268
+ const debugAction = (event) => {
269
+ if (!debugEvalDir) {
270
+ return;
271
+ }
272
+ ensureDebugEvalDir();
273
+ if (!debugActionStream) {
274
+ debugActionStream = fs.createWriteStream(path.join(debugEvalDir, 'actions.jsonl'));
275
+ }
276
+ debugActionStream.write(`${JSON.stringify(event)}\n`);
277
+ };
278
+ const flushDebugStreams = () => {
279
+ debugActionStream?.end();
280
+ debugActionStream = null;
281
+ };
282
+ // ---------------------------------------------------------------------------
283
+ const warnedUnknownImportsByServices = new WeakMap();
284
+ const getWarnedUnknownImports = (services) => {
285
+ const cached = warnedUnknownImportsByServices.get(services);
286
+ if (cached)
287
+ return cached;
288
+ const created = new Set();
289
+ warnedUnknownImportsByServices.set(services, created);
290
+ return created;
291
+ };
292
+ const warnedSlowImportsByServices = new WeakMap();
293
+ const getWarnedSlowImports = (services) => {
294
+ const cached = warnedSlowImportsByServices.get(services);
295
+ if (cached)
296
+ return cached;
297
+ const created = new Set();
298
+ warnedSlowImportsByServices.set(services, created);
299
+ return created;
300
+ };
301
+ const isWarningEnabled = (value) => Boolean(value) && value !== '0' && value !== 'false';
302
+ const getSlowImportThresholdMs = () => {
303
+ const raw = process.env.WYW_WARN_SLOW_IMPORTS_MS;
304
+ if (!raw)
305
+ return 50;
306
+ const parsed = Number(raw);
307
+ if (!Number.isFinite(parsed))
308
+ return 50;
309
+ return parsed;
310
+ };
311
+ const getEvalOptions = (services) => ({
312
+ ...DEFAULT_EVAL_OPTIONS,
313
+ ...(services.options.pluginOptions.eval ?? {}),
314
+ });
315
+ const buildRunnerPath = () => {
316
+ const url = new URL('./runner.js', import.meta.url);
317
+ return fileURLToPath(url);
318
+ };
319
+ export const stripEntrypointGlobalsFromRunnerContext = (globals, entrypoint) => {
320
+ const entrypointDir = path.dirname(entrypoint);
321
+ const shouldStripFilename = Object.prototype.hasOwnProperty.call(globals, '__filename') &&
322
+ globals.__filename === entrypoint;
323
+ const shouldStripDirname = Object.prototype.hasOwnProperty.call(globals, '__dirname') &&
324
+ globals.__dirname === entrypointDir;
325
+ if (!shouldStripFilename && !shouldStripDirname) {
326
+ return globals;
327
+ }
328
+ const nextGlobals = { ...globals };
329
+ if (shouldStripFilename) {
330
+ delete nextGlobals.__filename;
331
+ }
332
+ if (shouldStripDirname) {
333
+ delete nextGlobals.__dirname;
334
+ }
335
+ return nextGlobals;
336
+ };
337
+ const getEntrypointResolveRoot = (entrypoint) => {
338
+ let current = entrypoint;
339
+ const seen = new Set();
340
+ while (current.parents.length > 0 && !seen.has(current.name)) {
341
+ seen.add(current.name);
342
+ [current] = current.parents;
343
+ }
344
+ return current.name;
345
+ };
346
+ const buildRunnerInitPayload = (services, entrypoint, featuresOverride) => {
347
+ const evalOptions = getEvalOptions(services);
348
+ const { pluginOptions } = services.options;
349
+ const root = services.options.root ?? process.cwd();
350
+ const { overrideContext, importOverrides, extensions } = pluginOptions;
351
+ const features = featuresOverride ?? pluginOptions.features;
352
+ const baseGlobals = {
353
+ ...(evalOptions.globals ?? {}),
354
+ };
355
+ const withFilename = {
356
+ ...baseGlobals,
357
+ __filename: entrypoint.name,
358
+ __dirname: path.dirname(entrypoint.name),
359
+ };
360
+ const globals = overrideContext
361
+ ? overrideContext(withFilename, entrypoint.name)
362
+ : baseGlobals;
363
+ const sanitizedGlobals = stripEntrypointGlobalsFromRunnerContext(globals, entrypoint.name);
364
+ return {
365
+ evalOptions: {
366
+ globals: encodeGlobalsCached(sanitizedGlobals),
367
+ importOverrides,
368
+ mode: evalOptions.mode ?? 'strict',
369
+ require: evalOptions.require ?? 'warn-and-run',
370
+ root,
371
+ extensions,
372
+ },
373
+ features,
374
+ entrypoint: entrypoint.name,
375
+ };
376
+ };
377
+ const emitWarning = (services, message) => {
378
+ if (services.emitWarning) {
379
+ services.emitWarning(message);
380
+ return;
381
+ }
382
+ // eslint-disable-next-line no-console
383
+ console.warn(message);
384
+ };
385
+ const emitEvalWarning = (services, warning) => {
386
+ const { onWarn } = getEvalOptions(services);
387
+ onWarn?.(warning);
388
+ emitWarning(services, warning.message);
389
+ };
390
+ const defaultImportLoaders = {
391
+ raw: 'raw',
392
+ url: 'url',
393
+ };
394
+ const loadByImportLoaders = (services, request, resolved, importer) => {
395
+ const { pluginOptions } = services.options;
396
+ const importLoaders = pluginOptions.importLoaders === undefined
397
+ ? defaultImportLoaders
398
+ : { ...defaultImportLoaders, ...pluginOptions.importLoaders };
399
+ const { query, hash } = parseRequest(request);
400
+ if (!query)
401
+ return { handled: false, value: undefined };
402
+ const params = new URLSearchParams(query);
403
+ const matchedKey = Array.from(params.keys()).find((key) => importLoaders[key] !== undefined && importLoaders[key] !== false);
404
+ if (!matchedKey)
405
+ return { handled: false, value: undefined };
406
+ const loader = importLoaders[matchedKey];
407
+ const filename = stripQueryAndHash(resolved);
408
+ const importerFilename = stripQueryAndHash(importer);
409
+ const importerDir = path.dirname(importerFilename);
410
+ const toUrl = () => {
411
+ const relative = path
412
+ .relative(importerDir, filename)
413
+ .replace(/\\/g, path.posix.sep);
414
+ if (relative.startsWith('.') || path.isAbsolute(relative)) {
415
+ return relative;
416
+ }
417
+ return `./${relative}`;
418
+ };
419
+ const readFile = () => fs.readFileSync(filename, 'utf-8');
420
+ const context = {
421
+ importer: importerFilename,
422
+ request,
423
+ resolved,
424
+ filename,
425
+ query,
426
+ hash,
427
+ emitWarning: (message) => emitWarning(services, message),
428
+ readFile,
429
+ toUrl,
430
+ };
431
+ if (loader === 'raw') {
432
+ return { handled: true, value: context.readFile() };
433
+ }
434
+ if (loader === 'url') {
435
+ return { handled: true, value: context.toUrl() };
436
+ }
437
+ if (typeof loader === 'function') {
438
+ return { handled: true, value: loader(context) };
439
+ }
440
+ return { handled: false, value: undefined };
441
+ };
442
+ const hashContent = (content) => createHash('sha256').update(content).digest('hex');
443
+ const isTypeOnlyImport = (statement) => {
444
+ if (statement.type !== 'ImportDeclaration') {
445
+ return false;
446
+ }
447
+ if (statement.importKind === 'type') {
448
+ return true;
449
+ }
450
+ if (statement.specifiers.length === 0) {
451
+ return false;
452
+ }
453
+ return statement.specifiers.every((specifier) => specifier.type === 'ImportSpecifier' && specifier.importKind === 'type');
454
+ };
455
+ const isTypeOnlyExport = (statement) => statement.exportKind === 'type';
456
+ const getModuleExportName = (node) => node.type === 'Identifier' ? node.name : node.value;
457
+ const getImportSpecifierName = (specifier) => getModuleExportName(specifier.imported);
458
+ const buildDirectBarrelProxy = (services, id, only) => {
459
+ const requested = only.filter((key) => !isEvalOnlyKey(key));
460
+ if (requested.length === 0 || requested.includes('*')) {
461
+ return null;
462
+ }
463
+ const loadedAndParsed = services.loadAndParseFn(services, id, undefined, services.log);
464
+ if (loadedAndParsed.evaluator === 'ignored' ||
465
+ loadedAndParsed.ast === undefined) {
466
+ return null;
467
+ }
468
+ if (loadedAndParsed.evaluator === oxcShaker) {
469
+ return buildDirectOxcBarrelProxy(id, loadedAndParsed.code, only);
470
+ }
471
+ const importedBindings = new Map();
472
+ const exportedBindings = new Map();
473
+ const ast = loadedAndParsed.ast;
474
+ for (const statement of ast.program.body) {
475
+ if (statement.type === 'ImportDeclaration') {
476
+ if (isTypeOnlyImport(statement)) {
477
+ continue;
478
+ }
479
+ if (statement.specifiers.length === 0) {
480
+ return null;
481
+ }
482
+ for (const specifier of statement.specifiers) {
483
+ if (specifier.type === 'ImportSpecifier' &&
484
+ specifier.importKind === 'type') {
485
+ continue;
486
+ }
487
+ if (specifier.type === 'ImportSpecifier') {
488
+ importedBindings.set(specifier.local.name, {
489
+ kind: 'named',
490
+ imported: getImportSpecifierName(specifier),
491
+ source: statement.source.value,
492
+ });
493
+ continue;
494
+ }
495
+ if (specifier.type === 'ImportDefaultSpecifier') {
496
+ importedBindings.set(specifier.local.name, {
497
+ kind: 'named',
498
+ imported: 'default',
499
+ source: statement.source.value,
500
+ });
501
+ continue;
502
+ }
503
+ importedBindings.set(specifier.local.name, {
504
+ kind: 'namespace',
505
+ source: statement.source.value,
506
+ });
507
+ }
508
+ continue;
509
+ }
510
+ if (statement.type === 'ExportNamedDeclaration') {
511
+ if (isTypeOnlyExport(statement)) {
512
+ continue;
513
+ }
514
+ if (statement.source) {
515
+ for (const specifier of statement.specifiers) {
516
+ if (specifier.type === 'ExportSpecifier') {
517
+ if (specifier.exportKind === 'type') {
518
+ continue;
519
+ }
520
+ exportedBindings.set(getModuleExportName(specifier.exported), {
521
+ kind: 'named',
522
+ imported: getModuleExportName(specifier.local),
523
+ source: statement.source.value,
524
+ });
525
+ continue;
526
+ }
527
+ if (specifier.type === 'ExportDefaultSpecifier') {
528
+ exportedBindings.set(getModuleExportName(specifier.exported), {
529
+ kind: 'named',
530
+ imported: 'default',
531
+ source: statement.source.value,
532
+ });
533
+ continue;
534
+ }
535
+ if (specifier.type === 'ExportNamespaceSpecifier') {
536
+ exportedBindings.set(getModuleExportName(specifier.exported), {
537
+ kind: 'namespace',
538
+ source: statement.source.value,
539
+ });
540
+ continue;
541
+ }
542
+ return null;
543
+ }
544
+ continue;
545
+ }
546
+ if (statement.declaration) {
547
+ return null;
548
+ }
549
+ for (const specifier of statement.specifiers) {
550
+ if (specifier.type !== 'ExportSpecifier' ||
551
+ specifier.exportKind === 'type') {
552
+ return null;
553
+ }
554
+ if (specifier.local.type !== 'Identifier') {
555
+ return null;
556
+ }
557
+ const binding = importedBindings.get(specifier.local.name);
558
+ if (!binding) {
559
+ return null;
560
+ }
561
+ exportedBindings.set(getModuleExportName(specifier.exported), binding);
562
+ }
563
+ continue;
564
+ }
565
+ if (statement.type === 'ExportDefaultDeclaration') {
566
+ if (statement.declaration.type !== 'Identifier') {
567
+ return null;
568
+ }
569
+ const binding = importedBindings.get(statement.declaration.name);
570
+ if (!binding || binding.kind !== 'named') {
571
+ return null;
572
+ }
573
+ exportedBindings.set('default', binding);
574
+ continue;
575
+ }
576
+ if (statement.type === 'EmptyStatement' ||
577
+ statement.type === 'TSDeclareFunction' ||
578
+ statement.type === 'TSInterfaceDeclaration' ||
579
+ statement.type === 'TSTypeAliasDeclaration') {
580
+ continue;
581
+ }
582
+ return null;
583
+ }
584
+ const imports = new Map();
585
+ const lines = [];
586
+ let namespaceIdx = 0;
587
+ const addImport = (source, imported) => {
588
+ if (!imports.has(source)) {
589
+ imports.set(source, []);
590
+ }
591
+ const bucket = imports.get(source);
592
+ if (!bucket.includes(imported)) {
593
+ bucket.push(imported);
594
+ }
595
+ };
596
+ for (const exported of requested) {
597
+ const binding = exportedBindings.get(exported);
598
+ if (!binding) {
599
+ return null;
600
+ }
601
+ if (binding.kind === 'namespace') {
602
+ if (exported === 'default' || !IDENTIFIER_RE.test(exported)) {
603
+ return null;
604
+ }
605
+ const local = `__wyw_ns_${namespaceIdx++}`;
606
+ lines.push(`import * as ${local} from ${JSON.stringify(binding.source)};`);
607
+ lines.push(`export { ${local} as ${exported} };`);
608
+ addImport(binding.source, '*');
609
+ continue;
610
+ }
611
+ if (binding.imported !== 'default' &&
612
+ !IDENTIFIER_RE.test(binding.imported)) {
613
+ return null;
614
+ }
615
+ if (exported !== 'default' && !IDENTIFIER_RE.test(exported)) {
616
+ return null;
617
+ }
618
+ const imported = binding.imported === 'default' ? 'default' : binding.imported;
619
+ const exportClause = exported === 'default'
620
+ ? `${imported} as default`
621
+ : imported === exported
622
+ ? imported
623
+ : `${imported} as ${exported}`;
624
+ lines.push(`export { ${exportClause} } from ${JSON.stringify(binding.source)};`);
625
+ addImport(binding.source, binding.imported);
626
+ }
627
+ if (lines.length === 0) {
628
+ return null;
629
+ }
630
+ return {
631
+ code: `${lines.join('\n')}\n`,
632
+ imports,
633
+ only,
634
+ };
635
+ };
636
+ const buildDirectOxcBarrelProxy = (id, code, only) => {
637
+ const requested = only.filter((key) => !isEvalOnlyKey(key));
638
+ const analyzed = analyzeOxcBarrelFile(code, id);
639
+ if (!('reexports' in analyzed)) {
640
+ return null;
641
+ }
642
+ const imports = new Map();
643
+ const lines = [];
644
+ let namespaceIdx = 0;
645
+ const addImport = (source, imported) => {
646
+ if (!imports.has(source)) {
647
+ imports.set(source, []);
648
+ }
649
+ const bucket = imports.get(source);
650
+ if (!bucket.includes(imported)) {
651
+ bucket.push(imported);
652
+ }
653
+ };
654
+ for (const exported of requested) {
655
+ const binding = analyzed.reexports.find((reexport) => reexport.exported === exported);
656
+ if (!binding) {
657
+ return null;
658
+ }
659
+ if (binding.kind === 'namespace') {
660
+ if (exported === 'default' || !IDENTIFIER_RE.test(exported)) {
661
+ return null;
662
+ }
663
+ const local = `__wyw_ns_${namespaceIdx++}`;
664
+ lines.push(`import * as ${local} from ${JSON.stringify(binding.source)};`);
665
+ lines.push(`export { ${local} as ${exported} };`);
666
+ addImport(binding.source, '*');
667
+ continue;
668
+ }
669
+ if (binding.imported !== 'default' &&
670
+ !IDENTIFIER_RE.test(binding.imported)) {
671
+ return null;
672
+ }
673
+ if (exported !== 'default' && !IDENTIFIER_RE.test(exported)) {
674
+ return null;
675
+ }
676
+ const imported = binding.imported === 'default' ? 'default' : binding.imported;
677
+ const exportClause = exported === 'default'
678
+ ? `${imported} as default`
679
+ : imported === exported
680
+ ? imported
681
+ : `${imported} as ${exported}`;
682
+ lines.push(`export { ${exportClause} } from ${JSON.stringify(binding.source)};`);
683
+ addImport(binding.source, binding.imported);
684
+ }
685
+ if (lines.length === 0) {
686
+ return null;
687
+ }
688
+ return {
689
+ code: `${lines.join('\n')}\n`,
690
+ imports,
691
+ only,
692
+ };
693
+ };
694
+ const isPlainObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
695
+ const canonicalizeForHash = (value) => {
696
+ if (Array.isArray(value)) {
697
+ return value.map((item) => canonicalizeForHash(item));
698
+ }
699
+ if (isPlainObject(value)) {
700
+ return Object.fromEntries(Object.keys(value)
701
+ .sort()
702
+ .map((key) => [key, canonicalizeForHash(value[key])]));
703
+ }
704
+ return value;
705
+ };
706
+ const getInitPayloadKey = (payload) => createHash('sha256')
707
+ .update(JSON.stringify(canonicalizeForHash(payload)))
708
+ .digest('hex');
709
+ // Hash everything in the init payload that affects whether the runner needs
710
+ // a fresh INIT — i.e. everything except `entrypoint` (which only affects
711
+ // __filename/__dirname rebinding, not context reuse). The broker memoizes
712
+ // this per-services so we replace per-evaluate SHA-256 of the full payload
713
+ // with one SHA-256 of the stable bits + a cheap string concat per
714
+ // entrypoint.
715
+ const getStableInitPayloadHash = (payload) => {
716
+ const { entrypoint: _entrypoint, ...stable } = payload;
717
+ return createHash('sha256')
718
+ .update(JSON.stringify(canonicalizeForHash(stable)))
719
+ .digest('hex');
720
+ };
721
+ // Memoize encodeGlobals on input reference. The user's globals object is
722
+ // stable across a build, so we can encode it once instead of per evaluate.
723
+ // If the input ref changes, fall through to a fresh encode (and reset the
724
+ // cache).
725
+ const encodeGlobalsMemo = new WeakMap();
726
+ const encodeGlobalsCached = (input) => {
727
+ if (input !== null && typeof input === 'object') {
728
+ const obj = input;
729
+ const cached = encodeGlobalsMemo.get(obj);
730
+ if (cached)
731
+ return cached;
732
+ const encoded = encodeGlobals(input);
733
+ encodeGlobalsMemo.set(obj, encoded);
734
+ return encoded;
735
+ }
736
+ return encodeGlobals(input);
737
+ };
738
+ const formatLoaderResult = (code, loader) => {
739
+ if (loader === 'json') {
740
+ return `export default ${JSON.stringify(JSON.parse(code))};`;
741
+ }
742
+ if (loader === 'raw' || loader === 'text') {
743
+ return `export default ${JSON.stringify(code)};`;
744
+ }
745
+ return code;
746
+ };
747
+ const toSerializedError = (error) => {
748
+ const err = error instanceof Error ? error : new Error(String(error));
749
+ return {
750
+ message: err.message,
751
+ name: err.name,
752
+ stack: err.stack,
753
+ };
754
+ };
755
+ export class EvalBroker {
756
+ services;
757
+ asyncResolve;
758
+ runner = null;
759
+ runnerInputQueue = null;
760
+ runnerReady = null;
761
+ lastInitKey = null;
762
+ lastHappyDomEnabled = false;
763
+ evalQueue = Promise.resolve();
764
+ pending = new Map();
765
+ nextId = 0;
766
+ resolveCache = new LruCache(RESOLVE_CACHE_SIZE);
767
+ resolveInFlight = new Map();
768
+ loadCache = new LruCache(LOAD_CACHE_SIZE);
769
+ loadInFlight = new Map();
770
+ importsByModule = new Map();
771
+ onlyByModule = new Map();
772
+ runtimeDependenciesByModule = new Map();
773
+ emittedDependencies = new Set();
774
+ // Mirrors the runner's view: for each module id, the (hash, mergedOnly) of
775
+ // the most recent LoadResult we shipped with non-empty `code`. Subsequent
776
+ // loads with a matching hash and a subset `only` skip the code transmission
777
+ // (and the eval dump) — the runner's hash-match branch returns its cached
778
+ // SourceTextModule. Cleared whenever the runner is killed/respawned so the
779
+ // mirror cannot drift from the runner's actual moduleCache.
780
+ lastSentLoadByModule = new Map();
781
+ // Batch queue: concurrent evaluate() callers (e.g. parallel webpack-loader
782
+ // transform() invocations) pile up here within one event-loop turn, then a
783
+ // microtask flushes them as a single sequential runner pass. Each call
784
+ // still gets its own resolved Promise; this only collapses the per-call
785
+ // evalQueue chain + state-clear churn.
786
+ pendingEvals = [];
787
+ evalFlushScheduled = false;
788
+ // Cached stable init payload hash. Keyed on the refs that feed the stable
789
+ // bits (pluginOptions.eval and pluginOptions itself). Any reference change
790
+ // invalidates the cache. The full per-entrypoint init key is
791
+ // `${stableHash}::${entrypoint.name}` — cheap string concat instead of
792
+ // re-canonicalizing+stringifying+SHA-256ing the whole payload per call.
793
+ stableInitHashCache = null;
794
+ evalSeq = 0;
795
+ happyDomDisabled = false;
796
+ happyDomDisableWarned = false;
797
+ activeResolveRootId = null;
798
+ currentServices;
799
+ constructor(services, asyncResolve) {
800
+ this.services = services;
801
+ this.asyncResolve = asyncResolve;
802
+ this.currentServices = services;
803
+ }
804
+ ensureImportsMapping(id, imports) {
805
+ if (!imports || imports.size === 0) {
806
+ if (!this.importsByModule.has(id)) {
807
+ this.importsByModule.set(id, new Map());
808
+ }
809
+ return;
810
+ }
811
+ const existing = this.importsByModule.get(id);
812
+ if (!existing || existing.size === 0) {
813
+ this.importsByModule.set(id, imports);
814
+ return;
815
+ }
816
+ // Merge: widen each specifier's import list rather than replacing.
817
+ // Different variants of the same module may import different subsets
818
+ // from the same dependency. The widest set must be preserved so that
819
+ // any still-linking variant can resolve all its bindings.
820
+ for (const [specifier, keys] of imports) {
821
+ const existingKeys = existing.get(specifier);
822
+ if (!existingKeys) {
823
+ existing.set(specifier, keys);
824
+ }
825
+ else {
826
+ existing.set(specifier, mergeOnly(existingKeys, keys));
827
+ }
828
+ }
829
+ }
830
+ getImportOnly(importerId, specifier) {
831
+ const importsOnly = importerId
832
+ ? this.importsByModule.get(importerId)?.get(specifier)
833
+ : undefined;
834
+ const importerOnly = importerId
835
+ ? this.onlyByModule.get(importerId) ?? ['*']
836
+ : ['*'];
837
+ return importerOnly.includes('__wywPreval')
838
+ ? mergeOnly(importsOnly ?? ['*'], ['__wywPreval'])
839
+ : importsOnly ?? ['*'];
840
+ }
841
+ getLoadRequestOnly(id, importerId, request) {
842
+ if (!request || !importerId || importerId === id) {
843
+ return null;
844
+ }
845
+ const imports = this.importsByModule.get(importerId);
846
+ if (!imports?.has(request)) {
847
+ return null;
848
+ }
849
+ const { root } = this.services.options;
850
+ const keyInfo = toImportKey({
851
+ source: request,
852
+ resolved: id,
853
+ root,
854
+ });
855
+ const override = getImportOverride(this.services.options.pluginOptions.importOverrides, keyInfo.key);
856
+ let nextOnly = applyImportOverrideToOnly(this.getImportOnly(importerId, request), override);
857
+ const cached = this.services.cache.get('entrypoints', id);
858
+ if (nextOnly.includes('__wywPreval') &&
859
+ cached?.evaluated &&
860
+ !cached.ignored &&
861
+ !hasCachedWywPrevalExport(this.services, id, cached)) {
862
+ nextOnly = nextOnly.filter((item) => item !== '__wywPreval');
863
+ }
864
+ return nextOnly;
865
+ }
866
+ async evaluate(entrypoint, services) {
867
+ return new Promise((resolve, reject) => {
868
+ this.pendingEvals.push({ entrypoint, services, resolve, reject });
869
+ this.scheduleEvalFlush();
870
+ });
871
+ }
872
+ scheduleEvalFlush() {
873
+ if (this.evalFlushScheduled)
874
+ return;
875
+ this.evalFlushScheduled = true;
876
+ queueMicrotask(() => {
877
+ this.evalFlushScheduled = false;
878
+ if (this.pendingEvals.length === 0)
879
+ return;
880
+ const batch = this.pendingEvals;
881
+ this.pendingEvals = [];
882
+ this.evalQueue = this.evalQueue.then(() => this.runEvalBatch(batch));
883
+ });
884
+ }
885
+ async runEvalBatch(batch) {
886
+ try {
887
+ await this.ensureRunner();
888
+ }
889
+ catch (error) {
890
+ for (const member of batch)
891
+ member.reject(error);
892
+ return;
893
+ }
894
+ for (const member of batch) {
895
+ try {
896
+ const result = await this.runOneEntrypoint(member.entrypoint, member.services);
897
+ member.resolve(result);
898
+ }
899
+ catch (error) {
900
+ member.reject(error);
901
+ }
902
+ }
903
+ }
904
+ async runOneEntrypoint(entrypoint, services) {
905
+ const activeServices = services ?? this.currentServices;
906
+ const resolveRootId = getEntrypointResolveRoot(entrypoint);
907
+ this.currentServices = activeServices;
908
+ this.activeResolveRootId = resolveRootId;
909
+ this.resetPerEntrypointState(entrypoint);
910
+ this.evalSeq += 1;
911
+ if (debugEvalDir) {
912
+ debugAction({
913
+ type: 'eval:start',
914
+ evalSeq: this.evalSeq,
915
+ entrypoint: entrypoint.name,
916
+ ts: performance.now(),
917
+ });
918
+ }
919
+ try {
920
+ await this.initRunner(entrypoint);
921
+ const payload = await this.request('EVAL', { id: entrypoint.name }, EVAL_TIMEOUT_MS);
922
+ if (debugEvalDir) {
923
+ debugAction({
924
+ type: 'eval:finish',
925
+ evalSeq: this.evalSeq,
926
+ entrypoint: entrypoint.name,
927
+ hasValues: Boolean(payload.values),
928
+ ts: performance.now(),
929
+ });
930
+ }
931
+ if (payload.modules) {
932
+ this.applyModuleExports(payload.modules);
933
+ }
934
+ if (!payload.values) {
935
+ return { values: null, dependencies: [] };
936
+ }
937
+ const values = new Map();
938
+ Object.entries(payload.values).forEach(([key, serialized]) => {
939
+ values.set(key, deserializeValue(serialized));
940
+ });
941
+ return {
942
+ values,
943
+ dependencies: this.collectEntrypointDependencies(entrypoint.name),
944
+ };
945
+ }
946
+ finally {
947
+ if (this.activeResolveRootId === resolveRootId) {
948
+ this.activeResolveRootId = null;
949
+ }
950
+ }
951
+ }
952
+ resetPerEntrypointState(entrypoint) {
953
+ this.runtimeDependenciesByModule.clear();
954
+ this.emittedDependencies.clear();
955
+ this.importsByModule.clear();
956
+ this.onlyByModule.clear();
957
+ this.resolveCache.clear();
958
+ this.resolveInFlight.clear();
959
+ this.onlyByModule.set(entrypoint.name, ['__wywPreval']);
960
+ }
961
+ applyModuleExports(modules) {
962
+ Object.entries(modules).forEach(([id, serializedExports]) => {
963
+ if (!serializedExports || Object.keys(serializedExports).length === 0) {
964
+ return;
965
+ }
966
+ const cached = this.services.cache.get('entrypoints', id);
967
+ if (!cached || cached.ignored) {
968
+ return;
969
+ }
970
+ const existingEvaluatedOnly = cached.evaluatedOnly ?? [];
971
+ const target = cached.evaluated || !('createEvaluated' in cached)
972
+ ? cached
973
+ : cached.createEvaluated();
974
+ const exportsProxy = target.exports;
975
+ Object.entries(serializedExports).forEach(([key, serialized]) => {
976
+ exportsProxy[key] = deserializeValue(serialized);
977
+ });
978
+ const knownExports = collectKnownExportNames(this.services, id, target);
979
+ const serializedKeys = Object.keys(serializedExports);
980
+ const coversAllKnownExports = Array.isArray(knownExports) &&
981
+ knownExports.filter((key) => !isEvalOnlyKey(key)).length > 0 &&
982
+ knownExports
983
+ .filter((key) => !isEvalOnlyKey(key))
984
+ .every((key) => serializedKeys.includes(key));
985
+ const coversModule = coversAllKnownExports;
986
+ const merged = mergeOnly(existingEvaluatedOnly, coversModule ? ['*'] : serializedKeys);
987
+ if (target.evaluatedOnly) {
988
+ target.evaluatedOnly.splice(0, target.evaluatedOnly.length, ...merged);
989
+ }
990
+ this.services.cache.add('entrypoints', id, target);
991
+ });
992
+ }
993
+ dispose() {
994
+ if (this.runner) {
995
+ this.runner.removeAllListeners();
996
+ this.runner.kill();
997
+ this.runner = null;
998
+ this.runnerReady = null;
999
+ this.runnerInputQueue = null;
1000
+ }
1001
+ this.lastInitKey = null;
1002
+ this.lastHappyDomEnabled = false;
1003
+ this.lastSentLoadByModule.clear();
1004
+ this.stableInitHashCache = null;
1005
+ flushDebugStreams();
1006
+ }
1007
+ createRunnerProcess() {
1008
+ const runnerPath = buildRunnerPath();
1009
+ const nodeBinary = process.env.WYW_NODE_BINARY ||
1010
+ (process.execPath.includes('bun') ? 'node' : process.execPath);
1011
+ const runner = spawn(nodeBinary, ['--experimental-vm-modules', runnerPath], {
1012
+ stdio: ['pipe', 'pipe', 'pipe'],
1013
+ cwd: this.services.options.root ?? process.cwd(),
1014
+ env: {
1015
+ ...process.env,
1016
+ WYW_EVAL_RUNNER: '1',
1017
+ NODE_NO_WARNINGS: '1',
1018
+ },
1019
+ });
1020
+ runner.stdout.setEncoding('utf8');
1021
+ return runner;
1022
+ }
1023
+ attachRunnerListeners(runner) {
1024
+ runner.stdout.on('data', (chunk) => this.onData(String(chunk)));
1025
+ runner.stderr.on('data', (chunk) => {
1026
+ this.handleRunnerStderr(chunk);
1027
+ });
1028
+ runner.on('exit', (code, signal) => {
1029
+ if (this.runner !== runner) {
1030
+ return;
1031
+ }
1032
+ const reason = `Eval runner exited (${code ?? 'null'} / ${signal ?? 'null'})`;
1033
+ this.rejectAllPending(new Error(reason));
1034
+ this.runner = null;
1035
+ this.runnerInputQueue = null;
1036
+ this.runnerReady = null;
1037
+ this.lastInitKey = null;
1038
+ this.lastHappyDomEnabled = false;
1039
+ this.lastSentLoadByModule.clear();
1040
+ });
1041
+ }
1042
+ async ensureRunner() {
1043
+ if (this.runnerReady) {
1044
+ await this.runnerReady;
1045
+ return;
1046
+ }
1047
+ this.runner = this.createRunnerProcess();
1048
+ this.runnerInputQueue = createWriteQueue(this.runner.stdin, 'eval runner stdin');
1049
+ this.attachRunnerListeners(this.runner);
1050
+ this.runnerReady = Promise.resolve();
1051
+ await this.runnerReady;
1052
+ }
1053
+ async initIsolatedRunner(payload, timeoutMs) {
1054
+ const runner = this.createRunnerProcess();
1055
+ const requestId = `candidate-init-${++this.nextId}`;
1056
+ let buffer = '';
1057
+ return new Promise((resolve, reject) => {
1058
+ let settled = false;
1059
+ const cleanup = () => {
1060
+ runner.stdout.off('data', onStdout);
1061
+ runner.stderr.off('data', onStderr);
1062
+ runner.off('exit', onExit);
1063
+ };
1064
+ const finalizeResolve = (value) => {
1065
+ if (settled) {
1066
+ return;
1067
+ }
1068
+ settled = true;
1069
+ clearTimeout(timeout);
1070
+ cleanup();
1071
+ resolve(value);
1072
+ };
1073
+ const finalizeReject = (value) => {
1074
+ if (settled) {
1075
+ return;
1076
+ }
1077
+ settled = true;
1078
+ clearTimeout(timeout);
1079
+ cleanup();
1080
+ reject(value);
1081
+ };
1082
+ const onStderr = (chunk) => {
1083
+ this.handleRunnerStderr(chunk);
1084
+ };
1085
+ const onExit = (code, signal) => {
1086
+ finalizeReject(new Error(`Eval runner exited during init (${code ?? 'null'} / ${signal ?? 'null'})`));
1087
+ };
1088
+ const onStdout = (chunk) => {
1089
+ const next = `${buffer}${chunk.toString()}`;
1090
+ const lines = next.split('\n');
1091
+ buffer = lines.pop() ?? '';
1092
+ lines.forEach((line) => {
1093
+ if (!line.trim())
1094
+ return;
1095
+ let message;
1096
+ try {
1097
+ message = JSON.parse(line);
1098
+ }
1099
+ catch {
1100
+ emitWarning(this.services, `[wyw-eval-runner] Failed to parse message: ${line}`);
1101
+ return;
1102
+ }
1103
+ if (message.type === 'WARN') {
1104
+ this.handleWarn(message.payload);
1105
+ return;
1106
+ }
1107
+ if (message.type !== 'INIT_ACK' || message.id !== requestId) {
1108
+ return;
1109
+ }
1110
+ if (message.error) {
1111
+ runner.kill();
1112
+ finalizeReject(message.error);
1113
+ return;
1114
+ }
1115
+ finalizeResolve(runner);
1116
+ });
1117
+ };
1118
+ const timeout = setTimeout(() => {
1119
+ const error = new Error(`[wyw-in-js] Eval runner timed out for INIT`);
1120
+ error.code = 'WYW_EVAL_TIMEOUT';
1121
+ runner.kill();
1122
+ finalizeReject(error);
1123
+ }, timeoutMs);
1124
+ runner.stdout.on('data', onStdout);
1125
+ runner.stderr.on('data', onStderr);
1126
+ runner.on('exit', onExit);
1127
+ const message = {
1128
+ type: 'INIT',
1129
+ id: requestId,
1130
+ payload,
1131
+ };
1132
+ writeToStream(runner.stdin, `${JSON.stringify(message)}\n`, 'eval runner stdin').catch((error) => {
1133
+ runner.kill();
1134
+ finalizeReject(error instanceof Error ? error : new Error(String(error)));
1135
+ });
1136
+ });
1137
+ }
1138
+ replaceRunner(nextRunner) {
1139
+ if (this.runner) {
1140
+ this.runner.removeAllListeners();
1141
+ this.runner.kill();
1142
+ }
1143
+ this.runner = nextRunner;
1144
+ this.runnerInputQueue = createWriteQueue(nextRunner.stdin, 'eval runner stdin');
1145
+ this.attachRunnerListeners(nextRunner);
1146
+ this.runnerReady = Promise.resolve();
1147
+ // New process ⇒ runner's moduleCache/moduleHashes are empty, so our mirror
1148
+ // of "what we already shipped" is stale.
1149
+ this.lastSentLoadByModule.clear();
1150
+ }
1151
+ getStableInitHash(services, features) {
1152
+ const pluginOptionsRef = services.options.pluginOptions;
1153
+ const evalOptionsRef = pluginOptionsRef.eval;
1154
+ const rootRef = services.options.root;
1155
+ if (this.stableInitHashCache !== null &&
1156
+ this.stableInitHashCache.pluginOptionsRef === pluginOptionsRef &&
1157
+ this.stableInitHashCache.evalOptionsRef === evalOptionsRef &&
1158
+ this.stableInitHashCache.featuresRef === features &&
1159
+ this.stableInitHashCache.rootRef === rootRef) {
1160
+ return this.stableInitHashCache.hash;
1161
+ }
1162
+ // Build a sample payload (entrypoint name doesn't affect stable hash; we
1163
+ // pass any name and strip it inside getStableInitPayloadHash).
1164
+ // encodeGlobals is memoized so this is the only place it actually runs
1165
+ // per config change.
1166
+ const samplePayload = buildRunnerInitPayload(services, { name: '\0stable-init-sample\0' }, features);
1167
+ samplePayload.reuseModules = true;
1168
+ const hash = getStableInitPayloadHash(samplePayload);
1169
+ this.stableInitHashCache = {
1170
+ pluginOptionsRef,
1171
+ evalOptionsRef,
1172
+ featuresRef: features,
1173
+ rootRef,
1174
+ hash,
1175
+ };
1176
+ return hash;
1177
+ }
1178
+ async initRunner(entrypoint) {
1179
+ const features = this.getRunnerFeatures();
1180
+ const stableHash = this.getStableInitHash(this.currentServices, features);
1181
+ const initKey = `${stableHash}::${entrypoint.name}`;
1182
+ if (this.lastInitKey === initKey) {
1183
+ return;
1184
+ }
1185
+ const nextHappyDomEnabled = isFeatureEnabled(features, 'happyDOM', entrypoint.name);
1186
+ const payload = buildRunnerInitPayload(this.services, entrypoint, features);
1187
+ payload.reuseModules = true;
1188
+ const timeoutMs = this.getInitTimeoutMs(entrypoint, features);
1189
+ if (this.runner &&
1190
+ this.lastInitKey !== null &&
1191
+ nextHappyDomEnabled &&
1192
+ !this.lastHappyDomEnabled &&
1193
+ !this.happyDomDisabled) {
1194
+ try {
1195
+ const nextRunner = await this.initIsolatedRunner(payload, timeoutMs);
1196
+ this.replaceRunner(nextRunner);
1197
+ this.lastInitKey = initKey;
1198
+ this.lastHappyDomEnabled = true;
1199
+ return;
1200
+ }
1201
+ catch (error) {
1202
+ if (isEvalTimeoutError(error)) {
1203
+ this.happyDomDisabled = true;
1204
+ this.warnHappyDomDisabledOnce(timeoutMs);
1205
+ const fallbackFeatures = this.getRunnerFeatures();
1206
+ const fallbackPayload = buildRunnerInitPayload(this.services, entrypoint, fallbackFeatures);
1207
+ fallbackPayload.reuseModules = true;
1208
+ await this.request('INIT', fallbackPayload, INIT_TIMEOUT_MS);
1209
+ this.lastInitKey = `${this.getStableInitHash(this.currentServices, fallbackFeatures)}::${entrypoint.name}`;
1210
+ this.lastHappyDomEnabled = false;
1211
+ return;
1212
+ }
1213
+ throw error;
1214
+ }
1215
+ }
1216
+ try {
1217
+ await this.request('INIT', payload, timeoutMs);
1218
+ this.lastInitKey = initKey;
1219
+ this.lastHappyDomEnabled = nextHappyDomEnabled;
1220
+ }
1221
+ catch (error) {
1222
+ if (isEvalTimeoutError(error) &&
1223
+ !this.happyDomDisabled &&
1224
+ isFeatureEnabled(features, 'happyDOM', entrypoint.name)) {
1225
+ this.happyDomDisabled = true;
1226
+ this.warnHappyDomDisabledOnce(timeoutMs);
1227
+ this.dispose();
1228
+ await this.ensureRunner();
1229
+ const fallbackFeatures = this.getRunnerFeatures();
1230
+ const fallbackPayload = buildRunnerInitPayload(this.services, entrypoint, fallbackFeatures);
1231
+ fallbackPayload.reuseModules = true;
1232
+ await this.request('INIT', fallbackPayload, INIT_TIMEOUT_MS);
1233
+ this.lastInitKey = getInitPayloadKey(fallbackPayload);
1234
+ this.lastHappyDomEnabled = false;
1235
+ return;
1236
+ }
1237
+ throw error;
1238
+ }
1239
+ }
1240
+ getRunnerFeatures() {
1241
+ const base = this.services.options.pluginOptions.features;
1242
+ if (!this.happyDomDisabled)
1243
+ return base;
1244
+ return { ...base, happyDOM: false };
1245
+ }
1246
+ getInitTimeoutMs(entrypoint, features) {
1247
+ if (this.happyDomDisabled ||
1248
+ !HAPPYDOM_INIT_TIMEOUT_MS ||
1249
+ HAPPYDOM_INIT_TIMEOUT_MS <= 0) {
1250
+ return INIT_TIMEOUT_MS;
1251
+ }
1252
+ if (isFeatureEnabled(features, 'happyDOM', entrypoint.name)) {
1253
+ return Math.min(INIT_TIMEOUT_MS, HAPPYDOM_INIT_TIMEOUT_MS);
1254
+ }
1255
+ return INIT_TIMEOUT_MS;
1256
+ }
1257
+ warnHappyDomDisabledOnce(timeoutMs) {
1258
+ if (this.happyDomDisableWarned)
1259
+ return;
1260
+ this.happyDomDisableWarned = true;
1261
+ emitWarning(this.services, [
1262
+ `[wyw-in-js] DOM emulation initialization exceeded ${timeoutMs}ms and will be disabled for this run.`,
1263
+ `WyW will continue without DOM emulation (as if features.happyDOM:false).`,
1264
+ ``,
1265
+ `To silence this warning: set features: { happyDOM: false }.`,
1266
+ `To restore DOM emulation, ensure "happy-dom" can be imported in the build-time runtime.`,
1267
+ `You can tune the timeout with WYW_EVAL_HAPPYDOM_INIT_TIMEOUT_MS.`,
1268
+ ].join('\n'));
1269
+ }
1270
+ onData(chunk) {
1271
+ const buffer = this.onData.buffer ?? '';
1272
+ const next = `${buffer}${chunk}`;
1273
+ const lines = next.split('\n');
1274
+ this.onData.buffer = lines.pop() ?? '';
1275
+ lines.forEach((line) => {
1276
+ if (!line.trim())
1277
+ return;
1278
+ let message;
1279
+ try {
1280
+ message = JSON.parse(line);
1281
+ }
1282
+ catch (error) {
1283
+ emitWarning(this.services, `[wyw-eval-runner] Failed to parse message: ${line}`);
1284
+ return;
1285
+ }
1286
+ this.handleMessage(message);
1287
+ });
1288
+ }
1289
+ handleMessage(message) {
1290
+ switch (message.type) {
1291
+ case 'INIT_ACK':
1292
+ if (message.error) {
1293
+ this.rejectPending(message.id, message.error);
1294
+ this.runner?.kill();
1295
+ return;
1296
+ }
1297
+ if (message.modulesReset) {
1298
+ // Runner just cleared its moduleCache during this INIT (full
1299
+ // context rebuild or reuseModules:false). Drop our shipped-code
1300
+ // mirror so handleLoad ships fresh code on the next LOAD.
1301
+ this.lastSentLoadByModule.clear();
1302
+ }
1303
+ this.resolvePending(message.id, {});
1304
+ return;
1305
+ case 'EVAL_RESULT':
1306
+ if (message.error) {
1307
+ this.rejectPending(message.id, message.error);
1308
+ return;
1309
+ }
1310
+ this.resolvePending(message.id, message.payload);
1311
+ return;
1312
+ case 'RESOLVE':
1313
+ this.handleResolve(message.id, message.payload).catch((error) => {
1314
+ void this.sendMessage({
1315
+ type: 'RESOLVE_RESULT',
1316
+ id: message.id,
1317
+ payload: {
1318
+ resolvedId: null,
1319
+ error: toSerializedError(error),
1320
+ },
1321
+ }).catch((sendError) => this.handleSendMessageError(sendError));
1322
+ });
1323
+ return;
1324
+ case 'LOAD':
1325
+ this.handleLoad(message.id, message.payload).catch((error) => {
1326
+ void this.sendMessage({
1327
+ type: 'LOAD_RESULT',
1328
+ id: message.id,
1329
+ payload: {
1330
+ id: message.payload.id,
1331
+ error: toSerializedError(error),
1332
+ },
1333
+ }).catch((sendError) => this.handleSendMessageError(sendError));
1334
+ });
1335
+ return;
1336
+ case 'WARN':
1337
+ this.handleWarn(message.payload);
1338
+ break;
1339
+ default:
1340
+ break;
1341
+ }
1342
+ }
1343
+ handleRunnerStderr(chunk) {
1344
+ const evalConsole = this.currentServices.options.pluginOptions.evalConsole ?? 'pipe';
1345
+ if (evalConsole === 'warning') {
1346
+ const text = chunk.toString('utf8');
1347
+ for (const line of text.split('\n')) {
1348
+ const trimmed = line.trim();
1349
+ if (trimmed) {
1350
+ emitWarning(this.currentServices, trimmed);
1351
+ }
1352
+ }
1353
+ }
1354
+ else if (evalConsole === 'pipe') {
1355
+ process.stderr.write(chunk);
1356
+ }
1357
+ }
1358
+ handleWarn(warning) {
1359
+ if (warning.importer && warning.specifier) {
1360
+ this.trackRuntimeDependency(warning.importer, warning.specifier);
1361
+ }
1362
+ emitEvalWarning(this.currentServices, warning);
1363
+ }
1364
+ async handleResolve(id, payload) {
1365
+ const result = await this.resolveImport(payload);
1366
+ if (debugEvalDir) {
1367
+ debugAction({
1368
+ type: 'resolve',
1369
+ evalSeq: this.evalSeq,
1370
+ specifier: payload.specifier,
1371
+ importer: payload.importerId,
1372
+ kind: payload.kind,
1373
+ resolvedId: result.resolvedId ?? null,
1374
+ external: result.external ?? false,
1375
+ ts: performance.now(),
1376
+ });
1377
+ }
1378
+ await this.sendMessage({
1379
+ type: 'RESOLVE_RESULT',
1380
+ id,
1381
+ payload: {
1382
+ resolvedId: result.resolvedId,
1383
+ external: result.external,
1384
+ },
1385
+ });
1386
+ }
1387
+ normalizeResolvedId(resolvedId, specifier, importerId) {
1388
+ const stripped = stripQueryAndHash(resolvedId);
1389
+ if (!stripped)
1390
+ return resolvedId;
1391
+ if (path.extname(stripped))
1392
+ return resolvedId;
1393
+ const isFileSpecifier = specifier.startsWith('.') || path.isAbsolute(specifier);
1394
+ if (!isFileSpecifier && !path.isAbsolute(stripped)) {
1395
+ return resolvedId;
1396
+ }
1397
+ let candidate = stripped;
1398
+ if (!path.isAbsolute(candidate)) {
1399
+ if (!importerId) {
1400
+ return resolvedId;
1401
+ }
1402
+ const importerFile = stripQueryAndHash(importerId);
1403
+ candidate = path.resolve(path.dirname(importerFile), candidate);
1404
+ }
1405
+ const suffix = resolvedId.slice(stripped.length);
1406
+ for (const ext of this.services.options.pluginOptions.extensions) {
1407
+ const fileCandidate = `${candidate}${ext}`;
1408
+ if (fs.existsSync(fileCandidate)) {
1409
+ return `${fileCandidate}${suffix}`;
1410
+ }
1411
+ const indexCandidate = path.join(candidate, `index${ext}`);
1412
+ if (fs.existsSync(indexCandidate)) {
1413
+ return `${indexCandidate}${suffix}`;
1414
+ }
1415
+ }
1416
+ if (importerId) {
1417
+ try {
1418
+ const importerFile = stripQueryAndHash(importerId);
1419
+ const resolved = DefaultModuleImplementation._resolveFilename(stripped, {
1420
+ id: importerFile,
1421
+ filename: importerFile,
1422
+ paths: DefaultModuleImplementation._nodeModulePaths(path.dirname(importerFile)),
1423
+ });
1424
+ if (resolved && resolved !== stripped) {
1425
+ return `${resolved}${suffix}`;
1426
+ }
1427
+ }
1428
+ catch {
1429
+ // ignore fallback failures
1430
+ }
1431
+ }
1432
+ return resolvedId;
1433
+ }
1434
+ async resolveImport({ specifier, importerId, kind, }) {
1435
+ return this.services.eventEmitter.action('eval:resolveImport', `${importerId}\0${kind}\0${specifier}`, importerId, () => this.resolveImportImpl({ specifier, importerId, kind }));
1436
+ }
1437
+ getResolveStack(importerId) {
1438
+ if (!this.activeResolveRootId || this.activeResolveRootId === importerId) {
1439
+ return [importerId];
1440
+ }
1441
+ return [importerId, this.activeResolveRootId];
1442
+ }
1443
+ async resolveImportImpl({ specifier, importerId, kind, }) {
1444
+ if (process.env.WYW_DEBUG_EVAL_RESOLVE) {
1445
+ // eslint-disable-next-line no-console
1446
+ console.warn('[wyw-eval:resolve]', { specifier, importerId, kind });
1447
+ }
1448
+ const key = `${kind}:${importerId}:${specifier}`;
1449
+ const evalOptions = getEvalOptions(this.services);
1450
+ const stack = this.getResolveStack(importerId);
1451
+ const importsOnly = this.importsByModule.get(importerId)?.get(specifier);
1452
+ const only = this.getImportOnly(importerId, specifier);
1453
+ if (process.env.WYW_DEBUG_EVAL_RESOLVE && !importsOnly) {
1454
+ // eslint-disable-next-line no-console
1455
+ console.warn('[wyw-eval:resolve:only-miss]', {
1456
+ specifier,
1457
+ importerId,
1458
+ kind,
1459
+ });
1460
+ }
1461
+ const strippedSpecifier = stripQueryAndHash(specifier);
1462
+ if (path.isAbsolute(strippedSpecifier)) {
1463
+ const normalized = this.normalizeResolvedId(specifier, specifier, importerId);
1464
+ const overridden = this.applyImportOverrides({
1465
+ source: specifier,
1466
+ resolved: normalized,
1467
+ only,
1468
+ external: false,
1469
+ }, importerId, stack);
1470
+ this.resolveCache.set(key, { resolvedId: normalized, external: false });
1471
+ return this.finalizeResolvedImport(importerId, specifier, overridden);
1472
+ }
1473
+ const cached = this.resolveCache.get(key);
1474
+ if (cached) {
1475
+ if (!cached.resolvedId) {
1476
+ return this.finalizeResolvedImport(importerId, specifier, {
1477
+ resolvedId: null,
1478
+ only: ['*'],
1479
+ });
1480
+ }
1481
+ const normalized = this.normalizeResolvedId(cached.resolvedId, specifier, importerId);
1482
+ const overridden = this.applyImportOverrides({
1483
+ source: specifier,
1484
+ resolved: normalized,
1485
+ only,
1486
+ external: cached.external,
1487
+ }, importerId, stack);
1488
+ if (cached.usedNodeFallback) {
1489
+ this.maybeWarnNodeFallback({
1490
+ importerId,
1491
+ specifier,
1492
+ resolvedId: normalized,
1493
+ kind,
1494
+ });
1495
+ }
1496
+ return this.finalizeResolvedImport(importerId, specifier, overridden);
1497
+ }
1498
+ const inFlight = this.resolveInFlight.get(key);
1499
+ if (inFlight) {
1500
+ const cachedResult = await inFlight;
1501
+ if (!cachedResult.resolvedId) {
1502
+ return this.finalizeResolvedImport(importerId, specifier, {
1503
+ resolvedId: null,
1504
+ only: ['*'],
1505
+ });
1506
+ }
1507
+ const normalized = this.normalizeResolvedId(cachedResult.resolvedId, specifier, importerId);
1508
+ const overridden = this.applyImportOverrides({
1509
+ source: specifier,
1510
+ resolved: normalized,
1511
+ only,
1512
+ external: cachedResult.external,
1513
+ }, importerId, stack);
1514
+ if (cachedResult.usedNodeFallback) {
1515
+ this.maybeWarnNodeFallback({
1516
+ importerId,
1517
+ specifier,
1518
+ resolvedId: normalized,
1519
+ kind,
1520
+ });
1521
+ }
1522
+ return this.finalizeResolvedImport(importerId, specifier, overridden);
1523
+ }
1524
+ const task = (async () => {
1525
+ if (evalOptions.customResolver) {
1526
+ const customResolved = await evalOptions.customResolver(specifier, importerId, kind);
1527
+ if (customResolved) {
1528
+ const normalized = this.normalizeResolvedId(customResolved.id, specifier, importerId);
1529
+ if (process.env.WYW_DEBUG_EVAL_RESOLVE) {
1530
+ // eslint-disable-next-line no-console
1531
+ console.warn('[wyw-eval:resolve:custom]', {
1532
+ specifier,
1533
+ importerId,
1534
+ resolved: customResolved.id,
1535
+ normalized,
1536
+ external: customResolved.external,
1537
+ });
1538
+ }
1539
+ return {
1540
+ resolvedId: normalized,
1541
+ external: customResolved.external,
1542
+ };
1543
+ }
1544
+ if (evalOptions.resolver === 'custom') {
1545
+ return { resolvedId: null };
1546
+ }
1547
+ }
1548
+ if (evalOptions.resolver !== 'node') {
1549
+ let resolved = null;
1550
+ try {
1551
+ resolved = await this.asyncResolve(specifier, importerId, stack);
1552
+ }
1553
+ catch {
1554
+ resolved = null;
1555
+ }
1556
+ if (resolved) {
1557
+ const normalized = this.normalizeResolvedId(resolved, specifier, importerId);
1558
+ if (process.env.WYW_DEBUG_EVAL_RESOLVE) {
1559
+ // eslint-disable-next-line no-console
1560
+ console.warn('[wyw-eval:resolve:async]', {
1561
+ specifier,
1562
+ importerId,
1563
+ resolved,
1564
+ normalized,
1565
+ });
1566
+ }
1567
+ return {
1568
+ resolvedId: normalized,
1569
+ };
1570
+ }
1571
+ }
1572
+ if (evalOptions.resolver === 'node' || evalOptions.require !== 'off') {
1573
+ const nodeResolved = this.resolveWithNodeFallback(specifier, importerId);
1574
+ if (process.env.WYW_DEBUG_EVAL_RESOLVE) {
1575
+ // eslint-disable-next-line no-console
1576
+ console.warn('[wyw-eval:resolve:node]', {
1577
+ specifier,
1578
+ importerId,
1579
+ resolved: nodeResolved.resolvedId,
1580
+ });
1581
+ }
1582
+ return {
1583
+ ...nodeResolved,
1584
+ usedNodeFallback: evalOptions.resolver !== 'node',
1585
+ };
1586
+ }
1587
+ if (process.env.WYW_DEBUG_EVAL_RESOLVE) {
1588
+ // eslint-disable-next-line no-console
1589
+ console.warn('[wyw-eval:resolve:none]', {
1590
+ specifier,
1591
+ importerId,
1592
+ });
1593
+ }
1594
+ return { resolvedId: null };
1595
+ })();
1596
+ this.resolveInFlight.set(key, task);
1597
+ try {
1598
+ const result = await task;
1599
+ this.resolveCache.set(key, result);
1600
+ if (!result.resolvedId) {
1601
+ return this.finalizeResolvedImport(importerId, specifier, {
1602
+ resolvedId: null,
1603
+ only: ['*'],
1604
+ });
1605
+ }
1606
+ const overridden = this.applyImportOverrides({
1607
+ source: specifier,
1608
+ resolved: result.resolvedId,
1609
+ only,
1610
+ external: result.external,
1611
+ }, importerId, stack);
1612
+ if (result.usedNodeFallback && result.resolvedId) {
1613
+ this.maybeWarnNodeFallback({
1614
+ importerId,
1615
+ specifier,
1616
+ resolvedId: result.resolvedId,
1617
+ kind,
1618
+ });
1619
+ }
1620
+ return this.finalizeResolvedImport(importerId, specifier, overridden);
1621
+ }
1622
+ finally {
1623
+ this.resolveInFlight.delete(key);
1624
+ }
1625
+ }
1626
+ finalizeResolvedImport(importerId, specifier, result) {
1627
+ this.trackImporterDependency(importerId, specifier, result.resolvedId, result.only);
1628
+ this.emitDependency(importerId, specifier, result.resolvedId, result.only);
1629
+ return result;
1630
+ }
1631
+ emitDependency(importerId, specifier, resolvedId, only) {
1632
+ if (resolvedId === null) {
1633
+ return;
1634
+ }
1635
+ const key = `${importerId}\0${specifier}\0${resolvedId}\0${only.join(',')}`;
1636
+ if (this.emittedDependencies.has(key)) {
1637
+ return;
1638
+ }
1639
+ this.emittedDependencies.add(key);
1640
+ this.services.eventEmitter.single({
1641
+ type: 'dependency',
1642
+ file: importerId,
1643
+ only,
1644
+ imports: [{ from: resolvedId, what: only }],
1645
+ fileIdx: getFileIdx(importerId),
1646
+ });
1647
+ }
1648
+ trackRuntimeDependency(importerId, specifier) {
1649
+ if (isBuiltinSpecifier(specifier) || isVirtualSpecifier(specifier)) {
1650
+ return;
1651
+ }
1652
+ const dependencies = this.runtimeDependenciesByModule.get(importerId) ?? new Set();
1653
+ dependencies.add(specifier);
1654
+ this.runtimeDependenciesByModule.set(importerId, dependencies);
1655
+ }
1656
+ trackImporterDependency(importerId, source, resolved, only) {
1657
+ const importerEntrypoint = this.services.cache.get('entrypoints', importerId);
1658
+ const dependencies = importerEntrypoint?.dependencies;
1659
+ if (!dependencies)
1660
+ return;
1661
+ if (resolved === null) {
1662
+ dependencies.delete(source);
1663
+ return;
1664
+ }
1665
+ const cached = dependencies.get(source);
1666
+ dependencies.set(source, {
1667
+ source,
1668
+ resolved,
1669
+ only: cached ? mergeOnly(cached.only, only) : [...only],
1670
+ });
1671
+ }
1672
+ collectEntrypointDependencies(entrypointId) {
1673
+ const collected = new Set(this.runtimeDependenciesByModule.get(entrypointId) ?? []);
1674
+ const cachedEntrypoint = this.services.cache.get('entrypoints', entrypointId);
1675
+ cachedEntrypoint?.dependencies?.forEach((dependency, specifier) => {
1676
+ if (dependency.resolved !== null &&
1677
+ !isBuiltinSpecifier(specifier) &&
1678
+ !isVirtualSpecifier(specifier)) {
1679
+ collected.add(specifier);
1680
+ }
1681
+ });
1682
+ return Array.from(collected);
1683
+ }
1684
+ applyImportOverrides(resolved, importerId, stack) {
1685
+ const { root } = this.services.options;
1686
+ const keyInfo = toImportKey({
1687
+ source: resolved.source,
1688
+ resolved: resolved.resolved,
1689
+ root,
1690
+ });
1691
+ const override = getImportOverride(this.services.options.pluginOptions.importOverrides, keyInfo.key);
1692
+ let nextResolved = resolved.resolved;
1693
+ let nextExternal = resolved.external;
1694
+ if (override?.mock) {
1695
+ nextResolved = resolveMockSpecifier({
1696
+ mock: override.mock,
1697
+ importer: importerId,
1698
+ root,
1699
+ stack,
1700
+ });
1701
+ nextExternal = false;
1702
+ }
1703
+ let nextOnly = applyImportOverrideToOnly(resolved.only, override);
1704
+ const cached = this.services.cache.get('entrypoints', nextResolved);
1705
+ if (nextOnly.includes('__wywPreval') &&
1706
+ cached?.evaluated &&
1707
+ !cached.ignored &&
1708
+ !hasCachedWywPrevalExport(this.services, nextResolved, cached)) {
1709
+ nextOnly = nextOnly.filter((item) => item !== '__wywPreval');
1710
+ }
1711
+ const storedOnly = this.onlyByModule.get(nextResolved);
1712
+ this.onlyByModule.set(nextResolved, storedOnly ? mergeOnly(storedOnly, nextOnly) : nextOnly);
1713
+ return {
1714
+ resolvedId: nextResolved,
1715
+ external: nextExternal,
1716
+ only: nextOnly,
1717
+ };
1718
+ }
1719
+ resolveWithNodeFallback(specifier, importerId) {
1720
+ const extensions = DefaultModuleImplementation._extensions;
1721
+ const added = [];
1722
+ const { conditionNames } = this.services.options.pluginOptions;
1723
+ const conditions = conditionNames?.length
1724
+ ? expandConditions(conditionNames)
1725
+ : undefined;
1726
+ try {
1727
+ this.services.options.pluginOptions.extensions.forEach((ext) => {
1728
+ if (ext in extensions)
1729
+ return;
1730
+ extensions[ext] = NOOP;
1731
+ added.push(ext);
1732
+ });
1733
+ const filename = importerId;
1734
+ const strippedId = stripQueryAndHash(specifier);
1735
+ let resolved;
1736
+ try {
1737
+ resolved = resolveFilenameWithConditions(DefaultModuleImplementation, strippedId, {
1738
+ id: filename,
1739
+ filename,
1740
+ paths: DefaultModuleImplementation._nodeModulePaths(path.dirname(filename)),
1741
+ }, conditions);
1742
+ }
1743
+ catch (error) {
1744
+ throw new Error([
1745
+ `[wyw-in-js] Node resolver failed during eval.`,
1746
+ ``,
1747
+ `importer: ${filename}`,
1748
+ `source: ${specifier}`,
1749
+ ``,
1750
+ `error: ${error instanceof Error ? error.message : String(error)}`,
1751
+ ].join('\n'));
1752
+ }
1753
+ const isFileSpecifier = strippedId.startsWith('.') || path.isAbsolute(strippedId);
1754
+ if (isFileSpecifier &&
1755
+ path.extname(strippedId) === '' &&
1756
+ resolved.endsWith('.cjs') &&
1757
+ fs.existsSync(`${resolved.slice(0, -4)}.js`)) {
1758
+ resolved = `${resolved.slice(0, -4)}.js`;
1759
+ }
1760
+ return {
1761
+ resolvedId: this.normalizeResolvedId(resolved, specifier, importerId),
1762
+ };
1763
+ }
1764
+ finally {
1765
+ added.forEach((ext) => delete extensions[ext]);
1766
+ }
1767
+ }
1768
+ maybeWarnNodeFallback({ importerId, specifier, resolvedId, kind, }) {
1769
+ const evalOptions = getEvalOptions(this.services);
1770
+ const { root } = this.services.options;
1771
+ const keyInfo = toImportKey({
1772
+ source: specifier,
1773
+ resolved: resolvedId,
1774
+ root,
1775
+ });
1776
+ const override = getImportOverride(this.services.options.pluginOptions.importOverrides, keyInfo.key);
1777
+ if (override && override.unknown === undefined) {
1778
+ return;
1779
+ }
1780
+ const basePolicy = evalOptions.require === 'warn-and-run' ? 'warn' : 'error';
1781
+ let policy = override?.unknown ?? basePolicy;
1782
+ if (evalOptions.require === 'off' && policy !== 'error') {
1783
+ policy = 'error';
1784
+ }
1785
+ if (policy === 'error') {
1786
+ throw new Error([
1787
+ `[wyw-in-js] Unknown import reached during eval (Node resolver fallback)`,
1788
+ ``,
1789
+ `importer: ${importerId}`,
1790
+ `source: ${specifier}`,
1791
+ `resolved: ${resolvedId}`,
1792
+ ``,
1793
+ `config key: ${keyInfo.key}`,
1794
+ `docs: https://wyw-in-js.dev/troubleshooting`,
1795
+ ]
1796
+ .filter(Boolean)
1797
+ .join('\n'));
1798
+ }
1799
+ const warnedUnknownImports = getWarnedUnknownImports(this.services);
1800
+ if (policy === 'warn' && !warnedUnknownImports.has(keyInfo.key)) {
1801
+ warnedUnknownImports.add(keyInfo.key);
1802
+ const warningMessage = [
1803
+ `[wyw-in-js] Unknown import reached during eval (Node resolver fallback)`,
1804
+ ``,
1805
+ `importer: ${importerId}`,
1806
+ `source: ${specifier}`,
1807
+ `resolved: ${resolvedId}`,
1808
+ ``,
1809
+ `config key: ${keyInfo.key}`,
1810
+ `hint: add { importOverrides: { ${JSON.stringify(keyInfo.key)}: { unknown: 'allow' } } } to silence warnings, or use { mock } / { noShake: true } overrides.`,
1811
+ `docs: https://wyw-in-js.dev/troubleshooting`,
1812
+ ]
1813
+ .filter(Boolean)
1814
+ .join('\n');
1815
+ emitEvalWarning(this.currentServices, {
1816
+ code: kind === 'require' ? 'require-fallback' : 'resolve-fallback',
1817
+ message: warningMessage,
1818
+ importer: importerId,
1819
+ specifier,
1820
+ resolved: resolvedId ?? null,
1821
+ callstack: [importerId],
1822
+ hint: `Use importOverrides or eval.require settings to avoid fallback.`,
1823
+ });
1824
+ }
1825
+ }
1826
+ async handleLoad(id, payload) {
1827
+ const prepared = await this.loadModule(payload);
1828
+ // Decide once whether the runner already has this exact prepared variant.
1829
+ // The runner caches by id and short-circuits when the LoadResult hash
1830
+ // matches `moduleHashes.get(id)` (runner.js:1834). So when our prior
1831
+ // shipment under the same hash already covered the requested `only`,
1832
+ // re-shipping the code is pure waste — both over IPC and to the dump dir.
1833
+ const previouslySent = prepared.hash
1834
+ ? this.lastSentLoadByModule.get(payload.id)
1835
+ : undefined;
1836
+ // Runner stores by hash but classifies storage by `only` shape: wildcard
1837
+ // (`['*']`) ⇒ moduleCache, anything else ⇒ moduleVariants (runner.js
1838
+ // isFullModuleLoad / runner.js:1832-1842). Reusing across shapes would
1839
+ // hit the wrong map and miss. Require the same shape AND the prepared
1840
+ // `only` to be a subset of what we already shipped — same hash already
1841
+ // implies identical bytes.
1842
+ const sameStorageShape = Boolean(previouslySent &&
1843
+ isWildcardOnly(previouslySent.only) === isWildcardOnly(prepared.only));
1844
+ const runnerHasCachedVariant = Boolean(prepared.hash &&
1845
+ previouslySent &&
1846
+ previouslySent.hash === prepared.hash &&
1847
+ sameStorageShape &&
1848
+ isSuperSet(previouslySent.only, prepared.only));
1849
+ const shouldShipCode = Boolean(prepared.code && !prepared.exports && !runnerHasCachedVariant);
1850
+ if (debugEvalDir) {
1851
+ if (shouldShipCode) {
1852
+ dumpEvalCode(payload.id, prepared.code, prepared.only, prepared.hash ? `cache:${prepared.hash}` : 'fresh', this.evalSeq);
1853
+ }
1854
+ debugAction({
1855
+ type: 'load',
1856
+ evalSeq: this.evalSeq,
1857
+ id: payload.id,
1858
+ importer: payload.importerId ?? null,
1859
+ only: prepared.only,
1860
+ hasCode: Boolean(prepared.code),
1861
+ hasExports: Boolean(prepared.exports),
1862
+ hash: prepared.hash ?? null,
1863
+ shipped: shouldShipCode,
1864
+ ts: performance.now(),
1865
+ });
1866
+ }
1867
+ await this.sendLoadResult(id, {
1868
+ id: payload.id,
1869
+ code: shouldShipCode ? prepared.code : '',
1870
+ map: null,
1871
+ hash: prepared.hash,
1872
+ only: prepared.only,
1873
+ exports: prepared.exports,
1874
+ });
1875
+ if (shouldShipCode && prepared.hash) {
1876
+ const merged = previouslySent?.hash === prepared.hash
1877
+ ? mergeOnly(previouslySent.only, prepared.only)
1878
+ : [...prepared.only];
1879
+ this.lastSentLoadByModule.set(payload.id, {
1880
+ hash: prepared.hash,
1881
+ only: merged,
1882
+ });
1883
+ }
1884
+ }
1885
+ async loadModule({ id, importerId, request, }) {
1886
+ const actionEntrypoint = importerId ?? id;
1887
+ return this.services.eventEmitter.action('eval:loadModule', `${actionEntrypoint}\0${id}`, actionEntrypoint, () => this.loadModuleImpl({ id, importerId, request }));
1888
+ }
1889
+ async loadModuleImpl({ id, importerId, request, }) {
1890
+ let cached = this.loadCache.get(id);
1891
+ if (this.services.cache.consumeInvalidation(id)) {
1892
+ this.loadCache.delete(id);
1893
+ cached = undefined;
1894
+ }
1895
+ const loadRequestOnly = this.getLoadRequestOnly(id, importerId, request);
1896
+ if (loadRequestOnly) {
1897
+ const storedOnly = this.onlyByModule.get(id);
1898
+ this.onlyByModule.set(id, storedOnly ? mergeOnly(storedOnly, loadRequestOnly) : loadRequestOnly);
1899
+ this.trackImporterDependency(importerId, request, id, loadRequestOnly);
1900
+ this.emitDependency(importerId, request, id, loadRequestOnly);
1901
+ }
1902
+ let requiredOnly = this.mergeKnownDependencyOnly(id);
1903
+ // Merge the specific exports the importer needs from this module.
1904
+ // The broker's onlyByModule is populated by RESOLVE handlers, but
1905
+ // concurrent message processing can cause a LOAD to arrive before
1906
+ // all pending RESOLVEs are complete. Directly consulting the
1907
+ // importer's imports map ensures we never serve a module with
1908
+ // fewer exports than the requesting importer actually imports.
1909
+ if (importerId && request) {
1910
+ const importerImports = this.importsByModule.get(importerId);
1911
+ if (importerImports) {
1912
+ const specifierOnly = importerImports.get(request);
1913
+ if (specifierOnly && specifierOnly.length > 0) {
1914
+ requiredOnly = requiredOnly.includes('*')
1915
+ ? requiredOnly
1916
+ : mergeOnly(requiredOnly, specifierOnly);
1917
+ }
1918
+ }
1919
+ }
1920
+ const cachedEntrypoint = this.services.cache.get('entrypoints', id);
1921
+ if (cachedEntrypoint &&
1922
+ cachedEntrypoint.evaluated &&
1923
+ !cachedEntrypoint.ignored &&
1924
+ cachedEntrypoint.exports &&
1925
+ !requiredOnly.includes('*') &&
1926
+ !requiredOnly.some(isEvalOnlyKey) &&
1927
+ isSuperSet(cachedEntrypoint.evaluatedOnly ?? [], requiredOnly)) {
1928
+ const serializeOnly = getSerializableStaticImportKeys(this.services, id, cachedEntrypoint, requiredOnly, request, importerId);
1929
+ if (serializeOnly) {
1930
+ const serialized = serializeCachedExports(cachedEntrypoint.exports, serializeOnly);
1931
+ if (serialized) {
1932
+ const hash = hashContent(`exports:${JSON.stringify(serialized)}`);
1933
+ return {
1934
+ code: '',
1935
+ imports: null,
1936
+ only: serializeOnly,
1937
+ hash,
1938
+ exports: serialized,
1939
+ };
1940
+ }
1941
+ }
1942
+ }
1943
+ // prepareModuleOnDemand is deterministic given (id, requiredOnly): the
1944
+ // shaker output depends only on source bytes (invalidated via
1945
+ // consumeInvalidation when the file changes) and the requested `only`.
1946
+ // Side effects from __wywPreval happen at runtime in the runner, not at
1947
+ // preparation time — so caching prepared bytes is safe even for self-loads
1948
+ // with __wywPreval. This lets incremental rebuilds reuse the prepared
1949
+ // entrypoint when its source is unchanged; my IPC dedup mirror then
1950
+ // suppresses re-shipping to the runner.
1951
+ if (cached && isPreparedCacheHit(cached, requiredOnly)) {
1952
+ this.ensureImportsMapping(id, cached.imports);
1953
+ return cached;
1954
+ }
1955
+ const inflight = this.loadInFlight.get(id);
1956
+ if (inflight) {
1957
+ const result = await inflight;
1958
+ if (isPreparedCacheHit(result, requiredOnly)) {
1959
+ this.ensureImportsMapping(id, result.imports);
1960
+ return result;
1961
+ }
1962
+ }
1963
+ const slowImportWarningsEnabled = isWarningEnabled(process.env.WYW_WARN_SLOW_IMPORTS);
1964
+ const slowImportThresholdMs = slowImportWarningsEnabled
1965
+ ? getSlowImportThresholdMs()
1966
+ : 0;
1967
+ const warnedSlowImports = slowImportWarningsEnabled
1968
+ ? getWarnedSlowImports(this.services)
1969
+ : null;
1970
+ const shouldWarnSlowImport = Boolean(slowImportWarningsEnabled &&
1971
+ warnedSlowImports &&
1972
+ slowImportThresholdMs > 0 &&
1973
+ request &&
1974
+ importerId &&
1975
+ importerId !== id);
1976
+ const slowImportStartedAt = shouldWarnSlowImport ? performance.now() : 0;
1977
+ const task = (async () => {
1978
+ const evalOptions = getEvalOptions(this.services);
1979
+ if (evalOptions.customLoader) {
1980
+ const loaded = await evalOptions.customLoader(id);
1981
+ if (loaded) {
1982
+ const code = formatLoaderResult(loaded.code, loaded.loader);
1983
+ return {
1984
+ code,
1985
+ imports: null,
1986
+ only: requiredOnly,
1987
+ hash: hashContent(code),
1988
+ };
1989
+ }
1990
+ }
1991
+ if (request && importerId) {
1992
+ const loaded = loadByImportLoaders(this.services, request, id, importerId);
1993
+ if (loaded.handled) {
1994
+ const code = `export default ${JSON.stringify(loaded.value)};`;
1995
+ return {
1996
+ code,
1997
+ imports: null,
1998
+ only: requiredOnly,
1999
+ hash: hashContent(code),
2000
+ };
2001
+ }
2002
+ }
2003
+ const strippedId = stripQueryAndHash(id);
2004
+ const extension = path.extname(strippedId);
2005
+ if (extension === '.json') {
2006
+ const jsonSource = fs.readFileSync(strippedId, 'utf-8');
2007
+ const code = `export default ${JSON.stringify(JSON.parse(jsonSource))};`;
2008
+ return {
2009
+ code,
2010
+ imports: null,
2011
+ only: requiredOnly,
2012
+ hash: hashContent(code),
2013
+ };
2014
+ }
2015
+ if (extension &&
2016
+ !this.services.options.pluginOptions.extensions.includes(extension)) {
2017
+ const code = `export default ${JSON.stringify(id)};`;
2018
+ return {
2019
+ code,
2020
+ imports: null,
2021
+ only: requiredOnly,
2022
+ hash: hashContent(code),
2023
+ };
2024
+ }
2025
+ const directBarrelProxy = buildDirectBarrelProxy(this.services, id, requiredOnly);
2026
+ if (directBarrelProxy) {
2027
+ return {
2028
+ ...directBarrelProxy,
2029
+ hash: hashContent(directBarrelProxy.code),
2030
+ };
2031
+ }
2032
+ if (!requiredOnly.includes('*')) {
2033
+ const loadedAndParsed = this.services.loadAndParseFn(this.services, id, undefined, this.services.log);
2034
+ if (loadedAndParsed.evaluator !== 'ignored' &&
2035
+ loadedAndParsed.evaluator === oxcShaker &&
2036
+ isStaticallyEvaluatableModule(loadedAndParsed.code, id)) {
2037
+ requiredOnly = ['*'];
2038
+ this.onlyByModule.set(id, requiredOnly);
2039
+ }
2040
+ }
2041
+ const prepareOnly = requiredOnly.includes('__wywPreval') || !cached
2042
+ ? requiredOnly
2043
+ : mergeOnly(cached.only, requiredOnly);
2044
+ const prepared = prepareModuleOnDemand(this.services, id, prepareOnly);
2045
+ this.ensureImportsMapping(id, prepared.imports);
2046
+ if (shouldWarnSlowImport && request && importerId) {
2047
+ const durationMs = performance.now() - slowImportStartedAt;
2048
+ if (durationMs >= slowImportThresholdMs) {
2049
+ const { root } = this.services.options;
2050
+ const resolvedKey = stripQueryAndHash(id);
2051
+ const { key: importKey } = toImportKey({
2052
+ source: request,
2053
+ resolved: resolvedKey,
2054
+ root,
2055
+ });
2056
+ const dedupeKey = `${importerId}::${importKey}`;
2057
+ if (warnedSlowImports && !warnedSlowImports.has(dedupeKey)) {
2058
+ warnedSlowImports.add(dedupeKey);
2059
+ const warning = [
2060
+ `[wyw-in-js] Slow import during prepare stage`,
2061
+ ``,
2062
+ `file: ${importerId}`,
2063
+ `import: ${request}`,
2064
+ `resolved: ${resolvedKey}`,
2065
+ `duration: ${durationMs.toFixed(1)}ms`,
2066
+ ``,
2067
+ `tip: if this import is runtime-only or heavy, mock it during evaluation via importOverrides:`,
2068
+ ` importOverrides: {`,
2069
+ ` '${importKey}': { mock: './path/to/mock' },`,
2070
+ ` }`,
2071
+ ``,
2072
+ `note: importOverrides affects only build-time evaluation (it does not change your bundler runtime behavior)`,
2073
+ ``,
2074
+ `note: configure threshold with WYW_WARN_SLOW_IMPORTS_MS (current: ${slowImportThresholdMs}ms)`,
2075
+ ].join('\n');
2076
+ emitWarning(this.currentServices, warning);
2077
+ }
2078
+ }
2079
+ }
2080
+ const hash = hashContent(prepared.code);
2081
+ return { ...prepared, hash };
2082
+ })();
2083
+ this.loadInFlight.set(id, task);
2084
+ try {
2085
+ const result = await task;
2086
+ // Register imports for ALL code paths (barrel proxy, prepareModuleOnDemand,
2087
+ // custom loaders). Without this, the barrel proxy path skips
2088
+ // ensureImportsMapping, so getLoadRequestOnly can't determine what a barrel
2089
+ // module imports from its sub-dependencies.
2090
+ this.ensureImportsMapping(id, result.imports);
2091
+ this.loadCache.set(id, result);
2092
+ return result;
2093
+ }
2094
+ finally {
2095
+ this.loadInFlight.delete(id);
2096
+ }
2097
+ }
2098
+ async sendLoadResult(id, payload) {
2099
+ if (!payload.code) {
2100
+ await this.sendMessage({
2101
+ type: 'LOAD_RESULT',
2102
+ id,
2103
+ payload,
2104
+ });
2105
+ return;
2106
+ }
2107
+ const message = {
2108
+ type: 'LOAD_RESULT',
2109
+ id,
2110
+ payload,
2111
+ };
2112
+ const serialized = JSON.stringify(message);
2113
+ if (serialized.length < MAX_MESSAGE_SIZE) {
2114
+ await this.sendMessage(message);
2115
+ return;
2116
+ }
2117
+ const { code } = payload;
2118
+ const chunkCount = Math.ceil(code.length / MAX_CHUNK_SIZE);
2119
+ for (let index = 0; index < chunkCount; index += 1) {
2120
+ const start = index * MAX_CHUNK_SIZE;
2121
+ const end = start + MAX_CHUNK_SIZE;
2122
+ const codeChunk = code.slice(start, end);
2123
+ const chunkPayload = {
2124
+ id: payload.id,
2125
+ codeChunk,
2126
+ chunkIndex: index,
2127
+ chunkCount,
2128
+ };
2129
+ if (index === 0) {
2130
+ chunkPayload.map = payload.map;
2131
+ chunkPayload.hash = payload.hash;
2132
+ chunkPayload.only = payload.only;
2133
+ chunkPayload.exports = payload.exports;
2134
+ chunkPayload.error = payload.error;
2135
+ }
2136
+ await this.sendMessage({
2137
+ type: 'LOAD_RESULT',
2138
+ id,
2139
+ payload: chunkPayload,
2140
+ });
2141
+ }
2142
+ }
2143
+ sendMessage(message) {
2144
+ const payload = `${JSON.stringify(message)}\n`;
2145
+ invariant(payload.length < MAX_MESSAGE_SIZE, 'Message too large');
2146
+ if (!this.runnerInputQueue) {
2147
+ return Promise.reject(new Error('Eval runner is not ready'));
2148
+ }
2149
+ return this.runnerInputQueue.write(payload);
2150
+ }
2151
+ handleSendMessageError(error, id) {
2152
+ const serialized = error instanceof Error
2153
+ ? { message: error.message, stack: error.stack }
2154
+ : { message: String(error) };
2155
+ if (id) {
2156
+ this.rejectPending(id, serialized);
2157
+ }
2158
+ this.runner?.kill();
2159
+ }
2160
+ request(type, payload, timeoutMs = REQUEST_TIMEOUT_MS) {
2161
+ this.nextId += 1;
2162
+ const id = `${this.nextId}`;
2163
+ const message = {
2164
+ type: type,
2165
+ id,
2166
+ payload: payload,
2167
+ };
2168
+ return new Promise((resolve, reject) => {
2169
+ const timeout = setTimeout(() => {
2170
+ this.pending.delete(id);
2171
+ this.runner?.kill();
2172
+ const error = new Error(`[wyw-in-js] Eval runner timed out for ${type}`);
2173
+ error.code = 'WYW_EVAL_TIMEOUT';
2174
+ reject(error);
2175
+ }, timeoutMs);
2176
+ this.pending.set(id, {
2177
+ resolve: resolve,
2178
+ reject,
2179
+ timeout,
2180
+ });
2181
+ this.sendMessage(message).catch((error) => this.handleSendMessageError(error, id));
2182
+ });
2183
+ }
2184
+ resolvePending(id, payload) {
2185
+ const pending = this.pending.get(id);
2186
+ if (!pending)
2187
+ return;
2188
+ clearTimeout(pending.timeout);
2189
+ this.pending.delete(id);
2190
+ pending.resolve(payload);
2191
+ }
2192
+ rejectPending(id, error) {
2193
+ const pending = this.pending.get(id);
2194
+ if (!pending)
2195
+ return;
2196
+ clearTimeout(pending.timeout);
2197
+ this.pending.delete(id);
2198
+ const cause = error.cause
2199
+ ? Object.assign(new Error(error.cause.message), {
2200
+ stack: error.cause.stack,
2201
+ })
2202
+ : undefined;
2203
+ const err = cause
2204
+ ? new Error(error.message, { cause })
2205
+ : new Error(error.message);
2206
+ if (error.stack) {
2207
+ err.stack = error.stack;
2208
+ }
2209
+ pending.reject(err);
2210
+ }
2211
+ rejectAllPending(error) {
2212
+ this.pending.forEach((pending) => {
2213
+ clearTimeout(pending.timeout);
2214
+ pending.reject(error);
2215
+ });
2216
+ this.pending.clear();
2217
+ }
2218
+ mergeKnownDependencyOnly(id) {
2219
+ const storedOnly = this.onlyByModule.get(id) ?? ['*'];
2220
+ if (storedOnly.includes('*')) {
2221
+ return storedOnly;
2222
+ }
2223
+ let mergedOnly = storedOnly;
2224
+ for (const cachedEntrypoint of this.services.cache.entrypoints.values()) {
2225
+ const { dependencies } = cachedEntrypoint;
2226
+ if (!dependencies) {
2227
+ continue;
2228
+ }
2229
+ for (const dependency of dependencies.values()) {
2230
+ if (dependency.resolved !== id || !dependency.only) {
2231
+ continue;
2232
+ }
2233
+ mergedOnly = mergeOnly(mergedOnly, dependency.only);
2234
+ if (mergedOnly.includes('*')) {
2235
+ this.onlyByModule.set(id, mergedOnly);
2236
+ return mergedOnly;
2237
+ }
2238
+ }
2239
+ }
2240
+ this.onlyByModule.set(id, mergedOnly);
2241
+ return mergedOnly;
2242
+ }
2243
+ }
2244
+ const evalBrokers = new WeakMap();
2245
+ export const disposeEvalBroker = (cache) => {
2246
+ const cached = evalBrokers.get(cache);
2247
+ if (!cached)
2248
+ return;
2249
+ cached.broker.dispose();
2250
+ evalBrokers.delete(cache);
2251
+ };
2252
+ export const getEvalBroker = (services, asyncResolve, cacheKey) => {
2253
+ const cached = evalBrokers.get(services.cache);
2254
+ if (cached && cached.key === cacheKey)
2255
+ return cached.broker;
2256
+ if (cached) {
2257
+ disposeEvalBroker(services.cache);
2258
+ }
2259
+ const broker = new EvalBroker(services, asyncResolve);
2260
+ evalBrokers.set(services.cache, { key: cacheKey, broker });
2261
+ return broker;
2262
+ };