vinext 1.2.0 → 1.3.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 (283) hide show
  1. package/README.md +13 -10
  2. package/dist/build/client-build-config.d.ts +64 -3
  3. package/dist/build/client-build-config.js +131 -4
  4. package/dist/build/client-reference-groups.d.ts +163 -0
  5. package/dist/build/client-reference-groups.js +534 -0
  6. package/dist/build/module-dependency-cache.d.ts +31 -0
  7. package/dist/build/module-dependency-cache.js +51 -1
  8. package/dist/build/next-client-runtime-manifests.d.ts +10 -1
  9. package/dist/build/next-client-runtime-manifests.js +19 -4
  10. package/dist/build/nitro-public-files.d.ts +24 -0
  11. package/dist/build/nitro-public-files.js +60 -0
  12. package/dist/build/output-root-externals.d.ts +21 -0
  13. package/dist/build/output-root-externals.js +40 -0
  14. package/dist/build/pages-client-assets-module.js +1 -1
  15. package/dist/build/prerender-paths.js +6 -3
  16. package/dist/build/prerender.js +1 -1
  17. package/dist/build/server-actions-encryption-key.d.ts +23 -0
  18. package/dist/build/server-actions-encryption-key.js +101 -0
  19. package/dist/build/ssr-manifest.d.ts +3 -0
  20. package/dist/build/ssr-manifest.js +10 -1
  21. package/dist/check.js +5 -6
  22. package/dist/cli-dev-config.js +3 -3
  23. package/dist/cli.js +1 -1
  24. package/dist/client/client-rewrite-params.d.ts +9 -0
  25. package/dist/client/client-rewrite-params.js +20 -0
  26. package/dist/client/dev-error-overlay.js +2 -1
  27. package/dist/client/navigation-runtime.d.ts +25 -2
  28. package/dist/client/navigation-runtime.js +42 -11
  29. package/dist/client/vinext-next-data.d.ts +0 -1
  30. package/dist/config/config-matchers.d.ts +15 -40
  31. package/dist/config/config-matchers.js +195 -262
  32. package/dist/config/config-source.d.ts +34 -0
  33. package/dist/config/config-source.js +141 -0
  34. package/dist/config/header-params.d.ts +29 -0
  35. package/dist/config/header-params.js +106 -0
  36. package/dist/config/next-config.d.ts +7 -5
  37. package/dist/config/next-config.js +120 -7
  38. package/dist/entries/app-browser-entry.d.ts +14 -1
  39. package/dist/entries/app-browser-entry.js +26 -36
  40. package/dist/entries/app-rsc-entry.d.ts +2 -0
  41. package/dist/entries/app-rsc-entry.js +92 -57
  42. package/dist/entries/app-rsc-manifest.d.ts +10 -0
  43. package/dist/entries/app-rsc-manifest.js +20 -1
  44. package/dist/entries/app-ssr-entry.d.ts +4 -1
  45. package/dist/entries/app-ssr-entry.js +9 -2
  46. package/dist/entries/pages-client-entry.d.ts +1 -0
  47. package/dist/entries/pages-client-entry.js +6 -6
  48. package/dist/entries/pages-server-entry.js +17 -14
  49. package/dist/index.d.ts +11 -0
  50. package/dist/index.js +350 -175
  51. package/dist/init.d.ts +1 -1
  52. package/dist/init.js +103 -6
  53. package/dist/plugins/ast-utils.d.ts +53 -2
  54. package/dist/plugins/ast-utils.js +90 -2
  55. package/dist/plugins/cloudflare-dev-module-urls.d.ts +41 -0
  56. package/dist/plugins/cloudflare-dev-module-urls.js +271 -0
  57. package/dist/plugins/commonjs-esm-facade.js +2 -2
  58. package/dist/plugins/css-module-imports.js +13 -20
  59. package/dist/plugins/dynamic-preload-metadata.js +2 -3
  60. package/dist/plugins/extensionless-dynamic-import.js +2 -3
  61. package/dist/plugins/fonts.d.ts +1 -1
  62. package/dist/plugins/fonts.js +3 -3
  63. package/dist/plugins/ignore-dynamic-requests.js +2 -3
  64. package/dist/plugins/import-meta-url.js +3 -4
  65. package/dist/plugins/instrumentation-client.js +3 -2
  66. package/dist/plugins/middleware-export-validation.js +2 -3
  67. package/dist/plugins/og-harfbuzz.js +2 -3
  68. package/dist/plugins/optimize-imports.d.ts +25 -2
  69. package/dist/plugins/optimize-imports.js +109 -24
  70. package/dist/plugins/pages-node-externals.js +2 -3
  71. package/dist/plugins/remove-console.js +2 -18
  72. package/dist/plugins/require-condition-resolution.js +3 -3
  73. package/dist/plugins/require-context.js +2 -3
  74. package/dist/plugins/rsc-client-reference-loaders.d.ts +45 -1
  75. package/dist/plugins/rsc-client-reference-loaders.js +336 -18
  76. package/dist/plugins/strip-server-exports.js +3 -4
  77. package/dist/plugins/styled-jsx.js +2 -3
  78. package/dist/plugins/use-cache-callable.d.ts +26 -2
  79. package/dist/plugins/use-cache-callable.js +101 -36
  80. package/dist/plugins/worker-image-imports.d.ts +1 -0
  81. package/dist/plugins/worker-image-imports.js +31 -34
  82. package/dist/plugins/worker-use-server.js +38 -14
  83. package/dist/routing/app-route-graph.d.ts +1 -1
  84. package/dist/routing/browser-route-manifest.d.ts +43 -0
  85. package/dist/routing/browser-route-manifest.js +195 -0
  86. package/dist/routing/hashed-route-patterns.d.ts +4 -0
  87. package/dist/routing/hashed-route-patterns.js +22 -0
  88. package/dist/routing/optimistic-route-trie.d.ts +27 -0
  89. package/dist/routing/optimistic-route-trie.js +102 -0
  90. package/dist/routing/route-validation.d.ts +6 -1
  91. package/dist/routing/route-validation.js +26 -10
  92. package/dist/server/api-handler.js +1 -1
  93. package/dist/server/app-action-forwarding.d.ts +8 -0
  94. package/dist/server/app-action-forwarding.js +17 -3
  95. package/dist/server/app-browser-entry.js +149 -62
  96. package/dist/server/app-browser-error.d.ts +3 -1
  97. package/dist/server/app-browser-error.js +7 -2
  98. package/dist/server/app-browser-interception-context.d.ts +2 -2
  99. package/dist/server/app-browser-interception-context.js +1 -1
  100. package/dist/server/app-browser-mpa-navigation.d.ts +1 -0
  101. package/dist/server/app-browser-mpa-navigation.js +6 -5
  102. package/dist/server/app-browser-navigation-controller.d.ts +17 -6
  103. package/dist/server/app-browser-navigation-controller.js +88 -26
  104. package/dist/server/app-browser-prefetch-response.js +2 -2
  105. package/dist/server/app-browser-state.d.ts +2 -2
  106. package/dist/server/app-browser-state.js +1 -1
  107. package/dist/server/app-browser-supplemental-refresh.js +1 -1
  108. package/dist/server/app-browser-visible-commit.d.ts +4 -4
  109. package/dist/server/app-client-reference-preloader.d.ts +23 -0
  110. package/dist/server/app-client-reference-preloader.js +30 -7
  111. package/dist/server/app-fallback-renderer.js +2 -1
  112. package/dist/server/app-middleware.d.ts +6 -0
  113. package/dist/server/app-middleware.js +8 -10
  114. package/dist/server/app-optimistic-routing.d.ts +8 -10
  115. package/dist/server/app-optimistic-routing.js +4 -95
  116. package/dist/server/app-page-boundary-render.js +8 -1
  117. package/dist/server/app-page-cache-render.js +1 -0
  118. package/dist/server/app-page-dispatch.js +3 -1
  119. package/dist/server/app-page-render.js +4 -2
  120. package/dist/server/app-page-rsc-render-status.d.ts +1 -0
  121. package/dist/server/app-page-rsc-render-status.js +1 -0
  122. package/dist/server/app-page-search-params-observation.d.ts +1 -4
  123. package/dist/server/app-page-search-params-observation.js +8 -10
  124. package/dist/server/app-page-stream.d.ts +7 -0
  125. package/dist/server/app-page-stream.js +1 -0
  126. package/dist/server/app-pages-bridge.d.ts +2 -1
  127. package/dist/server/app-pages-bridge.js +12 -8
  128. package/dist/server/app-prerender-endpoints.js +1 -1
  129. package/dist/server/app-prerender-static-params.js +3 -2
  130. package/dist/server/app-render-abort-error.d.ts +19 -0
  131. package/dist/server/app-render-abort-error.js +31 -1
  132. package/dist/server/app-render-dependency.js +42 -2
  133. package/dist/server/app-request-stage-independent-entry.js +7 -7
  134. package/dist/server/app-response-stage-entry.js +5 -5
  135. package/dist/server/app-route-handler-dispatch.js +3 -1
  136. package/dist/server/app-route-handler-execution.d.ts +2 -0
  137. package/dist/server/app-route-handler-execution.js +9 -2
  138. package/dist/server/app-route-handler-middleware-context.d.ts +2 -1
  139. package/dist/server/app-route-handler-middleware-context.js +3 -3
  140. package/dist/server/app-route-handler-response.d.ts +2 -1
  141. package/dist/server/app-route-handler-response.js +2 -2
  142. package/dist/server/app-route-handler-runtime.d.ts +2 -0
  143. package/dist/server/app-route-handler-runtime.js +21 -4
  144. package/dist/server/app-route-module-loader.js +7 -6
  145. package/dist/server/app-route-tree.d.ts +20 -0
  146. package/dist/server/app-route-tree.js +18 -0
  147. package/dist/server/app-router-entry.js +7 -7
  148. package/dist/server/app-rsc-cache-busting.d.ts +16 -0
  149. package/dist/server/app-rsc-cache-busting.js +29 -12
  150. package/dist/server/app-rsc-combined-handler.js +8 -1
  151. package/dist/server/app-rsc-errors.d.ts +7 -0
  152. package/dist/server/app-rsc-errors.js +9 -2
  153. package/dist/server/app-rsc-handler.d.ts +4 -1
  154. package/dist/server/app-rsc-handler.js +151 -135
  155. package/dist/server/app-rsc-request-normalization.js +3 -3
  156. package/dist/server/app-rsc-response-finalizer.d.ts +5 -0
  157. package/dist/server/app-rsc-response-finalizer.js +4 -1
  158. package/dist/server/app-rsc-response-stage.js +5 -3
  159. package/dist/server/app-rsc-route-matching.d.ts +1 -0
  160. package/dist/server/app-rsc-route-matching.js +1 -1
  161. package/dist/server/app-server-action-execution.d.ts +23 -6
  162. package/dist/server/app-server-action-execution.js +51 -57
  163. package/dist/server/app-ssr-entry.d.ts +6 -0
  164. package/dist/server/app-ssr-entry.js +21 -11
  165. package/dist/server/app-ssr-stream.d.ts +7 -0
  166. package/dist/server/app-ssr-stream.js +13 -1
  167. package/dist/server/app-visited-response-cache.js +2 -2
  168. package/dist/server/config-headers.js +13 -4
  169. package/dist/server/defer-until-stream-consumed.d.ts +2 -1
  170. package/dist/server/defer-until-stream-consumed.js +3 -1
  171. package/dist/server/dev-server.js +1 -1
  172. package/dist/server/image-optimization.js +1 -0
  173. package/dist/server/isolate-shared-promise.d.ts +10 -0
  174. package/dist/server/isolate-shared-promise.js +33 -0
  175. package/dist/server/isr-cache.js +3 -2
  176. package/dist/server/metadata-route-response.js +2 -2
  177. package/dist/server/middleware-matcher-pattern.d.ts +0 -1
  178. package/dist/server/middleware-matcher-pattern.js +11 -140
  179. package/dist/server/middleware-matcher.js +5 -6
  180. package/dist/server/middleware-response-headers.d.ts +31 -0
  181. package/dist/server/middleware-response-headers.js +57 -1
  182. package/dist/server/middleware-runtime.js +30 -17
  183. package/dist/server/navigation-planner.d.ts +2 -2
  184. package/dist/server/navigation-planner.js +1 -1
  185. package/dist/server/nitro-public-files-plugin.d.ts +38 -0
  186. package/dist/server/nitro-public-files-plugin.js +106 -0
  187. package/dist/server/nitro-public-files.d.ts +21 -0
  188. package/dist/server/nitro-public-files.js +39 -0
  189. package/dist/server/open-redirect.d.ts +11 -4
  190. package/dist/server/open-redirect.js +11 -4
  191. package/dist/server/pages-api-route.js +2 -2
  192. package/dist/server/pages-asset-tags.d.ts +5 -0
  193. package/dist/server/pages-asset-tags.js +9 -2
  194. package/dist/server/pages-client-assets.d.ts +4 -1
  195. package/dist/server/pages-data-route.js +2 -1
  196. package/dist/server/pages-i18n.d.ts +18 -0
  197. package/dist/server/pages-i18n.js +18 -2
  198. package/dist/server/pages-node-compat.d.ts +5 -3
  199. package/dist/server/pages-node-compat.js +35 -16
  200. package/dist/server/pages-page-data.js +1 -1
  201. package/dist/server/pages-page-handler.js +1 -1
  202. package/dist/server/pages-page-response.js +1 -1
  203. package/dist/server/pages-request-pipeline.d.ts +6 -1
  204. package/dist/server/pages-request-pipeline.js +35 -39
  205. package/dist/server/pages-request-stage-entry.js +20 -10
  206. package/dist/server/pages-response-stage-entry.js +1 -1
  207. package/dist/server/pages-revalidate.js +3 -2
  208. package/dist/server/pages-router-entry.js +2 -2
  209. package/dist/server/prod-server.d.ts +2 -2
  210. package/dist/server/prod-server.js +74 -36
  211. package/dist/server/proxy-trust.d.ts +6 -0
  212. package/dist/server/proxy-trust.js +12 -6
  213. package/dist/server/request-pipeline.d.ts +5 -0
  214. package/dist/server/request-pipeline.js +12 -1
  215. package/dist/server/rsc-stream-hints.d.ts +1 -1
  216. package/dist/server/rsc-stream-hints.js +21 -3
  217. package/dist/server/server-reference-loader.js +10 -1
  218. package/dist/server/static-file-cache.js +15 -3
  219. package/dist/server/worker-image-optimization.js +19 -6
  220. package/dist/server/worker-utils.js +1 -1
  221. package/dist/shims/cache-handler.js +3 -2
  222. package/dist/shims/cache-request-state.d.ts +40 -3
  223. package/dist/shims/cache-request-state.js +70 -5
  224. package/dist/shims/cache-runtime.js +47 -19
  225. package/dist/shims/cache.d.ts +2 -2
  226. package/dist/shims/cache.js +17 -8
  227. package/dist/shims/cdn-purge-routes.d.ts +24 -0
  228. package/dist/shims/cdn-purge-routes.js +77 -0
  229. package/dist/shims/dynamic.js +17 -4
  230. package/dist/shims/form.js +8 -3
  231. package/dist/shims/head.js +1 -1
  232. package/dist/shims/headers.js +2 -2
  233. package/dist/shims/image-props.d.ts +93 -0
  234. package/dist/shims/image-props.js +323 -0
  235. package/dist/shims/image.d.ts +4 -18
  236. package/dist/shims/image.js +4 -319
  237. package/dist/shims/internal/headers-state.d.ts +7 -0
  238. package/dist/shims/internal/headers-state.js +14 -2
  239. package/dist/shims/internal/hybrid-client-route-owner-direct.d.ts +4 -1
  240. package/dist/shims/internal/hybrid-client-route-owner-direct.js +32 -15
  241. package/dist/shims/internal/hybrid-client-route-owner.js +9 -8
  242. package/dist/shims/link-prefetch.js +5 -4
  243. package/dist/shims/link.js +7 -5
  244. package/dist/shims/navigation.d.ts +1 -1
  245. package/dist/shims/navigation.js +21 -10
  246. package/dist/shims/og.js +35 -10
  247. package/dist/shims/request-context.d.ts +13 -0
  248. package/dist/shims/router.js +28 -52
  249. package/dist/shims/server.d.ts +42 -8
  250. package/dist/shims/server.js +99 -15
  251. package/dist/shims/thenable-params.d.ts +6 -1
  252. package/dist/shims/thenable-params.js +11 -2
  253. package/dist/shims/unified-request-context.d.ts +5 -0
  254. package/dist/shims/unified-request-context.js +2 -0
  255. package/dist/shims/url-utils.d.ts +7 -0
  256. package/dist/shims/url-utils.js +12 -3
  257. package/dist/utils/asset-prefix.d.ts +7 -0
  258. package/dist/utils/asset-prefix.js +10 -1
  259. package/dist/utils/client-build-manifest.js +12 -5
  260. package/dist/utils/client-runtime-metadata.d.ts +1 -0
  261. package/dist/utils/client-runtime-metadata.js +15 -2
  262. package/dist/utils/domain-locale.js +1 -1
  263. package/dist/utils/lazy-chunks.d.ts +4 -0
  264. package/dist/utils/manifest-paths.d.ts +14 -0
  265. package/dist/utils/manifest-paths.js +22 -1
  266. package/dist/utils/path-to-regexp.d.ts +21 -0
  267. package/dist/{server/middleware-path-to-regexp.js → utils/path-to-regexp.js} +14 -11
  268. package/dist/utils/plugin-react-version.d.ts +15 -0
  269. package/dist/utils/plugin-react-version.js +64 -0
  270. package/dist/utils/plugin-rsc-version.d.ts +2 -0
  271. package/dist/utils/plugin-rsc-version.js +28 -2
  272. package/dist/utils/public-routes.d.ts +2 -0
  273. package/dist/utils/public-routes.js +18 -7
  274. package/dist/utils/react-version.d.ts +7 -0
  275. package/dist/utils/react-version.js +39 -16
  276. package/package.json +6 -6
  277. package/dist/build/nitro-route-rules.d.ts +0 -47
  278. package/dist/build/nitro-route-rules.js +0 -79
  279. package/dist/plugins/cache-flight-codec.d.ts +0 -19
  280. package/dist/plugins/cache-flight-codec.js +0 -68
  281. package/dist/server/middleware-path-to-regexp.d.ts +0 -13
  282. package/dist/utils/regex-safety.d.ts +0 -17
  283. package/dist/utils/regex-safety.js +0 -789
