@rangojs/router 0.0.0-experimental.bd6e11bc → 0.0.0-experimental.bdaf10aa
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.
- package/AGENTS.md +8 -4
- package/README.md +296 -887
- package/dist/bin/rango.js +459 -91
- package/dist/testing/vitest.js +36 -2
- package/dist/vite/index.js +1708 -414
- package/package.json +35 -10
- package/skills/api-client/SKILL.md +211 -0
- package/skills/breadcrumbs/SKILL.md +82 -5
- package/skills/bundle-analysis/SKILL.md +2 -2
- package/skills/cache-guide/SKILL.md +14 -9
- package/skills/caching/SKILL.md +221 -12
- package/skills/catalog.json +271 -0
- package/skills/comparison/SKILL.md +50 -0
- package/skills/comparison/agents/openai.yaml +4 -0
- package/skills/comparison/references/framework-comparison.md +837 -0
- package/skills/composability/SKILL.md +83 -2
- package/skills/css/SKILL.md +76 -0
- package/skills/debug-manifest/SKILL.md +5 -3
- package/skills/defer-hydration/SKILL.md +235 -0
- package/skills/document-cache/SKILL.md +11 -3
- package/skills/fonts/SKILL.md +1 -1
- package/skills/handler-use/SKILL.md +9 -9
- package/skills/hooks/SKILL.md +73 -900
- package/skills/hooks/data.md +273 -0
- package/skills/hooks/handle-and-actions.md +103 -0
- package/skills/hooks/navigation.md +110 -0
- package/skills/hooks/outlets.md +41 -0
- package/skills/hooks/state.md +228 -0
- package/skills/hooks/urls.md +135 -0
- package/skills/host-router/SKILL.md +84 -7
- package/skills/i18n/SKILL.md +1 -1
- package/skills/intercept/SKILL.md +51 -17
- package/skills/layout/SKILL.md +38 -16
- package/skills/links/SKILL.md +1 -1
- package/skills/loader/SKILL.md +48 -20
- package/skills/middleware/SKILL.md +11 -5
- package/skills/migrate-nextjs/SKILL.md +203 -20
- package/skills/migrate-react-router/SKILL.md +59 -675
- package/skills/migrate-react-router/cloudflare-workers.md +129 -0
- package/skills/migrate-react-router/component-migration.md +196 -0
- package/skills/migrate-react-router/data-and-actions.md +225 -0
- package/skills/migrate-react-router/route-mapping.md +271 -0
- package/skills/mime-routes/SKILL.md +3 -3
- package/skills/observability/SKILL.md +70 -5
- package/skills/parallel/SKILL.md +32 -8
- package/skills/ppr/SKILL.md +622 -0
- package/skills/prerender/SKILL.md +59 -28
- package/skills/rango/SKILL.md +124 -50
- package/skills/response-routes/SKILL.md +78 -46
- package/skills/route/SKILL.md +85 -6
- package/skills/router-setup/SKILL.md +41 -6
- package/skills/scripts/SKILL.md +179 -0
- package/skills/server-actions/SKILL.md +28 -3
- package/skills/shell-manifest/SKILL.md +185 -0
- package/skills/streams-and-websockets/SKILL.md +1 -1
- package/skills/tailwind/SKILL.md +28 -4
- package/skills/testing/SKILL.md +68 -654
- package/skills/testing/bindings.md +103 -0
- package/skills/testing/cache-prerender.md +127 -0
- package/skills/testing/client-components.md +124 -0
- package/skills/testing/e2e-parity.md +125 -0
- package/skills/testing/flight.md +91 -0
- package/skills/testing/handles.md +131 -0
- package/skills/testing/loader.md +128 -0
- package/skills/testing/middleware.md +99 -0
- package/skills/testing/render-handler.md +122 -0
- package/skills/testing/response-routes.md +95 -0
- package/skills/testing/reverse-and-types.md +85 -0
- package/skills/testing/server-actions.md +107 -0
- package/skills/testing/server-tree.md +128 -0
- package/skills/testing/setup.md +123 -0
- package/skills/theme/SKILL.md +1 -1
- package/skills/typesafety/SKILL.md +45 -918
- package/skills/typesafety/env-and-bindings.md +254 -0
- package/skills/typesafety/generated-files-and-cli.md +335 -0
- package/skills/typesafety/params-and-search.md +153 -0
- package/skills/typesafety/route-types.md +209 -0
- package/skills/use-cache/SKILL.md +47 -17
- package/skills/vercel/SKILL.md +128 -0
- package/skills/view-transitions/SKILL.md +44 -1
- package/src/__augment-tests__/augmented.check.ts +2 -3
- package/src/__internal.ts +0 -65
- package/src/browser/action-coordinator.ts +1 -1
- package/src/browser/action-fence.ts +47 -0
- package/src/browser/app-shell.ts +14 -27
- package/src/browser/connection-warmup.ts +134 -0
- package/src/browser/cookie-name.ts +140 -0
- package/src/browser/event-controller.ts +178 -100
- package/src/browser/invalidate-client-cache.ts +52 -0
- package/src/browser/logging.ts +28 -0
- package/src/browser/merge-segment-loaders.ts +6 -4
- package/src/browser/navigation-bridge.ts +81 -68
- package/src/browser/navigation-client.ts +115 -70
- package/src/browser/navigation-store-handle.ts +38 -0
- package/src/browser/navigation-store.ts +153 -88
- package/src/browser/navigation-transaction.ts +0 -32
- package/src/browser/network-error-handler.ts +34 -7
- package/src/browser/partial-update.ts +157 -144
- package/src/browser/prefetch/cache.ts +148 -81
- package/src/browser/prefetch/fetch.ts +231 -51
- package/src/browser/prefetch/queue.ts +25 -7
- package/src/browser/rango-state.ts +157 -115
- package/src/browser/react/Link.tsx +40 -7
- package/src/browser/react/NavigationProvider.tsx +140 -99
- package/src/browser/react/ScrollRestoration.tsx +10 -6
- package/src/browser/react/filter-segment-order.ts +17 -2
- package/src/browser/react/index.ts +0 -51
- package/src/browser/react/location-state-shared.ts +14 -15
- package/src/browser/react/location-state.ts +0 -1
- package/src/browser/react/use-action.ts +6 -15
- package/src/browser/react/use-handle.ts +0 -5
- package/src/browser/react/use-href.tsx +8 -1
- package/src/browser/react/use-link-status.ts +33 -8
- package/src/browser/react/use-navigation.ts +10 -5
- package/src/browser/react/use-params.ts +0 -2
- package/src/browser/react/use-router.ts +6 -4
- package/src/browser/react/use-search-params.ts +0 -5
- package/src/browser/react/use-segments.ts +0 -13
- package/src/browser/response-adapter.ts +74 -8
- package/src/browser/rsc-router.tsx +97 -22
- package/src/browser/scroll-restoration.ts +15 -8
- package/src/browser/segment-reconciler.ts +31 -21
- package/src/browser/server-action-bridge.ts +216 -38
- package/src/browser/types.ts +94 -22
- package/src/browser/validate-redirect-origin.ts +43 -16
- package/src/build/generate-manifest.ts +155 -131
- package/src/build/generate-route-types.ts +1 -1
- package/src/build/index.ts +11 -5
- package/src/build/prefix-tree-utils.ts +123 -0
- package/src/build/route-trie.ts +152 -22
- package/src/build/route-types/ast-route-extraction.ts +15 -8
- package/src/build/route-types/codegen.ts +12 -1
- package/src/build/route-types/include-resolution.ts +455 -61
- package/src/build/route-types/param-extraction.ts +6 -3
- package/src/build/route-types/per-module-writer.ts +15 -2
- package/src/build/route-types/router-processing.ts +77 -41
- package/src/build/route-types/source-scan.ts +105 -7
- package/src/build/runtime-discovery.ts +4 -1
- package/src/cache/cache-error.ts +104 -0
- package/src/cache/cache-key-utils.ts +58 -13
- package/src/cache/cache-policy.ts +108 -34
- package/src/cache/cache-runtime.ts +454 -101
- package/src/cache/cache-scope.ts +159 -54
- package/src/cache/cache-tag.ts +149 -0
- package/src/cache/cf/cf-base64.ts +33 -0
- package/src/cache/cf/cf-cache-constants.ts +127 -0
- package/src/cache/cf/cf-cache-store.ts +2170 -377
- package/src/cache/cf/cf-cache-types.ts +349 -0
- package/src/cache/cf/cf-kv-utils.ts +46 -0
- package/src/cache/cf/cf-tag-marker-memo.ts +105 -0
- package/src/cache/cf/index.ts +6 -16
- package/src/cache/document-cache.ts +126 -41
- package/src/cache/handle-snapshot.ts +70 -0
- package/src/cache/index.ts +23 -20
- package/src/cache/memory-segment-store.ts +243 -37
- package/src/cache/profile-registry.ts +46 -31
- package/src/cache/read-through-swr.ts +56 -12
- package/src/cache/segment-codec.ts +13 -21
- package/src/cache/shell-snapshot.ts +417 -0
- package/src/cache/tag-invalidation.ts +230 -0
- package/src/cache/types.ts +194 -99
- package/src/cache/vercel/index.ts +11 -0
- package/src/cache/vercel/vercel-cache-store.ts +1132 -0
- package/src/client.rsc.tsx +39 -22
- package/src/client.tsx +28 -58
- package/src/cloudflare/index.ts +11 -0
- package/src/cloudflare/tracing.ts +108 -0
- package/src/component-utils.ts +19 -0
- package/src/components/DefaultDocument.tsx +8 -2
- package/src/context-var.ts +13 -1
- package/src/decode-loader-results.ts +18 -2
- package/src/defer.ts +185 -0
- package/src/deps/ssr.ts +0 -1
- package/src/encode-kv.ts +49 -0
- package/src/errors.ts +0 -3
- package/src/escape-script.ts +52 -0
- package/src/handle.ts +57 -40
- package/src/handles/MetaTags.tsx +24 -53
- package/src/handles/Scripts.tsx +183 -0
- package/src/handles/breadcrumbs.ts +35 -8
- package/src/handles/deferred-resolution.ts +127 -0
- package/src/handles/is-thenable.ts +18 -0
- package/src/handles/meta.ts +14 -40
- package/src/handles/script.ts +244 -0
- package/src/host/cookie-handler.ts +9 -60
- package/src/host/errors.ts +13 -22
- package/src/host/index.ts +7 -0
- package/src/host/pattern-matcher.ts +23 -52
- package/src/host/router.ts +1 -65
- package/src/host/testing.ts +40 -27
- package/src/host/types.ts +6 -2
- package/src/href-client.ts +7 -12
- package/src/index.rsc.ts +88 -8
- package/src/index.ts +90 -16
- package/src/internal-debug.ts +11 -10
- package/src/loader.rsc.ts +19 -9
- package/src/loader.ts +12 -4
- package/src/outlet-provider.tsx +1 -5
- package/src/prerender/param-hash.ts +16 -16
- package/src/prerender/store.ts +32 -37
- package/src/prerender.ts +75 -7
- package/src/redirect-origin.ts +114 -0
- package/src/regex-escape.ts +8 -0
- package/src/render-error-thrower.tsx +20 -0
- package/src/response-utils.ts +25 -0
- package/src/root-error-boundary.tsx +1 -19
- package/src/route-content-wrapper.tsx +13 -49
- package/src/route-definition/dsl-helpers.ts +60 -53
- package/src/route-definition/helper-factories.ts +0 -2
- package/src/route-definition/helpers-types.ts +46 -46
- package/src/route-definition/index.ts +1 -2
- package/src/route-definition/redirect.ts +44 -11
- package/src/route-definition/resolve-handler-use.ts +6 -1
- package/src/route-definition/use-item-types.ts +3 -6
- package/src/route-map-builder.ts +41 -20
- package/src/route-types.ts +0 -5
- package/src/router/content-negotiation.ts +58 -23
- package/src/router/error-handling.ts +44 -17
- package/src/router/find-match.ts +129 -30
- package/src/router/handler-context.ts +6 -1
- package/src/router/instrument.ts +355 -0
- package/src/router/intercept-resolution.ts +35 -2
- package/src/router/lazy-includes.ts +79 -56
- package/src/router/loader-resolution.ts +151 -73
- package/src/router/logging.ts +0 -6
- package/src/router/manifest.ts +74 -40
- package/src/router/match-api.ts +76 -52
- package/src/router/match-context.ts +0 -22
- package/src/router/match-handlers.ts +181 -178
- package/src/router/match-middleware/background-revalidation.ts +40 -24
- package/src/router/match-middleware/cache-lookup.ts +115 -194
- package/src/router/match-middleware/cache-store.ts +61 -50
- package/src/router/match-middleware/intercept-resolution.ts +0 -22
- package/src/router/match-middleware/segment-resolution.ts +0 -22
- package/src/router/match-pipelines.ts +1 -42
- package/src/router/match-result.ts +36 -67
- package/src/router/metrics.ts +0 -34
- package/src/router/middleware-types.ts +0 -116
- package/src/router/middleware.ts +231 -120
- package/src/router/navigation-snapshot.ts +7 -56
- package/src/router/params-util.ts +23 -0
- package/src/router/parse-pattern.ts +115 -0
- package/src/router/pattern-matching.ts +99 -152
- package/src/router/prefetch-cache-ttl.ts +51 -0
- package/src/router/prefetch-limits.ts +37 -0
- package/src/router/prerender-match.ts +111 -66
- package/src/router/preview-match.ts +3 -1
- package/src/router/request-classification.ts +47 -42
- package/src/router/revalidation.ts +75 -81
- package/src/router/route-snapshot.ts +14 -3
- package/src/router/router-context.ts +6 -29
- package/src/router/router-interfaces.ts +70 -8
- package/src/router/router-options.ts +126 -4
- package/src/router/segment-resolution/fresh.ts +104 -80
- package/src/router/segment-resolution/helpers.ts +86 -6
- package/src/router/segment-resolution/loader-cache.ts +155 -39
- package/src/router/segment-resolution/loader-mask.ts +60 -0
- package/src/router/segment-resolution/loader-snapshot.ts +259 -0
- package/src/router/segment-resolution/mask-nested.ts +83 -0
- package/src/router/segment-resolution/revalidation.ts +215 -304
- package/src/router/segment-resolution/static-store.ts +19 -5
- package/src/router/segment-resolution/streamed-handler-telemetry.ts +52 -0
- package/src/router/segment-resolution/view-transition-default.ts +35 -15
- package/src/router/segment-resolution.ts +5 -1
- package/src/router/segment-wrappers.ts +6 -5
- package/src/router/state-cookie-name.ts +33 -0
- package/src/router/substitute-pattern-params.ts +54 -35
- package/src/router/telemetry-otel.ts +160 -200
- package/src/router/telemetry.ts +9 -23
- package/src/router/timeout.ts +0 -20
- package/src/router/tracing.ts +215 -0
- package/src/router/trie-matching.ts +171 -64
- package/src/router/types.ts +1 -63
- package/src/router/url-params.ts +13 -5
- package/src/router.ts +119 -48
- package/src/rsc/full-payload.ts +70 -0
- package/src/rsc/handler-context.ts +1 -0
- package/src/rsc/handler.ts +267 -152
- package/src/rsc/helpers.ts +78 -4
- package/src/rsc/index.ts +1 -4
- package/src/rsc/json-route-result.ts +38 -0
- package/src/rsc/loader-fetch.ts +114 -38
- package/src/rsc/manifest-init.ts +29 -42
- package/src/rsc/nonce.ts +10 -1
- package/src/rsc/origin-guard.ts +11 -15
- package/src/rsc/progressive-enhancement.ts +120 -13
- package/src/rsc/redirect-guard.ts +100 -0
- package/src/rsc/response-cache-serve.ts +238 -0
- package/src/rsc/response-error.ts +79 -12
- package/src/rsc/response-route-handler.ts +58 -141
- package/src/rsc/rsc-rendering.ts +492 -49
- package/src/rsc/runtime-warnings.ts +14 -0
- package/src/rsc/server-action.ts +268 -82
- package/src/rsc/shell-capture.ts +1190 -0
- package/src/rsc/shell-serve.ts +181 -0
- package/src/rsc/transition-gate.ts +89 -0
- package/src/rsc/types.ts +45 -3
- package/src/runtime-env.ts +18 -0
- package/src/search-params.ts +31 -26
- package/src/segment-loader-promise.ts +49 -4
- package/src/segment-system.tsx +260 -95
- package/src/server/context.ts +99 -9
- package/src/server/cookie-parse.ts +32 -0
- package/src/server/cookie-store.ts +125 -2
- package/src/server/handle-store.ts +21 -38
- package/src/server/loader-registry.ts +33 -42
- package/src/server/request-context.ts +379 -138
- package/src/ssr/index.tsx +491 -182
- package/src/ssr/inject-rsc-eager.ts +167 -0
- package/src/ssr/ssr-root.tsx +228 -0
- package/src/static-handler.ts +10 -13
- package/src/testing/cache-status.ts +44 -48
- package/src/testing/collect-handle.ts +14 -31
- package/src/testing/dispatch.ts +533 -160
- package/src/testing/e2e/fixture.ts +45 -11
- package/src/testing/e2e/index.ts +1 -22
- package/src/testing/e2e/matchers.ts +0 -16
- package/src/testing/e2e/parity.ts +85 -4
- package/src/testing/e2e/server.ts +12 -0
- package/src/testing/flight-matchers.ts +7 -14
- package/src/testing/flight-normalize.ts +11 -0
- package/src/testing/flight-runtime.d.ts +36 -0
- package/src/testing/flight-tree.ts +682 -0
- package/src/testing/flight.entry.ts +30 -0
- package/src/testing/flight.ts +145 -70
- package/src/testing/generated-routes.ts +26 -50
- package/src/testing/index.ts +18 -19
- package/src/testing/internal/context.ts +184 -68
- package/src/testing/internal/flight-client-globals.ts +30 -0
- package/src/testing/internal/seed-vars.ts +54 -0
- package/src/testing/render-handler.ts +357 -0
- package/src/testing/render-route.tsx +134 -115
- package/src/testing/run-loader.ts +140 -51
- package/src/testing/run-middleware.ts +59 -33
- package/src/testing/run-transition-when.ts +164 -0
- package/src/testing/vitest-stubs/cloudflare-email.ts +1 -1
- package/src/testing/vitest-stubs/cloudflare-workers.ts +1 -1
- package/src/testing/vitest.ts +138 -16
- package/src/theme/ThemeProvider.tsx +56 -84
- package/src/theme/ThemeScript.tsx +7 -9
- package/src/theme/constants.ts +52 -13
- package/src/theme/index.ts +0 -7
- package/src/theme/theme-context.ts +1 -5
- package/src/theme/theme-script.ts +22 -21
- package/src/theme/use-theme.ts +0 -3
- package/src/types/boundaries.ts +0 -35
- package/src/types/cache-types.ts +13 -4
- package/src/types/error-types.ts +30 -90
- package/src/types/global-namespace.ts +15 -15
- package/src/types/handler-context.ts +45 -15
- package/src/types/index.ts +2 -10
- package/src/types/loader-types.ts +6 -3
- package/src/types/request-scope.ts +8 -22
- package/src/types/route-config.ts +20 -52
- package/src/types/route-entry.ts +0 -6
- package/src/types/segments.ts +100 -13
- package/src/urls/include-helper.ts +10 -12
- package/src/urls/include-provider.ts +71 -0
- package/src/urls/index.ts +2 -8
- package/src/urls/path-helper-types.ts +52 -14
- package/src/urls/path-helper.ts +5 -54
- package/src/urls/pattern-types.ts +36 -0
- package/src/urls/type-extraction.ts +76 -42
- package/src/urls/urls-function.ts +0 -14
- package/src/use-loader.tsx +0 -186
- package/src/vercel/index.ts +11 -0
- package/src/vercel/tracing.ts +88 -0
- package/src/vite/discovery/bundle-postprocess.ts +2 -1
- package/src/vite/discovery/dev-prerender-cache.ts +117 -0
- package/src/vite/discovery/discover-routers.ts +34 -43
- package/src/vite/discovery/discovery-errors.ts +61 -0
- package/src/vite/discovery/prerender-collection.ts +33 -46
- package/src/vite/discovery/state.ts +12 -1
- package/src/vite/discovery/virtual-module-codegen.ts +1 -11
- package/src/vite/index.ts +9 -0
- package/src/vite/inject-client-debug.ts +88 -0
- package/src/vite/plugin-types.ts +143 -10
- package/src/vite/plugins/cjs-to-esm.ts +8 -12
- package/src/vite/plugins/client-ref-dedup.ts +0 -11
- package/src/vite/plugins/client-ref-hashing.ts +0 -10
- package/src/vite/plugins/cloudflare-protocol-stub.ts +0 -20
- package/src/vite/plugins/expose-action-id.ts +2 -73
- package/src/vite/plugins/expose-id-utils.ts +85 -56
- package/src/vite/plugins/expose-ids/export-analysis.ts +30 -43
- package/src/vite/plugins/expose-ids/handler-transform.ts +5 -31
- package/src/vite/plugins/expose-ids/loader-transform.ts +12 -20
- package/src/vite/plugins/expose-ids/router-transform.ts +98 -26
- package/src/vite/plugins/expose-internal-ids.ts +10 -1
- package/src/vite/plugins/performance-tracks.ts +0 -3
- package/src/vite/plugins/refresh-cmd.ts +1 -1
- package/src/vite/plugins/use-cache-transform.ts +21 -46
- package/src/vite/plugins/vercel-output.ts +384 -0
- package/src/vite/plugins/version-injector.ts +22 -27
- package/src/vite/plugins/version-plugin.ts +6 -66
- package/src/vite/plugins/virtual-entries.ts +137 -26
- package/src/vite/rango.ts +146 -135
- package/src/vite/router-discovery.ts +189 -48
- package/src/vite/utils/ast-handler-extract.ts +11 -20
- package/src/vite/utils/bundle-analysis.ts +6 -13
- package/src/vite/utils/client-chunks.ts +0 -6
- package/src/vite/utils/directive-prologue.ts +40 -0
- package/src/vite/utils/forward-user-plugins.ts +0 -22
- package/src/vite/utils/manifest-utils.ts +4 -75
- package/src/vite/utils/package-resolution.ts +1 -73
- package/src/vite/utils/prerender-utils.ts +71 -44
- package/src/vite/utils/shared-utils.ts +55 -37
- package/src/browser/react/use-client-cache.ts +0 -58
- package/src/browser/shallow.ts +0 -40
- package/src/handles/index.ts +0 -7
- package/src/network-error-thrower.tsx +0 -23
- package/src/router/middleware-cookies.ts +0 -55
package/src/vite/plugin-types.ts
CHANGED
|
@@ -12,7 +12,7 @@ export interface BuildEnvFactoryContext {
|
|
|
12
12
|
/** Vite command ("serve" for dev, "build" for production). */
|
|
13
13
|
command: "serve" | "build";
|
|
14
14
|
/** Router deployment preset. */
|
|
15
|
-
preset: "node" | "cloudflare";
|
|
15
|
+
preset: "node" | "cloudflare" | "vercel";
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/**
|
|
@@ -127,16 +127,43 @@ interface RangoBaseOptions {
|
|
|
127
127
|
clientChunks?: ClientChunks;
|
|
128
128
|
|
|
129
129
|
/**
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
130
|
+
* Filter which files route discovery scans, by glob. Paths are matched
|
|
131
|
+
* root-relative (e.g. `src/routes/**`). `include` restricts discovery to
|
|
132
|
+
* matching files; `exclude` removes matches (the defaults cover tests, dist,
|
|
133
|
+
* coverage, etc.). Mirrors the CLI's `--include`/`--exclude`.
|
|
133
134
|
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
135
|
+
* @example
|
|
136
|
+
* rango({ discovery: { include: ["src/routes/**"] } })
|
|
137
|
+
*/
|
|
138
|
+
discovery?: {
|
|
139
|
+
include?: string[];
|
|
140
|
+
exclude?: string[];
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* What to do when a `Prerender` route's or `Static` handler's render throws at
|
|
145
|
+
* build time. Otherwise the route error boundary catches it and the rendered
|
|
146
|
+
* error page is baked as the artifact, then served as an HTTP 200 — a silent,
|
|
147
|
+
* user-visible breakage (issue #587). Independent of this setting, a render may
|
|
148
|
+
* `throw new Skip()` (from `@rangojs/router`) to skip a single URL/handler.
|
|
136
149
|
*
|
|
137
|
-
*
|
|
150
|
+
* - `"fail"` (**default**): fail the build, naming the URL/handler and the
|
|
151
|
+
* original render error.
|
|
152
|
+
* - `"warn"`: log a warning and skip baking the artifact — it is never served as a
|
|
153
|
+
* baked 200 error page. `"warn"` is a build-unblock, NOT a runtime contract for
|
|
154
|
+
* the skipped entry: the route falls through to normal resolution, which may
|
|
155
|
+
* render live (its handler is still bundled — e.g. when nothing else baked) or
|
|
156
|
+
* 404 (once prerender handler eviction has run for other baked entries), so the
|
|
157
|
+
* outcome depends on the rest of the build, and a skipped `Static()` handler's
|
|
158
|
+
* evicted code can surface as an error. For DEFINED runtime behavior use
|
|
159
|
+
* `Passthrough()` (a live fallback) or `throw new Skip()` (an intentional skip);
|
|
160
|
+
* otherwise prefer the default `"fail"`.
|
|
161
|
+
*
|
|
162
|
+
* @default "fail"
|
|
138
163
|
*/
|
|
139
|
-
|
|
164
|
+
prerender?: {
|
|
165
|
+
onError?: "fail" | "warn";
|
|
166
|
+
};
|
|
140
167
|
}
|
|
141
168
|
|
|
142
169
|
/**
|
|
@@ -147,6 +174,31 @@ export interface RangoNodeOptions extends RangoBaseOptions {
|
|
|
147
174
|
* Deployment preset. Defaults to 'node' when not specified.
|
|
148
175
|
*/
|
|
149
176
|
preset?: "node";
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Path to a host-router entry (a module that calls `createHostRouter()` and
|
|
180
|
+
* exports the instance) to serve instead of a single `createRouter()` app.
|
|
181
|
+
* Root-relative (e.g. `"./src/worker.rsc.tsx"`).
|
|
182
|
+
*
|
|
183
|
+
* Set this when the app is a multi-app host router: auto-discovery otherwise
|
|
184
|
+
* finds the sub-apps' multiple `createRouter()` files and cannot pick an entry.
|
|
185
|
+
* When omitted, rango auto-detects a single `createHostRouter()` file if the
|
|
186
|
+
* app has several `createRouter()` files. The host module must export the
|
|
187
|
+
* `HostRouter` instance (default export or a named `hostRouter`/`router`
|
|
188
|
+
* export), not a Cloudflare-style `{ fetch }` object.
|
|
189
|
+
*/
|
|
190
|
+
hostRouter?: string;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Environment bindings available to Prerender and Static handlers at build
|
|
194
|
+
* time via `ctx.env`. Shared across all prerender invocations for the build.
|
|
195
|
+
*
|
|
196
|
+
* `"auto"` is Cloudflare-only (it resolves the wrangler platform proxy), so it
|
|
197
|
+
* is not accepted on the Node preset — pass an object or a factory instead.
|
|
198
|
+
*
|
|
199
|
+
* @default false
|
|
200
|
+
*/
|
|
201
|
+
buildEnv?: Exclude<BuildEnvOption, "auto">;
|
|
150
202
|
}
|
|
151
203
|
|
|
152
204
|
/**
|
|
@@ -156,15 +208,96 @@ export interface RangoCloudflareOptions extends RangoBaseOptions {
|
|
|
156
208
|
/**
|
|
157
209
|
* Deployment preset for Cloudflare Workers.
|
|
158
210
|
* When using cloudflare preset:
|
|
159
|
-
* - @vitejs/plugin-rsc
|
|
211
|
+
* - @vitejs/plugin-rsc IS still added by rango(), but with `serverHandler: false`
|
|
212
|
+
* (the cloudflare plugin owns the RSC worker/server entry); only `client` and
|
|
213
|
+
* `ssr` virtual entries are configured, no rsc entry
|
|
160
214
|
* - Your worker entry (e.g., worker.rsc.tsx) imports the router directly
|
|
161
215
|
* - Browser and SSR use virtual entries
|
|
162
216
|
* - Build-time manifest generation is auto-detected from the resolved RSC environment config
|
|
163
217
|
*/
|
|
164
218
|
preset: "cloudflare";
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Environment bindings available to Prerender and Static handlers at build
|
|
222
|
+
* time via `ctx.env`. Shared across all prerender invocations for the build.
|
|
223
|
+
*
|
|
224
|
+
* `"auto"` resolves the Cloudflare platform proxy via wrangler
|
|
225
|
+
* `getPlatformProxy()`.
|
|
226
|
+
*
|
|
227
|
+
* @default false
|
|
228
|
+
*/
|
|
229
|
+
buildEnv?: BuildEnvOption;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Per-function knobs for the Vercel deployment, written into the generated
|
|
234
|
+
* `.vc-config.json` (and `config.json` for `functionName`).
|
|
235
|
+
*/
|
|
236
|
+
export interface VercelPresetOptions {
|
|
237
|
+
/** Node runtime for the function. @default "nodejs22.x" */
|
|
238
|
+
runtime?: string;
|
|
239
|
+
/** Max execution time in seconds. @default 30 */
|
|
240
|
+
maxDuration?: number;
|
|
241
|
+
/** Function memory in MB (platform default when omitted). */
|
|
242
|
+
memory?: number;
|
|
243
|
+
/** Regions to pin the function to (platform default when omitted). */
|
|
244
|
+
regions?: string[];
|
|
245
|
+
/**
|
|
246
|
+
* Function name — the `<name>.func` directory and the `config.json` route
|
|
247
|
+
* destination. @default "index"
|
|
248
|
+
*/
|
|
249
|
+
functionName?: string;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Options for Vercel Functions deployment.
|
|
254
|
+
*
|
|
255
|
+
* Builds like the node preset (Vercel runs Node Functions, not Workers): rango
|
|
256
|
+
* owns the RSC entry, `process.env.NODE_ENV` is folded for the build, and after
|
|
257
|
+
* the build a `.vercel/output` directory (Build Output API v3) is assembled from
|
|
258
|
+
* `dist/` — a single streaming Node Function plus the static client assets. The
|
|
259
|
+
* app must install `@vercel/functions` (used by `VercelCacheStore` and the
|
|
260
|
+
* generated function launcher).
|
|
261
|
+
*/
|
|
262
|
+
export interface RangoVercelOptions extends RangoBaseOptions {
|
|
263
|
+
/**
|
|
264
|
+
* Deployment preset for Vercel Functions.
|
|
265
|
+
*/
|
|
266
|
+
preset: "vercel";
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Path to a host-router entry (a module that calls `createHostRouter()` and
|
|
270
|
+
* exports the instance) to serve instead of a single `createRouter()` app.
|
|
271
|
+
* Root-relative (e.g. `"./src/worker.rsc.tsx"`).
|
|
272
|
+
*
|
|
273
|
+
* Set this when the app is a multi-app host router: auto-discovery otherwise
|
|
274
|
+
* finds the sub-apps' multiple `createRouter()` files and cannot pick an entry.
|
|
275
|
+
* When omitted, rango auto-detects a single `createHostRouter()` file if the
|
|
276
|
+
* app has several `createRouter()` files. The host module must export the
|
|
277
|
+
* `HostRouter` instance (default export or a named `hostRouter`/`router`
|
|
278
|
+
* export), not a Cloudflare-style `{ fetch }` object. The Vercel function then
|
|
279
|
+
* runs `hostRouter.match()` for every request (single-function deploy).
|
|
280
|
+
*/
|
|
281
|
+
hostRouter?: string;
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Environment bindings available to Prerender and Static handlers at build
|
|
285
|
+
* time via `ctx.env`. `"auto"` is Cloudflare-only; pass an object or a factory.
|
|
286
|
+
*
|
|
287
|
+
* @default false
|
|
288
|
+
*/
|
|
289
|
+
buildEnv?: Exclude<BuildEnvOption, "auto">;
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Vercel function configuration written into the Build Output.
|
|
293
|
+
*/
|
|
294
|
+
vercel?: VercelPresetOptions;
|
|
165
295
|
}
|
|
166
296
|
|
|
167
297
|
/**
|
|
168
298
|
* Options for rango() Vite plugin
|
|
169
299
|
*/
|
|
170
|
-
export type RangoOptions =
|
|
300
|
+
export type RangoOptions =
|
|
301
|
+
| RangoNodeOptions
|
|
302
|
+
| RangoCloudflareOptions
|
|
303
|
+
| RangoVercelOptions;
|
|
@@ -8,15 +8,21 @@ const debug = createRangoDebugger(NS.transform);
|
|
|
8
8
|
* The react-server-dom vendor files are shipped as CJS which doesn't work in browsers.
|
|
9
9
|
*/
|
|
10
10
|
export function createCjsToEsmPlugin(): Plugin {
|
|
11
|
+
// Picked from Vite's resolved mode, not process.env.NODE_ENV, so the dev vs
|
|
12
|
+
// production vendor variant tracks the build mode the user actually ran.
|
|
13
|
+
let isProduction = false;
|
|
14
|
+
|
|
11
15
|
return {
|
|
12
16
|
name: "@rangojs/router:cjs-to-esm",
|
|
13
17
|
enforce: "pre",
|
|
18
|
+
configResolved(config) {
|
|
19
|
+
isProduction = config.isProduction;
|
|
20
|
+
},
|
|
14
21
|
transform(code, id) {
|
|
15
22
|
const cleanId = id.split("?")[0].replaceAll("\\", "/");
|
|
16
23
|
|
|
17
|
-
// Transform the client.browser.js entry point to re-export from CJS
|
|
18
24
|
if (cleanId.includes("vendor/react-server-dom/client.browser.js")) {
|
|
19
|
-
const isProd =
|
|
25
|
+
const isProd = isProduction;
|
|
20
26
|
const cjsFile = isProd
|
|
21
27
|
? "./cjs/react-server-dom-webpack-client.browser.production.js"
|
|
22
28
|
: "./cjs/react-server-dom-webpack-client.browser.development.js";
|
|
@@ -28,57 +34,47 @@ export function createCjsToEsmPlugin(): Plugin {
|
|
|
28
34
|
};
|
|
29
35
|
}
|
|
30
36
|
|
|
31
|
-
// Transform the actual CJS files to ESM
|
|
32
37
|
if (
|
|
33
38
|
cleanId.includes("vendor/react-server-dom/cjs/") &&
|
|
34
39
|
cleanId.includes("client.browser")
|
|
35
40
|
) {
|
|
36
41
|
let transformed = code;
|
|
37
42
|
|
|
38
|
-
// Extract the license comment to preserve it
|
|
39
43
|
const licenseMatch = transformed.match(/^\/\*\*[\s\S]*?\*\//);
|
|
40
44
|
const license = licenseMatch ? licenseMatch[0] : "";
|
|
41
45
|
if (license) {
|
|
42
46
|
transformed = transformed.slice(license.length);
|
|
43
47
|
}
|
|
44
48
|
|
|
45
|
-
// Remove "use strict" (both dev and prod have this)
|
|
46
49
|
transformed = transformed.replace(/^\s*["']use strict["'];\s*/, "");
|
|
47
50
|
|
|
48
|
-
// Remove the conditional IIFE wrapper (development only)
|
|
49
51
|
transformed = transformed.replace(
|
|
50
52
|
/^\s*["']production["']\s*!==\s*process\.env\.NODE_ENV\s*&&\s*\(function\s*\(\)\s*\{/,
|
|
51
53
|
"",
|
|
52
54
|
);
|
|
53
55
|
|
|
54
|
-
// Remove the closing of the conditional IIFE at the end (development only)
|
|
55
56
|
transformed = transformed.replace(/\}\)\(\);?\s*$/, "");
|
|
56
57
|
|
|
57
|
-
// Replace require('react') and require('react-dom') with imports (development)
|
|
58
58
|
transformed = transformed.replace(
|
|
59
59
|
/var\s+React\s*=\s*require\s*\(\s*["']react["']\s*\)\s*,[\s\n]+ReactDOM\s*=\s*require\s*\(\s*["']react-dom["']\s*\)\s*,/g,
|
|
60
60
|
'import React from "react";\nimport ReactDOM from "react-dom";\nvar ',
|
|
61
61
|
);
|
|
62
62
|
|
|
63
|
-
// Replace require('react-dom') only (production - doesn't import React)
|
|
64
63
|
transformed = transformed.replace(
|
|
65
64
|
/var\s+ReactDOM\s*=\s*require\s*\(\s*["']react-dom["']\s*\)\s*,/g,
|
|
66
65
|
'import ReactDOM from "react-dom";\nvar ',
|
|
67
66
|
);
|
|
68
67
|
|
|
69
|
-
// Transform exports.xyz = function() to export function xyz()
|
|
70
68
|
transformed = transformed.replace(
|
|
71
69
|
/exports\.(\w+)\s*=\s*function\s*\(/g,
|
|
72
70
|
"export function $1(",
|
|
73
71
|
);
|
|
74
72
|
|
|
75
|
-
// Transform exports.xyz = value to export const xyz = value
|
|
76
73
|
transformed = transformed.replace(
|
|
77
74
|
/exports\.(\w+)\s*=/g,
|
|
78
75
|
"export const $1 =",
|
|
79
76
|
);
|
|
80
77
|
|
|
81
|
-
// Reconstruct with license at the top
|
|
82
78
|
transformed = license + "\n" + transformed;
|
|
83
79
|
|
|
84
80
|
debug?.("cjs-to-esm body rewrite %s", id);
|
|
@@ -73,8 +73,6 @@ export function clientRefDedup(): Plugin {
|
|
|
73
73
|
apply: "serve",
|
|
74
74
|
|
|
75
75
|
configResolved(config: ResolvedConfig) {
|
|
76
|
-
// Respect user's optimizeDeps.exclude — if a package is explicitly
|
|
77
|
-
// excluded from pre-bundling, we shouldn't redirect it there.
|
|
78
76
|
const clientEnv = config.environments?.["client"];
|
|
79
77
|
clientExclude =
|
|
80
78
|
clientEnv?.optimizeDeps?.exclude ?? config.optimizeDeps?.exclude ?? [];
|
|
@@ -91,27 +89,19 @@ export function clientRefDedup(): Plugin {
|
|
|
91
89
|
},
|
|
92
90
|
|
|
93
91
|
resolveId(source, importer, options) {
|
|
94
|
-
// Only intercept in the client environment
|
|
95
92
|
if (this.environment?.name !== "client") return;
|
|
96
93
|
|
|
97
|
-
// Only handle imports from client-in-server-package-proxy virtual modules
|
|
98
94
|
if (!importer?.includes(CLIENT_IN_SERVER_PROXY_PREFIX)) return;
|
|
99
95
|
|
|
100
|
-
// Only handle absolute node_modules paths
|
|
101
96
|
if (!source.includes("/node_modules/")) return;
|
|
102
97
|
|
|
103
|
-
// Must have an importer
|
|
104
|
-
if (!importer) return;
|
|
105
|
-
|
|
106
98
|
const packageName = extractPackageName(source);
|
|
107
99
|
if (!packageName) return;
|
|
108
100
|
|
|
109
|
-
// Don't redirect packages that are excluded from optimization
|
|
110
101
|
if (clientExclude.includes(packageName)) return;
|
|
111
102
|
|
|
112
103
|
if (debug) dedupedPackages.add(packageName);
|
|
113
104
|
|
|
114
|
-
// Return a virtual module that re-exports via bare specifier
|
|
115
105
|
return `\0rango:dedup/${packageName}`;
|
|
116
106
|
},
|
|
117
107
|
|
|
@@ -120,7 +110,6 @@ export function clientRefDedup(): Plugin {
|
|
|
120
110
|
|
|
121
111
|
const packageName = id.slice("\0rango:dedup/".length);
|
|
122
112
|
|
|
123
|
-
// Re-export via bare specifier so Vite routes through pre-bundling
|
|
124
113
|
return [
|
|
125
114
|
`export * from ${JSON.stringify(packageName)};`,
|
|
126
115
|
`import * as __all__ from ${JSON.stringify(packageName)};`,
|
|
@@ -5,7 +5,6 @@ import { createRangoDebugger, createCounter, NS } from "../debug.js";
|
|
|
5
5
|
|
|
6
6
|
const debug = createRangoDebugger(NS.transform);
|
|
7
7
|
|
|
8
|
-
// Dev-mode client-reference key prefixes emitted by @vitejs/plugin-rsc
|
|
9
8
|
const CLIENT_PKG_PROXY_PREFIX =
|
|
10
9
|
"/@id/__x00__virtual:vite-rsc/client-package-proxy/";
|
|
11
10
|
const CLIENT_IN_SERVER_PKG_PROXY_PREFIX =
|
|
@@ -40,32 +39,24 @@ export function computeProductionHash(
|
|
|
40
39
|
let toHash: string;
|
|
41
40
|
|
|
42
41
|
if (refKey.startsWith(CLIENT_PKG_PROXY_PREFIX)) {
|
|
43
|
-
// /@id/__x00__virtual:vite-rsc/client-package-proxy/<pkg> -> hash("<pkg>")
|
|
44
42
|
toHash = refKey.slice(CLIENT_PKG_PROXY_PREFIX.length);
|
|
45
43
|
} else if (refKey.startsWith(CLIENT_IN_SERVER_PKG_PROXY_PREFIX)) {
|
|
46
|
-
// /@id/__x00__virtual:vite-rsc/client-in-server-package-proxy/<encodedAbsPath>
|
|
47
44
|
const absPath = decodeURIComponent(
|
|
48
45
|
refKey.slice(CLIENT_IN_SERVER_PKG_PROXY_PREFIX.length),
|
|
49
46
|
);
|
|
50
47
|
toHash = relative(projectRoot, absPath).replaceAll("\\", "/");
|
|
51
48
|
} else if (refKey.startsWith(FS_PREFIX)) {
|
|
52
|
-
// /@fs/abs/path.tsx -> hash(relative(root, "/abs/path.tsx"))
|
|
53
49
|
const absPath = refKey.slice(FS_PREFIX.length - 1); // keep leading /
|
|
54
50
|
toHash = relative(projectRoot, absPath).replaceAll("\\", "/");
|
|
55
51
|
} else if (refKey.startsWith("/")) {
|
|
56
|
-
// /src/Button.tsx -> hash("src/Button.tsx")
|
|
57
52
|
toHash = refKey.slice(1);
|
|
58
53
|
} else {
|
|
59
|
-
// Already hashed or unknown format — return unchanged
|
|
60
54
|
return refKey;
|
|
61
55
|
}
|
|
62
56
|
|
|
63
57
|
return hashRefKey(toHash);
|
|
64
58
|
}
|
|
65
59
|
|
|
66
|
-
// Regex to match registerClientReference() calls as emitted by @vitejs/plugin-rsc.
|
|
67
|
-
// Captures the reference key (second argument) from the call.
|
|
68
|
-
// Handles two proxy forms: parenthesized expression `(expr)` and arrow-throw `() => { ... }`.
|
|
69
60
|
const REGISTER_CLIENT_REF_RE =
|
|
70
61
|
/registerClientReference\(\s*(?:(?:\([^)]*\))|(?:\(\)[\s\S]*?\}))\s*,\s*"([^"]+)"\s*,\s*"[^"]+"\s*\)/g;
|
|
71
62
|
|
|
@@ -106,7 +97,6 @@ export function hashClientRefs(projectRoot: string): Plugin {
|
|
|
106
97
|
const counter = createCounter(debug, "hash-client-refs");
|
|
107
98
|
return {
|
|
108
99
|
name: "@rangojs/router:hash-client-refs",
|
|
109
|
-
// Run after the RSC plugin's transform (default enforce is normal)
|
|
110
100
|
enforce: "post",
|
|
111
101
|
applyToEnvironment(env) {
|
|
112
102
|
return env.name === "rsc";
|
|
@@ -32,14 +32,6 @@ const IMPORT_NODE_TYPES = new Set([
|
|
|
32
32
|
"ExportAllDeclaration",
|
|
33
33
|
]);
|
|
34
34
|
|
|
35
|
-
// Keep in sync with `STUBS` in cloudflare-protocol-loader-hook.mjs —
|
|
36
|
-
// both paths (Vite transform and Node loader) need to hand out the same
|
|
37
|
-
// classes. Unknown `cloudflare:*` modules fall back to an empty default
|
|
38
|
-
// export so third-party packages (e.g. the Cloudflare Agents SDK) can
|
|
39
|
-
// pull them into the graph without crashing discovery. Discovery only
|
|
40
|
-
// evaluates module top-level code — no handlers run — so missing named
|
|
41
|
-
// exports only fail if something does `class X extends Missing {}` at
|
|
42
|
-
// module scope, which is rare outside the already-stubbed classes.
|
|
43
35
|
const STUBS: Record<string, string> = {
|
|
44
36
|
"cloudflare:workers": `
|
|
45
37
|
export class DurableObject { constructor(_ctx, _env) {} }
|
|
@@ -65,15 +57,6 @@ export default {};
|
|
|
65
57
|
`,
|
|
66
58
|
};
|
|
67
59
|
|
|
68
|
-
// Policy: unknown `cloudflare:*` specifiers resolve permissively (empty
|
|
69
|
-
// default export) rather than throwing. We prioritize dependency-graph
|
|
70
|
-
// resilience over strict validation of user imports because third-party
|
|
71
|
-
// packages can pull `cloudflare:*` modules we haven't curated, and
|
|
72
|
-
// discovery should not fail just because those modules appear in the graph.
|
|
73
|
-
// Tradeoff: unsupported user-authored `cloudflare:*` imports may fail later
|
|
74
|
-
// with a generic JS/module error instead of a tailored rango-branded hint.
|
|
75
|
-
// The test below pins this behavior so dependency compatibility is not
|
|
76
|
-
// regressed accidentally.
|
|
77
60
|
const FALLBACK_STUB = `export default {};\n`;
|
|
78
61
|
|
|
79
62
|
interface AstNode {
|
|
@@ -163,9 +146,6 @@ export function createCloudflareProtocolStubPlugin(): Plugin {
|
|
|
163
146
|
|
|
164
147
|
if (hits.length === 0) return null;
|
|
165
148
|
|
|
166
|
-
// Rewrite from last to first so earlier offsets stay valid. `start`/
|
|
167
|
-
// `end` span the full literal including quotes, so we re-emit the
|
|
168
|
-
// same quote character around the new specifier.
|
|
169
149
|
hits.sort((a, b) => b.start - a.start);
|
|
170
150
|
let out = code;
|
|
171
151
|
for (const hit of hits) {
|
|
@@ -7,9 +7,6 @@ import { createRangoDebugger, createCounter, NS } from "../debug.js";
|
|
|
7
7
|
|
|
8
8
|
const debug = createRangoDebugger(NS.transform);
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
* Type for the RSC plugin's manager API
|
|
12
|
-
*/
|
|
13
10
|
interface RscPluginManager {
|
|
14
11
|
serverReferenceMetaMap: Record<
|
|
15
12
|
string,
|
|
@@ -26,14 +23,9 @@ interface RscPluginApi {
|
|
|
26
23
|
manager: RscPluginManager;
|
|
27
24
|
}
|
|
28
25
|
|
|
29
|
-
/**
|
|
30
|
-
* Get the RSC plugin's API from Vite config
|
|
31
|
-
*/
|
|
32
26
|
function getRscPluginApi(config: ResolvedConfig): RscPluginApi | undefined {
|
|
33
|
-
// Try by name first
|
|
34
27
|
let plugin = config.plugins.find((p) => p.name === "rsc:minimal");
|
|
35
28
|
|
|
36
|
-
// Fallback: find by API structure if name lookup fails
|
|
37
29
|
if (!plugin) {
|
|
38
30
|
plugin = config.plugins.find(
|
|
39
31
|
(p) =>
|
|
@@ -62,13 +54,11 @@ function getRscPluginApi(config: ResolvedConfig): RscPluginApi | undefined {
|
|
|
62
54
|
function isUseServerModule(filePath: string): boolean {
|
|
63
55
|
try {
|
|
64
56
|
const content = fs.readFileSync(filePath, "utf-8");
|
|
65
|
-
// Remove leading comments and whitespace to find the first meaningful content
|
|
66
57
|
const trimmed = content
|
|
67
|
-
.replace(/^\s*\/\/[^\n]*\n/gm, "")
|
|
68
|
-
.replace(/^\s*\/\*[\s\S]*?\*\/\s*/gm, "")
|
|
58
|
+
.replace(/^\s*\/\/[^\n]*\n/gm, "")
|
|
59
|
+
.replace(/^\s*\/\*[\s\S]*?\*\/\s*/gm, "")
|
|
69
60
|
.trimStart();
|
|
70
61
|
|
|
71
|
-
// Check if the file starts with "use server" directive
|
|
72
62
|
return (
|
|
73
63
|
trimmed.startsWith('"use server"') || trimmed.startsWith("'use server'")
|
|
74
64
|
);
|
|
@@ -77,18 +67,6 @@ function isUseServerModule(filePath: string): boolean {
|
|
|
77
67
|
}
|
|
78
68
|
}
|
|
79
69
|
|
|
80
|
-
/**
|
|
81
|
-
* Transform code to expose action IDs on createServerReference calls.
|
|
82
|
-
* Wraps each call with an IIFE that attaches $id to the returned function.
|
|
83
|
-
*
|
|
84
|
-
* @param code - The source code to transform
|
|
85
|
-
* @param sourceId - The source file identifier (for sourcemap)
|
|
86
|
-
* @param hashToFileMap - Optional mapping from hash to file path (for server bundles)
|
|
87
|
-
*/
|
|
88
|
-
/**
|
|
89
|
-
* Apply createServerReference wrapping to a MagicString instance.
|
|
90
|
-
* Returns true if any changes were made.
|
|
91
|
-
*/
|
|
92
70
|
function applyServerReferenceWrapping(
|
|
93
71
|
code: string,
|
|
94
72
|
s: MagicString,
|
|
@@ -98,11 +76,6 @@ function applyServerReferenceWrapping(
|
|
|
98
76
|
return false;
|
|
99
77
|
}
|
|
100
78
|
|
|
101
|
-
// Match: createServerReference("hash#actionName", ...) or $$ReactClient.createServerReference(...)
|
|
102
|
-
// The RSC plugin uses $$ReactClient namespace in transformed code.
|
|
103
|
-
// Note: [^)]* cannot handle nested parens in trailing args. This is safe in practice
|
|
104
|
-
// because the RSC plugin always generates simple variable references (e.g., callServer)
|
|
105
|
-
// as the second argument, never nested function calls.
|
|
106
79
|
const pattern =
|
|
107
80
|
/((?:\$\$\w+\.)?createServerReference)\(("[^"]+#[^"]+")([^)]*)\)/g;
|
|
108
81
|
|
|
@@ -115,23 +88,19 @@ function applyServerReferenceWrapping(
|
|
|
115
88
|
const start = match.index;
|
|
116
89
|
const end = start + fullMatch.length;
|
|
117
90
|
|
|
118
|
-
// Parse the ID to potentially replace hash with file path
|
|
119
91
|
let finalIdArg = idArg;
|
|
120
92
|
if (hashToFileMap) {
|
|
121
|
-
// idArg is like '"hash#actionName"', extract the parts
|
|
122
93
|
const idValue = idArg.slice(1, -1); // Remove quotes
|
|
123
94
|
const hashMatch = idValue.match(/^([^#]+)#(.+)$/);
|
|
124
95
|
if (hashMatch) {
|
|
125
96
|
const [, hash, actionName] = hashMatch;
|
|
126
97
|
const filePath = hashToFileMap.get(hash);
|
|
127
98
|
if (filePath) {
|
|
128
|
-
// Replace hash with file path for server-side
|
|
129
99
|
finalIdArg = `"${filePath}#${actionName}"`;
|
|
130
100
|
}
|
|
131
101
|
}
|
|
132
102
|
}
|
|
133
103
|
|
|
134
|
-
// Wrap the createServerReference call to attach $$id to the returned function
|
|
135
104
|
const replacement = `(function(fn) { fn.$$id = ${finalIdArg}; return fn; })(${fnCall}(${idArg}${rest}))`;
|
|
136
105
|
s.overwrite(start, end, replacement);
|
|
137
106
|
}
|
|
@@ -155,29 +124,6 @@ function transformServerReferences(
|
|
|
155
124
|
};
|
|
156
125
|
}
|
|
157
126
|
|
|
158
|
-
/**
|
|
159
|
-
* Transform registerServerReference calls in server bundles to use file paths instead of hashes.
|
|
160
|
-
* Pattern: registerServerReference(fn, "hash", "exportName")
|
|
161
|
-
* React's registerServerReference sets $$id = hash + "#" + exportName
|
|
162
|
-
* By replacing the hash with file path, $$id will contain the file path for revalidation matching.
|
|
163
|
-
*
|
|
164
|
-
* Only actions from module-level "use server" files are transformed.
|
|
165
|
-
* Inline actions (defined in RSC components with "use server" inside a function) are NOT in
|
|
166
|
-
* hashToFileMap and keep their hashed IDs. This is intentional for client security:
|
|
167
|
-
* - Module-level "use server" files: shared action modules, file path helps revalidation
|
|
168
|
-
* - Inline actions: one-off actions in RSC, hash ID prevents file path exposure to client
|
|
169
|
-
*
|
|
170
|
-
* @param code - The source code to transform
|
|
171
|
-
* @param sourceId - The source file identifier (for sourcemap)
|
|
172
|
-
* @param hashToFileMap - Mapping from hash to file path (only module-level "use server" files)
|
|
173
|
-
*/
|
|
174
|
-
/**
|
|
175
|
-
* Apply registerServerReference wrapping to a MagicString instance.
|
|
176
|
-
* Returns true if any changes were made.
|
|
177
|
-
*
|
|
178
|
-
* Only actions from module-level "use server" files are transformed.
|
|
179
|
-
* Inline actions keep their hashed IDs for client security.
|
|
180
|
-
*/
|
|
181
127
|
function applyRegisterReferenceWrapping(
|
|
182
128
|
code: string,
|
|
183
129
|
s: MagicString,
|
|
@@ -187,8 +133,6 @@ function applyRegisterReferenceWrapping(
|
|
|
187
133
|
return false;
|
|
188
134
|
}
|
|
189
135
|
|
|
190
|
-
// Match: registerServerReference(fn, "hash", "exportName")
|
|
191
|
-
// The hash is the second argument, exportName is the third
|
|
192
136
|
const pattern =
|
|
193
137
|
/registerServerReference\(([^,]+),\s*"([^"]+)",\s*"([^"]+)"\)/g;
|
|
194
138
|
|
|
@@ -200,15 +144,9 @@ function applyRegisterReferenceWrapping(
|
|
|
200
144
|
const start = match.index;
|
|
201
145
|
const end = start + fullMatch.length;
|
|
202
146
|
|
|
203
|
-
// Look up the file path for this hash
|
|
204
147
|
const filePath = hashToFileMap.get(hash);
|
|
205
148
|
if (filePath) {
|
|
206
149
|
hasChanges = true;
|
|
207
|
-
// WRAP the call to add $id property with file path
|
|
208
|
-
// Keep the original hash for React's action registry (so loadServerAction works)
|
|
209
|
-
// Add $id (single dollar) with file path for revalidation matching
|
|
210
|
-
// Note: We use $id instead of $$id because React's registerServerReference
|
|
211
|
-
// sets $$id as a non-writable property
|
|
212
150
|
const filePathId = `${filePath}#${exportName}`;
|
|
213
151
|
const replacement = `(function(fn) { fn.$id = "${filePathId}"; return fn; })(registerServerReference(${fnArg}, "${hash}", "${exportName}"))`;
|
|
214
152
|
s.overwrite(start, end, replacement);
|
|
@@ -336,27 +274,19 @@ export function exposeActionId(): Plugin {
|
|
|
336
274
|
|
|
337
275
|
const start = counterTransform ? performance.now() : 0;
|
|
338
276
|
try {
|
|
339
|
-
// Dev mode: no hash-to-file mapping needed (IDs are already file paths)
|
|
340
277
|
return transformServerReferences(code, id);
|
|
341
278
|
} finally {
|
|
342
279
|
counterTransform?.record(id, performance.now() - start);
|
|
343
280
|
}
|
|
344
281
|
},
|
|
345
282
|
|
|
346
|
-
// Build mode: renderChunk runs after all transforms and bundling complete
|
|
347
283
|
renderChunk(code, chunk) {
|
|
348
284
|
const start = counterRender ? performance.now() : 0;
|
|
349
285
|
try {
|
|
350
|
-
// Only RSC bundle should get file paths for revalidation matching
|
|
351
|
-
// SSR bundle must NOT use file paths because client components run there
|
|
352
|
-
// and need to match the client bundle during hydration (otherwise: error #418)
|
|
353
286
|
const isRscEnv = this.environment?.name === "rsc";
|
|
354
287
|
|
|
355
|
-
// Only use file path mapping for RSC environment
|
|
356
288
|
const effectiveMap = isRscEnv ? hashToFileMap : undefined;
|
|
357
289
|
|
|
358
|
-
// For RSC bundles, both createServerReference and registerServerReference
|
|
359
|
-
// may need transforming. Use a single MagicString for correct sourcemaps.
|
|
360
290
|
if (isRscEnv && hashToFileMap) {
|
|
361
291
|
const s = new MagicString(code);
|
|
362
292
|
const changed1 = applyServerReferenceWrapping(code, s, effectiveMap);
|
|
@@ -377,7 +307,6 @@ export function exposeActionId(): Plugin {
|
|
|
377
307
|
return null;
|
|
378
308
|
}
|
|
379
309
|
|
|
380
|
-
// Non-RSC environments: only transform createServerReference calls
|
|
381
310
|
return transformServerReferences(code, chunk.fileName, effectiveMap);
|
|
382
311
|
} finally {
|
|
383
312
|
counterRender?.record(chunk.fileName, performance.now() - start);
|