vite-plugin-react-server 0.3.4 → 0.3.6

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 (484) hide show
  1. package/README.md +113 -222
  2. package/bin/patch.mjs +84 -0
  3. package/dist/client.d.ts +3 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +7 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/index.d.ts +7 -2
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +32 -3
  10. package/dist/index.js.map +1 -1
  11. package/dist/node_modules/magic-string/dist/magic-string.es.js +1283 -0
  12. package/dist/node_modules/magic-string/dist/magic-string.es.js.map +1 -0
  13. package/dist/package.json +130 -0
  14. package/dist/plugin/assertServerCondition.d.ts.map +1 -0
  15. package/dist/plugin/assertServerCondition.js +15 -0
  16. package/dist/plugin/build/createClientBuildConfig.d.ts +3 -0
  17. package/dist/plugin/build/createClientBuildConfig.d.ts.map +1 -0
  18. package/dist/plugin/build/createClientBuildConfig.js +14 -0
  19. package/dist/plugin/build/createServerBuildConfig.d.ts +12 -0
  20. package/dist/plugin/build/createServerBuildConfig.d.ts.map +1 -0
  21. package/dist/plugin/build/createServerBuildConfig.js +40 -0
  22. package/dist/plugin/build/createSharedBuildConfig.d.ts +5 -0
  23. package/dist/plugin/build/createSharedBuildConfig.d.ts.map +1 -0
  24. package/dist/plugin/build/createSharedBuildConfig.js +28 -0
  25. package/dist/plugin/build/mergeInputs.d.ts +9 -0
  26. package/dist/plugin/build/mergeInputs.d.ts.map +1 -0
  27. package/dist/plugin/build/mergeInputs.js +56 -0
  28. package/dist/plugin/checkFilesExist.d.ts +3 -0
  29. package/dist/plugin/checkFilesExist.d.ts.map +1 -0
  30. package/dist/plugin/checkFilesExist.js +78 -0
  31. package/dist/plugin/checkFilesExist.js.map +1 -0
  32. package/dist/{collect-css-manifest.d.ts → plugin/collect-css-manifest.d.ts} +2 -2
  33. package/dist/plugin/collect-css-manifest.d.ts.map +1 -0
  34. package/dist/{src → plugin}/collect-css-manifest.js +8 -5
  35. package/dist/plugin/collect-css-manifest.js.map +1 -0
  36. package/dist/plugin/components.d.ts.map +1 -0
  37. package/dist/{src → plugin}/components.js +9 -4
  38. package/dist/plugin/components.js.map +1 -0
  39. package/dist/plugin/config/defaults.d.ts +51 -0
  40. package/dist/plugin/config/defaults.d.ts.map +1 -0
  41. package/dist/plugin/config/defaults.js +52 -0
  42. package/dist/plugin/config/defaults.js.map +1 -0
  43. package/dist/plugin/config/getCondition.d.ts +2 -0
  44. package/dist/plugin/config/getCondition.d.ts.map +1 -0
  45. package/dist/plugin/config/getCondition.js +11 -0
  46. package/dist/plugin/config/getCondition.js.map +1 -0
  47. package/dist/plugin/config/getPaths.d.ts +5 -0
  48. package/dist/plugin/config/getPaths.d.ts.map +1 -0
  49. package/dist/plugin/config/getPaths.js +21 -0
  50. package/dist/plugin/config/getPaths.js.map +1 -0
  51. package/dist/plugin/config/getWorkerPath.d.ts +2 -0
  52. package/dist/plugin/config/getWorkerPath.d.ts.map +1 -0
  53. package/dist/plugin/config/getWorkerPath.js +4 -0
  54. package/dist/plugin/config/index.d.ts +8 -0
  55. package/dist/plugin/config/index.d.ts.map +1 -0
  56. package/dist/plugin/config/index.js +7 -0
  57. package/dist/plugin/config/moduleIdDefault.d.ts +8 -0
  58. package/dist/plugin/config/moduleIdDefault.d.ts.map +1 -0
  59. package/dist/plugin/config/moduleIdDefault.js +23 -0
  60. package/dist/plugin/config/moduleIdDefault.js.map +1 -0
  61. package/dist/plugin/config/resolveOptions.d.ts +9 -0
  62. package/dist/plugin/config/resolveOptions.d.ts.map +1 -0
  63. package/dist/plugin/config/resolveOptions.js +56 -0
  64. package/dist/plugin/config/resolveOptions.js.map +1 -0
  65. package/dist/plugin/config/resolvePages.d.ts +9 -0
  66. package/dist/plugin/config/resolvePages.d.ts.map +1 -0
  67. package/dist/plugin/config/resolvePages.js +38 -0
  68. package/dist/plugin/config/resolvePages.js.map +1 -0
  69. package/dist/plugin/config/resolveUserConfig.d.ts +18 -0
  70. package/dist/plugin/config/resolveUserConfig.d.ts.map +1 -0
  71. package/dist/plugin/config/resolveUserConfig.js +84 -0
  72. package/dist/plugin/config/resolveUserConfig.js.map +1 -0
  73. package/dist/plugin/copy-dir.d.ts.map +1 -0
  74. package/dist/plugin/copy-dir.js +20 -0
  75. package/dist/plugin/getCondition.d.ts +2 -0
  76. package/dist/plugin/getCondition.d.ts.map +1 -0
  77. package/dist/plugin/getCondition.js +1 -0
  78. package/dist/plugin/getEnv.d.ts.map +1 -0
  79. package/dist/plugin/getEnv.js +107 -0
  80. package/dist/plugin/helpers/createClientInputNormalizer.d.ts +8 -0
  81. package/dist/plugin/helpers/createClientInputNormalizer.d.ts.map +1 -0
  82. package/dist/plugin/helpers/createClientInputNormalizer.js +35 -0
  83. package/dist/plugin/helpers/createServerInputNormalizer.d.ts +9 -0
  84. package/dist/plugin/helpers/createServerInputNormalizer.d.ts.map +1 -0
  85. package/dist/plugin/helpers/createServerInputNormalizer.js +37 -0
  86. package/dist/plugin/helpers/createStaticInputNormalizer.d.ts +7 -0
  87. package/dist/plugin/helpers/createStaticInputNormalizer.d.ts.map +1 -0
  88. package/dist/plugin/helpers/createStaticInputNormalizer.js +18 -0
  89. package/dist/plugin/helpers/getModuleManifest.d.ts +12 -0
  90. package/dist/plugin/helpers/getModuleManifest.d.ts.map +1 -0
  91. package/dist/plugin/helpers/getModuleManifest.js +24 -0
  92. package/dist/plugin/helpers/getModuleManifest.js.map +1 -0
  93. package/dist/plugin/helpers/inputNormalizer.d.ts +3 -0
  94. package/dist/plugin/helpers/inputNormalizer.d.ts.map +1 -0
  95. package/dist/plugin/helpers/inputNormalizer.js +35 -0
  96. package/dist/plugin/helpers/inputNormalizer.js.map +1 -0
  97. package/dist/plugin/helpers/inputNormalizerWorker.d.ts +11 -0
  98. package/dist/plugin/helpers/inputNormalizerWorker.d.ts.map +1 -0
  99. package/dist/plugin/helpers/inputNormalizerWorker.js +30 -0
  100. package/dist/plugin/helpers/normalizedRelativePath.d.ts.map +1 -0
  101. package/dist/plugin/helpers/normalizedRelativePath.js +36 -0
  102. package/dist/plugin/helpers/resolveFilePath.d.ts +13 -0
  103. package/dist/plugin/helpers/resolveFilePath.d.ts.map +1 -0
  104. package/dist/plugin/helpers/resolveFilePath.js +74 -0
  105. package/dist/plugin/helpers/resolveWorkerModule.d.ts +6 -0
  106. package/dist/plugin/helpers/resolveWorkerModule.d.ts.map +1 -0
  107. package/dist/plugin/helpers/resolveWorkerModule.js +24 -0
  108. package/dist/plugin/helpers/tryManifest.d.ts.map +1 -0
  109. package/dist/{src → plugin}/helpers/tryManifest.js +11 -6
  110. package/dist/plugin/helpers/tryManifest.js.map +1 -0
  111. package/dist/plugin/helpers/validateModuleBase.d.ts +3 -0
  112. package/dist/plugin/helpers/validateModuleBase.d.ts.map +1 -0
  113. package/dist/plugin/helpers/validateModuleBase.js +16 -0
  114. package/dist/plugin/helpers/validateResolvedConfig.d.ts +3 -0
  115. package/dist/plugin/helpers/validateResolvedConfig.d.ts.map +1 -0
  116. package/dist/plugin/helpers/validateResolvedConfig.js +17 -0
  117. package/dist/plugin/index.d.ts +3 -0
  118. package/dist/plugin/index.d.ts.map +1 -0
  119. package/dist/plugin/index.js +1 -0
  120. package/dist/plugin/loader/createBuildLoader.d.ts +9 -0
  121. package/dist/plugin/loader/createBuildLoader.d.ts.map +1 -0
  122. package/dist/plugin/loader/createBuildLoader.js +38 -0
  123. package/dist/plugin/loader/createBuildLoader.js.map +1 -0
  124. package/dist/plugin/loader/createCssLoader.d.ts +30 -0
  125. package/dist/plugin/loader/createCssLoader.d.ts.map +1 -0
  126. package/dist/plugin/loader/createCssLoader.js +35 -0
  127. package/dist/{html → plugin/loader}/createPageLoader.d.ts +2 -4
  128. package/dist/plugin/loader/createPageLoader.d.ts.map +1 -0
  129. package/dist/plugin/loader/createPageLoader.js +57 -0
  130. package/dist/plugin/manifest.d.ts.map +1 -0
  131. package/dist/plugin/manifest.js +13 -0
  132. package/dist/plugin/module-graph.d.ts.map +1 -0
  133. package/dist/plugin/module-graph.js +35 -0
  134. package/dist/plugin/plugin.d.ts +7 -0
  135. package/dist/plugin/plugin.d.ts.map +1 -0
  136. package/dist/plugin/plugin.js +8 -0
  137. package/dist/plugin/preserver/index.d.ts +2 -0
  138. package/dist/plugin/preserver/index.d.ts.map +1 -0
  139. package/dist/plugin/preserver/index.js +1 -0
  140. package/dist/plugin/preserver/plugin.d.ts +3 -0
  141. package/dist/plugin/preserver/plugin.d.ts.map +1 -0
  142. package/dist/plugin/preserver/plugin.js +90 -0
  143. package/dist/plugin/preserver/plugin.js.map +1 -0
  144. package/dist/plugin/react-client/index.d.ts +2 -0
  145. package/dist/plugin/react-client/index.d.ts.map +1 -0
  146. package/dist/plugin/react-client/index.js +1 -0
  147. package/dist/plugin/react-client/plugin.d.ts +4 -0
  148. package/dist/plugin/react-client/plugin.d.ts.map +1 -0
  149. package/dist/plugin/react-client/plugin.js +34 -0
  150. package/dist/plugin/react-client/plugin.js.map +1 -0
  151. package/dist/plugin/react-server/createDevMiddleware.d.ts.map +1 -0
  152. package/dist/plugin/react-server/createDevMiddleware.js +68 -0
  153. package/dist/plugin/react-server/createDevServer.d.ts.map +1 -0
  154. package/dist/plugin/react-server/createDevServer.js +4 -0
  155. package/dist/plugin/react-server/createHandler.d.ts +17 -0
  156. package/dist/plugin/react-server/createHandler.d.ts.map +1 -0
  157. package/dist/{src → plugin}/react-server/createHandler.js +28 -17
  158. package/dist/plugin/react-server/createHandler.js.map +1 -0
  159. package/dist/plugin/react-server/createReactNodeStreamer.d.ts.map +1 -0
  160. package/dist/plugin/react-server/createReactNodeStreamer.js +7 -0
  161. package/dist/{react-server → plugin/react-server}/createRscStream.d.ts +1 -1
  162. package/dist/plugin/react-server/createRscStream.d.ts.map +1 -0
  163. package/dist/{src → plugin}/react-server/createRscStream.js +14 -13
  164. package/dist/plugin/react-server/createRscStream.js.map +1 -0
  165. package/dist/{react-server → plugin/react-server}/createSsrHandler.d.ts +1 -1
  166. package/dist/plugin/react-server/createSsrHandler.d.ts.map +1 -0
  167. package/dist/plugin/react-server/createSsrHandler.js +102 -0
  168. package/dist/plugin/react-server/index.d.ts +3 -0
  169. package/dist/plugin/react-server/index.d.ts.map +1 -0
  170. package/dist/plugin/react-server/index.js +10 -0
  171. package/dist/plugin/react-server/plugin.d.ts +7 -0
  172. package/dist/plugin/react-server/plugin.d.ts.map +1 -0
  173. package/dist/plugin/react-server/plugin.js +377 -0
  174. package/dist/plugin/react-server/plugin.js.map +1 -0
  175. package/dist/plugin/resolvePage.d.ts.map +1 -0
  176. package/dist/{src → plugin}/resolvePage.js +8 -4
  177. package/dist/plugin/resolvePage.js.map +1 -0
  178. package/dist/plugin/resolveProps.d.ts.map +1 -0
  179. package/dist/{src → plugin}/resolveProps.js +21 -8
  180. package/dist/plugin/resolveProps.js.map +1 -0
  181. package/dist/plugin/server.d.ts +2 -0
  182. package/dist/plugin/server.d.ts.map +1 -0
  183. package/dist/plugin/server.js +1 -0
  184. package/dist/plugin/transformer/index.d.ts +2 -0
  185. package/dist/plugin/transformer/index.d.ts.map +1 -0
  186. package/dist/plugin/transformer/index.js +1 -0
  187. package/dist/{transformer/index.d.ts → plugin/transformer/plugin.d.ts} +3 -4
  188. package/dist/plugin/transformer/plugin.d.ts.map +1 -0
  189. package/dist/plugin/transformer/plugin.js +94 -0
  190. package/dist/plugin/transformer/plugin.js.map +1 -0
  191. package/dist/plugin/transformer/transformer-client-components.d.ts +21 -0
  192. package/dist/plugin/transformer/transformer-client-components.d.ts.map +1 -0
  193. package/dist/plugin/transformer/transformer-client-components.js +61 -0
  194. package/dist/plugin/transformer/transformer-client-components.js.map +1 -0
  195. package/dist/{transformer/transformer.d.ts → plugin/transformer/transformer-server-actions.d.ts} +3 -4
  196. package/dist/plugin/transformer/transformer-server-actions.d.ts.map +1 -0
  197. package/dist/plugin/transformer/transformer-server-actions.js +76 -0
  198. package/dist/plugin/transformer/transformer-server-actions.js.map +1 -0
  199. package/dist/plugin/transformer/types.d.ts.map +1 -0
  200. package/dist/plugin/transformer/types.js +1 -0
  201. package/dist/plugin/types.d.ts +225 -0
  202. package/dist/plugin/types.d.ts.map +1 -0
  203. package/dist/plugin/types.js +1 -0
  204. package/dist/plugin/worker/createWorker.d.ts +15 -0
  205. package/dist/plugin/worker/createWorker.d.ts.map +1 -0
  206. package/dist/plugin/worker/createWorker.js +71 -0
  207. package/dist/plugin/worker/createWorker.js.map +1 -0
  208. package/dist/plugin/worker/html/html-worker.d.ts +2 -0
  209. package/dist/plugin/worker/html/html-worker.d.ts.map +1 -0
  210. package/dist/plugin/worker/html/html-worker.development.d.ts +2 -0
  211. package/dist/plugin/worker/html/html-worker.development.d.ts.map +1 -0
  212. package/dist/plugin/worker/html/html-worker.development.js +12 -0
  213. package/dist/plugin/worker/html/html-worker.development.js.map +1 -0
  214. package/dist/plugin/worker/html/html-worker.js +4 -0
  215. package/dist/plugin/worker/html/html-worker.production.d.ts +2 -0
  216. package/dist/plugin/worker/html/html-worker.production.d.ts.map +1 -0
  217. package/dist/plugin/worker/html/html-worker.production.js +12 -0
  218. package/dist/plugin/worker/html/html-worker.production.js.map +1 -0
  219. package/dist/plugin/worker/html/index.d.ts +2 -0
  220. package/dist/plugin/worker/html/index.d.ts.map +1 -0
  221. package/dist/plugin/worker/html/index.js +7 -0
  222. package/dist/plugin/worker/html/index.js.map +1 -0
  223. package/dist/plugin/worker/html/messageHandler.d.ts +3 -0
  224. package/dist/plugin/worker/html/messageHandler.d.ts.map +1 -0
  225. package/dist/plugin/worker/html/messageHandler.js +98 -0
  226. package/dist/plugin/worker/html/messageHandler.js.map +1 -0
  227. package/dist/plugin/worker/html/plugin.d.ts +4 -0
  228. package/dist/plugin/worker/html/plugin.d.ts.map +1 -0
  229. package/dist/plugin/worker/html/plugin.js +89 -0
  230. package/dist/plugin/worker/html/renderPages.d.ts +38 -0
  231. package/dist/plugin/worker/html/renderPages.d.ts.map +1 -0
  232. package/dist/plugin/worker/html/renderPages.js +123 -0
  233. package/dist/plugin/worker/html/renderPages.js.map +1 -0
  234. package/dist/plugin/worker/loader.d.ts.map +1 -0
  235. package/dist/plugin/worker/loader.js +11 -0
  236. package/dist/plugin/worker/loader.js.map +1 -0
  237. package/dist/plugin/worker/plugin.d.ts +10 -0
  238. package/dist/plugin/worker/plugin.d.ts.map +1 -0
  239. package/dist/plugin/worker/plugin.js +14 -0
  240. package/dist/plugin/worker/rsc/createRscStream.d.ts +5 -0
  241. package/dist/plugin/worker/rsc/createRscStream.d.ts.map +1 -0
  242. package/dist/plugin/worker/rsc/createRscStream.js +39 -0
  243. package/dist/plugin/worker/rsc/createRscStream.js.map +1 -0
  244. package/dist/plugin/worker/rsc/development.d.ts +5 -0
  245. package/dist/plugin/worker/rsc/development.d.ts.map +1 -0
  246. package/dist/plugin/worker/rsc/development.js +13 -0
  247. package/dist/plugin/worker/rsc/development.js.map +1 -0
  248. package/dist/plugin/worker/rsc/index.d.ts +4 -0
  249. package/dist/plugin/worker/rsc/index.d.ts.map +1 -0
  250. package/dist/plugin/worker/rsc/index.js +15 -0
  251. package/dist/plugin/worker/rsc/index.js.map +1 -0
  252. package/dist/plugin/worker/rsc/plugin.d.ts +4 -0
  253. package/dist/plugin/worker/rsc/plugin.d.ts.map +1 -0
  254. package/dist/plugin/worker/rsc/plugin.js +82 -0
  255. package/dist/plugin/worker/rsc/plugin.js.map +1 -0
  256. package/dist/plugin/worker/rsc/production.d.ts +5 -0
  257. package/dist/plugin/worker/rsc/production.d.ts.map +1 -0
  258. package/dist/plugin/worker/rsc/production.js +13 -0
  259. package/dist/plugin/worker/rsc/production.js.map +1 -0
  260. package/dist/plugin/worker/rsc/rsc-worker.d.ts +2 -0
  261. package/dist/plugin/worker/rsc/rsc-worker.d.ts.map +1 -0
  262. package/dist/plugin/worker/rsc/rsc-worker.js +106 -0
  263. package/dist/plugin/worker/types.d.ts +99 -0
  264. package/dist/plugin/worker/types.d.ts.map +1 -0
  265. package/dist/plugin/worker/types.js +1 -0
  266. package/dist/server.d.ts +4 -1
  267. package/dist/server.d.ts.map +1 -1
  268. package/dist/server.js +9 -0
  269. package/dist/server.js.map +1 -0
  270. package/dist/tsconfig.tsbuildinfo +1 -0
  271. package/dist/types.d.ts +1 -201
  272. package/dist/types.d.ts.map +1 -1
  273. package/dist/types.js +1 -0
  274. package/package.json +42 -25
  275. package/plugin/build/createClientBuildConfig.ts +21 -0
  276. package/plugin/build/createServerBuildConfig.ts +66 -0
  277. package/plugin/build/createSharedBuildConfig.ts +35 -0
  278. package/plugin/build/mergeInputs.ts +58 -0
  279. package/plugin/checkFilesExist.ts +82 -0
  280. package/{src → plugin}/collect-css-manifest.ts +4 -2
  281. package/plugin/config/defaults.ts +44 -0
  282. package/plugin/config/getCondition.ts +3 -0
  283. package/plugin/config/getPaths.ts +25 -0
  284. package/plugin/config/getWorkerPath.ts +5 -0
  285. package/plugin/config/index.ts +8 -0
  286. package/plugin/config/moduleIdDefault.ts +23 -0
  287. package/plugin/config/resolveOptions.ts +53 -0
  288. package/plugin/config/resolvePages.ts +43 -0
  289. package/plugin/config/resolveUserConfig.ts +97 -0
  290. package/{src → plugin}/getEnv.ts +1 -1
  291. package/plugin/helpers/createClientInputNormalizer.ts +48 -0
  292. package/plugin/helpers/createServerInputNormalizer.ts +52 -0
  293. package/plugin/helpers/createStaticInputNormalizer.ts +26 -0
  294. package/plugin/helpers/getModuleManifest.ts +31 -0
  295. package/plugin/helpers/inputNormalizer.ts +39 -0
  296. package/plugin/helpers/inputNormalizerWorker.ts +47 -0
  297. package/plugin/helpers/resolveFilePath.ts +108 -0
  298. package/plugin/helpers/resolveWorkerModule.ts +41 -0
  299. package/plugin/helpers/validateModuleBase.ts +30 -0
  300. package/plugin/helpers/validateResolvedConfig.ts +21 -0
  301. package/plugin/index.ts +2 -0
  302. package/plugin/loader/createBuildLoader.ts +44 -0
  303. package/plugin/loader/createCssLoader.ts +73 -0
  304. package/{src/html → plugin/loader}/createPageLoader.ts +35 -27
  305. package/plugin/plugin.ts +8 -0
  306. package/plugin/preserver/index.ts +1 -0
  307. package/plugin/preserver/plugin.ts +109 -0
  308. package/plugin/react-client/index.ts +1 -0
  309. package/plugin/react-client/plugin.ts +33 -0
  310. package/{src → plugin}/react-server/createDevMiddleware.ts +17 -1
  311. package/{src → plugin}/react-server/createDevServer.ts +0 -1
  312. package/{src → plugin}/react-server/createHandler.ts +31 -10
  313. package/{src → plugin}/react-server/createReactNodeStreamer.ts +1 -0
  314. package/{src → plugin}/react-server/createRscStream.ts +8 -12
  315. package/{src → plugin}/react-server/createSsrHandler.ts +16 -19
  316. package/plugin/react-server/index.ts +12 -0
  317. package/plugin/react-server/plugin.ts +464 -0
  318. package/{src → plugin}/resolvePage.ts +1 -1
  319. package/{src → plugin}/resolveProps.ts +15 -6
  320. package/plugin/server.tsx +0 -0
  321. package/plugin/transformer/index.ts +1 -0
  322. package/plugin/transformer/plugin.ts +141 -0
  323. package/plugin/transformer/transformer-client-components.ts +94 -0
  324. package/{src/transformer/transformer.ts → plugin/transformer/transformer-server-actions.ts} +14 -27
  325. package/{src → plugin}/types.ts +97 -55
  326. package/plugin/worker/createWorker.ts +84 -0
  327. package/plugin/worker/html/html-worker.development.tsx +8 -0
  328. package/plugin/worker/html/html-worker.production.tsx +8 -0
  329. package/plugin/worker/html/html-worker.ts +5 -0
  330. package/plugin/worker/html/index.ts +5 -0
  331. package/plugin/worker/html/messageHandler.ts +116 -0
  332. package/plugin/worker/html/plugin.ts +97 -0
  333. package/plugin/worker/html/renderPages.ts +188 -0
  334. package/plugin/worker/plugin.ts +17 -0
  335. package/plugin/worker/rsc/createRscStream.ts +42 -0
  336. package/plugin/worker/rsc/development.ts +6 -0
  337. package/plugin/worker/rsc/index.ts +14 -0
  338. package/plugin/worker/rsc/plugin.ts +83 -0
  339. package/plugin/worker/rsc/production.ts +6 -0
  340. package/plugin/worker/rsc/rsc-worker.tsx +128 -0
  341. package/plugin/worker/types.ts +124 -0
  342. package/scripts/check-react-version.mjs +48 -30
  343. package/scripts/react+0.0.0-experimental-b3a95caf-20250113.patch +4291 -0
  344. package/scripts/react-dom+0.0.0-experimental-b3a95caf-20250113.patch +108798 -0
  345. package/scripts/react-server-dom-esm+0.0.0-experimental-b3a95caf-20250113.patch +24775 -0
  346. package/{patches → scripts}/react-server-dom-esm+0.0.1.patch +11354 -3
  347. package/tsconfig.json +15 -13
  348. package/dist/assertServerCondition.d.ts.map +0 -1
  349. package/dist/bin/patch.js +0 -51
  350. package/dist/bin/patch.js.map +0 -1
  351. package/dist/build/createBuildConfig.d.ts +0 -13
  352. package/dist/build/createBuildConfig.d.ts.map +0 -1
  353. package/dist/build/mergeInputs.d.ts +0 -5
  354. package/dist/build/mergeInputs.d.ts.map +0 -1
  355. package/dist/checkFilesExist.d.ts +0 -8
  356. package/dist/checkFilesExist.d.ts.map +0 -1
  357. package/dist/collect-css-manifest.d.ts.map +0 -1
  358. package/dist/components.d.ts.map +0 -1
  359. package/dist/copy-dir.d.ts.map +0 -1
  360. package/dist/getEnv.d.ts.map +0 -1
  361. package/dist/helpers/inputNormalizer.d.ts +0 -6
  362. package/dist/helpers/inputNormalizer.d.ts.map +0 -1
  363. package/dist/helpers/normalizedRelativePath.d.ts.map +0 -1
  364. package/dist/helpers/tryManifest.d.ts.map +0 -1
  365. package/dist/html/createPageLoader.d.ts.map +0 -1
  366. package/dist/manifest.d.ts.map +0 -1
  367. package/dist/module-graph.d.ts.map +0 -1
  368. package/dist/options.d.ts +0 -91
  369. package/dist/options.d.ts.map +0 -1
  370. package/dist/plugin.d.ts +0 -3
  371. package/dist/plugin.d.ts.map +0 -1
  372. package/dist/react-client/plugin.d.ts +0 -4
  373. package/dist/react-client/plugin.d.ts.map +0 -1
  374. package/dist/react-client/plugin.js +0 -28
  375. package/dist/react-client/plugin.js.map +0 -1
  376. package/dist/react-server/createDevMiddleware.d.ts.map +0 -1
  377. package/dist/react-server/createDevServer.d.ts.map +0 -1
  378. package/dist/react-server/createHandler.d.ts +0 -23
  379. package/dist/react-server/createHandler.d.ts.map +0 -1
  380. package/dist/react-server/createReactNodeStreamer.d.ts.map +0 -1
  381. package/dist/react-server/createRscStream.d.ts.map +0 -1
  382. package/dist/react-server/createSsrHandler.d.ts.map +0 -1
  383. package/dist/react-server/plugin.d.ts +0 -8
  384. package/dist/react-server/plugin.d.ts.map +0 -1
  385. package/dist/react-server/plugin.js +0 -345
  386. package/dist/react-server/plugin.js.map +0 -1
  387. package/dist/resolvePage.d.ts.map +0 -1
  388. package/dist/resolveProps.d.ts.map +0 -1
  389. package/dist/scripts/check-react-version.js +0 -34
  390. package/dist/scripts/check-react-version.js.map +0 -1
  391. package/dist/src/build/createBuildConfig.js +0 -44
  392. package/dist/src/build/createBuildConfig.js.map +0 -1
  393. package/dist/src/build/mergeInputs.js +0 -16
  394. package/dist/src/build/mergeInputs.js.map +0 -1
  395. package/dist/src/checkFilesExist.js +0 -61
  396. package/dist/src/checkFilesExist.js.map +0 -1
  397. package/dist/src/collect-css-manifest.js.map +0 -1
  398. package/dist/src/components.js.map +0 -1
  399. package/dist/src/getEnv.js +0 -76
  400. package/dist/src/getEnv.js.map +0 -1
  401. package/dist/src/helpers/inputNormalizer.js +0 -11
  402. package/dist/src/helpers/inputNormalizer.js.map +0 -1
  403. package/dist/src/helpers/normalizedRelativePath.js +0 -34
  404. package/dist/src/helpers/normalizedRelativePath.js.map +0 -1
  405. package/dist/src/helpers/tryManifest.js.map +0 -1
  406. package/dist/src/html/createPageLoader.js +0 -72
  407. package/dist/src/html/createPageLoader.js.map +0 -1
  408. package/dist/src/options.js +0 -307
  409. package/dist/src/options.js.map +0 -1
  410. package/dist/src/react-server/createHandler.js.map +0 -1
  411. package/dist/src/react-server/createRscStream.js.map +0 -1
  412. package/dist/src/resolvePage.js.map +0 -1
  413. package/dist/src/resolveProps.js.map +0 -1
  414. package/dist/src/worker/createHtmlStream.js +0 -62
  415. package/dist/src/worker/createHtmlStream.js.map +0 -1
  416. package/dist/src/worker/createWorker.js +0 -34
  417. package/dist/src/worker/createWorker.js.map +0 -1
  418. package/dist/src/worker/renderPages.js +0 -99
  419. package/dist/src/worker/renderPages.js.map +0 -1
  420. package/dist/transformer/index.d.ts.map +0 -1
  421. package/dist/transformer/preserveDirectives.d.ts +0 -4
  422. package/dist/transformer/preserveDirectives.d.ts.map +0 -1
  423. package/dist/transformer/preserver.d.ts +0 -2
  424. package/dist/transformer/preserver.d.ts.map +0 -1
  425. package/dist/transformer/transformer.d.ts.map +0 -1
  426. package/dist/transformer/types.d.ts.map +0 -1
  427. package/dist/worker/createHtmlStream.d.ts +0 -7
  428. package/dist/worker/createHtmlStream.d.ts.map +0 -1
  429. package/dist/worker/createWorker.d.ts +0 -10
  430. package/dist/worker/createWorker.d.ts.map +0 -1
  431. package/dist/worker/loader.d.ts.map +0 -1
  432. package/dist/worker/loader.js +0 -7
  433. package/dist/worker/loader.js.map +0 -1
  434. package/dist/worker/renderPages.d.ts +0 -18
  435. package/dist/worker/renderPages.d.ts.map +0 -1
  436. package/dist/worker/types.d.ts +0 -31
  437. package/dist/worker/types.d.ts.map +0 -1
  438. package/dist/worker/worker.d.ts +0 -7
  439. package/dist/worker/worker.d.ts.map +0 -1
  440. package/dist/worker/worker.js +0 -112
  441. package/dist/worker/worker.js.map +0 -1
  442. package/src/build/createBuildConfig.ts +0 -57
  443. package/src/build/mergeInputs.ts +0 -42
  444. package/src/checkFilesExist.ts +0 -67
  445. package/src/helpers/inputNormalizer.ts +0 -22
  446. package/src/index.ts +0 -4
  447. package/src/options.ts +0 -423
  448. package/src/plugin.ts +0 -5
  449. package/src/react-client/plugin.ts +0 -34
  450. package/src/react-server/plugin.ts +0 -409
  451. package/src/transformer/index.ts +0 -112
  452. package/src/transformer/preserveDirectives.ts +0 -100
  453. package/src/transformer/preserver.ts +0 -47
  454. package/src/worker/createHtmlStream.ts +0 -76
  455. package/src/worker/createWorker.ts +0 -44
  456. package/src/worker/renderPages.ts +0 -144
  457. package/src/worker/types.ts +0 -38
  458. package/src/worker/worker.tsx +0 -136
  459. /package/dist/{assertServerCondition.d.ts → plugin/assertServerCondition.d.ts} +0 -0
  460. /package/dist/{components.d.ts → plugin/components.d.ts} +0 -0
  461. /package/dist/{copy-dir.d.ts → plugin/copy-dir.d.ts} +0 -0
  462. /package/dist/{getEnv.d.ts → plugin/getEnv.d.ts} +0 -0
  463. /package/dist/{helpers → plugin/helpers}/normalizedRelativePath.d.ts +0 -0
  464. /package/dist/{helpers → plugin/helpers}/tryManifest.d.ts +0 -0
  465. /package/dist/{manifest.d.ts → plugin/manifest.d.ts} +0 -0
  466. /package/dist/{module-graph.d.ts → plugin/module-graph.d.ts} +0 -0
  467. /package/dist/{react-server → plugin/react-server}/createDevMiddleware.d.ts +0 -0
  468. /package/dist/{react-server → plugin/react-server}/createDevServer.d.ts +0 -0
  469. /package/dist/{react-server → plugin/react-server}/createReactNodeStreamer.d.ts +0 -0
  470. /package/dist/{resolvePage.d.ts → plugin/resolvePage.d.ts} +0 -0
  471. /package/dist/{resolveProps.d.ts → plugin/resolveProps.d.ts} +0 -0
  472. /package/dist/{transformer → plugin/transformer}/types.d.ts +0 -0
  473. /package/dist/{worker → plugin/worker}/loader.d.ts +0 -0
  474. /package/{src → plugin}/assertServerCondition.ts +0 -0
  475. /package/{src → plugin}/components.tsx +0 -0
  476. /package/{src → plugin}/copy-dir.ts +0 -0
  477. /package/{src/server.tsx → plugin/getCondition.ts} +0 -0
  478. /package/{src → plugin}/helpers/normalizedRelativePath.ts +0 -0
  479. /package/{src → plugin}/helpers/tryManifest.ts +0 -0
  480. /package/{src → plugin}/manifest.ts +0 -0
  481. /package/{src → plugin}/module-graph.ts +0 -0
  482. /package/{src → plugin}/transformer/README.md +0 -0
  483. /package/{src → plugin}/transformer/types.ts +0 -0
  484. /package/{src → plugin}/worker/loader.ts +0 -0