@@ -1,11 +1,10 @@
1
1
  import path, { toSlash } from "../deps/.pnpm/pathslash@0.1.0/deps/pathslash/dist/index.js";
2
2
  import { escapeRegExp } from "../utils/regex.js";
3
- import { collectBindingNames, getAstName } from "./ast-utils.js";
3
+ import { collectBindingNames, getAstName, parseProgram } from "./ast-utils.js";
4
4
  import { magicStringTransformResult } from "./transform-result.js";
5
5
  import { VIRTUAL_MODULE_ID_RE } from "../utils/virtual-module.js";
6
6
  import { createRequire } from "node:module";
7
7
  import fsp from "node:fs/promises";
8
- import { parseAst } from "vite";
9
8
  import MagicString from "magic-string";
10
9
  //#region src/plugins/optimize-imports.ts
11
10
  /**
@@ -22,7 +21,7 @@ async function readFileSafe(filepath) {
22
21
  /**
23
22
  * Packages whose barrel imports are automatically optimized.
24
23
  * Matches Next.js's built-in optimizePackageImports defaults plus vinext-specific
25
- * compatibility entries for radix-ui and @chakra-ui/react.
24
+ * compatibility entries for radix-ui, @chakra-ui/react, and @heroui/react.
26
25
  * @see https://github.com/vercel/next.js/blob/9c31bbdaa/packages/next/src/server/config.ts#L1301
27
26
  */
