@teyik0/furin 0.7.0-alpha.3 → 0.8.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/adapter/build.d.ts +9 -0
- package/dist/adapter/bun/server.d.ts +1 -0
- package/dist/adapter/bun.d.ts +7 -3
- package/dist/adapter/package.d.ts +1 -0
- package/dist/adapter/prerender.d.ts +21 -0
- package/dist/adapter/runtime-build.d.ts +24 -10
- package/dist/adapter/server-bundle.d.ts +20 -0
- package/dist/adapter/static.d.ts +2 -2
- package/dist/adapter/types.d.ts +81 -0
- package/dist/adapter/vercel/bootstrap.d.ts +5 -0
- package/dist/adapter/vercel/data.d.ts +23 -0
- package/dist/adapter/vercel/handler.d.ts +10 -0
- package/dist/adapter/vercel.d.ts +13 -3
- package/dist/adapter/vercel.js +516 -0
- package/dist/build/bun-build.d.ts +4 -0
- package/dist/build/compile-entry.ts +1 -0
- package/dist/build/discover-mounts-process.d.ts +1 -0
- package/dist/build/discover-mounts.d.ts +6 -0
- package/dist/build/elysia-aot-worker.d.ts +4 -0
- package/dist/build/elysia-aot.d.ts +7 -1
- package/dist/build/entry-template.d.ts +14 -7
- package/dist/build/entry-template.ts +33 -18
- package/dist/build/hydrate.d.ts +15 -2
- package/dist/build/index.d.ts +7 -3
- package/dist/build/index.js +8842 -15395
- package/dist/build/private-server-sourcemaps.d.ts +2 -0
- package/dist/build/route-types.d.ts +8 -2
- package/dist/build/server-app-entry.d.ts +2 -2
- package/dist/build/shared.d.ts +3 -5
- package/dist/build/types.d.ts +16 -16
- package/dist/cache.js +117 -32
- package/dist/cli/config.d.ts +2 -2
- package/dist/cli/index.js +8953 -15269
- package/dist/client/chunks/client-brdfd773.js +852 -0
- package/dist/client/chunks/client-k29262a4.js +59 -0
- package/dist/client/chunks/client-k88668k7.js +159 -0
- package/dist/client/chunks/client-xy6g38kx.js +317 -0
- package/dist/client/default-screens.d.ts +0 -11
- package/dist/client/define-route.d.ts +2 -1
- package/dist/client/hmr.d.ts +1 -1
- package/dist/client/internal/runtime-types.d.ts +7 -1
- package/dist/client/link.d.ts +1 -0
- package/dist/client/link.js +1790 -4190
- package/dist/client/mutation.d.ts +5 -0
- package/dist/client/query-store.d.ts +17 -7
- package/dist/client/query.d.ts +6 -8
- package/dist/client/request-id.d.ts +1 -0
- package/dist/client/route-api.d.ts +4 -4
- package/dist/client/route-element.d.ts +51 -0
- package/dist/client/router/boundary-tree.d.ts +1 -9
- package/dist/client/router/cache-generation.d.ts +15 -0
- package/dist/client/router/context.d.ts +13 -2
- package/dist/client/router/fetch-observers.d.ts +9 -0
- package/dist/client/router/hmr-refresh.d.ts +12 -0
- package/dist/client/router/index.d.ts +5 -7
- package/dist/client/router/link-utils.d.ts +0 -2
- package/dist/client/router/navigation-controller.d.ts +76 -0
- package/dist/client/router/navigation.d.ts +1 -0
- package/dist/client/router/page-fetch.d.ts +38 -0
- package/dist/client/router/provider.d.ts +2 -3
- package/dist/client/router/search/index.d.ts +11 -4
- package/dist/client/router/search/index.js +60 -0
- package/dist/client/router/spa-response.d.ts +9 -26
- package/dist/client/router/sync-catch-up.d.ts +3 -2
- package/dist/client/router/transport.d.ts +4 -0
- package/dist/client/router/types.d.ts +120 -46
- package/dist/client/router/use-fetch-interception.d.ts +12 -0
- package/dist/client/router/use-history-sync.d.ts +7 -0
- package/dist/client/router/use-hmr-refresh.d.ts +8 -0
- package/dist/client/router/use-sync-stream.d.ts +16 -0
- package/dist/client/server-error.d.ts +4 -7
- package/dist/client/sync.d.ts +43 -30
- package/dist/client.d.ts +2 -1
- package/dist/client.js +279 -829
- package/dist/config.d.ts +11 -12
- package/dist/config.js +0 -30
- package/dist/define-route.d.ts +262 -517
- package/dist/devtools/protocol.d.ts +4 -3
- package/dist/furin.d.ts +41 -31
- package/dist/furin.js +20560 -18970
- package/dist/plugin/binding-scope.d.ts +15 -2
- package/dist/plugin/client-plugin.d.ts +19 -0
- package/dist/plugin/dead-code-elimination.d.ts +14 -3
- package/dist/plugin/dev-routes.d.ts +22 -0
- package/dist/plugin/hmr-data-signature.d.ts +3 -0
- package/dist/plugin/hmr-hook-signature.d.ts +9 -0
- package/dist/plugin/index.d.ts +1 -6
- package/dist/plugin/index.js +1753 -1214
- package/dist/plugin/module-bindings.d.ts +11 -0
- package/dist/plugin/module-transform.d.ts +29 -0
- package/dist/plugin/preflight.d.ts +8 -0
- package/dist/plugin/route-chains.d.ts +35 -0
- package/dist/plugin/route-config.d.ts +28 -0
- package/dist/plugin/route-modules.d.ts +35 -0
- package/dist/plugin/route-registry.d.ts +12 -0
- package/dist/plugin/route-tree.d.ts +43 -0
- package/dist/plugin/strip-server-stages.d.ts +10 -0
- package/dist/plugin/strip-server.d.ts +10 -0
- package/dist/plugin/transform-client-module.d.ts +3 -0
- package/dist/plugin/transform-client.d.ts +0 -2
- package/dist/plugin/transform-isomorphic.d.ts +3 -2
- package/dist/routes.d.ts +15 -5
- package/dist/rsc/build/client-references.d.ts +8 -0
- package/dist/rsc/build/discover.d.ts +7 -0
- package/dist/rsc/build/flight-loader.d.ts +1 -0
- package/dist/rsc/build/index.d.ts +4 -1
- package/dist/rsc/build/paths.d.ts +2 -0
- package/dist/rsc/bundled-client-codec.d.ts +1 -0
- package/dist/rsc/client-codec.d.ts +3 -0
- package/dist/rsc/client-references.d.ts +22 -0
- package/dist/rsc/server-client-codec.d.ts +1 -0
- package/dist/rsc/server-codec.js +3889 -2
- package/dist/rsc/slot-proxy.d.ts +11 -0
- package/dist/rsc/transport-benchmark.d.ts +2 -1
- package/dist/rsc-client.js +16 -164
- package/dist/rsc-server.d.ts +1 -5
- package/dist/rsc.d.ts +1 -5
- package/dist/rsc.js +233 -73
- package/dist/server/assets/client-dir.d.ts +1 -0
- package/dist/server/auto-invalidate/index.d.ts +2 -2
- package/dist/server/auto-invalidate/plugin.d.ts +21 -15
- package/dist/server/auto-invalidate/types.d.ts +0 -2
- package/dist/server/bounded-body.d.ts +6 -0
- package/dist/server/browser-events/plugin.d.ts +4 -1
- package/dist/server/browser-events/shutdown.d.ts +1 -1
- package/dist/server/cache/dev-loader.d.ts +6 -1
- package/dist/server/cache/index.d.ts +4 -9
- package/dist/server/cache/invalidation.d.ts +7 -2
- package/dist/server/cache/isr-ssg.d.ts +6 -1
- package/dist/server/cache/page-cache.d.ts +5 -0
- package/dist/server/cache/purger.d.ts +14 -4
- package/dist/server/cache/redis/index.d.ts +1 -34
- package/dist/server/cache/redis/index.js +51 -25
- package/dist/server/cache/redis/scripts.d.ts +1 -1
- package/dist/server/cache/route-cache.d.ts +18 -2
- package/dist/server/cache/runtime-cache.d.ts +7 -0
- package/dist/server/context-logger.d.ts +6 -1
- package/dist/server/dev/graph.d.ts +4 -3
- package/dist/server/dev/runtime.d.ts +4 -0
- package/dist/server/dev/runtime.production.d.ts +1 -0
- package/dist/server/dev/stack-location.d.ts +9 -0
- package/dist/server/dev-page-imports.d.ts +42 -0
- package/dist/server/dev-page-plugin.d.ts +2 -49
- package/dist/server/devtools/instrumentation.d.ts +1 -3
- package/dist/server/devtools/instrumentation.production.d.ts +0 -2
- package/dist/server/devtools/request-context.d.ts +7 -0
- package/dist/server/evlog.d.ts +18 -6
- package/dist/server/instance.d.ts +52 -44
- package/dist/server/internal.d.ts +51 -14
- package/dist/server/internal.js +9749 -2095
- package/dist/server/lang-detect.d.ts +4 -4
- package/dist/server/logger.d.ts +6 -0
- package/dist/server/logging/ingest-plugin.d.ts +32 -0
- package/dist/server/mode.d.ts +17 -0
- package/dist/server/prerender.d.ts +32 -0
- package/dist/server/prod/runtime.d.ts +5 -0
- package/dist/server/production-server.js +992 -726
- package/dist/server/registrations.d.ts +14 -0
- package/dist/server/render/assemble.d.ts +0 -22
- package/dist/server/render/buffered-render.d.ts +20 -0
- package/dist/server/render/document-stream.d.ts +45 -0
- package/dist/server/render/document.d.ts +1 -1
- package/dist/server/render/element.d.ts +2 -5
- package/dist/server/render/index.d.ts +4 -5
- package/dist/server/render/index.ts +4 -21
- package/dist/server/render/isr-revalidation.d.ts +5 -0
- package/dist/server/render/isr-shared-cache.d.ts +23 -0
- package/dist/server/render/isr.d.ts +19 -1
- package/dist/server/render/loader-errors.d.ts +33 -0
- package/dist/server/render/loader-segments.d.ts +40 -0
- package/dist/server/render/loaders.d.ts +16 -32
- package/dist/server/render/mixed-cache.d.ts +2 -2
- package/dist/server/render/navigation-head.d.ts +13 -0
- package/dist/server/render/not-found-plugin.d.ts +18 -0
- package/dist/server/render/observe-loader.d.ts +1 -0
- package/dist/server/render/ppr-document.d.ts +1 -1
- package/dist/server/render/ppr-request.d.ts +0 -2
- package/dist/server/render/ppr-route.d.ts +13 -2
- package/dist/server/render/prepare-render.d.ts +57 -0
- package/dist/server/render/request-loaders.d.ts +19 -0
- package/dist/server/render/route-frame-transport.d.ts +7 -1
- package/dist/server/render/shell.d.ts +1 -1
- package/dist/server/render/shell.ts +1 -1
- package/dist/server/render/ssr-transport.d.ts +11 -0
- package/dist/server/render/ssr.d.ts +1 -116
- package/dist/server/render/static-params.d.ts +15 -0
- package/dist/server/router/discovery.d.ts +1 -0
- package/dist/server/router/dispatch.d.ts +96 -0
- package/dist/server/router/hmr.d.ts +14 -24
- package/dist/server/router/patterns.d.ts +15 -20
- package/dist/server/router/plugin.d.ts +26 -24
- package/dist/server/router/schemas.d.ts +6 -24
- package/dist/server/router/types.d.ts +0 -2
- package/dist/server/runtime-env.d.ts +6 -1
- package/dist/server/static-export.d.ts +38 -0
- package/dist/server/static-export.js +10667 -0
- package/dist/server/sync/adapter.d.ts +11 -0
- package/dist/server/sync/atomic.d.ts +0 -2
- package/dist/server/sync/config.d.ts +0 -1
- package/dist/server/sync/drizzle/index.js +112 -68
- package/dist/server/sync/fingerprint.d.ts +0 -1
- package/dist/server/sync/index.d.ts +2 -6
- package/dist/server/sync/index.js +1689 -1364
- package/dist/server/sync/invalidate-sync.d.ts +10 -0
- package/dist/server/sync/plugin.d.ts +18 -55
- package/dist/server/sync/postgres/execute.d.ts +2 -2
- package/dist/server/sync/postgres/index.d.ts +3 -1
- package/dist/server/sync/postgres/index.js +68 -37
- package/dist/server/sync/postgres/migration.sql +2 -0
- package/dist/server/sync/principal-journal.d.ts +11 -0
- package/dist/server/sync/prisma/index.js +74 -39
- package/dist/server/sync/queries.d.ts +57 -16
- package/dist/server/sync/redis/index.d.ts +2 -1
- package/dist/server/sync/redis/index.js +49 -5
- package/dist/server/sync/redis/scripts.d.ts +2 -1
- package/dist/server/sync/response.d.ts +3 -0
- package/dist/server/sync/runtime.d.ts +5 -1
- package/dist/server/sync/sqlite/index.d.ts +3 -1
- package/dist/server/sync/sqlite/index.js +58 -27
- package/dist/server/sync/stream.d.ts +32 -7
- package/dist/server/weak-registry.d.ts +9 -0
- package/dist/shared/await.d.ts +2 -1
- package/dist/shared/compact-json.d.ts +1 -0
- package/dist/shared/defer.d.ts +1 -1
- package/dist/shared/deferred-ndjson.d.ts +3 -2
- package/dist/shared/elysia-contract.d.ts +2 -0
- package/dist/shared/in-memory-apps.d.ts +7 -0
- package/dist/shared/invalidation-header.d.ts +13 -0
- package/dist/shared/navigation-data.d.ts +7 -0
- package/dist/shared/prefix.d.ts +11 -5
- package/dist/shared/route-frame.d.ts +3 -0
- package/dist/shared/route-map.d.ts +7 -0
- package/dist/shared/route-matches.d.ts +41 -0
- package/dist/shared/route-specificity.d.ts +2 -0
- package/dist/shared/search-params.d.ts +2 -3
- package/dist/shared/sync-query.d.ts +6 -13
- package/dist/shared/utils/ast-walk.d.ts +3 -11
- package/package.json +32 -21
- package/src/adapter/README.md +37 -16
- package/src/adapter/build.ts +269 -0
- package/src/adapter/bun/server.ts +14 -0
- package/src/adapter/bun.ts +89 -265
- package/src/adapter/furin-app.d.ts +14 -0
- package/src/adapter/package.ts +31 -27
- package/src/adapter/prerender.ts +97 -0
- package/src/adapter/runtime-build.ts +83 -93
- package/src/adapter/server-bundle.ts +110 -0
- package/src/adapter/static.ts +259 -247
- package/src/adapter/types.ts +89 -0
- package/src/adapter/vercel/bootstrap.ts +85 -0
- package/src/adapter/vercel/data.ts +21 -0
- package/src/adapter/vercel/handler.ts +132 -0
- package/src/adapter/vercel.ts +329 -659
- package/src/build/bun-build.ts +3 -0
- package/src/build/client.ts +22 -60
- package/src/build/compile-entry.ts +1 -0
- package/src/build/discover-mounts-process.ts +13 -0
- package/src/build/discover-mounts.ts +40 -0
- package/src/build/elysia-aot-worker.ts +84 -2
- package/src/build/elysia-aot.ts +61 -2
- package/src/build/entry-template.ts +33 -18
- package/src/build/hydrate.ts +122 -44
- package/src/build/index.ts +156 -108
- package/src/build/private-server-sourcemaps.ts +5 -0
- package/src/build/production-instrumentation.ts +22 -19
- package/src/build/route-types.ts +93 -11
- package/src/build/server-app-entry.ts +4 -21
- package/src/build/shared.ts +9 -41
- package/src/build/types.ts +14 -20
- package/src/build/virtual-entry.ts +2 -4
- package/src/cli/config.ts +133 -6
- package/src/cli/index.ts +59 -21
- package/src/cli/preview.ts +2 -1
- package/src/client/boundaries.tsx +5 -14
- package/src/client/default-screens.tsx +2 -2
- package/src/client/define-route.ts +3 -2
- package/src/client/hmr.ts +1 -1
- package/src/client/internal/runtime-types.ts +7 -1
- package/src/client/link.tsx +80 -43
- package/src/client/mutation.ts +98 -3
- package/src/client/query-store.ts +160 -29
- package/src/client/query.tsx +12 -29
- package/src/client/request-id.ts +10 -0
- package/src/client/route-api.tsx +6 -6
- package/src/client/route-element.tsx +155 -0
- package/src/client/router/boundary-tree.tsx +2 -93
- package/src/client/router/cache-generation.ts +37 -0
- package/src/client/router/context.ts +40 -4
- package/src/client/router/fetch-observers.ts +46 -0
- package/src/client/router/hmr-refresh.ts +50 -0
- package/src/client/router/index.ts +7 -29
- package/src/client/router/link-utils.ts +19 -31
- package/src/client/router/navigation-controller.ts +547 -0
- package/src/client/router/navigation.ts +3 -2
- package/src/client/router/optimistic.ts +2 -3
- package/src/client/router/page-fetch.ts +272 -0
- package/src/client/router/provider.tsx +223 -1079
- package/src/client/router/search/index.ts +38 -14
- package/src/client/router/spa-response.ts +19 -70
- package/src/client/router/sync-catch-up.ts +65 -7
- package/src/client/router/transport.ts +23 -0
- package/src/client/router/types.ts +203 -61
- package/src/client/router/use-fetch-interception.ts +52 -0
- package/src/client/router/use-history-sync.ts +56 -0
- package/src/client/router/use-hmr-refresh.ts +51 -0
- package/src/client/router/use-sync-stream.ts +129 -0
- package/src/client/server-error.ts +2 -40
- package/src/client/sync.ts +268 -378
- package/src/client.ts +2 -7
- package/src/config.ts +23 -45
- package/src/define-route.ts +487 -882
- package/src/devtools/devtools-element.js +10 -3
- package/src/devtools/protocol.ts +3 -3
- package/src/furin.ts +253 -1086
- package/src/plugin/binding-scope.ts +219 -94
- package/src/plugin/client-plugin.ts +145 -0
- package/src/plugin/dead-code-elimination.ts +390 -111
- package/src/plugin/dev-routes.ts +453 -0
- package/src/plugin/hmr-data-signature.ts +161 -0
- package/src/plugin/hmr-dependencies.ts +2 -4
- package/src/plugin/hmr-hook-signature.ts +252 -0
- package/src/plugin/index.ts +3 -96
- package/src/plugin/module-bindings.ts +216 -0
- package/src/plugin/module-transform.ts +63 -0
- package/src/plugin/preflight.ts +66 -0
- package/src/plugin/route-chains.ts +309 -0
- package/src/plugin/route-config.ts +451 -0
- package/src/plugin/route-modules.ts +209 -0
- package/src/plugin/route-registry.ts +316 -0
- package/src/plugin/route-tree.ts +248 -0
- package/src/plugin/strip-server-stages.ts +197 -0
- package/src/plugin/strip-server.ts +22 -0
- package/src/plugin/transform-client-module.ts +36 -31
- package/src/plugin/transform-client.ts +33 -1114
- package/src/plugin/transform-dev-page.ts +84 -92
- package/src/plugin/transform-isomorphic.ts +396 -185
- package/src/routes.ts +15 -5
- package/src/rsc/build/client-references.ts +267 -0
- package/src/rsc/build/discover.ts +226 -0
- package/src/rsc/build/flight-loader.ts +69 -0
- package/src/rsc/build/index.ts +53 -31
- package/src/rsc/build/paths.ts +7 -0
- package/src/rsc/bundled-client-codec.ts +4 -0
- package/src/rsc/client-codec.ts +12 -3
- package/src/rsc/client-references.ts +138 -0
- package/src/rsc/codec.ts +30 -21
- package/src/rsc/flight-drain.ts +6 -29
- package/src/rsc/react-server-dom-webpack.d.ts +1 -1
- package/src/rsc/server-client-codec.ts +14 -0
- package/src/rsc/server-codec.ts +5 -8
- package/src/rsc/slot-proxy.ts +34 -0
- package/src/rsc/transport-benchmark.ts +1 -1
- package/src/rsc-server.tsx +5 -23
- package/src/rsc.tsx +5 -29
- package/src/server/assets/client-dir.ts +110 -0
- package/src/server/assets/production.ts +1 -1
- package/src/server/auto-invalidate/index.ts +2 -7
- package/src/server/auto-invalidate/plugin.ts +43 -15
- package/src/server/auto-invalidate/registry.ts +6 -6
- package/src/server/auto-invalidate/runtime.ts +16 -98
- package/src/server/auto-invalidate/types.ts +0 -6
- package/src/server/bounded-body.ts +38 -0
- package/src/server/browser-events/plugin.ts +109 -22
- package/src/server/browser-events/shutdown.ts +28 -9
- package/src/server/cache/dev-loader.ts +30 -16
- package/src/server/cache/index.ts +2 -15
- package/src/server/cache/invalidation.ts +27 -46
- package/src/server/cache/isr-ssg.ts +6 -1
- package/src/server/cache/isr.ts +11 -11
- package/src/server/cache/page-cache-state.ts +2 -2
- package/src/server/cache/page-cache.ts +13 -9
- package/src/server/cache/purger.ts +64 -8
- package/src/server/cache/redis/index.ts +17 -27
- package/src/server/cache/redis/scripts.ts +21 -4
- package/src/server/cache/registry.ts +3 -3
- package/src/server/cache/route-cache.ts +47 -13
- package/src/server/cache/runtime-cache.ts +144 -24
- package/src/server/cache/ssg.ts +7 -5
- package/src/server/context-logger.ts +46 -8
- package/src/server/dev/diagnostics.ts +6 -56
- package/src/server/dev/error.ts +3 -34
- package/src/server/dev/graph.ts +35 -22
- package/src/server/dev/rewrite-module-specifiers.ts +15 -19
- package/src/server/dev/runtime.production.ts +4 -0
- package/src/server/dev/runtime.ts +404 -0
- package/src/server/dev/stack-location.ts +46 -0
- package/src/server/dev-page-imports.ts +183 -0
- package/src/server/dev-page-plugin.ts +39 -302
- package/src/server/devtools/hub.ts +3 -3
- package/src/server/devtools/instrumentation.production.ts +0 -8
- package/src/server/devtools/instrumentation.ts +6 -8
- package/src/server/devtools/plugin.ts +2 -1
- package/src/server/devtools/request-context.ts +67 -16
- package/src/server/evlog.ts +119 -55
- package/src/server/instance.ts +187 -138
- package/src/server/internal.ts +66 -62
- package/src/server/lang-detect.ts +31 -22
- package/src/server/logger.ts +20 -7
- package/src/server/logging/ingest-plugin.ts +123 -0
- package/src/server/mode.ts +19 -0
- package/src/server/prerender.ts +122 -0
- package/src/server/prod/runtime.ts +181 -0
- package/src/server/production-server.ts +43 -17
- package/src/server/registrations.ts +38 -0
- package/src/server/render/assemble.ts +5 -8
- package/src/server/render/buffered-render.ts +171 -0
- package/src/server/render/document-stream.ts +188 -0
- package/src/server/render/document.tsx +5 -3
- package/src/server/render/element.tsx +6 -73
- package/src/server/render/index.ts +4 -21
- package/src/server/render/isr-revalidation.ts +246 -0
- package/src/server/render/isr-shared-cache.ts +141 -0
- package/src/server/render/isr.ts +212 -516
- package/src/server/render/loader-errors.ts +183 -0
- package/src/server/render/loader-segments.ts +149 -0
- package/src/server/render/loaders.ts +126 -547
- package/src/server/render/mixed-cache.ts +28 -27
- package/src/server/render/navigation-head.ts +33 -0
- package/src/server/render/not-found-plugin.ts +31 -0
- package/src/server/render/not-found.ts +32 -8
- package/src/server/render/observe-loader.ts +46 -0
- package/src/server/render/ppr-document.ts +36 -14
- package/src/server/render/ppr-request.ts +2 -2
- package/src/server/render/ppr-route.ts +79 -18
- package/src/server/render/prepare-render.ts +251 -0
- package/src/server/render/request-loaders.ts +216 -0
- package/src/server/render/route-frame-transport.ts +41 -4
- package/src/server/render/shell.ts +1 -1
- package/src/server/render/ssg.ts +9 -20
- package/src/server/render/ssr-transport.ts +221 -0
- package/src/server/render/ssr.ts +95 -956
- package/src/server/render/static-params.ts +32 -0
- package/src/server/render/template.ts +7 -7
- package/src/server/router/discovery.ts +82 -159
- package/src/server/router/dispatch.ts +337 -0
- package/src/server/router/hmr.ts +177 -190
- package/src/server/router/patterns.ts +39 -65
- package/src/server/router/plugin.ts +234 -359
- package/src/server/router/schemas.ts +6 -39
- package/src/server/router/source-version.ts +5 -4
- package/src/server/router/types.ts +0 -2
- package/src/server/runtime-env.ts +6 -1
- package/src/server/static-export.ts +143 -0
- package/src/server/sync/adapter.ts +11 -0
- package/src/server/sync/atomic.ts +16 -36
- package/src/server/sync/config.ts +6 -14
- package/src/server/sync/drizzle/index.ts +12 -20
- package/src/server/sync/fingerprint.ts +1 -1
- package/src/server/sync/index.ts +2 -10
- package/src/server/sync/invalidate-sync.ts +31 -0
- package/src/server/sync/mutation-handler.ts +6 -0
- package/src/server/sync/plugin.ts +171 -129
- package/src/server/sync/postgres/execute.ts +9 -1
- package/src/server/sync/postgres/index.ts +71 -39
- package/src/server/sync/postgres/migration.sql +2 -0
- package/src/server/sync/principal-journal.ts +78 -0
- package/src/server/sync/prisma/index.ts +1 -0
- package/src/server/sync/queries.ts +133 -40
- package/src/server/sync/query-context.ts +38 -5
- package/src/server/sync/redis/index.ts +34 -6
- package/src/server/sync/redis/scripts.ts +10 -1
- package/src/server/sync/response.ts +47 -67
- package/src/server/sync/runtime.ts +24 -7
- package/src/server/sync/sqlite/index.ts +92 -61
- package/src/server/sync/sqlite/migration.sql +1 -0
- package/src/server/sync/stream.ts +47 -26
- package/src/server/sync/validation.ts +1 -1
- package/src/server/weak-registry.ts +37 -0
- package/src/shared/await.tsx +2 -1
- package/src/shared/compact-json.ts +1 -1
- package/src/shared/defer.ts +1 -1
- package/src/shared/deferred-ndjson.ts +142 -120
- package/src/shared/elysia-contract.ts +7 -0
- package/src/shared/error.ts +1 -4
- package/src/shared/in-memory-apps.ts +24 -0
- package/src/shared/invalidation-header.ts +38 -0
- package/src/shared/navigation-data.ts +29 -0
- package/src/shared/prefix.ts +42 -6
- package/src/shared/route-frame.ts +82 -26
- package/src/shared/route-map.ts +23 -39
- package/src/shared/route-matches.ts +59 -0
- package/src/shared/route-specificity.ts +28 -0
- package/src/shared/search-params.ts +6 -35
- package/src/shared/sync-query.ts +30 -23
- package/src/shared/utils/ast-walk.ts +9 -25
- package/src/shared/utils/index.ts +1 -1
- package/dist/build/scan-server.d.ts +0 -14
- package/dist/build/ssg-cache.d.ts +0 -18
- package/dist/plugin/route-config-autofix.d.ts +0 -31
- package/dist/plugin/routes.d.ts +0 -34
- package/dist/server/router/hmr.production.d.ts +0 -7
- package/src/build/scan-server.ts +0 -112
- package/src/build/ssg-cache.ts +0 -126
- package/src/plugin/route-config-autofix.ts +0 -1149
- package/src/plugin/routes.ts +0 -1140
- package/src/server/router/hmr.production.ts +0 -18
|
@@ -1,1124 +1,59 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { detectLangFromPath } from "../server/lang-detect.ts";
|
|
2
|
+
import { pruneOrphanedCode } from "./dead-code-elimination.ts";
|
|
3
|
+
import { createHmrDataSignature } from "./hmr-data-signature.ts";
|
|
4
|
+
import { collectClientHookSignature, unusedIdentifier } from "./hmr-hook-signature.ts";
|
|
5
|
+
import { parseModule } from "./module-transform.ts";
|
|
6
|
+
import {
|
|
7
|
+
analyzeRouteChains,
|
|
8
|
+
assertCompleteRouteChains,
|
|
9
|
+
collectDefineRouteBindings,
|
|
10
|
+
} from "./route-chains.ts";
|
|
11
|
+
import { removeChainedServerCalls, type StrippedCode } from "./strip-server-stages.ts";
|
|
12
|
+
import { rewriteClientModules } from "./transform-client-module.ts";
|
|
13
|
+
import { selectIsomorphicFunctions } from "./transform-isomorphic.ts";
|
|
11
14
|
|
|
12
|
-
const FURIN_CLIENT_MODULES = new Set(["@teyik0/furin/client", "furin/client"]);
|
|
13
|
-
const FURIN_SERVER_MODULES = new Set(["@teyik0/furin", "furin"]);
|
|
14
|
-
const REACT_COMPONENT_WRAPPERS = new Set(["forwardRef", "memo"]);
|
|
15
|
-
const SERVER_ONLY_METHODS = new Set(["config", "head", "loader", "requestLoader", "staticParams"]);
|
|
16
|
-
const REACT_HOOK_NAME_RE = /^use[A-Z0-9]/;
|
|
17
15
|
const HMR_DATA_SIGNATURE = "furin.hmr.data-signature";
|
|
18
16
|
const HMR_SOURCE_SIGNATURE = "furin.hmr.source-signature";
|
|
19
|
-
const TYPESCRIPT_RUNTIME_WRAPPERS = new Map([
|
|
20
|
-
["TSAsExpression", ["expression"]],
|
|
21
|
-
["TSInstantiationExpression", ["expression"]],
|
|
22
|
-
["TSNonNullExpression", ["expression"]],
|
|
23
|
-
["TSParameterProperty", ["decorators", "parameter"]],
|
|
24
|
-
["TSSatisfiesExpression", ["expression"]],
|
|
25
|
-
["TSTypeAssertion", ["expression"]],
|
|
26
|
-
]);
|
|
27
17
|
|
|
28
18
|
interface TransformResult {
|
|
29
19
|
code: string;
|
|
30
|
-
map: ReturnType<MagicString["generateMap"]> | null;
|
|
31
20
|
removedServerCode: boolean;
|
|
32
21
|
}
|
|
33
22
|
|
|
34
|
-
function isFurinRouteModule(source: unknown): source is string {
|
|
35
|
-
return (
|
|
36
|
-
typeof source === "string" &&
|
|
37
|
-
(FURIN_SERVER_MODULES.has(source) || FURIN_CLIENT_MODULES.has(source))
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function importedName(specifier: AstNode): string | null {
|
|
42
|
-
const { imported } = specifier;
|
|
43
|
-
if (!imported || typeof imported !== "object") {
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
const node = imported as AstNode;
|
|
47
|
-
if (node.type === "Identifier" && typeof node.name === "string") {
|
|
48
|
-
return node.name;
|
|
49
|
-
}
|
|
50
|
-
if (node.type === "Literal" && typeof node.value === "string") {
|
|
51
|
-
return node.value;
|
|
52
|
-
}
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function localName(specifier: AstNode): string | null {
|
|
57
|
-
const { local } = specifier;
|
|
58
|
-
if (!local || typeof local !== "object") {
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
const node = local as AstNode;
|
|
62
|
-
return node.type === "Identifier" && typeof node.name === "string" ? node.name : null;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function collectDefineRouteBindings(program: Program): Set<string> {
|
|
66
|
-
const bindings = new Set<string>();
|
|
67
|
-
for (const statement of program.body) {
|
|
68
|
-
if (statement.type !== "ImportDeclaration") {
|
|
69
|
-
continue;
|
|
70
|
-
}
|
|
71
|
-
const declaration = statement as unknown as ImportDeclaration;
|
|
72
|
-
if (declaration.importKind === "type") {
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
const source = declaration.source.value;
|
|
76
|
-
if (!isFurinRouteModule(source)) {
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
for (const specifier of declaration.specifiers as unknown as AstNode[]) {
|
|
80
|
-
if (specifier.type !== "ImportSpecifier" || specifier.importKind === "type") {
|
|
81
|
-
continue;
|
|
82
|
-
}
|
|
83
|
-
const imported = importedName(specifier);
|
|
84
|
-
if (imported !== "defineRoute" && imported !== "defineRootRoute") {
|
|
85
|
-
continue;
|
|
86
|
-
}
|
|
87
|
-
const local = localName(specifier);
|
|
88
|
-
if (local) {
|
|
89
|
-
bindings.add(local);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
return bindings;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function rewriteClientImports(
|
|
97
|
-
source: MagicString,
|
|
98
|
-
program: Program,
|
|
99
|
-
bindings: Set<string>
|
|
100
|
-
): boolean {
|
|
101
|
-
let rewritten = false;
|
|
102
|
-
for (const statement of program.body) {
|
|
103
|
-
if (statement.type !== "ImportDeclaration") {
|
|
104
|
-
continue;
|
|
105
|
-
}
|
|
106
|
-
const declaration = statement as unknown as ImportDeclaration;
|
|
107
|
-
const moduleName = declaration.source.value;
|
|
108
|
-
if (typeof moduleName !== "string" || !FURIN_SERVER_MODULES.has(moduleName)) {
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
const importsClientValue = (declaration.specifiers as unknown as AstNode[]).some(
|
|
112
|
-
(specifier) => {
|
|
113
|
-
const local = localName(specifier);
|
|
114
|
-
const imported = importedName(specifier);
|
|
115
|
-
return (
|
|
116
|
-
(local !== null && bindings.has(local)) ||
|
|
117
|
-
imported === "HeadContent" ||
|
|
118
|
-
imported === "Scripts"
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
);
|
|
122
|
-
if (!importsClientValue) {
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
const clientModule = moduleName === "furin" ? "furin/client" : "@teyik0/furin/client";
|
|
126
|
-
source.update(declaration.source.start + 1, declaration.source.end - 1, clientModule);
|
|
127
|
-
rewritten = true;
|
|
128
|
-
}
|
|
129
|
-
return rewritten;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function asAstNode(node: unknown): AstNode | null {
|
|
133
|
-
if (!node || typeof node !== "object" || !("type" in node)) {
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
return unwrapTSExpression(node as { type: string }) as AstNode;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function chainRootIsDefineRoute(
|
|
140
|
-
node: unknown,
|
|
141
|
-
bindings: Set<string>,
|
|
142
|
-
ancestors: AstNode[]
|
|
143
|
-
): boolean {
|
|
144
|
-
let current = asAstNode(node);
|
|
145
|
-
while (current) {
|
|
146
|
-
if (current.type === "MemberExpression") {
|
|
147
|
-
current = asAstNode(current.object);
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
if (current.type === "CallExpression") {
|
|
151
|
-
current = asAstNode(current.callee);
|
|
152
|
-
continue;
|
|
153
|
-
}
|
|
154
|
-
return (
|
|
155
|
-
current.type === "Identifier" &&
|
|
156
|
-
typeof current.name === "string" &&
|
|
157
|
-
bindings.has(current.name) &&
|
|
158
|
-
!hasShadowingDeclaration(current.name, ancestors)
|
|
159
|
-
);
|
|
160
|
-
}
|
|
161
|
-
return false;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function assertCompleteRouteChains(
|
|
165
|
-
program: Program,
|
|
166
|
-
bindings: Set<string>,
|
|
167
|
-
filename: string
|
|
168
|
-
): void {
|
|
169
|
-
const namespaces = new Set<string>();
|
|
170
|
-
for (const statement of program.body) {
|
|
171
|
-
if (statement.type !== "ImportDeclaration") {
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
const declaration = statement as unknown as ImportDeclaration;
|
|
175
|
-
if (!isFurinRouteModule(declaration.source.value) || declaration.importKind === "type") {
|
|
176
|
-
continue;
|
|
177
|
-
}
|
|
178
|
-
for (const specifier of declaration.specifiers as unknown as AstNode[]) {
|
|
179
|
-
const name = localName(specifier);
|
|
180
|
-
if (name && specifier.type === "ImportNamespaceSpecifier") {
|
|
181
|
-
namespaces.add(name);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
walk(program, {
|
|
186
|
-
MemberExpression(node, context) {
|
|
187
|
-
const object = asAstNode(node.object);
|
|
188
|
-
const property = asAstNode(node.property);
|
|
189
|
-
const method = property?.type === "Identifier" ? property.name : property?.value;
|
|
190
|
-
if (
|
|
191
|
-
object?.type === "Identifier" &&
|
|
192
|
-
typeof object.name === "string" &&
|
|
193
|
-
namespaces.has(object.name) &&
|
|
194
|
-
(method === "defineRoute" || method === "defineRootRoute") &&
|
|
195
|
-
!hasShadowingDeclaration(object.name, context.ancestors() as AstNode[])
|
|
196
|
-
) {
|
|
197
|
-
throw new Error(`[furin] ${filename}: route builders require a named import.`);
|
|
198
|
-
}
|
|
199
|
-
if (
|
|
200
|
-
node.computed &&
|
|
201
|
-
context.parent?.type === "CallExpression" &&
|
|
202
|
-
chainRootIsDefineRoute(node.object, bindings, context.ancestors() as AstNode[])
|
|
203
|
-
) {
|
|
204
|
-
throw new Error(`[furin] ${filename}: route builders require static builder methods.`);
|
|
205
|
-
}
|
|
206
|
-
},
|
|
207
|
-
VariableDeclarator(node, context) {
|
|
208
|
-
const initializer = asAstNode(node.init);
|
|
209
|
-
if (
|
|
210
|
-
!(
|
|
211
|
-
initializer &&
|
|
212
|
-
chainRootIsDefineRoute(initializer, bindings, context.ancestors() as AstNode[])
|
|
213
|
-
)
|
|
214
|
-
) {
|
|
215
|
-
return;
|
|
216
|
-
}
|
|
217
|
-
const callee = initializer.type === "CallExpression" ? asAstNode(initializer.callee) : null;
|
|
218
|
-
const method = callee?.type === "MemberExpression" ? asAstNode(callee.property) : null;
|
|
219
|
-
if (method?.type !== "Identifier" || (method.name !== "page" && method.name !== "layout")) {
|
|
220
|
-
throw new Error(
|
|
221
|
-
`[furin] ${filename}: route builders must use one fluent chain ending in .page() or .layout().`
|
|
222
|
-
);
|
|
223
|
-
}
|
|
224
|
-
},
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
function removeChainedServerCalls(
|
|
229
|
-
source: MagicString,
|
|
230
|
-
program: Program,
|
|
231
|
-
bindings: Set<string>,
|
|
232
|
-
configBindings: Set<string>,
|
|
233
|
-
filename: string
|
|
234
|
-
): boolean {
|
|
235
|
-
let transformed = rewriteClientImports(source, program, bindings);
|
|
236
|
-
const { declarations } = collectModuleBindings(program);
|
|
237
|
-
|
|
238
|
-
walk(program, {
|
|
239
|
-
CallExpression(call, context) {
|
|
240
|
-
const callee = asAstNode(call.callee);
|
|
241
|
-
if (
|
|
242
|
-
callee?.type !== "MemberExpression" ||
|
|
243
|
-
callee.computed === true ||
|
|
244
|
-
!callee.property ||
|
|
245
|
-
typeof callee.property !== "object"
|
|
246
|
-
) {
|
|
247
|
-
return;
|
|
248
|
-
}
|
|
249
|
-
const property = callee.property as AstNode;
|
|
250
|
-
if (
|
|
251
|
-
property.type !== "Identifier" ||
|
|
252
|
-
typeof property.name !== "string" ||
|
|
253
|
-
!SERVER_ONLY_METHODS.has(property.name) ||
|
|
254
|
-
!chainRootIsDefineRoute(callee.object, bindings, context.ancestors() as AstNode[])
|
|
255
|
-
) {
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
const object = asAstNode(callee.object);
|
|
259
|
-
if (!object) {
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
if (property.name === "config" && Array.isArray(call.arguments)) {
|
|
263
|
-
const config = asAstNode(call.arguments[0]);
|
|
264
|
-
if (config?.type === "ObjectExpression" && Array.isArray(config.properties)) {
|
|
265
|
-
collectConfigBindings(config, declarations, configBindings);
|
|
266
|
-
const remount = resolveRemountProperty(
|
|
267
|
-
config,
|
|
268
|
-
declarations,
|
|
269
|
-
configBindings,
|
|
270
|
-
new Set(),
|
|
271
|
-
filename
|
|
272
|
-
);
|
|
273
|
-
if (remount) {
|
|
274
|
-
source.update(
|
|
275
|
-
object.end,
|
|
276
|
-
call.end,
|
|
277
|
-
`.config({ ${source.original.slice(remount.start, remount.end)} })`
|
|
278
|
-
);
|
|
279
|
-
transformed = true;
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
source.remove(object.end, call.end);
|
|
285
|
-
transformed = true;
|
|
286
|
-
},
|
|
287
|
-
});
|
|
288
|
-
return transformed;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
function collectConfigBindings(
|
|
292
|
-
config: AstNode | null,
|
|
293
|
-
declarations: Map<string, AstNode>,
|
|
294
|
-
names: Set<string>
|
|
295
|
-
): void {
|
|
296
|
-
if (config?.type === "Identifier" && typeof config.name === "string" && !names.has(config.name)) {
|
|
297
|
-
names.add(config.name);
|
|
298
|
-
const declaration = declarations.get(config.name);
|
|
299
|
-
collectConfigBindings(asAstNode(declaration?.init), declarations, names);
|
|
300
|
-
} else if (config?.type === "ObjectExpression" && Array.isArray(config.properties)) {
|
|
301
|
-
for (const property of config.properties) {
|
|
302
|
-
const entry = asAstNode(property);
|
|
303
|
-
if (entry?.type === "SpreadElement") {
|
|
304
|
-
collectConfigBindings(asAstNode(entry.argument), declarations, names);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
function resolveRemountProperty(
|
|
311
|
-
config: AstNode | null,
|
|
312
|
-
declarations: Map<string, AstNode>,
|
|
313
|
-
configBindings: Set<string>,
|
|
314
|
-
visiting: Set<string>,
|
|
315
|
-
filename: string
|
|
316
|
-
): AstNode | null {
|
|
317
|
-
if (
|
|
318
|
-
config?.type === "Identifier" &&
|
|
319
|
-
typeof config.name === "string" &&
|
|
320
|
-
!visiting.has(config.name)
|
|
321
|
-
) {
|
|
322
|
-
visiting.add(config.name);
|
|
323
|
-
configBindings.add(config.name);
|
|
324
|
-
const declaration = declarations.get(config.name);
|
|
325
|
-
const result = resolveRemountProperty(
|
|
326
|
-
asAstNode(declaration?.init),
|
|
327
|
-
declarations,
|
|
328
|
-
configBindings,
|
|
329
|
-
visiting,
|
|
330
|
-
filename
|
|
331
|
-
);
|
|
332
|
-
visiting.delete(config.name);
|
|
333
|
-
return result;
|
|
334
|
-
}
|
|
335
|
-
if (config?.type !== "ObjectExpression" || !Array.isArray(config.properties)) {
|
|
336
|
-
throw new Error(
|
|
337
|
-
`[furin] ${filename}: route config spreads must resolve to local object literals; declare remountDeps after dynamic spreads.`
|
|
338
|
-
);
|
|
339
|
-
}
|
|
340
|
-
for (let index = config.properties.length - 1; index >= 0; index -= 1) {
|
|
341
|
-
const entry = asAstNode(config.properties[index]);
|
|
342
|
-
if (entry?.type === "SpreadElement") {
|
|
343
|
-
const remount = resolveRemountProperty(
|
|
344
|
-
asAstNode(entry.argument),
|
|
345
|
-
declarations,
|
|
346
|
-
configBindings,
|
|
347
|
-
visiting,
|
|
348
|
-
filename
|
|
349
|
-
);
|
|
350
|
-
if (remount) {
|
|
351
|
-
return remount;
|
|
352
|
-
}
|
|
353
|
-
} else if (entry?.type === "Property") {
|
|
354
|
-
const key = asAstNode(entry.key);
|
|
355
|
-
if (
|
|
356
|
-
(key?.type === "Identifier" && entry.computed !== true && key.name === "remountDeps") ||
|
|
357
|
-
(key?.type === "Literal" && key.value === "remountDeps")
|
|
358
|
-
) {
|
|
359
|
-
return entry;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
return null;
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
function pruneUnusedConfigBindings(
|
|
367
|
-
source: MagicString,
|
|
368
|
-
filename: string,
|
|
369
|
-
names: Set<string>
|
|
370
|
-
): MagicString {
|
|
371
|
-
let result = source;
|
|
372
|
-
let pruned = true;
|
|
373
|
-
while (pruned) {
|
|
374
|
-
pruned = false;
|
|
375
|
-
const code = result.toString();
|
|
376
|
-
const { program } = parseSource(code, detectLangFromPath(filename));
|
|
377
|
-
const references = new Set<string>();
|
|
378
|
-
walkWithAncestors(program, [], (node, ancestors) => {
|
|
379
|
-
if (
|
|
380
|
-
node.type === "Identifier" &&
|
|
381
|
-
typeof node.name === "string" &&
|
|
382
|
-
isReferenceIdentifier(node, ancestors)
|
|
383
|
-
) {
|
|
384
|
-
references.add(node.name);
|
|
385
|
-
}
|
|
386
|
-
});
|
|
387
|
-
result = new MagicString(code);
|
|
388
|
-
for (const statement of program.body) {
|
|
389
|
-
if (statement.type !== "VariableDeclaration") {
|
|
390
|
-
continue;
|
|
391
|
-
}
|
|
392
|
-
const retained = statement.declarations.filter(
|
|
393
|
-
(declaration) =>
|
|
394
|
-
declaration.id.type !== "Identifier" ||
|
|
395
|
-
!names.has(declaration.id.name) ||
|
|
396
|
-
references.has(declaration.id.name)
|
|
397
|
-
);
|
|
398
|
-
if (retained.length !== statement.declarations.length) {
|
|
399
|
-
result.update(
|
|
400
|
-
statement.start,
|
|
401
|
-
statement.end,
|
|
402
|
-
retained.length
|
|
403
|
-
? `${statement.kind} ${retained.map((node) => code.slice(node.start, node.end)).join(", ")};`
|
|
404
|
-
: ""
|
|
405
|
-
);
|
|
406
|
-
pruned = true;
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
return result;
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
function collectBindingNames(pattern: unknown, names: string[]): void {
|
|
414
|
-
const node = asAstNode(pattern);
|
|
415
|
-
if (!node) {
|
|
416
|
-
return;
|
|
417
|
-
}
|
|
418
|
-
if (node.type === "Identifier" && typeof node.name === "string") {
|
|
419
|
-
names.push(node.name);
|
|
420
|
-
return;
|
|
421
|
-
}
|
|
422
|
-
if (node.type === "AssignmentPattern") {
|
|
423
|
-
collectBindingNames(node.left, names);
|
|
424
|
-
return;
|
|
425
|
-
}
|
|
426
|
-
if (node.type === "RestElement") {
|
|
427
|
-
collectBindingNames(node.argument, names);
|
|
428
|
-
return;
|
|
429
|
-
}
|
|
430
|
-
if (node.type === "ArrayPattern" && Array.isArray(node.elements)) {
|
|
431
|
-
for (const element of node.elements) {
|
|
432
|
-
collectBindingNames(element, names);
|
|
433
|
-
}
|
|
434
|
-
return;
|
|
435
|
-
}
|
|
436
|
-
if (node.type === "ObjectPattern" && Array.isArray(node.properties)) {
|
|
437
|
-
for (const property of node.properties) {
|
|
438
|
-
const propertyNode = asAstNode(property);
|
|
439
|
-
collectBindingNames(
|
|
440
|
-
propertyNode?.type === "Property" ? propertyNode.value : propertyNode?.argument,
|
|
441
|
-
names
|
|
442
|
-
);
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
function collectDeclarationBindings(
|
|
448
|
-
declaration: AstNode | null,
|
|
449
|
-
declarations: Map<string, AstNode>
|
|
450
|
-
): void {
|
|
451
|
-
if (declaration?.type === "FunctionDeclaration" || declaration?.type === "ClassDeclaration") {
|
|
452
|
-
const identifier = asAstNode(declaration.id);
|
|
453
|
-
if (identifier?.type === "Identifier" && typeof identifier.name === "string") {
|
|
454
|
-
declarations.set(identifier.name, declaration);
|
|
455
|
-
}
|
|
456
|
-
return;
|
|
457
|
-
}
|
|
458
|
-
if (declaration?.type !== "VariableDeclaration" || !Array.isArray(declaration.declarations)) {
|
|
459
|
-
return;
|
|
460
|
-
}
|
|
461
|
-
for (const item of declaration.declarations as AstNode[]) {
|
|
462
|
-
const names: string[] = [];
|
|
463
|
-
collectBindingNames(item.id, names);
|
|
464
|
-
for (const name of names) {
|
|
465
|
-
declarations.set(name, item);
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
function collectModuleBindings(program: Program): {
|
|
471
|
-
declarations: Map<string, AstNode>;
|
|
472
|
-
imports: Map<string, ImportDeclaration>;
|
|
473
|
-
} {
|
|
474
|
-
const declarations = new Map<string, AstNode>();
|
|
475
|
-
const imports = new Map<string, ImportDeclaration>();
|
|
476
|
-
for (const statement of program.body as unknown as AstNode[]) {
|
|
477
|
-
if (statement.type === "ImportDeclaration" && Array.isArray(statement.specifiers)) {
|
|
478
|
-
for (const specifier of statement.specifiers as AstNode[]) {
|
|
479
|
-
const local = localName(specifier);
|
|
480
|
-
if (local) {
|
|
481
|
-
imports.set(local, statement as unknown as ImportDeclaration);
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
continue;
|
|
485
|
-
}
|
|
486
|
-
const declaration =
|
|
487
|
-
statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration"
|
|
488
|
-
? asAstNode(statement.declaration)
|
|
489
|
-
: statement;
|
|
490
|
-
collectDeclarationBindings(declaration, declarations);
|
|
491
|
-
}
|
|
492
|
-
return { declarations, imports };
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
function isBindingIdentifier(identifier: AstNode, pattern: unknown): boolean {
|
|
496
|
-
const node = asAstNode(pattern);
|
|
497
|
-
if (!node) {
|
|
498
|
-
return false;
|
|
499
|
-
}
|
|
500
|
-
if (node.type === "Identifier") {
|
|
501
|
-
return node.start === identifier.start && node.end === identifier.end;
|
|
502
|
-
}
|
|
503
|
-
if (node.type === "AssignmentPattern") {
|
|
504
|
-
return isBindingIdentifier(identifier, node.left);
|
|
505
|
-
}
|
|
506
|
-
if (node.type === "RestElement") {
|
|
507
|
-
return isBindingIdentifier(identifier, node.argument);
|
|
508
|
-
}
|
|
509
|
-
if (node.type === "ArrayPattern" && Array.isArray(node.elements)) {
|
|
510
|
-
return node.elements.some((element) => isBindingIdentifier(identifier, element));
|
|
511
|
-
}
|
|
512
|
-
if (node.type === "ObjectPattern" && Array.isArray(node.properties)) {
|
|
513
|
-
return node.properties.some((property) => {
|
|
514
|
-
const propertyNode = asAstNode(property);
|
|
515
|
-
return isBindingIdentifier(
|
|
516
|
-
identifier,
|
|
517
|
-
propertyNode?.type === "Property" ? propertyNode.value : propertyNode?.argument
|
|
518
|
-
);
|
|
519
|
-
});
|
|
520
|
-
}
|
|
521
|
-
return false;
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
function nodeContains(container: unknown, node: AstNode): boolean {
|
|
525
|
-
if (Array.isArray(container)) {
|
|
526
|
-
return container.some((entry) => nodeContains(entry, node));
|
|
527
|
-
}
|
|
528
|
-
const containerNode = asAstNode(container);
|
|
529
|
-
return Boolean(
|
|
530
|
-
containerNode && containerNode.start <= node.start && node.end <= containerNode.end
|
|
531
|
-
);
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
function isTypePosition(node: AstNode, ancestors: AstNode[]): boolean {
|
|
535
|
-
return ancestors.some((ancestor) => {
|
|
536
|
-
if (!ancestor.type.startsWith("TS")) {
|
|
537
|
-
return false;
|
|
538
|
-
}
|
|
539
|
-
const runtimeChildren = TYPESCRIPT_RUNTIME_WRAPPERS.get(ancestor.type);
|
|
540
|
-
if (runtimeChildren) {
|
|
541
|
-
return !runtimeChildren.some((key) => nodeContains(ancestor[key], node));
|
|
542
|
-
}
|
|
543
|
-
return true;
|
|
544
|
-
});
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
function isNonReferenceKey(node: AstNode, parent: AstNode | undefined): boolean {
|
|
548
|
-
return Boolean(
|
|
549
|
-
parent &&
|
|
550
|
-
((parent.type === "MemberExpression" &&
|
|
551
|
-
parent.computed !== true &&
|
|
552
|
-
parent.property === node) ||
|
|
553
|
-
(parent.type === "Property" &&
|
|
554
|
-
parent.computed !== true &&
|
|
555
|
-
parent.shorthand !== true &&
|
|
556
|
-
parent.key === node) ||
|
|
557
|
-
((parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") &&
|
|
558
|
-
parent.computed !== true &&
|
|
559
|
-
parent.key === node) ||
|
|
560
|
-
((parent.type === "LabeledStatement" ||
|
|
561
|
-
parent.type === "BreakStatement" ||
|
|
562
|
-
parent.type === "ContinueStatement") &&
|
|
563
|
-
parent.label === node))
|
|
564
|
-
);
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
function isReferenceIdentifier(node: AstNode, ancestors: AstNode[]): boolean {
|
|
568
|
-
if (isTypePosition(node, ancestors) || isNonReferenceKey(node, ancestors.at(-1))) {
|
|
569
|
-
return false;
|
|
570
|
-
}
|
|
571
|
-
for (const ancestor of ancestors) {
|
|
572
|
-
if (
|
|
573
|
-
(ancestor.type === "VariableDeclarator" && isBindingIdentifier(node, ancestor.id)) ||
|
|
574
|
-
((ancestor.type === "FunctionDeclaration" ||
|
|
575
|
-
ancestor.type === "FunctionExpression" ||
|
|
576
|
-
ancestor.type === "ArrowFunctionExpression") &&
|
|
577
|
-
(isBindingIdentifier(node, ancestor.id) ||
|
|
578
|
-
(Array.isArray(ancestor.params) &&
|
|
579
|
-
ancestor.params.some((parameter) => isBindingIdentifier(node, parameter))))) ||
|
|
580
|
-
(ancestor.type === "ClassDeclaration" && isBindingIdentifier(node, ancestor.id)) ||
|
|
581
|
-
(ancestor.type === "CatchClause" && isBindingIdentifier(node, ancestor.param))
|
|
582
|
-
) {
|
|
583
|
-
return false;
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
return true;
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
function walkWithAncestors(
|
|
590
|
-
value: unknown,
|
|
591
|
-
ancestors: AstNode[],
|
|
592
|
-
visitor: (node: AstNode, ancestors: AstNode[]) => void
|
|
593
|
-
): void {
|
|
594
|
-
if (Array.isArray(value)) {
|
|
595
|
-
for (const entry of value) {
|
|
596
|
-
walkWithAncestors(entry, ancestors, visitor);
|
|
597
|
-
}
|
|
598
|
-
return;
|
|
599
|
-
}
|
|
600
|
-
const node = asAstNode(value);
|
|
601
|
-
if (!node) {
|
|
602
|
-
return;
|
|
603
|
-
}
|
|
604
|
-
visitor(node, ancestors);
|
|
605
|
-
const nextAncestors = [...ancestors, node];
|
|
606
|
-
for (const [key, child] of Object.entries(node)) {
|
|
607
|
-
if (key !== "type" && key !== "start" && key !== "end" && key !== "comments") {
|
|
608
|
-
walkWithAncestors(child, nextAncestors, visitor);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
interface HmrDependencyState {
|
|
614
|
-
dependencies: Map<number, AstNode>;
|
|
615
|
-
dynamicImports: Set<string>;
|
|
616
|
-
imports: Set<ImportDeclaration>;
|
|
617
|
-
importTrackedDependencies: Set<number>;
|
|
618
|
-
moduleBindings: ReturnType<typeof collectModuleBindings>;
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
function collectDependencyIdentifier(
|
|
622
|
-
child: AstNode,
|
|
623
|
-
ancestors: AstNode[],
|
|
624
|
-
trackImports: boolean,
|
|
625
|
-
state: HmrDependencyState
|
|
626
|
-
): void {
|
|
627
|
-
const parent = ancestors.at(-1);
|
|
628
|
-
const jsxReference =
|
|
629
|
-
child.type === "JSXIdentifier" &&
|
|
630
|
-
((parent?.type === "JSXOpeningElement" &&
|
|
631
|
-
parent.name === child &&
|
|
632
|
-
typeof child.name === "string" &&
|
|
633
|
-
child.name[0] === child.name[0]?.toUpperCase()) ||
|
|
634
|
-
(parent?.type === "JSXMemberExpression" && parent.object === child));
|
|
635
|
-
if (
|
|
636
|
-
!(child.type === "Identifier" || jsxReference) ||
|
|
637
|
-
typeof child.name !== "string" ||
|
|
638
|
-
!isReferenceIdentifier(child, ancestors) ||
|
|
639
|
-
hasShadowingDeclaration(child.name, ancestors)
|
|
640
|
-
) {
|
|
641
|
-
return;
|
|
642
|
-
}
|
|
643
|
-
const declaration = state.moduleBindings.declarations.get(child.name);
|
|
644
|
-
if (!declaration) {
|
|
645
|
-
const imported = state.moduleBindings.imports.get(child.name);
|
|
646
|
-
if (trackImports && imported) {
|
|
647
|
-
state.imports.add(imported);
|
|
648
|
-
}
|
|
649
|
-
return;
|
|
650
|
-
}
|
|
651
|
-
const alreadyTracked = state.dependencies.has(declaration.start);
|
|
652
|
-
if (!alreadyTracked) {
|
|
653
|
-
state.dependencies.set(declaration.start, declaration);
|
|
654
|
-
}
|
|
655
|
-
if (trackImports) {
|
|
656
|
-
if (state.importTrackedDependencies.has(declaration.start)) {
|
|
657
|
-
return;
|
|
658
|
-
}
|
|
659
|
-
state.importTrackedDependencies.add(declaration.start);
|
|
660
|
-
} else if (alreadyTracked) {
|
|
661
|
-
return;
|
|
662
|
-
}
|
|
663
|
-
collectDependencies(declaration, trackImports, state);
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
function collectDependencies(
|
|
667
|
-
node: AstNode,
|
|
668
|
-
trackImports: boolean,
|
|
669
|
-
state: HmrDependencyState
|
|
670
|
-
): void {
|
|
671
|
-
walkWithAncestors(node, [], (child, ancestors) => {
|
|
672
|
-
collectDependencyIdentifier(child, ancestors, trackImports, state);
|
|
673
|
-
if (trackImports && child.type === "ImportExpression") {
|
|
674
|
-
const specifier = asAstNode(child.source)?.value;
|
|
675
|
-
if (typeof specifier === "string") {
|
|
676
|
-
state.dynamicImports.add(specifier);
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
});
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
function createHmrDataSignature(
|
|
683
|
-
code: string,
|
|
684
|
-
program: Program,
|
|
685
|
-
bindings: Set<string>,
|
|
686
|
-
filename: string
|
|
687
|
-
): string {
|
|
688
|
-
const serverStages: Array<{ source: string; start: number }> = [];
|
|
689
|
-
const dependencyState: HmrDependencyState = {
|
|
690
|
-
dependencies: new Map(),
|
|
691
|
-
dynamicImports: new Set(),
|
|
692
|
-
imports: new Set(),
|
|
693
|
-
importTrackedDependencies: new Set(),
|
|
694
|
-
moduleBindings: collectModuleBindings(program),
|
|
695
|
-
};
|
|
696
|
-
|
|
697
|
-
walk(program, {
|
|
698
|
-
CallExpression(call, context) {
|
|
699
|
-
const callee = asAstNode(call.callee);
|
|
700
|
-
if (
|
|
701
|
-
callee?.type !== "MemberExpression" ||
|
|
702
|
-
callee.computed === true ||
|
|
703
|
-
!callee.property ||
|
|
704
|
-
typeof callee.property !== "object"
|
|
705
|
-
) {
|
|
706
|
-
return;
|
|
707
|
-
}
|
|
708
|
-
const property = callee.property as AstNode;
|
|
709
|
-
if (
|
|
710
|
-
property.type !== "Identifier" ||
|
|
711
|
-
typeof property.name !== "string" ||
|
|
712
|
-
!SERVER_ONLY_METHODS.has(property.name) ||
|
|
713
|
-
!chainRootIsDefineRoute(callee.object, bindings, context.ancestors() as AstNode[])
|
|
714
|
-
) {
|
|
715
|
-
return;
|
|
716
|
-
}
|
|
717
|
-
const object = asAstNode(callee.object);
|
|
718
|
-
if (!object) {
|
|
719
|
-
return;
|
|
720
|
-
}
|
|
721
|
-
serverStages.push({
|
|
722
|
-
source: code.slice(object.end, call.end),
|
|
723
|
-
start: call.start,
|
|
724
|
-
});
|
|
725
|
-
if (Array.isArray(call.arguments)) {
|
|
726
|
-
for (const argument of call.arguments) {
|
|
727
|
-
const argumentNode = asAstNode(argument);
|
|
728
|
-
if (argumentNode) {
|
|
729
|
-
collectDependencies(argumentNode, property.name !== "config", dependencyState);
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
},
|
|
734
|
-
});
|
|
735
|
-
|
|
736
|
-
const dataSource = [
|
|
737
|
-
...serverStages,
|
|
738
|
-
...[...dependencyState.imports].map((imported) => ({
|
|
739
|
-
source: code.slice(imported.start, imported.end),
|
|
740
|
-
start: imported.start,
|
|
741
|
-
})),
|
|
742
|
-
...[...dependencyState.dependencies.values()].map((dependency) => ({
|
|
743
|
-
source: code.slice(dependency.start, dependency.end),
|
|
744
|
-
start: dependency.start,
|
|
745
|
-
})),
|
|
746
|
-
]
|
|
747
|
-
.sort((left, right) => left.start - right.start)
|
|
748
|
-
.map((entry) => entry.source)
|
|
749
|
-
.join("\n");
|
|
750
|
-
const hash = new Bun.CryptoHasher("sha256").update(dataSource).digest("hex");
|
|
751
|
-
if (dependencyState.imports.size === 0 && dependencyState.dynamicImports.size === 0) {
|
|
752
|
-
return hash;
|
|
753
|
-
}
|
|
754
|
-
const dependencies = hmrDependencySignature(
|
|
755
|
-
new Set([
|
|
756
|
-
...[...dependencyState.imports].map((imported) => imported.source.value),
|
|
757
|
-
...dependencyState.dynamicImports,
|
|
758
|
-
]),
|
|
759
|
-
filename
|
|
760
|
-
);
|
|
761
|
-
return dependencies === undefined ? `external:${hash}` : `imports:${hash}:${dependencies}`;
|
|
762
|
-
}
|
|
763
|
-
|
|
764
23
|
/** Server stages and their imports share one signature across both HMR graphs. */
|
|
765
24
|
export function getHmrDataSignature(code: string, filename: string): string {
|
|
766
|
-
const {
|
|
767
|
-
const
|
|
768
|
-
|
|
769
|
-
throw new Error(`Failed to parse ${filename}: ${error.message}`);
|
|
770
|
-
}
|
|
771
|
-
return createHmrDataSignature(code, program, collectDefineRouteBindings(program), filename);
|
|
772
|
-
}
|
|
773
|
-
|
|
774
|
-
function calledHookName(call: AstNode): string | null {
|
|
775
|
-
const callee = asAstNode(call.callee);
|
|
776
|
-
if (callee?.type === "Identifier" && typeof callee.name === "string") {
|
|
777
|
-
return REACT_HOOK_NAME_RE.test(callee.name) ? callee.name : null;
|
|
778
|
-
}
|
|
779
|
-
if (
|
|
780
|
-
callee?.type === "MemberExpression" &&
|
|
781
|
-
callee.computed !== true &&
|
|
782
|
-
callee.property &&
|
|
783
|
-
typeof callee.property === "object"
|
|
784
|
-
) {
|
|
785
|
-
const property = callee.property as AstNode;
|
|
786
|
-
if (
|
|
787
|
-
property.type === "Identifier" &&
|
|
788
|
-
typeof property.name === "string" &&
|
|
789
|
-
REACT_HOOK_NAME_RE.test(property.name)
|
|
790
|
-
) {
|
|
791
|
-
return property.name;
|
|
792
|
-
}
|
|
793
|
-
}
|
|
794
|
-
return null;
|
|
795
|
-
}
|
|
796
|
-
|
|
797
|
-
function routeComponentExpression(program: Program, bindings: Set<string>): AstNode | null {
|
|
798
|
-
let component: AstNode | null = null;
|
|
799
|
-
walk(program, {
|
|
800
|
-
CallExpression(call, context) {
|
|
801
|
-
const callee = asAstNode(call.callee);
|
|
802
|
-
if (
|
|
803
|
-
callee?.type !== "MemberExpression" ||
|
|
804
|
-
callee.computed === true ||
|
|
805
|
-
!callee.property ||
|
|
806
|
-
typeof callee.property !== "object"
|
|
807
|
-
) {
|
|
808
|
-
return;
|
|
809
|
-
}
|
|
810
|
-
const property = callee.property as AstNode;
|
|
811
|
-
const ancestors = context.ancestors() as AstNode[];
|
|
812
|
-
const routeDeclarator = ancestors.find((ancestor) => {
|
|
813
|
-
if (ancestor.type !== "VariableDeclarator") {
|
|
814
|
-
return false;
|
|
815
|
-
}
|
|
816
|
-
const identifier = asAstNode(ancestor.id);
|
|
817
|
-
return identifier?.type === "Identifier" && identifier.name === "route";
|
|
818
|
-
});
|
|
819
|
-
if (
|
|
820
|
-
!(
|
|
821
|
-
routeDeclarator &&
|
|
822
|
-
ancestors.some((ancestor) => ancestor.type === "ExportNamedDeclaration")
|
|
823
|
-
) ||
|
|
824
|
-
property.type !== "Identifier" ||
|
|
825
|
-
(property.name !== "page" && property.name !== "layout") ||
|
|
826
|
-
!chainRootIsDefineRoute(callee.object, bindings, ancestors)
|
|
827
|
-
) {
|
|
828
|
-
return;
|
|
829
|
-
}
|
|
830
|
-
const args = call.arguments;
|
|
831
|
-
component = Array.isArray(args) ? asAstNode(args[0]) : null;
|
|
832
|
-
context.stop();
|
|
833
|
-
},
|
|
834
|
-
});
|
|
835
|
-
return component;
|
|
836
|
-
}
|
|
837
|
-
|
|
838
|
-
function namedFunction(declaration: AstNode, name: string): AstNode | null {
|
|
839
|
-
if (declaration.type === "FunctionDeclaration") {
|
|
840
|
-
const identifier = asAstNode(declaration.id);
|
|
841
|
-
return identifier?.type === "Identifier" && identifier.name === name ? declaration : null;
|
|
842
|
-
}
|
|
843
|
-
if (declaration.type === "VariableDeclaration" && Array.isArray(declaration.declarations)) {
|
|
844
|
-
for (const item of declaration.declarations as AstNode[]) {
|
|
845
|
-
const identifier = asAstNode(item.id);
|
|
846
|
-
const initializer = asAstNode(item.init);
|
|
847
|
-
if (
|
|
848
|
-
identifier?.type === "Identifier" &&
|
|
849
|
-
identifier.name === name &&
|
|
850
|
-
(initializer?.type === "ArrowFunctionExpression" ||
|
|
851
|
-
initializer?.type === "FunctionExpression")
|
|
852
|
-
) {
|
|
853
|
-
return initializer;
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
}
|
|
857
|
-
return null;
|
|
858
|
-
}
|
|
859
|
-
|
|
860
|
-
function localFunction(program: Program, name: string): AstNode | null {
|
|
861
|
-
for (const statement of program.body as unknown as AstNode[]) {
|
|
862
|
-
const declaration =
|
|
863
|
-
statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration"
|
|
864
|
-
? asAstNode(statement.declaration)
|
|
865
|
-
: statement;
|
|
866
|
-
const component = declaration ? namedFunction(declaration, name) : null;
|
|
867
|
-
if (component) {
|
|
868
|
-
return component;
|
|
869
|
-
}
|
|
870
|
-
}
|
|
871
|
-
return null;
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
interface ImportedRouteComponent {
|
|
875
|
-
end: number;
|
|
876
|
-
expression: string;
|
|
877
|
-
start: number;
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
function isImportedBinding(program: Program, name: string): boolean {
|
|
881
|
-
return program.body.some(
|
|
882
|
-
(statement) =>
|
|
883
|
-
statement.type === "ImportDeclaration" &&
|
|
884
|
-
statement.importKind !== "type" &&
|
|
885
|
-
(statement.specifiers as unknown as AstNode[]).some(
|
|
886
|
-
(specifier) => specifier.importKind !== "type" && localName(specifier) === name
|
|
887
|
-
)
|
|
888
|
-
);
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
interface ReactWrapperBindings {
|
|
892
|
-
members: Set<string>;
|
|
893
|
-
namespaces: Set<string>;
|
|
894
|
-
}
|
|
895
|
-
|
|
896
|
-
function collectReactWrapperBindings(program: Program): ReactWrapperBindings {
|
|
897
|
-
const bindings: ReactWrapperBindings = {
|
|
898
|
-
members: new Set<string>(),
|
|
899
|
-
namespaces: new Set<string>(),
|
|
900
|
-
};
|
|
901
|
-
for (const statement of program.body) {
|
|
902
|
-
if (statement.type !== "ImportDeclaration" || statement.source.value !== "react") {
|
|
903
|
-
continue;
|
|
904
|
-
}
|
|
905
|
-
for (const specifier of statement.specifiers as unknown as AstNode[]) {
|
|
906
|
-
const local = localName(specifier);
|
|
907
|
-
if (!local || specifier.importKind === "type") {
|
|
908
|
-
continue;
|
|
909
|
-
}
|
|
910
|
-
const imported = importedName(specifier);
|
|
911
|
-
if (
|
|
912
|
-
specifier.type === "ImportSpecifier" &&
|
|
913
|
-
imported &&
|
|
914
|
-
REACT_COMPONENT_WRAPPERS.has(imported)
|
|
915
|
-
) {
|
|
916
|
-
bindings.members.add(local);
|
|
917
|
-
} else if (
|
|
918
|
-
specifier.type === "ImportDefaultSpecifier" ||
|
|
919
|
-
specifier.type === "ImportNamespaceSpecifier" ||
|
|
920
|
-
(specifier.type === "ImportSpecifier" && imported === "default")
|
|
921
|
-
) {
|
|
922
|
-
bindings.namespaces.add(local);
|
|
923
|
-
}
|
|
924
|
-
}
|
|
925
|
-
}
|
|
926
|
-
return bindings;
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
function wrappedComponentFunction(
|
|
930
|
-
program: Program,
|
|
931
|
-
expression: AstNode,
|
|
932
|
-
bindings: ReactWrapperBindings
|
|
933
|
-
): AstNode | null {
|
|
934
|
-
if (expression.type === "ArrowFunctionExpression" || expression.type === "FunctionExpression") {
|
|
935
|
-
return expression;
|
|
936
|
-
}
|
|
937
|
-
if (expression.type === "Identifier" && typeof expression.name === "string") {
|
|
938
|
-
return localFunction(program, expression.name);
|
|
939
|
-
}
|
|
940
|
-
if (expression.type !== "CallExpression") {
|
|
941
|
-
return null;
|
|
942
|
-
}
|
|
943
|
-
const callee = asAstNode(expression.callee);
|
|
944
|
-
const isNamedWrapper =
|
|
945
|
-
callee?.type === "Identifier" &&
|
|
946
|
-
typeof callee.name === "string" &&
|
|
947
|
-
bindings.members.has(callee.name);
|
|
948
|
-
const object = callee?.type === "MemberExpression" ? asAstNode(callee.object) : null;
|
|
949
|
-
const property = callee?.type === "MemberExpression" ? asAstNode(callee.property) : null;
|
|
950
|
-
const isNamespaceWrapper =
|
|
951
|
-
callee?.type === "MemberExpression" &&
|
|
952
|
-
callee.computed !== true &&
|
|
953
|
-
object?.type === "Identifier" &&
|
|
954
|
-
typeof object.name === "string" &&
|
|
955
|
-
bindings.namespaces.has(object.name) &&
|
|
956
|
-
property?.type === "Identifier" &&
|
|
957
|
-
typeof property.name === "string" &&
|
|
958
|
-
REACT_COMPONENT_WRAPPERS.has(property.name);
|
|
959
|
-
if (!(isNamedWrapper || isNamespaceWrapper)) {
|
|
960
|
-
return null;
|
|
961
|
-
}
|
|
962
|
-
const args = expression.arguments;
|
|
963
|
-
const wrapped = Array.isArray(args) ? asAstNode(args[0]) : null;
|
|
964
|
-
return wrapped ? wrappedComponentFunction(program, wrapped, bindings) : null;
|
|
965
|
-
}
|
|
966
|
-
|
|
967
|
-
function importedMemberBinding(program: Program, expression: AstNode): string | null {
|
|
968
|
-
let object = expression.type === "MemberExpression" ? asAstNode(expression.object) : null;
|
|
969
|
-
while (object?.type === "MemberExpression") {
|
|
970
|
-
object = asAstNode(object.object);
|
|
971
|
-
}
|
|
972
|
-
return object?.type === "Identifier" &&
|
|
973
|
-
typeof object.name === "string" &&
|
|
974
|
-
isImportedBinding(program, object.name)
|
|
975
|
-
? object.name
|
|
976
|
-
: null;
|
|
977
|
-
}
|
|
978
|
-
|
|
979
|
-
function sourceText(code: string, node: AstNode | null): string {
|
|
980
|
-
return node ? code.slice(node.start, node.end) : "";
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
function unusedIdentifier(program: Program, preferred: string): string {
|
|
984
|
-
const identifiers = new Set<string>();
|
|
985
|
-
walk(program, {
|
|
986
|
-
Identifier(node) {
|
|
987
|
-
identifiers.add(node.name);
|
|
988
|
-
},
|
|
989
|
-
});
|
|
990
|
-
if (!identifiers.has(preferred)) {
|
|
991
|
-
return preferred;
|
|
992
|
-
}
|
|
993
|
-
let suffix = 1;
|
|
994
|
-
while (identifiers.has(`${preferred}_${suffix}`)) {
|
|
995
|
-
suffix += 1;
|
|
996
|
-
}
|
|
997
|
-
return `${preferred}_${suffix}`;
|
|
998
|
-
}
|
|
999
|
-
|
|
1000
|
-
function hookCallsiteSignature(
|
|
1001
|
-
code: string,
|
|
1002
|
-
call: AstNode,
|
|
1003
|
-
hookName: string,
|
|
1004
|
-
parent: AstNode | null
|
|
1005
|
-
): string {
|
|
1006
|
-
let key = parent?.type === "VariableDeclarator" ? sourceText(code, asAstNode(parent.id)) : "";
|
|
1007
|
-
const args = call.arguments;
|
|
1008
|
-
const stateArgument = Array.isArray(args) ? asAstNode(args[0]) : null;
|
|
1009
|
-
const reducerArgument = Array.isArray(args) ? asAstNode(args[1]) : null;
|
|
1010
|
-
if (hookName === "useState" && stateArgument) {
|
|
1011
|
-
key += `(${sourceText(code, stateArgument)})`;
|
|
1012
|
-
} else if (hookName === "useReducer" && reducerArgument) {
|
|
1013
|
-
key += `(${sourceText(code, reducerArgument)})`;
|
|
1014
|
-
}
|
|
1015
|
-
return `${hookName}{${key}}`;
|
|
1016
|
-
}
|
|
1017
|
-
|
|
1018
|
-
function collectFunctionHookSignature(code: string, component: AstNode): string[] {
|
|
1019
|
-
const hooks: string[] = [];
|
|
1020
|
-
walk(component as never, {
|
|
1021
|
-
CallExpression(call, context) {
|
|
1022
|
-
const callNode = call as unknown as AstNode;
|
|
1023
|
-
const name = calledHookName(callNode);
|
|
1024
|
-
if (name) {
|
|
1025
|
-
hooks.push(
|
|
1026
|
-
hookCallsiteSignature(code, callNode, name, asAstNode(context.parent as unknown))
|
|
1027
|
-
);
|
|
1028
|
-
}
|
|
1029
|
-
},
|
|
1030
|
-
Function(node, context) {
|
|
1031
|
-
if ((node as unknown as AstNode) !== component) {
|
|
1032
|
-
context.skip();
|
|
1033
|
-
}
|
|
1034
|
-
},
|
|
1035
|
-
});
|
|
1036
|
-
return hooks;
|
|
1037
|
-
}
|
|
1038
|
-
|
|
1039
|
-
function collectClientHookSignature(
|
|
1040
|
-
code: string,
|
|
1041
|
-
filename: string
|
|
1042
|
-
): string[] | ImportedRouteComponent | "external" | null {
|
|
1043
|
-
const lang = detectLangFromPath(filename);
|
|
1044
|
-
const { program } = parseSource(code, lang);
|
|
1045
|
-
const componentExpression = routeComponentExpression(
|
|
1046
|
-
program,
|
|
1047
|
-
collectDefineRouteBindings(program)
|
|
1048
|
-
);
|
|
1049
|
-
if (!componentExpression) {
|
|
1050
|
-
return null;
|
|
1051
|
-
}
|
|
1052
|
-
const wrappedComponent = wrappedComponentFunction(
|
|
1053
|
-
program,
|
|
1054
|
-
componentExpression,
|
|
1055
|
-
collectReactWrapperBindings(program)
|
|
1056
|
-
);
|
|
1057
|
-
if (wrappedComponent) {
|
|
1058
|
-
return collectFunctionHookSignature(code, wrappedComponent);
|
|
1059
|
-
}
|
|
1060
|
-
if (componentExpression.type !== "Identifier" || typeof componentExpression.name !== "string") {
|
|
1061
|
-
if (importedMemberBinding(program, componentExpression)) {
|
|
1062
|
-
return {
|
|
1063
|
-
end: componentExpression.end,
|
|
1064
|
-
expression: sourceText(code, componentExpression),
|
|
1065
|
-
start: componentExpression.start,
|
|
1066
|
-
};
|
|
1067
|
-
}
|
|
1068
|
-
return collectFunctionHookSignature(code, componentExpression);
|
|
1069
|
-
}
|
|
1070
|
-
const component = localFunction(program, componentExpression.name);
|
|
1071
|
-
if (component) {
|
|
1072
|
-
return collectFunctionHookSignature(code, component);
|
|
1073
|
-
}
|
|
1074
|
-
return isImportedBinding(program, componentExpression.name)
|
|
1075
|
-
? {
|
|
1076
|
-
end: componentExpression.end,
|
|
1077
|
-
expression: componentExpression.name,
|
|
1078
|
-
start: componentExpression.start,
|
|
1079
|
-
}
|
|
1080
|
-
: "external";
|
|
25
|
+
const { program } = parseModule(code, filename);
|
|
26
|
+
const chains = analyzeRouteChains(program, collectDefineRouteBindings(program));
|
|
27
|
+
return createHmrDataSignature(code, program, chains.serverStages, filename);
|
|
1081
28
|
}
|
|
1082
29
|
|
|
1083
30
|
export function transformForClient(code: string, filename: string): TransformResult {
|
|
1084
31
|
const lang = detectLangFromPath(filename);
|
|
1085
32
|
if (lang === "dts") {
|
|
1086
|
-
return { code,
|
|
33
|
+
return { code, removedServerCode: false };
|
|
1087
34
|
}
|
|
1088
35
|
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
const
|
|
1092
|
-
const
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
}
|
|
1096
|
-
|
|
1097
|
-
let source = new MagicString(clientSource);
|
|
36
|
+
// Every step below reads this one AST and edits its MagicString in `code`
|
|
37
|
+
// coordinates; nothing is reparsed until Bun bundles the result.
|
|
38
|
+
const module = parseModule(code, filename);
|
|
39
|
+
const { program, source } = module;
|
|
40
|
+
const clientModules = rewriteClientModules(module, "client");
|
|
41
|
+
const isomorphic = selectIsomorphicFunctions(module, "client");
|
|
1098
42
|
const routeBindings = collectDefineRouteBindings(program);
|
|
1099
43
|
assertCompleteRouteChains(program, routeBindings, filename);
|
|
1100
|
-
const
|
|
1101
|
-
const
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
routeBindings,
|
|
1105
|
-
configBindings,
|
|
1106
|
-
filename
|
|
1107
|
-
);
|
|
1108
|
-
const removedServerCode = isomorphicResult.transformed || removedRouteCode;
|
|
44
|
+
const chains = analyzeRouteChains(program, routeBindings);
|
|
45
|
+
const stripped: StrippedCode = { configBindings: new Set(), retained: new Set() };
|
|
46
|
+
const removedRouteCode = removeChainedServerCalls(module, routeBindings, chains, stripped);
|
|
47
|
+
const removedServerCode = clientModules || isomorphic || removedRouteCode;
|
|
1109
48
|
if (removedServerCode) {
|
|
1110
|
-
|
|
1111
|
-
source = pruneUnusedConfigBindings(source, filename, configBindings);
|
|
1112
|
-
}
|
|
1113
|
-
source = deadCodeElimination(source, code, lang);
|
|
49
|
+
pruneOrphanedCode(module, stripped.configBindings, stripped.retained);
|
|
1114
50
|
}
|
|
1115
|
-
const transformedCode = source.toString();
|
|
1116
51
|
let hookSignature =
|
|
1117
|
-
routeBindings.size > 0 ? collectClientHookSignature(
|
|
52
|
+
routeBindings.size > 0 ? collectClientHookSignature(code, program, chains.component) : null;
|
|
1118
53
|
if (hookSignature && typeof hookSignature === "object" && !Array.isArray(hookSignature)) {
|
|
1119
54
|
const createElementBinding = unusedIdentifier(program, "__furinCreateElement");
|
|
1120
|
-
|
|
1121
|
-
source.prepend(`import { createElement as ${createElementBinding} } from "react"
|
|
55
|
+
// On the first line: an added line would shift every stack frame of the module.
|
|
56
|
+
source.prepend(`import { createElement as ${createElementBinding} } from "react"; `);
|
|
1122
57
|
source.update(
|
|
1123
58
|
hookSignature.start,
|
|
1124
59
|
hookSignature.end,
|
|
@@ -1127,19 +62,7 @@ export function transformForClient(code: string, filename: string): TransformRes
|
|
|
1127
62
|
hookSignature = [];
|
|
1128
63
|
}
|
|
1129
64
|
if (routeBindings.size > 0 && hookSignature !== null) {
|
|
1130
|
-
const
|
|
1131
|
-
const originalError = originalParse.diagnostics.find(
|
|
1132
|
-
(diagnostic) => diagnostic.severity === "error"
|
|
1133
|
-
);
|
|
1134
|
-
if (originalError) {
|
|
1135
|
-
throw new Error(`Failed to parse ${filename}: ${originalError.message}`);
|
|
1136
|
-
}
|
|
1137
|
-
const hmrDataSignature = createHmrDataSignature(
|
|
1138
|
-
code,
|
|
1139
|
-
originalParse.program,
|
|
1140
|
-
collectDefineRouteBindings(originalParse.program),
|
|
1141
|
-
filename
|
|
1142
|
-
);
|
|
65
|
+
const hmrDataSignature = createHmrDataSignature(code, program, chains.serverStages, filename);
|
|
1143
66
|
const signatureValue = Array.isArray(hookSignature)
|
|
1144
67
|
? JSON.stringify(hookSignature)
|
|
1145
68
|
: 'route.component[Symbol.for("furin.hmr.hook-signature")] ?? [String(route.component)]';
|
|
@@ -1182,9 +105,5 @@ if (import.meta.hot && route?.component) {
|
|
|
1182
105
|
`);
|
|
1183
106
|
}
|
|
1184
107
|
|
|
1185
|
-
return {
|
|
1186
|
-
code: source.toString(),
|
|
1187
|
-
map: source.generateMap({ includeContent: true, source: filename }),
|
|
1188
|
-
removedServerCode,
|
|
1189
|
-
};
|
|
108
|
+
return { code: source.toString(), removedServerCode };
|
|
1190
109
|
}
|