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