28
27
  const DEFAULT_OPTIMIZE_PACKAGES = [
@@ -102,10 +101,40 @@ const DEFAULT_OPTIMIZE_PACKAGES = [
102
101
  "react-icons/ti",
103
102
  "react-icons/vsc",
104
103
  "react-icons/wi",
105
- "radix-ui"
104
+ "radix-ui",
105
+ "@heroui/react"
106
106
  ];
107
- function createOptimizedImportSourceMatcher(packages) {
108
- const pattern = [...packages].map(escapeRegExp).join("|");
107
+ /**
108
+ * Packages whose subpath imports (`pkg/Sub`) are also optimized, in the RSC
109
+ * environment only. Their subpath entries are themselves barrels: heroui's
110
+ * Toast imports `UNSTABLE_ToastQueue` from "react-aria-components/Toast", whose
111
+ * other exports call createContext() at module scope and import "client-only"
112
+ * (https://github.com/cloudflare/vinext/issues/4007). Like Next.js's default
113
+ * Turbopack build, this drops the side-effect-free entry's "client-only"
114
+ * import: server-safe exports work, and client components still fail when
115
+ * their own modules are evaluated. SSR and the client keep the package's own
116
+ * module graph, so context modules are never instantiated twice there.
117
+ */
118
+ const SUBPATH_OPTIMIZE_PACKAGES = ["react-aria-components"];
119
+ function isOptimizedSubpath(source, subpathPackages) {
120
+ for (const pkg of subpathPackages) if (source.length > pkg.length + 1 && source.startsWith(`${pkg}/`)) return true;
121
+ return false;
122
+ }
123
+ /**
124
+ * Return the directive that marks a module as an RSC boundary, if any.
125
+ * Re-exports must not be followed through such a module: importing its leaves
126
+ * directly would bypass the client reference, server reference, or cache wrapper.
127
+ */
128
+ function findBoundaryDirective(ast) {
129
+ for (const statement of ast.body) {
130
+ if (statement.type !== "ExpressionStatement" || typeof statement.directive !== "string") return null;
131
+ const directive = statement.directive;
132
+ if (directive === "use client" || directive === "use server" || directive.startsWith("use cache")) return directive;
133
+ }
134
+ return null;
135
+ }
136
+ function createOptimizedImportSourceMatcher(packages, subpathPackages = []) {
137
+ const pattern = [...[...packages].map(escapeRegExp), ...[...subpathPackages].map((pkg) => String.raw`${escapeRegExp(pkg)}/[^"'\s]+`)].join("|");
109
138
  if (!pattern) return () => false;
110
139
  const importFromPattern = new RegExp(String.raw`(?:^|[;}\n\r])\s*import(?!\s*\()(?:(?!\bfrom\b)[\s\S])*?\bfrom\s*["'](?:${pattern})["']`, "m");
111
140
  const quotedSourcePattern = new RegExp(String.raw`["'](?:${pattern})["']`);
@@ -234,11 +263,12 @@ async function buildExportMapFromFile(filePath, readFile, cache, visited, initia
234
263
  if (!content) return /* @__PURE__ */ new Map();
235
264
  let ast;
236
265
  try {
237
- ast = parseAst(content);
266
+ ast = parseProgram(content);
238
267
  } catch {
239
268
  return /* @__PURE__ */ new Map();
240
269
  }
241
270
  const exportMap = /* @__PURE__ */ new Map();
271
+ const boundaryDirective = findBoundaryDirective(ast);
242
272
  const importBindings = /* @__PURE__ */ new Map();
243
273
  const localDeclarations = /* @__PURE__ */ new Set();
244
274
  const fileDir = path.dirname(filePath);
@@ -380,6 +410,14 @@ async function buildExportMapFromFile(filePath, readFile, cache, visited, initia
380
410
  break;
381
411
  }
382
412
  }
413
+ if (boundaryDirective) {
414
+ const isSsrBoundary = boundaryDirective !== "use client";
415
+ for (const [name, entry] of exportMap) exportMap.set(name, {
416
+ ...entry,
417
+ rscBoundary: filePath,
418
+ ssrBoundary: isSsrBoundary ? filePath : entry.ssrBoundary
419
+ });
420
+ }
383
421
  cache.set(filePath, exportMap);
384
422
  return exportMap;
385
423
  }
@@ -408,6 +446,15 @@ async function buildBarrelExportMap(entryPath, readFile, cache) {
408
446
  return await buildExportMapFromFile(entryPath, readFile, exportMapCache, /* @__PURE__ */ new Set(), content);
409
447
  }
410
448
  /**
449
+ * Resolve a specifier with Vite's resolver, returning the id only when it is a
450
+ * file in the module graph (not external or virtual).
451
+ */
452
+ async function resolveFileId(ctx, source, importer) {
453
+ const resolved = await ctx.resolve(source, importer, { skipSelf: true });
454
+ if (!resolved || resolved.external || !path.isAbsolute(resolved.id)) return null;
455
+ return resolved.id;
456
+ }
457
+ /**
411
458
  * Creates the vinext:optimize-imports Vite plugin.
412
459
  *
413
460
  * @param nextConfig - Resolved Next.js config (may be undefined before config hook runs).
@@ -421,6 +468,7 @@ function createOptimizeImportsPlugin(getNextConfig, getRoot) {
421
468
  const entryPathCache = /* @__PURE__ */ new Map();
422
469
  let optimizedPackages = /* @__PURE__ */ new Set();
423
470
  let hasOptimizedImportSource = () => false;
471
+ let hasOptimizedRscImportSource = () => false;
424
472
  const registeredBarrels = /* @__PURE__ */ new Set();
425
473
  return {
426
474
  name: "vinext:optimize-imports",
@@ -428,6 +476,7 @@ function createOptimizeImportsPlugin(getNextConfig, getRoot) {
428
476
  const nextConfig = getNextConfig();
429
477
  optimizedPackages = /* @__PURE__ */ new Set([...DEFAULT_OPTIMIZE_PACKAGES, ...nextConfig?.optimizePackageImports ?? []]);
430
478
  hasOptimizedImportSource = createOptimizedImportSourceMatcher(optimizedPackages);
479
+ hasOptimizedRscImportSource = createOptimizedImportSourceMatcher(optimizedPackages, SUBPATH_OPTIMIZE_PACKAGES);
431
480
  entryPathCache.clear();
432
481
  barrelCaches.exportMapCache.clear();
433
482
  barrelCaches.subpkgOrigin.clear();
@@ -435,56 +484,77 @@ function createOptimizeImportsPlugin(getNextConfig, getRoot) {
435
484
  },
436
485
  async resolveId(source) {
437
486
  if (this.environment?.name === "client") return;
438
- const envName = this.environment?.name ?? "ssr";
439
- const barrelEntry = barrelCaches.subpkgOrigin.get(envName)?.get(source) ?? barrelCaches.subpkgOrigin.get(envName === "rsc" ? "ssr" : "rsc")?.get(source);
487
+ const envKey = this.environment?.name === "rsc" ? "rsc" : "ssr";
488
+ const barrelEntry = barrelCaches.subpkgOrigin.get(envKey)?.get(source);
440
489
  if (!barrelEntry) return;
441
490
  return await this.resolve(source, barrelEntry, { skipSelf: true }) ?? void 0;
442
491
  },
443
492
  transform: {
444
493
  filter: {
445
494
  id: {
446
- include: /\.(tsx?|jsx?|mjs)$/,
495
+ include: /\.(tsx?|jsx?|mjs)(?:\?v=\w+)?$/,
447
496
  exclude: VIRTUAL_MODULE_ID_RE
448
497
  },
449
498
  code: /\bimport\b[\s\S]*\bfrom\b/
450
499
  },
451
- async handler(code) {
500
+ async handler(code, id) {
452
501
  const env = this.environment;
453
502
  if (env?.name === "client") return null;
454
503
  const preferReactServer = env?.name === "rsc";
455
504
  const packages = optimizedPackages;
456
- if (!hasOptimizedImportSource(code)) return null;
505
+ if (!(preferReactServer ? hasOptimizedRscImportSource : hasOptimizedImportSource)(code)) return null;
457
506
  let ast;
458
507
  try {
459
- ast = parseAst(code);
508
+ ast = parseProgram(code);
460
509
  } catch {
461
510
  return null;
462
511
  }
512
+ if (preferReactServer && findBoundaryDirective(ast) === "use client") return null;
463
513
  const s = new MagicString(code);
464
514
  let hasChanges = false;
465
515
  const root = getRoot();
466
516
  for (const node of ast.body) {
467
517
  if (node.type !== "ImportDeclaration") continue;
468
518
  const importSource = typeof node.source?.value === "string" ? node.source.value : null;
469
- if (!importSource || !packages.has(importSource)) continue;
470
- const cacheKey = `${preferReactServer ? "rsc" : "ssr"}:${importSource}`;
471
- let barrelEntry = entryPathCache.get(cacheKey);
472
- if (barrelEntry === void 0) {
473
- barrelEntry = await resolvePackageEntry(importSource, root, preferReactServer);
474
- entryPathCache.set(cacheKey, barrelEntry ?? null);
519
+ if (!importSource) continue;
520
+ const isSubpath = !packages.has(importSource) && preferReactServer && isOptimizedSubpath(importSource, SUBPATH_OPTIMIZE_PACKAGES);
521
+ if (!isSubpath && !packages.has(importSource)) continue;
522
+ let barrelEntry;
523
+ if (isSubpath) {
524
+ barrelEntry = await resolveFileId(this, importSource, id);
525
+ if (barrelEntry) barrelEntry = barrelEntry.replace(/\?.*$/, "");
526
+ } else {
527
+ const cacheKey = `${preferReactServer ? "rsc" : "ssr"}:${importSource}`;
528
+ barrelEntry = entryPathCache.get(cacheKey);
529
+ if (barrelEntry === void 0) {
530
+ barrelEntry = await resolvePackageEntry(importSource, root, preferReactServer);
531
+ entryPathCache.set(cacheKey, barrelEntry ?? null);
532
+ }
475
533
  }
476
534
  const exportMap = await buildBarrelExportMap(barrelEntry, readFileSafe, barrelCaches.exportMapCache);
477
535
  if (!exportMap || !barrelEntry) continue;
536
+ const getEntry = (name) => {
537
+ const entry = exportMap.get(name);
538
+ const boundary = preferReactServer ? entry?.rscBoundary : entry?.ssrBoundary;
539
+ return boundary ? {
540
+ source: boundary,
541
+ isNamespace: false,
542
+ originalName: name
543
+ } : entry;
544
+ };
478
545
  const envKey = preferReactServer ? "rsc" : "ssr";
479
546
  const registeredKey = `${envKey}:${barrelEntry}`;
480
- if (!registeredBarrels.has(registeredKey)) {
547
+ if (!isSubpath && !registeredBarrels.has(registeredKey)) {
481
548
  registeredBarrels.add(registeredKey);
482
549
  let envOriginMap = barrelCaches.subpkgOrigin.get(envKey);
483
550
  if (!envOriginMap) {
484
551
  envOriginMap = /* @__PURE__ */ new Map();
485
552
  barrelCaches.subpkgOrigin.set(envKey, envOriginMap);
486
553
  }
487
- for (const entry of exportMap.values()) if (!entry.source.startsWith("/") && !entry.source.startsWith(".") && !envOriginMap.has(entry.source)) envOriginMap.set(entry.source, barrelEntry);
554
+ for (const name of exportMap.keys()) {
555
+ const source = getEntry(name).source;
556
+ if (!source.startsWith("/") && !source.startsWith(".") && !envOriginMap.has(source)) envOriginMap.set(source, barrelEntry);
557
+ }
488
558
  }
489
559
  const specifiers = [];
490
560
  let allResolved = true;
@@ -529,11 +599,26 @@ function createOptimizeImportsPlugin(getNextConfig, getRoot) {
529
599
  }
530
600
  continue;
531
601
  }
602
+ if (isSubpath && specifiers.every(({ imported }) => getEntry(imported)?.source === barrelEntry)) continue;
603
+ const resolvedPackageSources = /* @__PURE__ */ new Map();
604
+ if (isSubpath) {
605
+ for (const { imported } of specifiers) {
606
+ const source = getEntry(imported)?.source;
607
+ if (!source || path.isAbsolute(source) || resolvedPackageSources.has(source)) continue;
608
+ const resolvedId = await resolveFileId(this, source, barrelEntry);
609
+ if (!resolvedId) {
610
+ allResolved = false;
611
+ break;
612
+ }
613
+ resolvedPackageSources.set(source, resolvedId);
614
+ }
615
+ if (!allResolved) continue;
616
+ }
532
617
  const bySource = /* @__PURE__ */ new Map();
533
618
  for (const { local, imported } of specifiers) {
534
- const entry = exportMap.get(imported);
619
+ const entry = getEntry(imported);
535
620
  if (!entry) continue;
536
- const resolvedSource = entry.source;
621
+ const resolvedSource = resolvedPackageSources.get(entry.source) ?? entry.source;
537
622
  const key = `${resolvedSource}::${entry.isNamespace}`;
538
623
  let group = bySource.get(key);
539
624
  if (!group) {
@@ -570,4 +655,4 @@ function createOptimizeImportsPlugin(getNextConfig, getRoot) {
570
655
  };
571
656
  }
572
657
  //#endregion
573
- export { DEFAULT_OPTIMIZE_PACKAGES, buildBarrelExportMap, createOptimizeImportsPlugin, createOptimizedImportSourceMatcher };
658
+ export { DEFAULT_OPTIMIZE_PACKAGES, SUBPATH_OPTIMIZE_PACKAGES, buildBarrelExportMap, createOptimizeImportsPlugin, createOptimizedImportSourceMatcher };
@@ -1,10 +1,9 @@
1
1
  import path from "../deps/.pnpm/pathslash@0.1.0/deps/pathslash/dist/index.js";
2
- import { SCRIPT_MODULE_ID_RE, mayContainDynamicImport, scriptParserLanguage, staticStringValue, walkAst } from "./ast-utils.js";
2
+ import { SCRIPT_MODULE_ID_RE, mayContainDynamicImport, parseProgram, scriptParserLanguage, staticStringValue, walkAst } from "./ast-utils.js";
3
3
  import { canonicalizeFilePath, isPathInsideOrEqual, stripViteModuleQuery } from "../utils/path.js";
4
4
  import { isServerEnvironment } from "./environment.js";
5
5
  import { packageNameFromSpecifier } from "../utils/package-name.js";
6
6
  import fs from "node:fs";
7
- import { parseAst } from "vite";
8
7
  //#region src/plugins/pages-node-externals.ts
9
8
  const FRAMEWORK_PACKAGES = /* @__PURE__ */ new Set([
10
9
  "@vitejs/plugin-react",
@@ -47,7 +46,7 @@ function matchesAlias(id, aliases) {
47
46
  function moduleDependencySpecifiers(code, id) {
48
47
  let ast;
49
48
  try {
50
- ast = parseAst(code, { lang: scriptParserLanguage(id) ?? "jsx" });
49
+ ast = parseProgram(code, { lang: scriptParserLanguage(id) ?? "jsx" });
51
50
  } catch {
52
51
  return [];
53
52
  }
@@ -1,23 +1,7 @@
1
+ import { parseProgram } from "./ast-utils.js";
1
2
  import { magicStringTransformResult } from "./transform-result.js";
2
- import { parseAst } from "vite";
3
3
  import MagicString from "magic-string";
4
4
  //#region src/plugins/remove-console.ts
5
- /**
6
- * Strip `console.*` calls from client bundle code.
7
- *
8
- * Mirrors Next.js's SWC `remove_console` transform:
9
- * - Strips all `console.<method>()` calls regardless of context (top-level,
10
- * JSX expressions, function arguments, return values, ternary branches)
11
- * - Replaces removed calls with `void 0` to keep the AST valid in every
12
- * position a CallExpression can appear
13
- * - Respects `exclude: ["error"]` to preserve certain methods (case-insensitive)
14
- * - Preserves calls when `console` is shadowed (local variable, function
15
- * parameter, or destructured binding)
16
- * - Preserves computed property access `console[prop]()`
17
- *
18
- * Uses Vite's `parseAst` (OXC/acorn) for parsing and `MagicString` for
19
- * surgical source replacement. Returns `null` when no changes are made.
20
- */
21
5
  const SCOPE_ENTERING_TYPES = /* @__PURE__ */ new Set([
22
6
  "FunctionDeclaration",
23
7
  "FunctionExpression",
@@ -36,7 +20,7 @@ function removeConsoleCalls(code, config) {
36
20
  if (!code.match(/\bconsole\b/)) return null;
37
21
  let ast;
38
22
  try {
39
- ast = parseAst(code);
23
+ ast = parseProgram(code);
40
24
  } catch {
41
25
  return null;
42
26
  }
@@ -1,10 +1,10 @@
1
1
  import path from "../deps/.pnpm/pathslash@0.1.0/deps/pathslash/dist/index.js";
2
- import { SCRIPT_MODULE_ID_RE, collectBindingNames, forEachAstChild, isIdentifierNamed, scriptParserLanguage, staticStringValue, unwrapExpression } from "./ast-utils.js";
2
+ import { SCRIPT_MODULE_ID_RE, collectBindingNames, forEachAstChild, isIdentifierNamed, parseProgram, scriptParserLanguage, staticStringValue, unwrapExpression } from "./ast-utils.js";
3
3
  import { magicStringTransformResult } from "./transform-result.js";
4
4
  import { stripViteModuleQuery } from "../utils/path.js";
5
5
  import { collectDirectScopeBindings, collectLoopScopeBindings, collectSwitchScopeBindings, collectVarScopeBindings, createAstScope, hasAstBinding, isFunctionNode } from "./ast-scope.js";
6
6
  import { readFile } from "node:fs/promises";
7
- import { createIdResolver, parseAst } from "vite";
7
+ import { createIdResolver } from "vite";
8
8
  import MagicString from "magic-string";
9
9
  //#region src/plugins/require-condition-resolution.ts
10
10
  const LITERAL_REQUIRE_RE = /\brequire\s*\(/;
@@ -30,7 +30,7 @@ function isPackageSpecifier(specifier) {
30
30
  function collectLiteralRequires(code, id) {
31
31
  let ast;
32
32
  try {
33
- ast = parseAst(code, { lang: scriptParserLanguage(id) ?? "jsx" });
33
+ ast = parseProgram(code, { lang: scriptParserLanguage(id) ?? "jsx" });
34
34
  } catch {
35
35
  return [];
36
36
  }
@@ -1,9 +1,8 @@
1
1
  import path, { toSlash } from "../deps/.pnpm/pathslash@0.1.0/deps/pathslash/dist/index.js";
2
- import { SCRIPT_MODULE_ID_RE, booleanLiteralValue, scriptParserLanguage, stringLiteralValue, walkAst } from "./ast-utils.js";
2
+ import { SCRIPT_MODULE_ID_RE, booleanLiteralValue, parseProgram, scriptParserLanguage, stringLiteralValue, walkAst } from "./ast-utils.js";
3
3
  import { magicStringTransformResult } from "./transform-result.js";
4
4
  import { stripViteModuleQuery } from "../utils/path.js";
5
5
  import { readdir, realpath, stat } from "node:fs/promises";
6
- import { parseAst } from "vite";
7
6
  import MagicString from "magic-string";
8
7
  //#region src/plugins/require-context.ts
9
8
  function createRequireContextPlugin() {
@@ -82,7 +81,7 @@ async function transformRequireContext(code, id) {
82
81
  const lang = scriptParserLanguage(id);
83
82
  let ast;
84
83
  try {
85
- ast = parseAst(code, { lang });
84
+ ast = parseProgram(code, { lang });
86
85
  } catch {
87
86
  return null;
88
87
  }
@@ -1,4 +1,48 @@
1
+ import { AppRoute } from "../routing/app-route-graph.js";
1
2
  import { Plugin } from "vite";
2
3
  //#region src/plugins/rsc-client-reference-loaders.d.ts
3
- export declare function createRscClientReferenceLoadersPlugin(): Plugin;
4
+ export type RscClientReferenceRouteOptions = {
5
+ /** Maps route file paths to the module ids used by the RSC module graph. */
6
+ canonicalizeModuleId: (id: string) => string;
7
+ /** App Router routes of the current build, or null outside App Router builds. */
8
+ getRoutes: () => readonly AppRoute[] | null;
9
+ /**
10
+ * Files any route's render can show: global-error, and the root boundaries
11
+ * that routes without their own not-found, forbidden or unauthorized use.
12
+ */
13
+ getAlwaysRenderedFiles: () => readonly string[];
14
+ /**
15
+ * The files a render without a route (a route-miss 404) shows: the root
16
+ * layouts and boundaries, and global-not-found.
17
+ */
18
+ getRoutelessFiles: () => readonly string[];
19
+ /** Route-independent roots such as global-error and global-not-found. */
20
+ getSharedRoots: () => readonly string[];
21
+ };
22
+ export type RscClientReferenceGroupingOptions = {
23
+ /** Modules that live in chunks every page loads anyway (React, vinext runtime). */
24
+ isAlwaysLoadedClientModule: (id: string) => boolean;
25
+ /** vinext's own client chunk groups. Any other chunk group disables grouping. */
26
+ ownClientChunkGroups: ReadonlySet<unknown>;
27
+ };
28
+ /**
29
+ * Replaces @vitejs/plugin-rsc's client-reference facades with per-reference
30
+ * loaders. With `routes`, the SSR build also exports the client references
31
+ * each App Router route preloads. With `grouping`, production client builds
32
+ * additionally load references that the same App Router routes reach through
33
+ * one shared route group module (see build/client-reference-groups.ts), so a
34
+ * page fetches one chunk per route group instead of one per `"use client"`
35
+ * module.
36
+ */
37
+ export declare function createRscClientReferenceLoadersPlugin(options?: {
38
+ routes?: RscClientReferenceRouteOptions;
39
+ grouping?: RscClientReferenceGroupingOptions;
40
+ }): Plugin;
41
+ /**
42
+ * Exports a null preload plan from the client-references virtual module when
43
+ * the app registers @vitejs/plugin-rsc itself (`rsc: false`), where vinext
44
+ * doesn't add the loaders plugin above. The App Router SSR entry imports the
45
+ * plan, and null makes it preload every client reference.
46
+ */
47
+ export declare function createRouteClientReferencesFallbackPlugin(): Plugin;
4
48
  //#endregion