@octanejs/tanstack-start 0.1.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/LICENSE +22 -0
- package/README.md +52 -0
- package/THIRD_PARTY_NOTICES.md +11 -0
- package/package.json +149 -0
- package/src/client-only.d.ts +1 -0
- package/src/client-only.js +0 -0
- package/src/client-rpc.d.ts +1 -0
- package/src/client-rpc.js +1 -0
- package/src/client.d.ts +3 -0
- package/src/client.js +2 -0
- package/src/default-entry/client.js +12 -0
- package/src/default-entry/server.js +13 -0
- package/src/default-entry/start.js +1 -0
- package/src/default-entry-paths.d.ts +5 -0
- package/src/default-entry-paths.js +11 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +22 -0
- package/src/internal/README.md +15 -0
- package/src/internal/router-generator/config.d.ts +255 -0
- package/src/internal/router-generator/config.js +134 -0
- package/src/internal/router-generator/filesystem/physical/getRouteNodes.d.ts +53 -0
- package/src/internal/router-generator/filesystem/physical/getRouteNodes.js +335 -0
- package/src/internal/router-generator/filesystem/physical/rootPathId.d.ts +1 -0
- package/src/internal/router-generator/filesystem/physical/rootPathId.js +4 -0
- package/src/internal/router-generator/filesystem/virtual/config.d.ts +3 -0
- package/src/internal/router-generator/filesystem/virtual/config.js +36 -0
- package/src/internal/router-generator/filesystem/virtual/getRouteNodes.d.ts +31 -0
- package/src/internal/router-generator/filesystem/virtual/getRouteNodes.js +272 -0
- package/src/internal/router-generator/filesystem/virtual/loadConfigFile.d.ts +1 -0
- package/src/internal/router-generator/filesystem/virtual/loadConfigFile.js +10 -0
- package/src/internal/router-generator/generator.d.ts +119 -0
- package/src/internal/router-generator/generator.js +1080 -0
- package/src/internal/router-generator/index.d.ts +40 -0
- package/src/internal/router-generator/index.js +49 -0
- package/src/internal/router-generator/logger.d.ts +8 -0
- package/src/internal/router-generator/logger.js +30 -0
- package/src/internal/router-generator/plugin/types.d.ts +37 -0
- package/src/internal/router-generator/template.d.ts +38 -0
- package/src/internal/router-generator/template.js +220 -0
- package/src/internal/router-generator/transform/transform.d.ts +8 -0
- package/src/internal/router-generator/transform/transform.js +385 -0
- package/src/internal/router-generator/transform/types.d.ts +34 -0
- package/src/internal/router-generator/types.d.ts +73 -0
- package/src/internal/router-generator/utils.d.ts +270 -0
- package/src/internal/router-generator/utils.js +864 -0
- package/src/internal/router-generator/validate-route-params.d.ts +13 -0
- package/src/internal/router-generator/validate-route-params.js +75 -0
- package/src/internal/router-plugin/context.d.ts +2 -0
- package/src/internal/router-plugin/context.js +2 -0
- package/src/internal/router-plugin/core/code-splitter/compilers.d.ts +69 -0
- package/src/internal/router-plugin/core/code-splitter/compilers.js +1042 -0
- package/src/internal/router-plugin/core/code-splitter/framework-options.d.ts +11 -0
- package/src/internal/router-plugin/core/code-splitter/framework-options.js +41 -0
- package/src/internal/router-plugin/core/code-splitter/plugins/framework-plugins.d.ts +12 -0
- package/src/internal/router-plugin/core/code-splitter/plugins/framework-plugins.js +35 -0
- package/src/internal/router-plugin/core/code-splitter/plugins/octane-hmr-split-route-components.d.ts +5 -0
- package/src/internal/router-plugin/core/code-splitter/plugins/octane-hmr-split-route-components.js +106 -0
- package/src/internal/router-plugin/core/code-splitter/plugins/octane-split-route-components.d.ts +8 -0
- package/src/internal/router-plugin/core/code-splitter/plugins/octane-split-route-components.js +85 -0
- package/src/internal/router-plugin/core/code-splitter/plugins/react-refresh-ignored-route-exports.d.ts +2 -0
- package/src/internal/router-plugin/core/code-splitter/plugins/react-refresh-ignored-route-exports.js +55 -0
- package/src/internal/router-plugin/core/code-splitter/plugins/react-refresh-route-components.d.ts +2 -0
- package/src/internal/router-plugin/core/code-splitter/plugins/react-refresh-route-components.js +50 -0
- package/src/internal/router-plugin/core/code-splitter/plugins/react-stable-hmr-split-route-components.d.ts +5 -0
- package/src/internal/router-plugin/core/code-splitter/plugins/react-stable-hmr-split-route-components.js +54 -0
- package/src/internal/router-plugin/core/code-splitter/plugins.d.ts +62 -0
- package/src/internal/router-plugin/core/code-splitter/types.d.ts +9 -0
- package/src/internal/router-plugin/core/config.d.ts +294 -0
- package/src/internal/router-plugin/core/config.js +56 -0
- package/src/internal/router-plugin/core/constants.d.ts +12 -0
- package/src/internal/router-plugin/core/constants.js +13 -0
- package/src/internal/router-plugin/core/framework-plugin-order.d.ts +8 -0
- package/src/internal/router-plugin/core/framework-plugin-order.js +78 -0
- package/src/internal/router-plugin/core/hmr/handle-route-update.d.ts +1 -0
- package/src/internal/router-plugin/core/hmr/handle-route-update.js +126 -0
- package/src/internal/router-plugin/core/hmr/index.d.ts +5 -0
- package/src/internal/router-plugin/core/hmr/select-adapter.d.ts +17 -0
- package/src/internal/router-plugin/core/hmr/select-adapter.js +24 -0
- package/src/internal/router-plugin/core/hmr/vite-adapter.d.ts +18 -0
- package/src/internal/router-plugin/core/hmr/vite-adapter.js +73 -0
- package/src/internal/router-plugin/core/hmr/webpack-adapter.d.ts +22 -0
- package/src/internal/router-plugin/core/hmr/webpack-adapter.js +78 -0
- package/src/internal/router-plugin/core/router-code-splitter-plugin.d.ts +10 -0
- package/src/internal/router-plugin/core/router-code-splitter-plugin.js +244 -0
- package/src/internal/router-plugin/core/router-composed-plugin.d.ts +5 -0
- package/src/internal/router-plugin/core/router-composed-plugin.js +62 -0
- package/src/internal/router-plugin/core/router-generator-plugin.d.ts +10 -0
- package/src/internal/router-plugin/core/router-generator-plugin.js +125 -0
- package/src/internal/router-plugin/core/router-hmr-plugin.d.ts +13 -0
- package/src/internal/router-plugin/core/router-hmr-plugin.js +101 -0
- package/src/internal/router-plugin/core/router-plugin-context.d.ts +5 -0
- package/src/internal/router-plugin/core/router-plugin-context.js +6 -0
- package/src/internal/router-plugin/core/utils.d.ts +17 -0
- package/src/internal/router-plugin/core/utils.js +47 -0
- package/src/internal/router-plugin/esbuild.d.ts +197 -0
- package/src/internal/router-plugin/esbuild.js +58 -0
- package/src/internal/router-plugin/index.d.ts +19 -0
- package/src/internal/router-plugin/index.js +17 -0
- package/src/internal/router-plugin/rspack.d.ts +61 -0
- package/src/internal/router-plugin/rspack.js +92 -0
- package/src/internal/router-plugin/vite.d.ts +201 -0
- package/src/internal/router-plugin/vite.js +62 -0
- package/src/internal/router-plugin/webpack.d.ts +50 -0
- package/src/internal/router-plugin/webpack.js +77 -0
- package/src/internal/start-plugin-core/build-sitemap.d.ts +34 -0
- package/src/internal/start-plugin-core/build-sitemap.js +125 -0
- package/src/internal/start-plugin-core/config-context.d.ts +30 -0
- package/src/internal/start-plugin-core/config-context.js +89 -0
- package/src/internal/start-plugin-core/constants.d.ts +20 -0
- package/src/internal/start-plugin-core/constants.js +24 -0
- package/src/internal/start-plugin-core/debug.d.ts +1 -0
- package/src/internal/start-plugin-core/hydrate-when-transform.d.ts +5 -0
- package/src/internal/start-plugin-core/hydrate-when-transform.js +661 -0
- package/src/internal/start-plugin-core/hydration-constants.d.ts +1 -0
- package/src/internal/start-plugin-core/hydration-constants.js +4 -0
- package/src/internal/start-plugin-core/import-protection/adapterUtils.d.ts +40 -0
- package/src/internal/start-plugin-core/import-protection/adapterUtils.js +42 -0
- package/src/internal/start-plugin-core/import-protection/analysis.d.ts +63 -0
- package/src/internal/start-plugin-core/import-protection/analysis.js +533 -0
- package/src/internal/start-plugin-core/import-protection/constants.d.ts +11 -0
- package/src/internal/start-plugin-core/import-protection/constants.js +39 -0
- package/src/internal/start-plugin-core/import-protection/defaults.d.ts +19 -0
- package/src/internal/start-plugin-core/import-protection/defaults.js +34 -0
- package/src/internal/start-plugin-core/import-protection/extensionlessAbsoluteIdResolver.d.ts +22 -0
- package/src/internal/start-plugin-core/import-protection/extensionlessAbsoluteIdResolver.js +94 -0
- package/src/internal/start-plugin-core/import-protection/matchers.d.ts +18 -0
- package/src/internal/start-plugin-core/import-protection/matchers.js +39 -0
- package/src/internal/start-plugin-core/import-protection/rewrite.d.ts +29 -0
- package/src/internal/start-plugin-core/import-protection/rewrite.js +184 -0
- package/src/internal/start-plugin-core/import-protection/sourceLocation.d.ts +170 -0
- package/src/internal/start-plugin-core/import-protection/sourceLocation.js +376 -0
- package/src/internal/start-plugin-core/import-protection/trace.d.ts +89 -0
- package/src/internal/start-plugin-core/import-protection/trace.js +186 -0
- package/src/internal/start-plugin-core/import-protection/utils.d.ts +78 -0
- package/src/internal/start-plugin-core/import-protection/utils.js +171 -0
- package/src/internal/start-plugin-core/import-protection/virtualModules.d.ts +38 -0
- package/src/internal/start-plugin-core/import-protection/virtualModules.js +241 -0
- package/src/internal/start-plugin-core/index.d.ts +8 -0
- package/src/internal/start-plugin-core/index.js +2 -0
- package/src/internal/start-plugin-core/planning.d.ts +44 -0
- package/src/internal/start-plugin-core/planning.js +98 -0
- package/src/internal/start-plugin-core/post-build.d.ts +12 -0
- package/src/internal/start-plugin-core/post-build.js +40 -0
- package/src/internal/start-plugin-core/prerender.d.ts +17 -0
- package/src/internal/start-plugin-core/prerender.js +172 -0
- package/src/internal/start-plugin-core/queue.d.ts +37 -0
- package/src/internal/start-plugin-core/queue.js +118 -0
- package/src/internal/start-plugin-core/resolve-entries.d.ts +10 -0
- package/src/internal/start-plugin-core/resolve-entries.js +32 -0
- package/src/internal/start-plugin-core/schema.d.ts +1799 -0
- package/src/internal/start-plugin-core/schema.js +252 -0
- package/src/internal/start-plugin-core/serialization-adapters-module.d.ts +18 -0
- package/src/internal/start-plugin-core/serialization-adapters-module.js +37 -0
- package/src/internal/start-plugin-core/start-compiler/compiler.d.ts +181 -0
- package/src/internal/start-plugin-core/start-compiler/compiler.js +1078 -0
- package/src/internal/start-plugin-core/start-compiler/config.d.ts +20 -0
- package/src/internal/start-plugin-core/start-compiler/config.js +84 -0
- package/src/internal/start-plugin-core/start-compiler/handleClientOnlyJSX.d.ts +24 -0
- package/src/internal/start-plugin-core/start-compiler/handleClientOnlyJSX.js +25 -0
- package/src/internal/start-plugin-core/start-compiler/handleCreateIsomorphicFn.d.ts +11 -0
- package/src/internal/start-plugin-core/start-compiler/handleCreateIsomorphicFn.js +24 -0
- package/src/internal/start-plugin-core/start-compiler/handleCreateMiddleware.d.ts +11 -0
- package/src/internal/start-plugin-core/start-compiler/handleCreateMiddleware.js +35 -0
- package/src/internal/start-plugin-core/start-compiler/handleCreateServerFn.d.ts +22 -0
- package/src/internal/start-plugin-core/start-compiler/handleCreateServerFn.js +292 -0
- package/src/internal/start-plugin-core/start-compiler/handleEnvOnly.d.ts +14 -0
- package/src/internal/start-plugin-core/start-compiler/handleEnvOnly.js +41 -0
- package/src/internal/start-plugin-core/start-compiler/host.d.ts +44 -0
- package/src/internal/start-plugin-core/start-compiler/host.js +75 -0
- package/src/internal/start-plugin-core/start-compiler/server-fn-resolver-module.d.ts +10 -0
- package/src/internal/start-plugin-core/start-compiler/server-fn-resolver-module.js +87 -0
- package/src/internal/start-plugin-core/start-compiler/types.d.ts +119 -0
- package/src/internal/start-plugin-core/start-compiler/utils.d.ts +27 -0
- package/src/internal/start-plugin-core/start-compiler/utils.js +36 -0
- package/src/internal/start-plugin-core/start-manifest-plugin/inlineCss.d.ts +16 -0
- package/src/internal/start-plugin-core/start-manifest-plugin/inlineCss.js +131 -0
- package/src/internal/start-plugin-core/start-manifest-plugin/manifestBuilder.d.ts +64 -0
- package/src/internal/start-plugin-core/start-manifest-plugin/manifestBuilder.js +441 -0
- package/src/internal/start-plugin-core/start-router-plugin/constants.d.ts +1 -0
- package/src/internal/start-plugin-core/start-router-plugin/constants.js +4 -0
- package/src/internal/start-plugin-core/start-router-plugin/generator-plugins/compose-generator-plugins.d.ts +6 -0
- package/src/internal/start-plugin-core/start-router-plugin/generator-plugins/compose-generator-plugins.js +6 -0
- package/src/internal/start-plugin-core/start-router-plugin/generator-plugins/prerender-routes-plugin.d.ts +6 -0
- package/src/internal/start-plugin-core/start-router-plugin/generator-plugins/prerender-routes-plugin.js +27 -0
- package/src/internal/start-plugin-core/start-router-plugin/generator-plugins/routes-manifest-plugin.d.ts +6 -0
- package/src/internal/start-plugin-core/start-router-plugin/generator-plugins/routes-manifest-plugin.js +34 -0
- package/src/internal/start-plugin-core/start-router-plugin/pruneServerOnlySubtrees.d.ts +11 -0
- package/src/internal/start-plugin-core/start-router-plugin/pruneServerOnlySubtrees.js +42 -0
- package/src/internal/start-plugin-core/start-router-plugin/route-tree-footer.d.ts +6 -0
- package/src/internal/start-plugin-core/start-router-plugin/route-tree-footer.js +59 -0
- package/src/internal/start-plugin-core/types.d.ts +135 -0
- package/src/internal/start-plugin-core/types.js +4 -0
- package/src/internal/start-plugin-core/utils.d.ts +13 -0
- package/src/internal/start-plugin-core/utils.js +42 -0
- package/src/internal/start-plugin-core/vite/createVirtualModule.d.ts +15 -0
- package/src/internal/start-plugin-core/vite/createVirtualModule.js +27 -0
- package/src/internal/start-plugin-core/vite/dev-server-plugin/dev-styles.d.ts +26 -0
- package/src/internal/start-plugin-core/vite/dev-server-plugin/dev-styles.js +165 -0
- package/src/internal/start-plugin-core/vite/dev-server-plugin/plugin.d.ts +11 -0
- package/src/internal/start-plugin-core/vite/dev-server-plugin/plugin.js +170 -0
- package/src/internal/start-plugin-core/vite/import-protection-plugin/plugin.d.ts +4 -0
- package/src/internal/start-plugin-core/vite/import-protection-plugin/plugin.js +1553 -0
- package/src/internal/start-plugin-core/vite/import-protection-plugin/types.d.ts +103 -0
- package/src/internal/start-plugin-core/vite/import-protection-plugin/virtualModules.d.ts +40 -0
- package/src/internal/start-plugin-core/vite/import-protection-plugin/virtualModules.js +68 -0
- package/src/internal/start-plugin-core/vite/index.d.ts +13 -0
- package/src/internal/start-plugin-core/vite/index.js +4 -0
- package/src/internal/start-plugin-core/vite/load-env-plugin/plugin.d.ts +2 -0
- package/src/internal/start-plugin-core/vite/load-env-plugin/plugin.js +13 -0
- package/src/internal/start-plugin-core/vite/output-directory.d.ts +7 -0
- package/src/internal/start-plugin-core/vite/output-directory.js +18 -0
- package/src/internal/start-plugin-core/vite/planning.d.ts +112 -0
- package/src/internal/start-plugin-core/vite/planning.js +152 -0
- package/src/internal/start-plugin-core/vite/plugin.d.ts +7 -0
- package/src/internal/start-plugin-core/vite/plugin.js +208 -0
- package/src/internal/start-plugin-core/vite/plugins.d.ts +17 -0
- package/src/internal/start-plugin-core/vite/plugins.js +72 -0
- package/src/internal/start-plugin-core/vite/post-server-build.d.ts +9 -0
- package/src/internal/start-plugin-core/vite/post-server-build.js +22 -0
- package/src/internal/start-plugin-core/vite/prerender.d.ts +9 -0
- package/src/internal/start-plugin-core/vite/prerender.js +52 -0
- package/src/internal/start-plugin-core/vite/preview-server-plugin/plugin.d.ts +2 -0
- package/src/internal/start-plugin-core/vite/preview-server-plugin/plugin.js +54 -0
- package/src/internal/start-plugin-core/vite/schema.d.ts +2423 -0
- package/src/internal/start-plugin-core/vite/schema.js +17 -0
- package/src/internal/start-plugin-core/vite/serialization-adapters-plugin.d.ts +5 -0
- package/src/internal/start-plugin-core/vite/serialization-adapters-plugin.js +63 -0
- package/src/internal/start-plugin-core/vite/start-compiler-plugin/hot-update.d.ts +5 -0
- package/src/internal/start-plugin-core/vite/start-compiler-plugin/hot-update.js +14 -0
- package/src/internal/start-plugin-core/vite/start-compiler-plugin/module-specifier.d.ts +5 -0
- package/src/internal/start-plugin-core/vite/start-compiler-plugin/module-specifier.js +25 -0
- package/src/internal/start-plugin-core/vite/start-compiler-plugin/plugin.d.ts +29 -0
- package/src/internal/start-plugin-core/vite/start-compiler-plugin/plugin.js +380 -0
- package/src/internal/start-plugin-core/vite/start-manifest-plugin/normalized-client-build.d.ts +11 -0
- package/src/internal/start-plugin-core/vite/start-manifest-plugin/normalized-client-build.js +114 -0
- package/src/internal/start-plugin-core/vite/start-manifest-plugin/plugin.d.ts +3 -0
- package/src/internal/start-plugin-core/vite/start-manifest-plugin/plugin.js +103 -0
- package/src/internal/start-plugin-core/vite/start-router-plugin/plugin.d.ts +9 -0
- package/src/internal/start-plugin-core/vite/start-router-plugin/plugin.js +143 -0
- package/src/internal/start-plugin-core/vite/types.d.ts +17 -0
- package/src/package-names.js +10 -0
- package/src/plugin-vite.d.ts +23 -0
- package/src/plugin-vite.js +62 -0
- package/src/runtime/client/StartClient.d.ts +11 -0
- package/src/runtime/client/StartClient.js +16 -0
- package/src/runtime/client/hydrateStart.d.ts +4 -0
- package/src/runtime/client/hydrateStart.js +30 -0
- package/src/runtime/server/StartServer.d.ts +10 -0
- package/src/runtime/server/StartServer.js +6 -0
- package/src/runtime/server/defaultRenderHandler.d.ts +3 -0
- package/src/runtime/server/defaultRenderHandler.js +11 -0
- package/src/runtime/server/defaultStreamHandler.d.ts +3 -0
- package/src/runtime/server/defaultStreamHandler.js +13 -0
- package/src/server-entry.d.ts +11 -0
- package/src/server-entry.js +13 -0
- package/src/server-only.d.ts +1 -0
- package/src/server-only.js +0 -0
- package/src/server-rpc.d.ts +1 -0
- package/src/server-rpc.js +1 -0
- package/src/server.d.ts +6 -0
- package/src/server.js +4 -0
- package/src/ssr-rpc.d.ts +1 -0
- package/src/ssr-rpc.js +1 -0
- package/src/use-server-fn.d.ts +3 -0
- package/src/use-server-fn.js +18 -0
- package/src/validate-options.js +7 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/constants.ts
|
|
2
|
+
var START_ENVIRONMENT_NAMES = {
|
|
3
|
+
server: 'ssr',
|
|
4
|
+
client: 'client',
|
|
5
|
+
};
|
|
6
|
+
var VITE_ENVIRONMENT_NAMES = START_ENVIRONMENT_NAMES;
|
|
7
|
+
var ENTRY_POINTS = {
|
|
8
|
+
client: 'virtual:tanstack-start-client-entry',
|
|
9
|
+
server: 'virtual:tanstack-start-server-entry',
|
|
10
|
+
start: '#tanstack-start-entry',
|
|
11
|
+
router: '#tanstack-router-entry',
|
|
12
|
+
};
|
|
13
|
+
var DEV_CLIENT_ENTRY = 'virtual:tanstack-start-dev-client-entry';
|
|
14
|
+
var SERVER_FN_LOOKUP = 'server-fn-module-lookup';
|
|
15
|
+
var TRANSFORM_ID_REGEX = [/\.[cm]?[tj]sx?($|\?)/, /\.tsrx($|\?)/];
|
|
16
|
+
//#endregion
|
|
17
|
+
export {
|
|
18
|
+
DEV_CLIENT_ENTRY,
|
|
19
|
+
ENTRY_POINTS,
|
|
20
|
+
SERVER_FN_LOOKUP,
|
|
21
|
+
START_ENVIRONMENT_NAMES,
|
|
22
|
+
TRANSFORM_ID_REGEX,
|
|
23
|
+
VITE_ENVIRONMENT_NAMES,
|
|
24
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const debug: boolean | '' | undefined;
|
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
import { cleanId, codeFrameError } from './start-compiler/utils.js';
|
|
2
|
+
import { tssHydrate } from './hydration-constants.js';
|
|
3
|
+
import { relative } from 'node:path';
|
|
4
|
+
import * as t from '@babel/types';
|
|
5
|
+
import {
|
|
6
|
+
buildDeclarationMap,
|
|
7
|
+
buildDependencyGraph,
|
|
8
|
+
collectIdentifiersFromNode,
|
|
9
|
+
collectIdentifiersFromPattern,
|
|
10
|
+
collectLocalBindingsFromStatement,
|
|
11
|
+
deadCodeElimination,
|
|
12
|
+
expandTransitively,
|
|
13
|
+
findReferencedIdentifiers,
|
|
14
|
+
generateFromAst,
|
|
15
|
+
parseAst,
|
|
16
|
+
removeModuleLevelBindings,
|
|
17
|
+
retainModuleLevelDeclarations,
|
|
18
|
+
stripUnreferencedTopLevelExpressionStatements,
|
|
19
|
+
unwrapExportedDeclarations,
|
|
20
|
+
} from '@tanstack/router-utils';
|
|
21
|
+
import crypto from 'node:crypto';
|
|
22
|
+
import babel from '@babel/core';
|
|
23
|
+
import { getRouterPackage, getStartPackage } from '#tanstack-start/package-names';
|
|
24
|
+
//#region src/hydrate-when-transform.ts
|
|
25
|
+
var MissingHydrateSourceError = class extends Error {
|
|
26
|
+
constructor(id) {
|
|
27
|
+
super(
|
|
28
|
+
`Missing Hydrate source for virtual module ${id}. The parent module must be transformed before its Hydrate child chunk is loaded.`,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Detection pattern used by the transform code filter to pre-scan files for
|
|
34
|
+
* `<Hydrate>` JSX before any AST parsing happens.
|
|
35
|
+
*/
|
|
36
|
+
var HYDRATE_DETECTION_PATTERN = /\bHydrate\b/;
|
|
37
|
+
function createBoundaryId(root, sourceId) {
|
|
38
|
+
const normalized = relative(root, sourceId).replaceAll('\\', '/');
|
|
39
|
+
const sourceHash = crypto.createHash('sha1').update(normalized).digest('hex').slice(0, 10);
|
|
40
|
+
return (index) => {
|
|
41
|
+
return `${index.toString(36)}_${sourceHash}`;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function getJSXElementName(node) {
|
|
45
|
+
const name = node.openingElement.name;
|
|
46
|
+
return t.isJSXIdentifier(name) ? name.name : void 0;
|
|
47
|
+
}
|
|
48
|
+
function getJSXAttribute(node, name) {
|
|
49
|
+
for (const item of node.attributes)
|
|
50
|
+
if (t.isJSXAttribute(item) && t.isJSXIdentifier(item.name, { name })) return item;
|
|
51
|
+
}
|
|
52
|
+
function getBooleanProp(node, name) {
|
|
53
|
+
const attr = getJSXAttribute(node, name);
|
|
54
|
+
if (!attr) return void 0;
|
|
55
|
+
if (!attr.value) return true;
|
|
56
|
+
if (t.isStringLiteral(attr.value)) return attr.value.value !== 'false';
|
|
57
|
+
if (t.isJSXExpressionContainer(attr.value)) {
|
|
58
|
+
if (t.isBooleanLiteral(attr.value.expression)) return attr.value.expression.value;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function parseHydrateVirtualId(id) {
|
|
62
|
+
const queryIndex = id.indexOf('?');
|
|
63
|
+
const sourceId = cleanId(queryIndex === -1 ? id : id.slice(0, queryIndex));
|
|
64
|
+
if (queryIndex === -1)
|
|
65
|
+
return {
|
|
66
|
+
sourceId,
|
|
67
|
+
splitId: null,
|
|
68
|
+
boundaryIndex: -1,
|
|
69
|
+
};
|
|
70
|
+
const rawQuery = id.slice(queryIndex + 1);
|
|
71
|
+
const splitId = new URLSearchParams(rawQuery).get(tssHydrate);
|
|
72
|
+
let boundaryIndex = -1;
|
|
73
|
+
if (splitId) {
|
|
74
|
+
const separatorIndex = splitId.indexOf('_');
|
|
75
|
+
if (separatorIndex > 0) {
|
|
76
|
+
const parsedIndex = Number.parseInt(splitId.slice(0, separatorIndex), 36);
|
|
77
|
+
if (Number.isInteger(parsedIndex)) boundaryIndex = parsedIndex;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
sourceId,
|
|
82
|
+
splitId,
|
|
83
|
+
boundaryIndex,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function isObjectPropertyName(property, name) {
|
|
87
|
+
if (t.isIdentifier(property.key) && !property.computed) return property.key.name === name;
|
|
88
|
+
return t.isStringLiteral(property.key) && property.key.value === name;
|
|
89
|
+
}
|
|
90
|
+
function isReferenceInsideAnyNode(referencePath, nodes) {
|
|
91
|
+
if (nodes.has(referencePath.node)) return true;
|
|
92
|
+
return Boolean(referencePath.findParent((parent) => nodes.has(parent.node)));
|
|
93
|
+
}
|
|
94
|
+
function stripBindingsOnlyReferencedBy(
|
|
95
|
+
path,
|
|
96
|
+
node,
|
|
97
|
+
seen = /* @__PURE__ */ new Set(),
|
|
98
|
+
preserve = /* @__PURE__ */ new Set(),
|
|
99
|
+
) {
|
|
100
|
+
stripBindingsOnlyReferencedByNodes(path.scope, [node], seen, preserve);
|
|
101
|
+
}
|
|
102
|
+
function stripBindingsOnlyReferencedByNodes(
|
|
103
|
+
scope,
|
|
104
|
+
nodes,
|
|
105
|
+
seen = /* @__PURE__ */ new Set(),
|
|
106
|
+
preserve = /* @__PURE__ */ new Set(),
|
|
107
|
+
) {
|
|
108
|
+
const nodeSet = new Set(nodes);
|
|
109
|
+
const names = /* @__PURE__ */ new Set();
|
|
110
|
+
nodes.forEach((node) => {
|
|
111
|
+
collectIdentifiersFromNode(node).forEach((name) => names.add(name));
|
|
112
|
+
});
|
|
113
|
+
for (const name of names) {
|
|
114
|
+
if (seen.has(name)) continue;
|
|
115
|
+
if (preserve.has(name)) continue;
|
|
116
|
+
const binding = scope.getBinding(name);
|
|
117
|
+
if (!binding?.constant) continue;
|
|
118
|
+
if (
|
|
119
|
+
binding.path.findParent(
|
|
120
|
+
(parentPath) =>
|
|
121
|
+
parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration(),
|
|
122
|
+
)
|
|
123
|
+
)
|
|
124
|
+
continue;
|
|
125
|
+
if (binding.referencePaths.length === 0) continue;
|
|
126
|
+
if (
|
|
127
|
+
!binding.referencePaths.every((referencePath) =>
|
|
128
|
+
isReferenceInsideAnyNode(referencePath, nodeSet),
|
|
129
|
+
)
|
|
130
|
+
)
|
|
131
|
+
continue;
|
|
132
|
+
seen.add(name);
|
|
133
|
+
const declarationPath = binding.path.isVariableDeclarator()
|
|
134
|
+
? binding.path
|
|
135
|
+
: binding.path.findParent((parentPath) => parentPath.isVariableDeclarator());
|
|
136
|
+
if (
|
|
137
|
+
declarationPath?.isVariableDeclarator() &&
|
|
138
|
+
!t.isIdentifier(declarationPath.node.id) &&
|
|
139
|
+
collectIdentifiersFromPattern(declarationPath.node.id).some((bindingName) => {
|
|
140
|
+
if (bindingName === binding.identifier.name) return false;
|
|
141
|
+
return binding.scope
|
|
142
|
+
.getBinding(bindingName)
|
|
143
|
+
?.referencePaths.some(
|
|
144
|
+
(referencePath) => !isReferenceInsideAnyNode(referencePath, nodeSet),
|
|
145
|
+
);
|
|
146
|
+
})
|
|
147
|
+
)
|
|
148
|
+
continue;
|
|
149
|
+
const bindingNode = binding.path.node;
|
|
150
|
+
if (t.isVariableDeclarator(bindingNode) && bindingNode.init)
|
|
151
|
+
stripBindingsOnlyReferencedByNodes(binding.scope, [bindingNode.init], seen, preserve);
|
|
152
|
+
else if (t.isFunctionDeclaration(bindingNode) || t.isClassDeclaration(bindingNode))
|
|
153
|
+
stripBindingsOnlyReferencedByNodes(binding.scope, [bindingNode], seen, preserve);
|
|
154
|
+
if (binding.path.isVariableDeclarator()) {
|
|
155
|
+
const declarationPath = binding.path.parentPath;
|
|
156
|
+
if (
|
|
157
|
+
declarationPath.isVariableDeclaration() &&
|
|
158
|
+
declarationPath.node.declarations.length === 1
|
|
159
|
+
) {
|
|
160
|
+
declarationPath.remove();
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
binding.path.remove();
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (
|
|
167
|
+
binding.path.isImportSpecifier() ||
|
|
168
|
+
binding.path.isImportDefaultSpecifier() ||
|
|
169
|
+
binding.path.isImportNamespaceSpecifier()
|
|
170
|
+
) {
|
|
171
|
+
const importPath = binding.path.parentPath;
|
|
172
|
+
if (importPath.isImportDeclaration() && importPath.node.specifiers.length === 1) {
|
|
173
|
+
importPath.remove();
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
binding.path.remove();
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
binding.path.remove();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function getSingleUseObjectExpressionBinding(path, identifier) {
|
|
183
|
+
const binding = path.scope.getBinding(identifier.name);
|
|
184
|
+
if (!binding?.constant) return void 0;
|
|
185
|
+
if (binding.referencePaths.length !== 1) return void 0;
|
|
186
|
+
if (binding.referencePaths[0]?.node !== identifier) return void 0;
|
|
187
|
+
if (!binding.path.isVariableDeclarator()) return void 0;
|
|
188
|
+
const init = binding.path.node.init;
|
|
189
|
+
return t.isObjectExpression(init) ? init : void 0;
|
|
190
|
+
}
|
|
191
|
+
function objectExpressionMayHaveProperty(node, name) {
|
|
192
|
+
return node.properties.some((property) => {
|
|
193
|
+
if (t.isSpreadElement(property)) return true;
|
|
194
|
+
if (!t.isObjectMethod(property) && !t.isObjectProperty(property)) return true;
|
|
195
|
+
if (property.computed) return true;
|
|
196
|
+
return isObjectPropertyName(property, name);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
function stripObjectExpressionProperty(path, node, name) {
|
|
200
|
+
let modified = false;
|
|
201
|
+
node.properties = node.properties.filter((property) => {
|
|
202
|
+
if (
|
|
203
|
+
(t.isObjectMethod(property) || t.isObjectProperty(property)) &&
|
|
204
|
+
isObjectPropertyName(property, name)
|
|
205
|
+
) {
|
|
206
|
+
stripBindingsOnlyReferencedBy(
|
|
207
|
+
path,
|
|
208
|
+
t.isObjectProperty(property) ? property.value : property.body,
|
|
209
|
+
);
|
|
210
|
+
modified = true;
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
return true;
|
|
214
|
+
});
|
|
215
|
+
return modified;
|
|
216
|
+
}
|
|
217
|
+
function throwBoundaryError(code, path, message) {
|
|
218
|
+
if (path.node.loc) throw codeFrameError(code, path.node.loc, message);
|
|
219
|
+
throw new Error(message);
|
|
220
|
+
}
|
|
221
|
+
function inspectSplitBoundary(options) {
|
|
222
|
+
const { path } = options;
|
|
223
|
+
const capturedNames = options.collectCaptured ? /* @__PURE__ */ new Set() : void 0;
|
|
224
|
+
const nestedHydrate = options.nestedHydrate;
|
|
225
|
+
let nestedBoundaryCount = 0;
|
|
226
|
+
if (options.validate) {
|
|
227
|
+
for (const child of path.node.children)
|
|
228
|
+
if (
|
|
229
|
+
t.isJSXExpressionContainer(child) &&
|
|
230
|
+
(t.isFunctionExpression(child.expression) || t.isArrowFunctionExpression(child.expression))
|
|
231
|
+
)
|
|
232
|
+
throwBoundaryError(
|
|
233
|
+
options.code,
|
|
234
|
+
path,
|
|
235
|
+
'Hydrate cannot code-split function-as-children. Use split={false} for this boundary.',
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
const rootVisitors = {
|
|
239
|
+
JSXOpeningElement(openingPath) {
|
|
240
|
+
if (openingPath.node === path.node.openingElement) openingPath.skip();
|
|
241
|
+
},
|
|
242
|
+
JSXClosingElement(closingPath) {
|
|
243
|
+
closingPath.skip();
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
const validateVisitors = options.validate
|
|
247
|
+
? {
|
|
248
|
+
CallExpression(callPath) {
|
|
249
|
+
if (!t.isIdentifier(callPath.node.callee)) return;
|
|
250
|
+
if (!/^use[A-Z0-9]/.test(callPath.node.callee.name)) return;
|
|
251
|
+
throwBoundaryError(
|
|
252
|
+
options.code,
|
|
253
|
+
path,
|
|
254
|
+
'Hydrate cannot code-split JSX that calls hooks during render. Move the hook call into a child component or use split={false}.',
|
|
255
|
+
);
|
|
256
|
+
},
|
|
257
|
+
ThisExpression(thisPath) {
|
|
258
|
+
throwBoundaryError(
|
|
259
|
+
options.code,
|
|
260
|
+
path,
|
|
261
|
+
'Hydrate cannot code-split JSX that captures this.',
|
|
262
|
+
);
|
|
263
|
+
},
|
|
264
|
+
Super(superPath) {
|
|
265
|
+
throwBoundaryError(
|
|
266
|
+
options.code,
|
|
267
|
+
path,
|
|
268
|
+
'Hydrate cannot code-split JSX that captures super.',
|
|
269
|
+
);
|
|
270
|
+
},
|
|
271
|
+
}
|
|
272
|
+
: {};
|
|
273
|
+
const nestedHydrateVisitors = nestedHydrate
|
|
274
|
+
? {
|
|
275
|
+
JSXElement(nestedPath) {
|
|
276
|
+
if (getJSXElementName(nestedPath.node) !== nestedHydrate.localName) return;
|
|
277
|
+
if (getBooleanProp(nestedPath.node.openingElement, 'split') === false) return;
|
|
278
|
+
nestedBoundaryCount++;
|
|
279
|
+
},
|
|
280
|
+
}
|
|
281
|
+
: {};
|
|
282
|
+
const captureVisitors = capturedNames
|
|
283
|
+
? {
|
|
284
|
+
Identifier(identifierPath) {
|
|
285
|
+
const parent = identifierPath.parent;
|
|
286
|
+
if (
|
|
287
|
+
t.isJSXOpeningElement(parent) ||
|
|
288
|
+
t.isJSXClosingElement(parent) ||
|
|
289
|
+
(t.isObjectProperty(parent, { key: identifierPath.node }) &&
|
|
290
|
+
!parent.computed &&
|
|
291
|
+
!parent.shorthand) ||
|
|
292
|
+
(t.isMemberExpression(parent, { property: identifierPath.node }) && !parent.computed)
|
|
293
|
+
)
|
|
294
|
+
return;
|
|
295
|
+
const binding = identifierPath.scope.getBinding(identifierPath.node.name);
|
|
296
|
+
if (!binding) return;
|
|
297
|
+
if (t.isProgram(binding.scope.block)) return;
|
|
298
|
+
if (path.node === binding.scope.block || path.isAncestor(binding.path)) return;
|
|
299
|
+
capturedNames.add(identifierPath.node.name);
|
|
300
|
+
},
|
|
301
|
+
JSXIdentifier(identifierPath) {
|
|
302
|
+
if (identifierPath.parentKey !== 'name') return;
|
|
303
|
+
const name = identifierPath.node.name;
|
|
304
|
+
if (!/^[A-Z]/.test(name)) return;
|
|
305
|
+
const binding = identifierPath.scope.getBinding(name);
|
|
306
|
+
if (!binding) return;
|
|
307
|
+
if (t.isProgram(binding.scope.block)) return;
|
|
308
|
+
capturedNames.add(name);
|
|
309
|
+
},
|
|
310
|
+
}
|
|
311
|
+
: {};
|
|
312
|
+
path.traverse({
|
|
313
|
+
...rootVisitors,
|
|
314
|
+
...validateVisitors,
|
|
315
|
+
...nestedHydrateVisitors,
|
|
316
|
+
...captureVisitors,
|
|
317
|
+
});
|
|
318
|
+
return {
|
|
319
|
+
captured: capturedNames ? [...capturedNames].sort() : [],
|
|
320
|
+
nestedBoundaryCount,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
function getHydrateImport(ast, framework) {
|
|
324
|
+
const hydrateImportSource = getStartPackage(framework);
|
|
325
|
+
for (const node of ast.program.body) {
|
|
326
|
+
if (!t.isImportDeclaration(node)) continue;
|
|
327
|
+
if (node.source.value !== hydrateImportSource) continue;
|
|
328
|
+
for (const specifier of node.specifiers)
|
|
329
|
+
if (t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported, { name: 'Hydrate' }))
|
|
330
|
+
return { hydrateLocalName: specifier.local.name };
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
function getMeaningfulChildren(children) {
|
|
334
|
+
return children.filter((child) => !(t.isJSXText(child) && child.value.trim() === ''));
|
|
335
|
+
}
|
|
336
|
+
function transformHydrateAst(options) {
|
|
337
|
+
if (!options.code.includes('Hydrate')) return null;
|
|
338
|
+
const hydrateImport = getHydrateImport(options.ast, options.framework);
|
|
339
|
+
if (!hydrateImport) return null;
|
|
340
|
+
const { hydrateLocalName: localName } = hydrateImport;
|
|
341
|
+
const sourceId = cleanId(options.id);
|
|
342
|
+
const getBoundaryId = createBoundaryId(options.root, sourceId);
|
|
343
|
+
let nextBoundaryIndex = options.indexOffset ?? 0;
|
|
344
|
+
const state = {
|
|
345
|
+
modified: false,
|
|
346
|
+
extractedChildNodes: [],
|
|
347
|
+
capturedNames: /* @__PURE__ */ new Set(),
|
|
348
|
+
};
|
|
349
|
+
let lazyIdent;
|
|
350
|
+
babel.traverse(options.ast, {
|
|
351
|
+
Program(programPath) {
|
|
352
|
+
programPath.traverse({
|
|
353
|
+
JSXElement(path) {
|
|
354
|
+
if (getJSXElementName(path.node) !== localName) return;
|
|
355
|
+
if (options.env === 'server')
|
|
356
|
+
path.node.openingElement.attributes = path.node.openingElement.attributes.filter(
|
|
357
|
+
(item) => {
|
|
358
|
+
if (t.isJSXAttribute(item) && t.isJSXIdentifier(item.name, { name: 'fallback' })) {
|
|
359
|
+
if (item.value) stripBindingsOnlyReferencedBy(path, item.value);
|
|
360
|
+
state.modified = true;
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
if (t.isJSXSpreadAttribute(item) && t.isObjectExpression(item.argument)) {
|
|
364
|
+
if (stripObjectExpressionProperty(path, item.argument, 'fallback'))
|
|
365
|
+
state.modified = true;
|
|
366
|
+
return item.argument.properties.length > 0;
|
|
367
|
+
}
|
|
368
|
+
if (t.isJSXSpreadAttribute(item) && t.isIdentifier(item.argument)) {
|
|
369
|
+
const init = getSingleUseObjectExpressionBinding(path, item.argument);
|
|
370
|
+
if (init && stripObjectExpressionProperty(path, init, 'fallback'))
|
|
371
|
+
state.modified = true;
|
|
372
|
+
}
|
|
373
|
+
return true;
|
|
374
|
+
},
|
|
375
|
+
);
|
|
376
|
+
if (getBooleanProp(path.node.openingElement, 'split') === false) return;
|
|
377
|
+
const boundaryInspection = inspectSplitBoundary({
|
|
378
|
+
code: options.code,
|
|
379
|
+
path,
|
|
380
|
+
validate: true,
|
|
381
|
+
collectCaptured: options.env === 'client',
|
|
382
|
+
...(options.env === 'client' ? { nestedHydrate: { localName } } : {}),
|
|
383
|
+
});
|
|
384
|
+
const index = nextBoundaryIndex;
|
|
385
|
+
nextBoundaryIndex += 1 + boundaryInspection.nestedBoundaryCount;
|
|
386
|
+
const id = getBoundaryId(index);
|
|
387
|
+
const exportName = `H${index}`;
|
|
388
|
+
const existingHydrateId = getJSXAttribute(path.node.openingElement, 'h');
|
|
389
|
+
if (existingHydrateId) existingHydrateId.value = t.stringLiteral(id);
|
|
390
|
+
else
|
|
391
|
+
path.node.openingElement.attributes.push(
|
|
392
|
+
t.jsxAttribute(t.jsxIdentifier('h'), t.stringLiteral(id)),
|
|
393
|
+
);
|
|
394
|
+
state.modified = true;
|
|
395
|
+
if (options.env === 'server') return;
|
|
396
|
+
const needsPreloadProp = path.node.openingElement.attributes.some((attribute) => {
|
|
397
|
+
if (t.isJSXAttribute(attribute))
|
|
398
|
+
return t.isJSXIdentifier(attribute.name, { name: 'prefetch' });
|
|
399
|
+
if (t.isJSXSpreadAttribute(attribute)) {
|
|
400
|
+
if (t.isObjectExpression(attribute.argument))
|
|
401
|
+
return objectExpressionMayHaveProperty(attribute.argument, 'prefetch');
|
|
402
|
+
if (t.isIdentifier(attribute.argument)) {
|
|
403
|
+
const init = getSingleUseObjectExpressionBinding(path, attribute.argument);
|
|
404
|
+
return init ? objectExpressionMayHaveProperty(init, 'prefetch') : true;
|
|
405
|
+
}
|
|
406
|
+
return true;
|
|
407
|
+
}
|
|
408
|
+
return false;
|
|
409
|
+
});
|
|
410
|
+
const childReferenceNodes = getMeaningfulChildren(path.node.children);
|
|
411
|
+
state.extractedChildNodes.push(...childReferenceNodes);
|
|
412
|
+
boundaryInspection.captured.forEach((name) => {
|
|
413
|
+
state.capturedNames.add(name);
|
|
414
|
+
});
|
|
415
|
+
if (!lazyIdent) {
|
|
416
|
+
lazyIdent = programPath.scope.generateUidIdentifier('lazyRouteComponent');
|
|
417
|
+
programPath.unshiftContainer('body', [
|
|
418
|
+
t.importDeclaration(
|
|
419
|
+
[t.importSpecifier(lazyIdent, t.identifier('lazyRouteComponent'))],
|
|
420
|
+
t.stringLiteral(getRouterPackage(options.framework)),
|
|
421
|
+
),
|
|
422
|
+
]);
|
|
423
|
+
}
|
|
424
|
+
const importIdParams = new URLSearchParams();
|
|
425
|
+
importIdParams.set(tssHydrate, id);
|
|
426
|
+
const componentIdent = programPath.scope.generateUidIdentifier(exportName);
|
|
427
|
+
const declarations = [
|
|
428
|
+
t.variableDeclarator(
|
|
429
|
+
componentIdent,
|
|
430
|
+
t.callExpression(lazyIdent, [
|
|
431
|
+
t.arrowFunctionExpression(
|
|
432
|
+
[],
|
|
433
|
+
t.callExpression(t.import(), [
|
|
434
|
+
t.stringLiteral(`${sourceId}?${importIdParams.toString()}`),
|
|
435
|
+
]),
|
|
436
|
+
),
|
|
437
|
+
t.stringLiteral(exportName),
|
|
438
|
+
]),
|
|
439
|
+
),
|
|
440
|
+
];
|
|
441
|
+
let preloadIdent;
|
|
442
|
+
if (needsPreloadProp) {
|
|
443
|
+
preloadIdent = programPath.scope.generateUidIdentifier(`${exportName}_preload`);
|
|
444
|
+
declarations.push(
|
|
445
|
+
t.variableDeclarator(
|
|
446
|
+
preloadIdent,
|
|
447
|
+
t.memberExpression(componentIdent, t.identifier('preload')),
|
|
448
|
+
),
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
programPath.unshiftContainer('body', [t.variableDeclaration('const', declarations)]);
|
|
452
|
+
if (preloadIdent)
|
|
453
|
+
path.node.openingElement.attributes.push(
|
|
454
|
+
t.jsxAttribute(t.jsxIdentifier('p'), t.jsxExpressionContainer(preloadIdent)),
|
|
455
|
+
);
|
|
456
|
+
path.node.children = [
|
|
457
|
+
t.jsxText('\n'),
|
|
458
|
+
t.jsxExpressionContainer(
|
|
459
|
+
t.jsxElement(
|
|
460
|
+
t.jsxOpeningElement(
|
|
461
|
+
t.jsxIdentifier(componentIdent.name),
|
|
462
|
+
boundaryInspection.captured.map((name) =>
|
|
463
|
+
t.jsxAttribute(
|
|
464
|
+
t.jsxIdentifier(name),
|
|
465
|
+
t.jsxExpressionContainer(t.identifier(name)),
|
|
466
|
+
),
|
|
467
|
+
),
|
|
468
|
+
true,
|
|
469
|
+
),
|
|
470
|
+
null,
|
|
471
|
+
[],
|
|
472
|
+
true,
|
|
473
|
+
),
|
|
474
|
+
),
|
|
475
|
+
t.jsxText('\n'),
|
|
476
|
+
];
|
|
477
|
+
path.skip();
|
|
478
|
+
},
|
|
479
|
+
});
|
|
480
|
+
if (state.extractedChildNodes.length > 0)
|
|
481
|
+
stripBindingsOnlyReferencedByNodes(
|
|
482
|
+
programPath.scope,
|
|
483
|
+
state.extractedChildNodes,
|
|
484
|
+
/* @__PURE__ */ new Set(),
|
|
485
|
+
state.capturedNames,
|
|
486
|
+
);
|
|
487
|
+
programPath.skip();
|
|
488
|
+
},
|
|
489
|
+
});
|
|
490
|
+
if (!state.modified) return null;
|
|
491
|
+
return true;
|
|
492
|
+
}
|
|
493
|
+
function loadHydrateVirtualModule(options) {
|
|
494
|
+
const { sourceId, splitId, boundaryIndex } = parseHydrateVirtualId(options.id);
|
|
495
|
+
if (!splitId || boundaryIndex < 0) return null;
|
|
496
|
+
const getBoundaryId = createBoundaryId(options.root, sourceId);
|
|
497
|
+
const ast = parseAst({
|
|
498
|
+
code: options.code,
|
|
499
|
+
sourceFilename: sourceId,
|
|
500
|
+
});
|
|
501
|
+
const hydrateImport = getHydrateImport(ast, options.framework);
|
|
502
|
+
if (!hydrateImport) return null;
|
|
503
|
+
const { hydrateLocalName: localName } = hydrateImport;
|
|
504
|
+
let target;
|
|
505
|
+
let targetIndex = -1;
|
|
506
|
+
let targetCaptured = [];
|
|
507
|
+
let index = 0;
|
|
508
|
+
babel.traverse(ast, {
|
|
509
|
+
JSXElement(path) {
|
|
510
|
+
if (getJSXElementName(path.node) !== localName) return;
|
|
511
|
+
if (getBooleanProp(path.node.openingElement, 'split') === false) return;
|
|
512
|
+
if (index === boundaryIndex) {
|
|
513
|
+
if (getBoundaryId(index) !== splitId) {
|
|
514
|
+
path.stop();
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
targetCaptured = inspectSplitBoundary({
|
|
518
|
+
code: options.code,
|
|
519
|
+
path,
|
|
520
|
+
collectCaptured: true,
|
|
521
|
+
}).captured;
|
|
522
|
+
target = t.cloneNode(path.node, true);
|
|
523
|
+
targetIndex = index;
|
|
524
|
+
path.stop();
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
index++;
|
|
528
|
+
},
|
|
529
|
+
});
|
|
530
|
+
if (!target || targetIndex < 0) return null;
|
|
531
|
+
const children = target.children;
|
|
532
|
+
const exportName = `H${targetIndex}`;
|
|
533
|
+
const refIdents = findReferencedIdentifiers(ast);
|
|
534
|
+
removeModuleLevelBindings(ast, new Set(['Route']));
|
|
535
|
+
const localBindings = /* @__PURE__ */ new Set();
|
|
536
|
+
for (const node of ast.program.body) collectLocalBindingsFromStatement(node, localBindings);
|
|
537
|
+
const keepBindings = /* @__PURE__ */ new Set();
|
|
538
|
+
const meaningfulChildren = getMeaningfulChildren(children);
|
|
539
|
+
let returnExpression = t.nullLiteral();
|
|
540
|
+
if (meaningfulChildren.length === 1) {
|
|
541
|
+
const child = meaningfulChildren[0];
|
|
542
|
+
if (t.isJSXExpressionContainer(child))
|
|
543
|
+
returnExpression = t.isJSXEmptyExpression(child.expression)
|
|
544
|
+
? t.nullLiteral()
|
|
545
|
+
: child.expression;
|
|
546
|
+
else if (t.isJSXElement(child) || t.isJSXFragment(child)) returnExpression = child;
|
|
547
|
+
else if (t.isJSXText(child)) returnExpression = t.stringLiteral(child.value);
|
|
548
|
+
} else if (meaningfulChildren.length > 1)
|
|
549
|
+
returnExpression = t.jsxFragment(t.jsxOpeningFragment(), t.jsxClosingFragment(), children);
|
|
550
|
+
for (const name of collectIdentifiersFromNode(returnExpression))
|
|
551
|
+
if (localBindings.has(name)) keepBindings.add(name);
|
|
552
|
+
if (keepBindings.size > 0)
|
|
553
|
+
expandTransitively(keepBindings, buildDependencyGraph(buildDeclarationMap(ast), localBindings));
|
|
554
|
+
retainModuleLevelDeclarations(ast, keepBindings);
|
|
555
|
+
unwrapExportedDeclarations(ast);
|
|
556
|
+
ast.program.body.push(
|
|
557
|
+
t.exportNamedDeclaration(
|
|
558
|
+
t.functionDeclaration(
|
|
559
|
+
t.identifier(exportName),
|
|
560
|
+
targetCaptured.length > 0
|
|
561
|
+
? [
|
|
562
|
+
t.objectPattern(
|
|
563
|
+
targetCaptured.map((name) =>
|
|
564
|
+
t.objectProperty(t.identifier(name), t.identifier(name), false, true),
|
|
565
|
+
),
|
|
566
|
+
),
|
|
567
|
+
]
|
|
568
|
+
: [],
|
|
569
|
+
t.blockStatement([t.returnStatement(returnExpression)]),
|
|
570
|
+
),
|
|
571
|
+
),
|
|
572
|
+
);
|
|
573
|
+
deadCodeElimination(ast, refIdents);
|
|
574
|
+
stripUnreferencedTopLevelExpressionStatements(ast);
|
|
575
|
+
return generateFromAst(ast, {
|
|
576
|
+
sourceMaps: true,
|
|
577
|
+
sourceFileName: options.id,
|
|
578
|
+
filename: options.id,
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
function createHydrateCompilerPlugin() {
|
|
582
|
+
const sourcesByEnvironment = /* @__PURE__ */ new Map();
|
|
583
|
+
const getEnvironmentSources = (envName) => {
|
|
584
|
+
let sources = sourcesByEnvironment.get(envName);
|
|
585
|
+
if (!sources) {
|
|
586
|
+
sources = /* @__PURE__ */ new Map();
|
|
587
|
+
sourcesByEnvironment.set(envName, sources);
|
|
588
|
+
}
|
|
589
|
+
return sources;
|
|
590
|
+
};
|
|
591
|
+
const setSource = (envName, id, code, framework) => {
|
|
592
|
+
const sourceId = cleanId(id);
|
|
593
|
+
const sources = getEnvironmentSources(envName);
|
|
594
|
+
const existing = sources.get(sourceId);
|
|
595
|
+
if (existing?.code === code && existing.framework === framework) return existing;
|
|
596
|
+
const entry = {
|
|
597
|
+
code,
|
|
598
|
+
framework,
|
|
599
|
+
virtualModules: /* @__PURE__ */ new Map(),
|
|
600
|
+
};
|
|
601
|
+
sources.set(sourceId, entry);
|
|
602
|
+
return entry;
|
|
603
|
+
};
|
|
604
|
+
const getSourceEntry = (envName, id) => sourcesByEnvironment.get(envName)?.get(cleanId(id));
|
|
605
|
+
const deleteSource = (envName, id) => {
|
|
606
|
+
sourcesByEnvironment.get(envName)?.delete(cleanId(id));
|
|
607
|
+
};
|
|
608
|
+
return {
|
|
609
|
+
name: 'tanstack-start-core:hydrate',
|
|
610
|
+
detect: HYDRATE_DETECTION_PATTERN,
|
|
611
|
+
virtualModuleIdPattern: new RegExp(`[?&]${tssHydrate}=`),
|
|
612
|
+
transformAst(context) {
|
|
613
|
+
const virtualModule = parseHydrateVirtualId(context.id);
|
|
614
|
+
const indexOffset =
|
|
615
|
+
virtualModule.boundaryIndex < 0 ? void 0 : virtualModule.boundaryIndex + 1;
|
|
616
|
+
const result = transformHydrateAst({
|
|
617
|
+
ast: context.ast,
|
|
618
|
+
code: context.code,
|
|
619
|
+
id: context.id,
|
|
620
|
+
root: context.root,
|
|
621
|
+
env: context.env,
|
|
622
|
+
framework: context.framework,
|
|
623
|
+
indexOffset,
|
|
624
|
+
});
|
|
625
|
+
if (result && virtualModule.boundaryIndex < 0)
|
|
626
|
+
setSource(context.envName, context.id, context.code, context.framework);
|
|
627
|
+
return !!result;
|
|
628
|
+
},
|
|
629
|
+
loadVirtualModule(context) {
|
|
630
|
+
const virtualModule = parseHydrateVirtualId(context.id);
|
|
631
|
+
if (!virtualModule.splitId || virtualModule.boundaryIndex < 0) return null;
|
|
632
|
+
const existingSourceEntry = getSourceEntry(context.envName, virtualModule.sourceId);
|
|
633
|
+
const sourceEntry =
|
|
634
|
+
context.code === void 0
|
|
635
|
+
? existingSourceEntry
|
|
636
|
+
: setSource(
|
|
637
|
+
context.envName,
|
|
638
|
+
virtualModule.sourceId,
|
|
639
|
+
context.code,
|
|
640
|
+
existingSourceEntry?.framework ??
|
|
641
|
+
(context.code.includes('@tanstack/solid-start') ? 'solid' : 'react'),
|
|
642
|
+
);
|
|
643
|
+
if (!sourceEntry) throw new MissingHydrateSourceError(context.id);
|
|
644
|
+
if (sourceEntry.virtualModules.has(context.id))
|
|
645
|
+
return sourceEntry.virtualModules.get(context.id);
|
|
646
|
+
const result = loadHydrateVirtualModule({
|
|
647
|
+
code: sourceEntry.code,
|
|
648
|
+
id: context.id,
|
|
649
|
+
root: context.root,
|
|
650
|
+
framework: sourceEntry.framework,
|
|
651
|
+
});
|
|
652
|
+
sourceEntry.virtualModules.set(context.id, result);
|
|
653
|
+
return result;
|
|
654
|
+
},
|
|
655
|
+
invalidateModule(context) {
|
|
656
|
+
deleteSource(context.envName, context.id);
|
|
657
|
+
},
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
//#endregion
|
|
661
|
+
export { MissingHydrateSourceError, createHydrateCompilerPlugin };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const tssHydrate = 'tss-hydrate';
|