@@ -0,0 +1,1283 @@
1
+ /**
2
+ * vite-plugin-react-server
3
+ * Copyright (c) Nico Brinkkemper
4
+ * MIT License
5
+ */
6
+ import { encode } from '@jridgewell/sourcemap-codec';
7
+
8
+ class BitSet {
9
+ constructor(arg) {
10
+ this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
11
+ }
12
+
13
+ add(n) {
14
+ this.bits[n >> 5] |= 1 << (n & 31);
15
+ }
16
+
17
+ has(n) {
18
+ return !!(this.bits[n >> 5] & (1 << (n & 31)));
19
+ }
20
+ }
21
+
22
+ class Chunk {
23
+ constructor(start, end, content) {
24
+ this.start = start;
25
+ this.end = end;
26
+ this.original = content;
27
+
28
+ this.intro = '';
29
+ this.outro = '';
30
+
31
+ this.content = content;
32
+ this.storeName = false;
33
+ this.edited = false;
34
+
35
+ {
36
+ this.previous = null;
37
+ this.next = null;
38
+ }
39
+ }
40
+
41
+ appendLeft(content) {
42
+ this.outro += content;
43
+ }
44
+
45
+ appendRight(content) {
46
+ this.intro = this.intro + content;
47
+ }
48
+
49
+ clone() {
50
+ const chunk = new Chunk(this.start, this.end, this.original);
51
+
52
+ chunk.intro = this.intro;
53
+ chunk.outro = this.outro;
54
+ chunk.content = this.content;
55
+ chunk.storeName = this.storeName;
56
+ chunk.edited = this.edited;
57
+
58
+ return chunk;
59
+ }
60
+
61
+ contains(index) {
62
+ return this.start < index && index < this.end;
63
+ }
64
+
65
+ eachNext(fn) {
66
+ let chunk = this;
67
+ while (chunk) {
68
+ fn(chunk);
69
+ chunk = chunk.next;
70
+ }
71
+ }
72
+
73
+ eachPrevious(fn) {
74
+ let chunk = this;
75
+ while (chunk) {
76
+ fn(chunk);
77
+ chunk = chunk.previous;
78
+ }
79
+ }
80
+
81
+ edit(content, storeName, contentOnly) {
82
+ this.content = content;
83
+ if (!contentOnly) {
84
+ this.intro = '';
85
+ this.outro = '';
86
+ }
87
+ this.storeName = storeName;
88
+
89
+ this.edited = true;
90
+
91
+ return this;
92
+ }
93
+
94
+ prependLeft(content) {
95
+ this.outro = content + this.outro;
96
+ }
97
+
98
+ prependRight(content) {
99
+ this.intro = content + this.intro;
100
+ }
101
+
102
+ reset() {
103
+ this.intro = '';
104
+ this.outro = '';
105
+ if (this.edited) {
106
+ this.content = this.original;
107
+ this.storeName = false;
108
+ this.edited = false;
109
+ }
110
+ }
111
+
112
+ split(index) {
113
+ const sliceIndex = index - this.start;
114
+
115
+ const originalBefore = this.original.slice(0, sliceIndex);
116
+ const originalAfter = this.original.slice(sliceIndex);
117
+
118
+ this.original = originalBefore;
119
+
120
+ const newChunk = new Chunk(index, this.end, originalAfter);
121
+ newChunk.outro = this.outro;
122
+ this.outro = '';
123
+
124
+ this.end = index;
125
+
126
+ if (this.edited) {
127
+ // after split we should save the edit content record into the correct chunk
128
+ // to make sure sourcemap correct
129
+ // For example:
130
+ // ' test'.trim()
131
+ // split -> ' ' + 'test'
132
+ // ✔️ edit -> '' + 'test'
133
+ // ✖️ edit -> 'test' + ''
134
+ // TODO is this block necessary?...
135
+ newChunk.edit('', false);
136
+ this.content = '';
137
+ } else {
138
+ this.content = originalBefore;
139
+ }
140
+
141
+ newChunk.next = this.next;
142
+ if (newChunk.next) newChunk.next.previous = newChunk;
143
+ newChunk.previous = this;
144
+ this.next = newChunk;
145
+
146
+ return newChunk;
147
+ }
148
+
149
+ toString() {
150
+ return this.intro + this.content + this.outro;
151
+ }
152
+
153
+ trimEnd(rx) {
154
+ this.outro = this.outro.replace(rx, '');
155
+ if (this.outro.length) return true;
156
+
157
+ const trimmed = this.content.replace(rx, '');
158
+
159
+ if (trimmed.length) {
160
+ if (trimmed !== this.content) {
161
+ this.split(this.start + trimmed.length).edit('', undefined, true);
162
+ if (this.edited) {
163
+ // save the change, if it has been edited
164
+ this.edit(trimmed, this.storeName, true);
165
+ }
166
+ }
167
+ return true;
168
+ } else {
169
+ this.edit('', undefined, true);
170
+
171
+ this.intro = this.intro.replace(rx, '');
172
+ if (this.intro.length) return true;
173
+ }
174
+ }
175
+
176
+ trimStart(rx) {
177
+ this.intro = this.intro.replace(rx, '');
178
+ if (this.intro.length) return true;
179
+
180
+ const trimmed = this.content.replace(rx, '');
181
+
182
+ if (trimmed.length) {
183
+ if (trimmed !== this.content) {
184
+ const newChunk = this.split(this.end - trimmed.length);
185
+ if (this.edited) {
186
+ // save the change, if it has been edited
187
+ newChunk.edit(trimmed, this.storeName, true);
188
+ }
189
+ this.edit('', undefined, true);
190
+ }
191
+ return true;
192
+ } else {
193
+ this.edit('', undefined, true);
194
+
195
+ this.outro = this.outro.replace(rx, '');
196
+ if (this.outro.length) return true;
197
+ }
198
+ }
199
+ }
200
+
201
+ function getBtoa() {
202
+ if (typeof globalThis !== 'undefined' && typeof globalThis.btoa === 'function') {
203
+ return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
204
+ } else if (typeof Buffer === 'function') {
205
+ return (str) => Buffer.from(str, 'utf-8').toString('base64');
206
+ } else {
207
+ return () => {
208
+ throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
209
+ };
210
+ }
211
+ }
212
+
213
+ const btoa = /*#__PURE__*/ getBtoa();
214
+
215
+ class SourceMap {
216
+ constructor(properties) {
217
+ this.version = 3;
218
+ this.file = properties.file;
219
+ this.sources = properties.sources;
220
+ this.sourcesContent = properties.sourcesContent;
221
+ this.names = properties.names;
222
+ this.mappings = encode(properties.mappings);
223
+ if (typeof properties.x_google_ignoreList !== 'undefined') {
224
+ this.x_google_ignoreList = properties.x_google_ignoreList;
225
+ }
226
+ if (typeof properties.debugId !== 'undefined') {
227
+ this.debugId = properties.debugId;
228
+ }
229
+ }
230
+
231
+ toString() {
232
+ return JSON.stringify(this);
233
+ }
234
+
235
+ toUrl() {
236
+ return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
237
+ }
238
+ }
239
+
240
+ function guessIndent(code) {
241
+ const lines = code.split('\n');
242
+
243
+ const tabbed = lines.filter((line) => /^\t+/.test(line));
244
+ const spaced = lines.filter((line) => /^ {2,}/.test(line));
245
+
246
+ if (tabbed.length === 0 && spaced.length === 0) {
247
+ return null;
248
+ }
249
+
250
+ // More lines tabbed than spaced? Assume tabs, and
251
+ // default to tabs in the case of a tie (or nothing
252
+ // to go on)
253
+ if (tabbed.length >= spaced.length) {
254
+ return '\t';
255
+ }
256
+
257
+ // Otherwise, we need to guess the multiple
258
+ const min = spaced.reduce((previous, current) => {
259
+ const numSpaces = /^ +/.exec(current)[0].length;
260
+ return Math.min(numSpaces, previous);
261
+ }, Infinity);
262
+
263
+ return new Array(min + 1).join(' ');
264
+ }
265
+
266
+ function getRelativePath(from, to) {
267
+ const fromParts = from.split(/[/\\]/);
268
+ const toParts = to.split(/[/\\]/);
269
+
270
+ fromParts.pop(); // get dirname
271
+
272
+ while (fromParts[0] === toParts[0]) {
273
+ fromParts.shift();
274
+ toParts.shift();
275
+ }
276
+
277
+ if (fromParts.length) {
278
+ let i = fromParts.length;
279
+ while (i--) fromParts[i] = '..';
280
+ }
281
+
282
+ return fromParts.concat(toParts).join('/');
283
+ }
284
+
285
+ const toString = Object.prototype.toString;
286
+
287
+ function isObject(thing) {
288
+ return toString.call(thing) === '[object Object]';
289
+ }
290
+
291
+ function getLocator(source) {
292
+ const originalLines = source.split('\n');
293
+ const lineOffsets = [];
294
+
295
+ for (let i = 0, pos = 0; i < originalLines.length; i++) {
296
+ lineOffsets.push(pos);
297
+ pos += originalLines[i].length + 1;
298
+ }
299
+
300
+ return function locate(index) {
301
+ let i = 0;
302
+ let j = lineOffsets.length;
303
+ while (i < j) {
304
+ const m = (i + j) >> 1;
305
+ if (index < lineOffsets[m]) {
306
+ j = m;
307
+ } else {
308
+ i = m + 1;
309
+ }
310
+ }
311
+ const line = i - 1;
312
+ const column = index - lineOffsets[line];
313
+ return { line, column };
314
+ };
315
+ }
316
+
317
+ const wordRegex = /\w/;
318
+
319
+ class Mappings {
320
+ constructor(hires) {
321
+ this.hires = hires;
322
+ this.generatedCodeLine = 0;
323
+ this.generatedCodeColumn = 0;
324
+ this.raw = [];
325
+ this.rawSegments = this.raw[this.generatedCodeLine] = [];
326
+ this.pending = null;
327
+ }
328
+
329
+ addEdit(sourceIndex, content, loc, nameIndex) {
330
+ if (content.length) {
331
+ const contentLengthMinusOne = content.length - 1;
332
+ let contentLineEnd = content.indexOf('\n', 0);
333
+ let previousContentLineEnd = -1;
334
+ // Loop through each line in the content and add a segment, but stop if the last line is empty,
335
+ // else code afterwards would fill one line too many
336
+ while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
337
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
338
+ if (nameIndex >= 0) {
339
+ segment.push(nameIndex);
340
+ }
341
+ this.rawSegments.push(segment);
342
+
343
+ this.generatedCodeLine += 1;
344
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
345
+ this.generatedCodeColumn = 0;
346
+
347
+ previousContentLineEnd = contentLineEnd;
348
+ contentLineEnd = content.indexOf('\n', contentLineEnd + 1);
349
+ }
350
+
351
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
352
+ if (nameIndex >= 0) {
353
+ segment.push(nameIndex);
354
+ }
355
+ this.rawSegments.push(segment);
356
+
357
+ this.advance(content.slice(previousContentLineEnd + 1));
358
+ } else if (this.pending) {
359
+ this.rawSegments.push(this.pending);
360
+ this.advance(content);
361
+ }
362
+
363
+ this.pending = null;
364
+ }
365
+
366
+ addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
367
+ let originalCharIndex = chunk.start;
368
+ let first = true;
369
+ // when iterating each char, check if it's in a word boundary
370
+ let charInHiresBoundary = false;
371
+
372
+ while (originalCharIndex < chunk.end) {
373
+ if (original[originalCharIndex] === '\n') {
374
+ loc.line += 1;
375
+ loc.column = 0;
376
+ this.generatedCodeLine += 1;
377
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
378
+ this.generatedCodeColumn = 0;
379
+ first = true;
380
+ charInHiresBoundary = false;
381
+ } else {
382
+ if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
383
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
384
+
385
+ if (this.hires === 'boundary') {
386
+ // in hires "boundary", group segments per word boundary than per char
387
+ if (wordRegex.test(original[originalCharIndex])) {
388
+ // for first char in the boundary found, start the boundary by pushing a segment
389
+ if (!charInHiresBoundary) {
390
+ this.rawSegments.push(segment);
391
+ charInHiresBoundary = true;
392
+ }
393
+ } else {
394
+ // for non-word char, end the boundary by pushing a segment
395
+ this.rawSegments.push(segment);
396
+ charInHiresBoundary = false;
397
+ }
398
+ } else {
399
+ this.rawSegments.push(segment);
400
+ }
401
+ }
402
+
403
+ loc.column += 1;
404
+ this.generatedCodeColumn += 1;
405
+ first = false;
406
+ }
407
+
408
+ originalCharIndex += 1;
409
+ }
410
+
411
+ this.pending = null;
412
+ }
413
+
414
+ advance(str) {
415
+ if (!str) return;
416
+
417
+ const lines = str.split('\n');
418
+
419
+ if (lines.length > 1) {
420
+ for (let i = 0; i < lines.length - 1; i++) {
421
+ this.generatedCodeLine++;
422
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
423
+ }
424
+ this.generatedCodeColumn = 0;
425
+ }
426
+
427
+ this.generatedCodeColumn += lines[lines.length - 1].length;
428
+ }
429
+ }
430
+
431
+ const n = '\n';
432
+
433
+ const warned = {
434
+ insertLeft: false,
435
+ insertRight: false,
436
+ storeName: false,
437
+ };
438
+
439
+ class MagicString {
440
+ constructor(string, options = {}) {
441
+ const chunk = new Chunk(0, string.length, string);
442
+
443
+ Object.defineProperties(this, {
444
+ original: { writable: true, value: string },
445
+ outro: { writable: true, value: '' },
446
+ intro: { writable: true, value: '' },
447
+ firstChunk: { writable: true, value: chunk },
448
+ lastChunk: { writable: true, value: chunk },
449
+ lastSearchedChunk: { writable: true, value: chunk },
450
+ byStart: { writable: true, value: {} },
451
+ byEnd: { writable: true, value: {} },
452
+ filename: { writable: true, value: options.filename },
453
+ indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
454
+ sourcemapLocations: { writable: true, value: new BitSet() },
455
+ storedNames: { writable: true, value: {} },
456
+ indentStr: { writable: true, value: undefined },
457
+ ignoreList: { writable: true, value: options.ignoreList },
458
+ offset: { writable: true, value: options.offset || 0 },
459
+ });
460
+
461
+ this.byStart[0] = chunk;
462
+ this.byEnd[string.length] = chunk;
463
+ }
464
+
465
+ addSourcemapLocation(char) {
466
+ this.sourcemapLocations.add(char);
467
+ }
468
+
469
+ append(content) {
470
+ if (typeof content !== 'string') throw new TypeError('outro content must be a string');
471
+
472
+ this.outro += content;
473
+ return this;
474
+ }
475
+
476
+ appendLeft(index, content) {
477
+ index = index + this.offset;
478
+
479
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
480
+
481
+ this._split(index);
482
+
483
+ const chunk = this.byEnd[index];
484
+
485
+ if (chunk) {
486
+ chunk.appendLeft(content);
487
+ } else {
488
+ this.intro += content;
489
+ }
490
+ return this;
491
+ }
492
+
493
+ appendRight(index, content) {
494
+ index = index + this.offset;
495
+
496
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
497
+
498
+ this._split(index);
499
+
500
+ const chunk = this.byStart[index];
501
+
502
+ if (chunk) {
503
+ chunk.appendRight(content);
504
+ } else {
505
+ this.outro += content;
506
+ }
507
+ return this;
508
+ }
509
+
510
+ clone() {
511
+ const cloned = new MagicString(this.original, { filename: this.filename, offset: this.offset });
512
+
513
+ let originalChunk = this.firstChunk;
514
+ let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone());
515
+
516
+ while (originalChunk) {
517
+ cloned.byStart[clonedChunk.start] = clonedChunk;
518
+ cloned.byEnd[clonedChunk.end] = clonedChunk;
519
+
520
+ const nextOriginalChunk = originalChunk.next;
521
+ const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
522
+
523
+ if (nextClonedChunk) {
524
+ clonedChunk.next = nextClonedChunk;
525
+ nextClonedChunk.previous = clonedChunk;
526
+
527
+ clonedChunk = nextClonedChunk;
528
+ }
529
+
530
+ originalChunk = nextOriginalChunk;
531
+ }
532
+
533
+ cloned.lastChunk = clonedChunk;
534
+
535
+ if (this.indentExclusionRanges) {
536
+ cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
537
+ }
538
+
539
+ cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
540
+
541
+ cloned.intro = this.intro;
542
+ cloned.outro = this.outro;
543
+
544
+ return cloned;
545
+ }
546
+
547
+ generateDecodedMap(options) {
548
+ options = options || {};
549
+
550
+ const sourceIndex = 0;
551
+ const names = Object.keys(this.storedNames);
552
+ const mappings = new Mappings(options.hires);
553
+
554
+ const locate = getLocator(this.original);
555
+
556
+ if (this.intro) {
557
+ mappings.advance(this.intro);
558
+ }
559
+
560
+ this.firstChunk.eachNext((chunk) => {
561
+ const loc = locate(chunk.start);
562
+
563
+ if (chunk.intro.length) mappings.advance(chunk.intro);
564
+
565
+ if (chunk.edited) {
566
+ mappings.addEdit(
567
+ sourceIndex,
568
+ chunk.content,
569
+ loc,
570
+ chunk.storeName ? names.indexOf(chunk.original) : -1,
571
+ );
572
+ } else {
573
+ mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
574
+ }
575
+
576
+ if (chunk.outro.length) mappings.advance(chunk.outro);
577
+ });
578
+
579
+ return {
580
+ file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
581
+ sources: [
582
+ options.source ? getRelativePath(options.file || '', options.source) : options.file || '',
583
+ ],
584
+ sourcesContent: options.includeContent ? [this.original] : undefined,
585
+ names,
586
+ mappings: mappings.raw,
587
+ x_google_ignoreList: this.ignoreList ? [sourceIndex] : undefined,
588
+ };
589
+ }
590
+
591
+ generateMap(options) {
592
+ return new SourceMap(this.generateDecodedMap(options));
593
+ }
594
+
595
+ _ensureindentStr() {
596
+ if (this.indentStr === undefined) {
597
+ this.indentStr = guessIndent(this.original);
598
+ }
599
+ }
600
+
601
+ _getRawIndentString() {
602
+ this._ensureindentStr();
603
+ return this.indentStr;
604
+ }
605
+
606
+ getIndentString() {
607
+ this._ensureindentStr();
608
+ return this.indentStr === null ? '\t' : this.indentStr;
609
+ }
610
+
611
+ indent(indentStr, options) {
612
+ const pattern = /^[^\r\n]/gm;
613
+
614
+ if (isObject(indentStr)) {
615
+ options = indentStr;
616
+ indentStr = undefined;
617
+ }
618
+
619
+ if (indentStr === undefined) {
620
+ this._ensureindentStr();
621
+ indentStr = this.indentStr || '\t';
622
+ }
623
+
624
+ if (indentStr === '') return this; // noop
625
+
626
+ options = options || {};
627
+
628
+ // Process exclusion ranges
629
+ const isExcluded = {};
630
+
631
+ if (options.exclude) {
632
+ const exclusions =
633
+ typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
634
+ exclusions.forEach((exclusion) => {
635
+ for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
636
+ isExcluded[i] = true;
637
+ }
638
+ });
639
+ }
640
+
641
+ let shouldIndentNextCharacter = options.indentStart !== false;
642
+ const replacer = (match) => {
643
+ if (shouldIndentNextCharacter) return `${indentStr}${match}`;
644
+ shouldIndentNextCharacter = true;
645
+ return match;
646
+ };
647
+
648
+ this.intro = this.intro.replace(pattern, replacer);
649
+
650
+ let charIndex = 0;
651
+ let chunk = this.firstChunk;
652
+
653
+ while (chunk) {
654
+ const end = chunk.end;
655
+
656
+ if (chunk.edited) {
657
+ if (!isExcluded[charIndex]) {
658
+ chunk.content = chunk.content.replace(pattern, replacer);
659
+
660
+ if (chunk.content.length) {
661
+ shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
662
+ }
663
+ }
664
+ } else {
665
+ charIndex = chunk.start;
666
+
667
+ while (charIndex < end) {
668
+ if (!isExcluded[charIndex]) {
669
+ const char = this.original[charIndex];
670
+
671
+ if (char === '\n') {
672
+ shouldIndentNextCharacter = true;
673
+ } else if (char !== '\r' && shouldIndentNextCharacter) {
674
+ shouldIndentNextCharacter = false;
675
+
676
+ if (charIndex === chunk.start) {
677
+ chunk.prependRight(indentStr);
678
+ } else {
679
+ this._splitChunk(chunk, charIndex);
680
+ chunk = chunk.next;
681
+ chunk.prependRight(indentStr);
682
+ }
683
+ }
684
+ }
685
+
686
+ charIndex += 1;
687
+ }
688
+ }
689
+
690
+ charIndex = chunk.end;
691
+ chunk = chunk.next;
692
+ }
693
+
694
+ this.outro = this.outro.replace(pattern, replacer);
695
+
696
+ return this;
697
+ }
698
+
699
+ insert() {
700
+ throw new Error(
701
+ 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)',
702
+ );
703
+ }
704
+
705
+ insertLeft(index, content) {
706
+ if (!warned.insertLeft) {
707
+ console.warn(
708
+ 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead',
709
+ );
710
+ warned.insertLeft = true;
711
+ }
712
+
713
+ return this.appendLeft(index, content);
714
+ }
715
+
716
+ insertRight(index, content) {
717
+ if (!warned.insertRight) {
718
+ console.warn(
719
+ 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead',
720
+ );
721
+ warned.insertRight = true;
722
+ }
723
+
724
+ return this.prependRight(index, content);
725
+ }
726
+
727
+ move(start, end, index) {
728
+ start = start + this.offset;
729
+ end = end + this.offset;
730
+ index = index + this.offset;
731
+
732
+ if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself');
733
+
734
+ this._split(start);
735
+ this._split(end);
736
+ this._split(index);
737
+
738
+ const first = this.byStart[start];
739
+ const last = this.byEnd[end];
740
+
741
+ const oldLeft = first.previous;
742
+ const oldRight = last.next;
743
+
744
+ const newRight = this.byStart[index];
745
+ if (!newRight && last === this.lastChunk) return this;
746
+ const newLeft = newRight ? newRight.previous : this.lastChunk;
747
+
748
+ if (oldLeft) oldLeft.next = oldRight;
749
+ if (oldRight) oldRight.previous = oldLeft;
750
+
751
+ if (newLeft) newLeft.next = first;
752
+ if (newRight) newRight.previous = last;
753
+
754
+ if (!first.previous) this.firstChunk = last.next;
755
+ if (!last.next) {
756
+ this.lastChunk = first.previous;
757
+ this.lastChunk.next = null;
758
+ }
759
+
760
+ first.previous = newLeft;
761
+ last.next = newRight || null;
762
+
763
+ if (!newLeft) this.firstChunk = first;
764
+ if (!newRight) this.lastChunk = last;
765
+ return this;
766
+ }
767
+
768
+ overwrite(start, end, content, options) {
769
+ options = options || {};
770
+ return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
771
+ }
772
+
773
+ update(start, end, content, options) {
774
+ start = start + this.offset;
775
+ end = end + this.offset;
776
+
777
+ if (typeof content !== 'string') throw new TypeError('replacement content must be a string');
778
+
779
+ if (this.original.length !== 0) {
780
+ while (start < 0) start += this.original.length;
781
+ while (end < 0) end += this.original.length;
782
+ }
783
+
784
+ if (end > this.original.length) throw new Error('end is out of bounds');
785
+ if (start === end)
786
+ throw new Error(
787
+ 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead',
788
+ );
789
+
790
+ this._split(start);
791
+ this._split(end);
792
+
793
+ if (options === true) {
794
+ if (!warned.storeName) {
795
+ console.warn(
796
+ 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
797
+ );
798
+ warned.storeName = true;
799
+ }
800
+
801
+ options = { storeName: true };
802
+ }
803
+ const storeName = options !== undefined ? options.storeName : false;
804
+ const overwrite = options !== undefined ? options.overwrite : false;
805
+
806
+ if (storeName) {
807
+ const original = this.original.slice(start, end);
808
+ Object.defineProperty(this.storedNames, original, {
809
+ writable: true,
810
+ value: true,
811
+ enumerable: true,
812
+ });
813
+ }
814
+
815
+ const first = this.byStart[start];
816
+ const last = this.byEnd[end];
817
+
818
+ if (first) {
819
+ let chunk = first;
820
+ while (chunk !== last) {
821
+ if (chunk.next !== this.byStart[chunk.end]) {
822
+ throw new Error('Cannot overwrite across a split point');
823
+ }
824
+ chunk = chunk.next;
825
+ chunk.edit('', false);
826
+ }
827
+
828
+ first.edit(content, storeName, !overwrite);
829
+ } else {
830
+ // must be inserting at the end
831
+ const newChunk = new Chunk(start, end, '').edit(content, storeName);
832
+
833
+ // TODO last chunk in the array may not be the last chunk, if it's moved...
834
+ last.next = newChunk;
835
+ newChunk.previous = last;
836
+ }
837
+ return this;
838
+ }
839
+
840
+ prepend(content) {
841
+ if (typeof content !== 'string') throw new TypeError('outro content must be a string');
842
+
843
+ this.intro = content + this.intro;
844
+ return this;
845
+ }
846
+
847
+ prependLeft(index, content) {
848
+ index = index + this.offset;
849
+
850
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
851
+
852
+ this._split(index);
853
+
854
+ const chunk = this.byEnd[index];
855
+
856
+ if (chunk) {
857
+ chunk.prependLeft(content);
858
+ } else {
859
+ this.intro = content + this.intro;
860
+ }
861
+ return this;
862
+ }
863
+
864
+ prependRight(index, content) {
865
+ index = index + this.offset;
866
+
867
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
868
+
869
+ this._split(index);
870
+
871
+ const chunk = this.byStart[index];
872
+
873
+ if (chunk) {
874
+ chunk.prependRight(content);
875
+ } else {
876
+ this.outro = content + this.outro;
877
+ }
878
+ return this;
879
+ }
880
+
881
+ remove(start, end) {
882
+ start = start + this.offset;
883
+ end = end + this.offset;
884
+
885
+ if (this.original.length !== 0) {
886
+ while (start < 0) start += this.original.length;
887
+ while (end < 0) end += this.original.length;
888
+ }
889
+
890
+ if (start === end) return this;
891
+
892
+ if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
893
+ if (start > end) throw new Error('end must be greater than start');
894
+
895
+ this._split(start);
896
+ this._split(end);
897
+
898
+ let chunk = this.byStart[start];
899
+
900
+ while (chunk) {
901
+ chunk.intro = '';
902
+ chunk.outro = '';
903
+ chunk.edit('');
904
+
905
+ chunk = end > chunk.end ? this.byStart[chunk.end] : null;
906
+ }
907
+ return this;
908
+ }
909
+
910
+ reset(start, end) {
911
+ start = start + this.offset;
912
+ end = end + this.offset;
913
+
914
+ if (this.original.length !== 0) {
915
+ while (start < 0) start += this.original.length;
916
+ while (end < 0) end += this.original.length;
917
+ }
918
+
919
+ if (start === end) return this;
920
+
921
+ if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
922
+ if (start > end) throw new Error('end must be greater than start');
923
+
924
+ this._split(start);
925
+ this._split(end);
926
+
927
+ let chunk = this.byStart[start];
928
+
929
+ while (chunk) {
930
+ chunk.reset();
931
+
932
+ chunk = end > chunk.end ? this.byStart[chunk.end] : null;
933
+ }
934
+ return this;
935
+ }
936
+
937
+ lastChar() {
938
+ if (this.outro.length) return this.outro[this.outro.length - 1];
939
+ let chunk = this.lastChunk;
940
+ do {
941
+ if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
942
+ if (chunk.content.length) return chunk.content[chunk.content.length - 1];
943
+ if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
944
+ } while ((chunk = chunk.previous));
945
+ if (this.intro.length) return this.intro[this.intro.length - 1];
946
+ return '';
947
+ }
948
+
949
+ lastLine() {
950
+ let lineIndex = this.outro.lastIndexOf(n);
951
+ if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
952
+ let lineStr = this.outro;
953
+ let chunk = this.lastChunk;
954
+ do {
955
+ if (chunk.outro.length > 0) {
956
+ lineIndex = chunk.outro.lastIndexOf(n);
957
+ if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
958
+ lineStr = chunk.outro + lineStr;
959
+ }
960
+
961
+ if (chunk.content.length > 0) {
962
+ lineIndex = chunk.content.lastIndexOf(n);
963
+ if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
964
+ lineStr = chunk.content + lineStr;
965
+ }
966
+
967
+ if (chunk.intro.length > 0) {
968
+ lineIndex = chunk.intro.lastIndexOf(n);
969
+ if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
970
+ lineStr = chunk.intro + lineStr;
971
+ }
972
+ } while ((chunk = chunk.previous));
973
+ lineIndex = this.intro.lastIndexOf(n);
974
+ if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
975
+ return this.intro + lineStr;
976
+ }
977
+
978
+ slice(start = 0, end = this.original.length - this.offset) {
979
+ start = start + this.offset;
980
+ end = end + this.offset;
981
+
982
+ if (this.original.length !== 0) {
983
+ while (start < 0) start += this.original.length;
984
+ while (end < 0) end += this.original.length;
985
+ }
986
+
987
+ let result = '';
988
+
989
+ // find start chunk
990
+ let chunk = this.firstChunk;
991
+ while (chunk && (chunk.start > start || chunk.end <= start)) {
992
+ // found end chunk before start
993
+ if (chunk.start < end && chunk.end >= end) {
994
+ return result;
995
+ }
996
+
997
+ chunk = chunk.next;
998
+ }
999
+
1000
+ if (chunk && chunk.edited && chunk.start !== start)
1001
+ throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
1002
+
1003
+ const startChunk = chunk;
1004
+ while (chunk) {
1005
+ if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
1006
+ result += chunk.intro;
1007
+ }
1008
+
1009
+ const containsEnd = chunk.start < end && chunk.end >= end;
1010
+ if (containsEnd && chunk.edited && chunk.end !== end)
1011
+ throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
1012
+
1013
+ const sliceStart = startChunk === chunk ? start - chunk.start : 0;
1014
+ const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
1015
+
1016
+ result += chunk.content.slice(sliceStart, sliceEnd);
1017
+
1018
+ if (chunk.outro && (!containsEnd || chunk.end === end)) {
1019
+ result += chunk.outro;
1020
+ }
1021
+
1022
+ if (containsEnd) {
1023
+ break;
1024
+ }
1025
+
1026
+ chunk = chunk.next;
1027
+ }
1028
+
1029
+ return result;
1030
+ }
1031
+
1032
+ // TODO deprecate this? not really very useful
1033
+ snip(start, end) {
1034
+ const clone = this.clone();
1035
+ clone.remove(0, start);
1036
+ clone.remove(end, clone.original.length);
1037
+
1038
+ return clone;
1039
+ }
1040
+
1041
+ _split(index) {
1042
+ if (this.byStart[index] || this.byEnd[index]) return;
1043
+
1044
+ let chunk = this.lastSearchedChunk;
1045
+ const searchForward = index > chunk.end;
1046
+
1047
+ while (chunk) {
1048
+ if (chunk.contains(index)) return this._splitChunk(chunk, index);
1049
+
1050
+ chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
1051
+ }
1052
+ }
1053
+
1054
+ _splitChunk(chunk, index) {
1055
+ if (chunk.edited && chunk.content.length) {
1056
+ // zero-length edited chunks are a special case (overlapping replacements)
1057
+ const loc = getLocator(this.original)(index);
1058
+ throw new Error(
1059
+ `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
1060
+ );
1061
+ }
1062
+
1063
+ const newChunk = chunk.split(index);
1064
+
1065
+ this.byEnd[index] = chunk;
1066
+ this.byStart[index] = newChunk;
1067
+ this.byEnd[newChunk.end] = newChunk;
1068
+
1069
+ if (chunk === this.lastChunk) this.lastChunk = newChunk;
1070
+
1071
+ this.lastSearchedChunk = chunk;
1072
+ return true;
1073
+ }
1074
+
1075
+ toString() {
1076
+ let str = this.intro;
1077
+
1078
+ let chunk = this.firstChunk;
1079
+ while (chunk) {
1080
+ str += chunk.toString();
1081
+ chunk = chunk.next;
1082
+ }
1083
+
1084
+ return str + this.outro;
1085
+ }
1086
+
1087
+ isEmpty() {
1088
+ let chunk = this.firstChunk;
1089
+ do {
1090
+ if (
1091
+ (chunk.intro.length && chunk.intro.trim()) ||
1092
+ (chunk.content.length && chunk.content.trim()) ||
1093
+ (chunk.outro.length && chunk.outro.trim())
1094
+ )
1095
+ return false;
1096
+ } while ((chunk = chunk.next));
1097
+ return true;
1098
+ }
1099
+
1100
+ length() {
1101
+ let chunk = this.firstChunk;
1102
+ let length = 0;
1103
+ do {
1104
+ length += chunk.intro.length + chunk.content.length + chunk.outro.length;
1105
+ } while ((chunk = chunk.next));
1106
+ return length;
1107
+ }
1108
+
1109
+ trimLines() {
1110
+ return this.trim('[\\r\\n]');
1111
+ }
1112
+
1113
+ trim(charType) {
1114
+ return this.trimStart(charType).trimEnd(charType);
1115
+ }
1116
+
1117
+ trimEndAborted(charType) {
1118
+ const rx = new RegExp((charType || '\\s') + '+$');
1119
+
1120
+ this.outro = this.outro.replace(rx, '');
1121
+ if (this.outro.length) return true;
1122
+
1123
+ let chunk = this.lastChunk;
1124
+
1125
+ do {
1126
+ const end = chunk.end;
1127
+ const aborted = chunk.trimEnd(rx);
1128
+
1129
+ // if chunk was trimmed, we have a new lastChunk
1130
+ if (chunk.end !== end) {
1131
+ if (this.lastChunk === chunk) {
1132
+ this.lastChunk = chunk.next;
1133
+ }
1134
+
1135
+ this.byEnd[chunk.end] = chunk;
1136
+ this.byStart[chunk.next.start] = chunk.next;
1137
+ this.byEnd[chunk.next.end] = chunk.next;
1138
+ }
1139
+
1140
+ if (aborted) return true;
1141
+ chunk = chunk.previous;
1142
+ } while (chunk);
1143
+
1144
+ return false;
1145
+ }
1146
+
1147
+ trimEnd(charType) {
1148
+ this.trimEndAborted(charType);
1149
+ return this;
1150
+ }
1151
+ trimStartAborted(charType) {
1152
+ const rx = new RegExp('^' + (charType || '\\s') + '+');
1153
+
1154
+ this.intro = this.intro.replace(rx, '');
1155
+ if (this.intro.length) return true;
1156
+
1157
+ let chunk = this.firstChunk;
1158
+
1159
+ do {
1160
+ const end = chunk.end;
1161
+ const aborted = chunk.trimStart(rx);
1162
+
1163
+ if (chunk.end !== end) {
1164
+ // special case...
1165
+ if (chunk === this.lastChunk) this.lastChunk = chunk.next;
1166
+
1167
+ this.byEnd[chunk.end] = chunk;
1168
+ this.byStart[chunk.next.start] = chunk.next;
1169
+ this.byEnd[chunk.next.end] = chunk.next;
1170
+ }
1171
+
1172
+ if (aborted) return true;
1173
+ chunk = chunk.next;
1174
+ } while (chunk);
1175
+
1176
+ return false;
1177
+ }
1178
+
1179
+ trimStart(charType) {
1180
+ this.trimStartAborted(charType);
1181
+ return this;
1182
+ }
1183
+
1184
+ hasChanged() {
1185
+ return this.original !== this.toString();
1186
+ }
1187
+
1188
+ _replaceRegexp(searchValue, replacement) {
1189
+ function getReplacement(match, str) {
1190
+ if (typeof replacement === 'string') {
1191
+ return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
1192
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
1193
+ if (i === '$') return '$';
1194
+ if (i === '&') return match[0];
1195
+ const num = +i;
1196
+ if (num < match.length) return match[+i];
1197
+ return `$${i}`;
1198
+ });
1199
+ } else {
1200
+ return replacement(...match, match.index, str, match.groups);
1201
+ }
1202
+ }
1203
+ function matchAll(re, str) {
1204
+ let match;
1205
+ const matches = [];
1206
+ while ((match = re.exec(str))) {
1207
+ matches.push(match);
1208
+ }
1209
+ return matches;
1210
+ }
1211
+ if (searchValue.global) {
1212
+ const matches = matchAll(searchValue, this.original);
1213
+ matches.forEach((match) => {
1214
+ if (match.index != null) {
1215
+ const replacement = getReplacement(match, this.original);
1216
+ if (replacement !== match[0]) {
1217
+ this.overwrite(match.index, match.index + match[0].length, replacement);
1218
+ }
1219
+ }
1220
+ });
1221
+ } else {
1222
+ const match = this.original.match(searchValue);
1223
+ if (match && match.index != null) {
1224
+ const replacement = getReplacement(match, this.original);
1225
+ if (replacement !== match[0]) {
1226
+ this.overwrite(match.index, match.index + match[0].length, replacement);
1227
+ }
1228
+ }
1229
+ }
1230
+ return this;
1231
+ }
1232
+
1233
+ _replaceString(string, replacement) {
1234
+ const { original } = this;
1235
+ const index = original.indexOf(string);
1236
+
1237
+ if (index !== -1) {
1238
+ this.overwrite(index, index + string.length, replacement);
1239
+ }
1240
+
1241
+ return this;
1242
+ }
1243
+
1244
+ replace(searchValue, replacement) {
1245
+ if (typeof searchValue === 'string') {
1246
+ return this._replaceString(searchValue, replacement);
1247
+ }
1248
+
1249
+ return this._replaceRegexp(searchValue, replacement);
1250
+ }
1251
+
1252
+ _replaceAllString(string, replacement) {
1253
+ const { original } = this;
1254
+ const stringLength = string.length;
1255
+ for (
1256
+ let index = original.indexOf(string);
1257
+ index !== -1;
1258
+ index = original.indexOf(string, index + stringLength)
1259
+ ) {
1260
+ const previous = original.slice(index, index + stringLength);
1261
+ if (previous !== replacement) this.overwrite(index, index + stringLength, replacement);
1262
+ }
1263
+
1264
+ return this;
1265
+ }
1266
+
1267
+ replaceAll(searchValue, replacement) {
1268
+ if (typeof searchValue === 'string') {
1269
+ return this._replaceAllString(searchValue, replacement);
1270
+ }
1271
+
1272
+ if (!searchValue.global) {
1273
+ throw new TypeError(
1274
+ 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
1275
+ );
1276
+ }
1277
+
1278
+ return this._replaceRegexp(searchValue, replacement);
1279
+ }
1280
+ }
1281
+
1282
+ export { SourceMap, MagicString as default };
1283
+ //# sourceMappingURL=magic-string.es.js.map