@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,1042 @@
|
|
|
1
|
+
import { tsrShared, tsrSplit } from '../constants.js';
|
|
2
|
+
import { createRouteHmrStatement } from '../hmr/select-adapter.js';
|
|
3
|
+
import { getObjectPropertyKeyName } from '../utils.js';
|
|
4
|
+
import { getFrameworkOptions } from './framework-options.js';
|
|
5
|
+
import {
|
|
6
|
+
buildDeclarationMap,
|
|
7
|
+
buildDependencyGraph,
|
|
8
|
+
collectIdentifiersFromPattern,
|
|
9
|
+
collectLocalBindingsFromStatement,
|
|
10
|
+
collectModuleLevelRefsFromNode,
|
|
11
|
+
createIdentifier,
|
|
12
|
+
deadCodeElimination,
|
|
13
|
+
expandDestructuredDeclarations,
|
|
14
|
+
expandSharedDestructuredDeclarators,
|
|
15
|
+
expandTransitively,
|
|
16
|
+
findReferencedIdentifiers,
|
|
17
|
+
generateFromAst,
|
|
18
|
+
parseAst,
|
|
19
|
+
removeBindingsTransitivelyDependingOn,
|
|
20
|
+
retainModuleLevelDeclarations,
|
|
21
|
+
stripUnreferencedTopLevelExpressionStatements,
|
|
22
|
+
unwrapExportedDeclarations,
|
|
23
|
+
} from '@tanstack/router-utils';
|
|
24
|
+
import * as t from '@babel/types';
|
|
25
|
+
import * as babel from '@babel/core';
|
|
26
|
+
import * as template from '@babel/template';
|
|
27
|
+
//#region src/core/code-splitter/compilers.ts
|
|
28
|
+
function hashRouteHmrSignature(code) {
|
|
29
|
+
let hash = 14695981039346656037n;
|
|
30
|
+
for (let index = 0; index < code.length; index++) {
|
|
31
|
+
hash ^= BigInt(code.charCodeAt(index));
|
|
32
|
+
hash = BigInt.asUintN(64, hash * 1099511628211n);
|
|
33
|
+
}
|
|
34
|
+
return hash.toString(16).padStart(16, '0');
|
|
35
|
+
}
|
|
36
|
+
function getRouteHmrSignatureSource(ast, filename) {
|
|
37
|
+
const signatureAst = t.cloneNode(ast, true);
|
|
38
|
+
const localBindings = /* @__PURE__ */ new Set();
|
|
39
|
+
for (const node of signatureAst.program.body)
|
|
40
|
+
collectLocalBindingsFromStatement(node, localBindings);
|
|
41
|
+
const dependencyGraph = buildDependencyGraph(buildDeclarationMap(signatureAst), localBindings);
|
|
42
|
+
const keepBindings = new Set(['Route']);
|
|
43
|
+
expandTransitively(keepBindings, dependencyGraph);
|
|
44
|
+
retainModuleLevelDeclarations(signatureAst, keepBindings);
|
|
45
|
+
unwrapExportedDeclarations(signatureAst);
|
|
46
|
+
signatureAst.program.body = signatureAst.program.body.filter(
|
|
47
|
+
(statement) => !t.isImportDeclaration(statement),
|
|
48
|
+
);
|
|
49
|
+
return generateFromAst(signatureAst, {
|
|
50
|
+
sourceMaps: false,
|
|
51
|
+
sourceFileName: filename,
|
|
52
|
+
filename,
|
|
53
|
+
}).code;
|
|
54
|
+
}
|
|
55
|
+
var SPLIT_NODES_CONFIG = new Map([
|
|
56
|
+
[
|
|
57
|
+
'loader',
|
|
58
|
+
{
|
|
59
|
+
routeIdent: 'loader',
|
|
60
|
+
localImporterIdent: '$$splitLoaderImporter',
|
|
61
|
+
splitStrategy: 'lazyFn',
|
|
62
|
+
localExporterIdent: 'SplitLoader',
|
|
63
|
+
exporterIdent: 'loader',
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
[
|
|
67
|
+
'component',
|
|
68
|
+
{
|
|
69
|
+
routeIdent: 'component',
|
|
70
|
+
localImporterIdent: '$$splitComponentImporter',
|
|
71
|
+
splitStrategy: 'lazyRouteComponent',
|
|
72
|
+
localExporterIdent: 'SplitComponent',
|
|
73
|
+
exporterIdent: 'component',
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
[
|
|
77
|
+
'pendingComponent',
|
|
78
|
+
{
|
|
79
|
+
routeIdent: 'pendingComponent',
|
|
80
|
+
localImporterIdent: '$$splitPendingComponentImporter',
|
|
81
|
+
splitStrategy: 'lazyRouteComponent',
|
|
82
|
+
localExporterIdent: 'SplitPendingComponent',
|
|
83
|
+
exporterIdent: 'pendingComponent',
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
[
|
|
87
|
+
'errorComponent',
|
|
88
|
+
{
|
|
89
|
+
routeIdent: 'errorComponent',
|
|
90
|
+
localImporterIdent: '$$splitErrorComponentImporter',
|
|
91
|
+
splitStrategy: 'lazyRouteComponent',
|
|
92
|
+
localExporterIdent: 'SplitErrorComponent',
|
|
93
|
+
exporterIdent: 'errorComponent',
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
[
|
|
97
|
+
'notFoundComponent',
|
|
98
|
+
{
|
|
99
|
+
routeIdent: 'notFoundComponent',
|
|
100
|
+
localImporterIdent: '$$splitNotFoundComponentImporter',
|
|
101
|
+
splitStrategy: 'lazyRouteComponent',
|
|
102
|
+
localExporterIdent: 'SplitNotFoundComponent',
|
|
103
|
+
exporterIdent: 'notFoundComponent',
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
]);
|
|
107
|
+
var KNOWN_SPLIT_ROUTE_IDENTS = [...SPLIT_NODES_CONFIG.keys()];
|
|
108
|
+
function addSplitSearchParamToFilename(filename, grouping) {
|
|
109
|
+
const [bareFilename] = filename.split('?');
|
|
110
|
+
const params = new URLSearchParams();
|
|
111
|
+
params.append(tsrSplit, createIdentifier(grouping));
|
|
112
|
+
return `${bareFilename}?${params.toString()}`;
|
|
113
|
+
}
|
|
114
|
+
function removeSplitSearchParamFromFilename(filename) {
|
|
115
|
+
const [bareFilename] = filename.split('?');
|
|
116
|
+
return bareFilename;
|
|
117
|
+
}
|
|
118
|
+
function addSharedSearchParamToFilename(filename) {
|
|
119
|
+
const [bareFilename] = filename.split('?');
|
|
120
|
+
return `${bareFilename}?${tsrShared}=1`;
|
|
121
|
+
}
|
|
122
|
+
var splittableCreateRouteFns = ['createFileRoute'];
|
|
123
|
+
var unsplittableCreateRouteFns = ['createRootRoute', 'createRootRouteWithContext'];
|
|
124
|
+
var allCreateRouteFns = [...splittableCreateRouteFns, ...unsplittableCreateRouteFns];
|
|
125
|
+
/**
|
|
126
|
+
* Computes module-level bindings that are shared between split and non-split
|
|
127
|
+
* route properties. These bindings need to be extracted into a shared virtual
|
|
128
|
+
* module to avoid double-initialization.
|
|
129
|
+
*
|
|
130
|
+
* A binding is "shared" if it is referenced by at least one split property
|
|
131
|
+
* AND at least one non-split property. Only locally-declared module-level
|
|
132
|
+
* bindings are candidates (not imports — bundlers dedupe those).
|
|
133
|
+
*/
|
|
134
|
+
function computeSharedBindings(opts) {
|
|
135
|
+
const ast = parseAst(opts);
|
|
136
|
+
const localModuleLevelBindings = /* @__PURE__ */ new Set();
|
|
137
|
+
for (const node of ast.program.body)
|
|
138
|
+
collectLocalBindingsFromStatement(node, localModuleLevelBindings);
|
|
139
|
+
localModuleLevelBindings.delete('Route');
|
|
140
|
+
if (localModuleLevelBindings.size === 0) return /* @__PURE__ */ new Set();
|
|
141
|
+
function findIndexForSplitNode(str) {
|
|
142
|
+
return opts.codeSplitGroupings.findIndex((group) => group.includes(str));
|
|
143
|
+
}
|
|
144
|
+
let routeOptions;
|
|
145
|
+
babel.traverse(ast, {
|
|
146
|
+
CallExpression(path) {
|
|
147
|
+
if (!t.isIdentifier(path.node.callee)) return;
|
|
148
|
+
if (!splittableCreateRouteFns.includes(path.node.callee.name)) return;
|
|
149
|
+
if (t.isCallExpression(path.parentPath.node)) {
|
|
150
|
+
const opts = resolveIdentifier(path, path.parentPath.node.arguments[0]);
|
|
151
|
+
if (t.isObjectExpression(opts)) routeOptions = opts;
|
|
152
|
+
} else if (t.isVariableDeclarator(path.parentPath.node)) {
|
|
153
|
+
const caller = resolveIdentifier(path, path.parentPath.node.init);
|
|
154
|
+
if (t.isCallExpression(caller)) {
|
|
155
|
+
const opts = resolveIdentifier(path, caller.arguments[0]);
|
|
156
|
+
if (t.isObjectExpression(opts)) routeOptions = opts;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
if (!routeOptions) return /* @__PURE__ */ new Set();
|
|
162
|
+
const splitGroupsPresent = /* @__PURE__ */ new Set();
|
|
163
|
+
let hasNonSplit = false;
|
|
164
|
+
for (const prop of routeOptions.properties) {
|
|
165
|
+
if (!t.isObjectProperty(prop)) continue;
|
|
166
|
+
const key = getObjectPropertyKeyName(prop);
|
|
167
|
+
if (!key) continue;
|
|
168
|
+
if (key === 'codeSplitGroupings') continue;
|
|
169
|
+
if (t.isIdentifier(prop.value) && prop.value.name === 'undefined') continue;
|
|
170
|
+
const groupIndex = findIndexForSplitNode(key);
|
|
171
|
+
if (groupIndex === -1) hasNonSplit = true;
|
|
172
|
+
else splitGroupsPresent.add(groupIndex);
|
|
173
|
+
}
|
|
174
|
+
if (!hasNonSplit && splitGroupsPresent.size < 2) return /* @__PURE__ */ new Set();
|
|
175
|
+
const declMap = buildDeclarationMap(ast);
|
|
176
|
+
const depGraph = buildDependencyGraph(declMap, localModuleLevelBindings);
|
|
177
|
+
const allLocalBindings = new Set(localModuleLevelBindings);
|
|
178
|
+
allLocalBindings.add('Route');
|
|
179
|
+
const fullDepGraph = buildDependencyGraph(declMap, allLocalBindings);
|
|
180
|
+
const refsByGroup = /* @__PURE__ */ new Map();
|
|
181
|
+
for (const prop of routeOptions.properties) {
|
|
182
|
+
if (!t.isObjectProperty(prop)) continue;
|
|
183
|
+
const key = getObjectPropertyKeyName(prop);
|
|
184
|
+
if (!key) continue;
|
|
185
|
+
if (key === 'codeSplitGroupings') continue;
|
|
186
|
+
const groupIndex = findIndexForSplitNode(key);
|
|
187
|
+
const directRefs = collectModuleLevelRefsFromNode(prop.value, localModuleLevelBindings);
|
|
188
|
+
const allRefs = new Set(directRefs);
|
|
189
|
+
expandTransitively(allRefs, depGraph);
|
|
190
|
+
for (const ref of allRefs) {
|
|
191
|
+
let groups = refsByGroup.get(ref);
|
|
192
|
+
if (!groups) {
|
|
193
|
+
groups = /* @__PURE__ */ new Set();
|
|
194
|
+
refsByGroup.set(ref, groups);
|
|
195
|
+
}
|
|
196
|
+
groups.add(groupIndex);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const shared = /* @__PURE__ */ new Set();
|
|
200
|
+
for (const [name, groups] of refsByGroup) if (groups.size >= 2) shared.add(name);
|
|
201
|
+
expandSharedDestructuredDeclarators(ast, refsByGroup, shared);
|
|
202
|
+
if (shared.size === 0) return shared;
|
|
203
|
+
expandDestructuredDeclarations(ast, shared);
|
|
204
|
+
removeBindingsTransitivelyDependingOn(shared, fullDepGraph, ['Route']);
|
|
205
|
+
return shared;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Find which shared bindings are user-exported in the original source.
|
|
209
|
+
* These need to be re-exported from the shared module.
|
|
210
|
+
*/
|
|
211
|
+
function findExportedSharedBindings(ast, sharedBindings) {
|
|
212
|
+
const exported = /* @__PURE__ */ new Set();
|
|
213
|
+
for (const stmt of ast.program.body) {
|
|
214
|
+
if (!t.isExportNamedDeclaration(stmt) || !stmt.declaration) continue;
|
|
215
|
+
if (t.isVariableDeclaration(stmt.declaration)) {
|
|
216
|
+
for (const decl of stmt.declaration.declarations)
|
|
217
|
+
for (const name of collectIdentifiersFromPattern(decl.id))
|
|
218
|
+
if (sharedBindings.has(name)) exported.add(name);
|
|
219
|
+
} else if (t.isFunctionDeclaration(stmt.declaration) && stmt.declaration.id) {
|
|
220
|
+
if (sharedBindings.has(stmt.declaration.id.name)) exported.add(stmt.declaration.id.name);
|
|
221
|
+
} else if (t.isClassDeclaration(stmt.declaration) && stmt.declaration.id) {
|
|
222
|
+
if (sharedBindings.has(stmt.declaration.id.name)) exported.add(stmt.declaration.id.name);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return exported;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Remove declarations of shared bindings from the AST.
|
|
229
|
+
* Handles both plain and exported declarations, including destructured patterns.
|
|
230
|
+
* Removes the entire statement if all bindings in it are shared.
|
|
231
|
+
*/
|
|
232
|
+
function removeSharedDeclarations(ast, sharedBindings) {
|
|
233
|
+
ast.program.body = ast.program.body.filter((stmt) => {
|
|
234
|
+
const decl = t.isExportNamedDeclaration(stmt) && stmt.declaration ? stmt.declaration : stmt;
|
|
235
|
+
if (t.isVariableDeclaration(decl)) {
|
|
236
|
+
decl.declarations = decl.declarations.filter((declarator) => {
|
|
237
|
+
return !collectIdentifiersFromPattern(declarator.id).every((n) => sharedBindings.has(n));
|
|
238
|
+
});
|
|
239
|
+
if (decl.declarations.length === 0) return false;
|
|
240
|
+
} else if (t.isFunctionDeclaration(decl) && decl.id) {
|
|
241
|
+
if (sharedBindings.has(decl.id.name)) return false;
|
|
242
|
+
} else if (t.isClassDeclaration(decl) && decl.id) {
|
|
243
|
+
if (sharedBindings.has(decl.id.name)) return false;
|
|
244
|
+
}
|
|
245
|
+
return true;
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
function compileCodeSplitReferenceRoute(opts) {
|
|
249
|
+
const ast = parseAst(opts);
|
|
250
|
+
const refIdents = findReferencedIdentifiers(ast);
|
|
251
|
+
const knownExportedIdents = /* @__PURE__ */ new Set();
|
|
252
|
+
function findIndexForSplitNode(str) {
|
|
253
|
+
return opts.codeSplitGroupings.findIndex((group) => group.includes(str));
|
|
254
|
+
}
|
|
255
|
+
const frameworkOptions = getFrameworkOptions(opts.targetFramework);
|
|
256
|
+
const PACKAGE = frameworkOptions.package;
|
|
257
|
+
const LAZY_ROUTE_COMPONENT_IDENT = frameworkOptions.idents.lazyRouteComponent;
|
|
258
|
+
const LAZY_FN_IDENT = frameworkOptions.idents.lazyFn;
|
|
259
|
+
const stableRouteOptionKeys = [
|
|
260
|
+
...new Set(
|
|
261
|
+
(opts.compilerPlugins ?? []).flatMap((plugin) => plugin.getStableRouteOptionKeys?.() ?? []),
|
|
262
|
+
),
|
|
263
|
+
];
|
|
264
|
+
let createRouteFn;
|
|
265
|
+
let modified = false;
|
|
266
|
+
let hmrAdded = false;
|
|
267
|
+
let sharedExportedNames;
|
|
268
|
+
babel.traverse(ast, {
|
|
269
|
+
Program: {
|
|
270
|
+
enter(programPath) {
|
|
271
|
+
/**
|
|
272
|
+
* If the component for the route is being imported from
|
|
273
|
+
* another file, this is to track the path to that file
|
|
274
|
+
* the path itself doesn't matter, we just need to keep
|
|
275
|
+
* track of it so that we can remove it from the imports
|
|
276
|
+
* list if it's not being used like:
|
|
277
|
+
*
|
|
278
|
+
* `import '../shared/imported'`
|
|
279
|
+
*/
|
|
280
|
+
const removableImportPaths = /* @__PURE__ */ new Set([]);
|
|
281
|
+
programPath.traverse({
|
|
282
|
+
CallExpression: (path) => {
|
|
283
|
+
if (!t.isIdentifier(path.node.callee)) return;
|
|
284
|
+
if (!allCreateRouteFns.includes(path.node.callee.name)) return;
|
|
285
|
+
createRouteFn = path.node.callee.name;
|
|
286
|
+
function babelHandleReference(routeOptions) {
|
|
287
|
+
const hasImportedOrDefinedIdentifier = (name) => {
|
|
288
|
+
return programPath.scope.hasBinding(name);
|
|
289
|
+
};
|
|
290
|
+
const addRouteHmr = (insertionPath, routeOptions) => {
|
|
291
|
+
if (!opts.addHmr || hmrAdded) return;
|
|
292
|
+
opts.compilerPlugins?.forEach((plugin) => {
|
|
293
|
+
if (
|
|
294
|
+
plugin.onAddHmr?.({
|
|
295
|
+
programPath,
|
|
296
|
+
callExpressionPath: path,
|
|
297
|
+
insertionPath,
|
|
298
|
+
routeOptions,
|
|
299
|
+
createRouteFn,
|
|
300
|
+
opts,
|
|
301
|
+
})?.modified
|
|
302
|
+
)
|
|
303
|
+
modified = true;
|
|
304
|
+
});
|
|
305
|
+
modified = true;
|
|
306
|
+
hmrAdded = true;
|
|
307
|
+
};
|
|
308
|
+
if (t.isObjectExpression(routeOptions)) {
|
|
309
|
+
const insertionPath = path.getStatementParent() ?? path;
|
|
310
|
+
opts.compilerPlugins?.forEach((plugin) => {
|
|
311
|
+
if (
|
|
312
|
+
plugin.onRouteOptions?.({
|
|
313
|
+
programPath,
|
|
314
|
+
callExpressionPath: path,
|
|
315
|
+
insertionPath,
|
|
316
|
+
routeOptions,
|
|
317
|
+
createRouteFn,
|
|
318
|
+
opts,
|
|
319
|
+
})?.modified
|
|
320
|
+
)
|
|
321
|
+
modified = true;
|
|
322
|
+
});
|
|
323
|
+
if (opts.deleteNodes && opts.deleteNodes.size > 0)
|
|
324
|
+
routeOptions.properties = routeOptions.properties.filter((prop) => {
|
|
325
|
+
if (t.isObjectProperty(prop)) {
|
|
326
|
+
const key = getObjectPropertyKeyName(prop);
|
|
327
|
+
if (key && opts.deleteNodes.has(key)) {
|
|
328
|
+
modified = true;
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return true;
|
|
333
|
+
});
|
|
334
|
+
if (!splittableCreateRouteFns.includes(createRouteFn)) {
|
|
335
|
+
opts.compilerPlugins?.forEach((plugin) => {
|
|
336
|
+
if (
|
|
337
|
+
plugin.onUnsplittableRoute?.({
|
|
338
|
+
programPath,
|
|
339
|
+
callExpressionPath: path,
|
|
340
|
+
insertionPath,
|
|
341
|
+
routeOptions,
|
|
342
|
+
createRouteFn,
|
|
343
|
+
opts,
|
|
344
|
+
})?.modified
|
|
345
|
+
)
|
|
346
|
+
modified = true;
|
|
347
|
+
});
|
|
348
|
+
addRouteHmr(insertionPath, routeOptions);
|
|
349
|
+
return programPath.stop();
|
|
350
|
+
}
|
|
351
|
+
routeOptions.properties.forEach((prop) => {
|
|
352
|
+
if (t.isObjectProperty(prop)) {
|
|
353
|
+
const key = getObjectPropertyKeyName(prop);
|
|
354
|
+
if (key) {
|
|
355
|
+
const codeSplitGroupingByKey = findIndexForSplitNode(key);
|
|
356
|
+
if (codeSplitGroupingByKey === -1) return;
|
|
357
|
+
const codeSplitGroup = [
|
|
358
|
+
...new Set(opts.codeSplitGroupings[codeSplitGroupingByKey]),
|
|
359
|
+
];
|
|
360
|
+
if (!SPLIT_NODES_CONFIG.has(key)) return;
|
|
361
|
+
if (
|
|
362
|
+
t.isBooleanLiteral(prop.value) ||
|
|
363
|
+
t.isNullLiteral(prop.value) ||
|
|
364
|
+
(t.isIdentifier(prop.value) && prop.value.name === 'undefined')
|
|
365
|
+
)
|
|
366
|
+
return;
|
|
367
|
+
const splitNodeMeta = SPLIT_NODES_CONFIG.get(key);
|
|
368
|
+
const splitUrl = addSplitSearchParamToFilename(opts.filename, codeSplitGroup);
|
|
369
|
+
if (splitNodeMeta.splitStrategy === 'lazyRouteComponent') {
|
|
370
|
+
const value = prop.value;
|
|
371
|
+
let shouldSplit = true;
|
|
372
|
+
if (t.isIdentifier(value)) {
|
|
373
|
+
const existingImportPath = getImportSpecifierAndPathFromLocalName(
|
|
374
|
+
programPath,
|
|
375
|
+
value.name,
|
|
376
|
+
).path;
|
|
377
|
+
if (existingImportPath) removableImportPaths.add(existingImportPath);
|
|
378
|
+
const isExported = hasExport(ast, value);
|
|
379
|
+
if (isExported) knownExportedIdents.add(value.name);
|
|
380
|
+
shouldSplit = !isExported;
|
|
381
|
+
if (shouldSplit) removeIdentifierLiteral(path, value);
|
|
382
|
+
}
|
|
383
|
+
if (!shouldSplit) return;
|
|
384
|
+
modified = true;
|
|
385
|
+
if (!hasImportedOrDefinedIdentifier(LAZY_ROUTE_COMPONENT_IDENT))
|
|
386
|
+
programPath.unshiftContainer('body', [
|
|
387
|
+
template.statement(
|
|
388
|
+
`import { ${LAZY_ROUTE_COMPONENT_IDENT} } from '${PACKAGE}'`,
|
|
389
|
+
)(),
|
|
390
|
+
]);
|
|
391
|
+
if (!hasImportedOrDefinedIdentifier(splitNodeMeta.localImporterIdent))
|
|
392
|
+
programPath.unshiftContainer('body', [
|
|
393
|
+
template.statement(
|
|
394
|
+
`const ${splitNodeMeta.localImporterIdent} = () => import('${splitUrl}')`,
|
|
395
|
+
)(),
|
|
396
|
+
]);
|
|
397
|
+
const insertionPath = path.getStatementParent() ?? path;
|
|
398
|
+
let splitPropValue;
|
|
399
|
+
for (const plugin of opts.compilerPlugins ?? []) {
|
|
400
|
+
const pluginPropValue = plugin.onSplitRouteProperty?.({
|
|
401
|
+
programPath,
|
|
402
|
+
callExpressionPath: path,
|
|
403
|
+
insertionPath,
|
|
404
|
+
routeOptions,
|
|
405
|
+
prop,
|
|
406
|
+
splitNodeMeta,
|
|
407
|
+
lazyRouteComponentIdent: LAZY_ROUTE_COMPONENT_IDENT,
|
|
408
|
+
opts,
|
|
409
|
+
});
|
|
410
|
+
if (!pluginPropValue) continue;
|
|
411
|
+
modified = true;
|
|
412
|
+
splitPropValue = pluginPropValue;
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
if (splitPropValue) prop.value = splitPropValue;
|
|
416
|
+
else
|
|
417
|
+
prop.value = template.expression(
|
|
418
|
+
`${LAZY_ROUTE_COMPONENT_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}')`,
|
|
419
|
+
)();
|
|
420
|
+
addRouteHmr(insertionPath, routeOptions);
|
|
421
|
+
} else {
|
|
422
|
+
const value = prop.value;
|
|
423
|
+
let shouldSplit = true;
|
|
424
|
+
if (t.isIdentifier(value)) {
|
|
425
|
+
const existingImportPath = getImportSpecifierAndPathFromLocalName(
|
|
426
|
+
programPath,
|
|
427
|
+
value.name,
|
|
428
|
+
).path;
|
|
429
|
+
if (existingImportPath) removableImportPaths.add(existingImportPath);
|
|
430
|
+
const isExported = hasExport(ast, value);
|
|
431
|
+
if (isExported) knownExportedIdents.add(value.name);
|
|
432
|
+
shouldSplit = !isExported;
|
|
433
|
+
if (shouldSplit) removeIdentifierLiteral(path, value);
|
|
434
|
+
}
|
|
435
|
+
if (!shouldSplit) return;
|
|
436
|
+
modified = true;
|
|
437
|
+
if (!hasImportedOrDefinedIdentifier(LAZY_FN_IDENT))
|
|
438
|
+
programPath.unshiftContainer(
|
|
439
|
+
'body',
|
|
440
|
+
template.smart(`import { ${LAZY_FN_IDENT} } from '${PACKAGE}'`)(),
|
|
441
|
+
);
|
|
442
|
+
if (!hasImportedOrDefinedIdentifier(splitNodeMeta.localImporterIdent))
|
|
443
|
+
programPath.unshiftContainer('body', [
|
|
444
|
+
template.statement(
|
|
445
|
+
`const ${splitNodeMeta.localImporterIdent} = () => import('${splitUrl}')`,
|
|
446
|
+
)(),
|
|
447
|
+
]);
|
|
448
|
+
prop.value = template.expression(
|
|
449
|
+
`${LAZY_FN_IDENT}(${splitNodeMeta.localImporterIdent}, '${splitNodeMeta.exporterIdent}')`,
|
|
450
|
+
)();
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
programPath.scope.crawl();
|
|
455
|
+
});
|
|
456
|
+
addRouteHmr(insertionPath, routeOptions);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
if (t.isCallExpression(path.parentPath.node))
|
|
460
|
+
babelHandleReference(resolveIdentifier(path, path.parentPath.node.arguments[0]));
|
|
461
|
+
else if (t.isVariableDeclarator(path.parentPath.node)) {
|
|
462
|
+
const caller = resolveIdentifier(path, path.parentPath.node.init);
|
|
463
|
+
if (t.isCallExpression(caller))
|
|
464
|
+
babelHandleReference(resolveIdentifier(path, caller.arguments[0]));
|
|
465
|
+
}
|
|
466
|
+
},
|
|
467
|
+
});
|
|
468
|
+
/**
|
|
469
|
+
* If the component for the route is being imported,
|
|
470
|
+
* and it's not being used, remove the import statement
|
|
471
|
+
* from the program, by checking that the import has no
|
|
472
|
+
* specifiers
|
|
473
|
+
*/
|
|
474
|
+
if (removableImportPaths.size > 0) {
|
|
475
|
+
modified = true;
|
|
476
|
+
programPath.traverse({
|
|
477
|
+
ImportDeclaration(path) {
|
|
478
|
+
if (path.node.specifiers.length > 0) return;
|
|
479
|
+
if (removableImportPaths.has(path.node.source.value)) path.remove();
|
|
480
|
+
},
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
if (opts.sharedBindings && opts.sharedBindings.size > 0) {
|
|
484
|
+
sharedExportedNames = findExportedSharedBindings(ast, opts.sharedBindings);
|
|
485
|
+
removeSharedDeclarations(ast, opts.sharedBindings);
|
|
486
|
+
const sharedModuleUrl = addSharedSearchParamToFilename(opts.filename);
|
|
487
|
+
const sharedImportSpecifiers = [...opts.sharedBindings].map((name) =>
|
|
488
|
+
t.importSpecifier(t.identifier(name), t.identifier(name)),
|
|
489
|
+
);
|
|
490
|
+
const [sharedImportPath] = programPath.unshiftContainer(
|
|
491
|
+
'body',
|
|
492
|
+
t.importDeclaration(sharedImportSpecifiers, t.stringLiteral(sharedModuleUrl)),
|
|
493
|
+
);
|
|
494
|
+
sharedImportPath.traverse({
|
|
495
|
+
Identifier(identPath) {
|
|
496
|
+
if (identPath.parentPath.isImportSpecifier() && identPath.key === 'local')
|
|
497
|
+
refIdents.add(identPath);
|
|
498
|
+
},
|
|
499
|
+
});
|
|
500
|
+
if (sharedExportedNames.size > 0) {
|
|
501
|
+
const reExportSpecifiers = [...sharedExportedNames].map((name) =>
|
|
502
|
+
t.exportSpecifier(t.identifier(name), t.identifier(name)),
|
|
503
|
+
);
|
|
504
|
+
programPath.pushContainer(
|
|
505
|
+
'body',
|
|
506
|
+
t.exportNamedDeclaration(null, reExportSpecifiers, t.stringLiteral(sharedModuleUrl)),
|
|
507
|
+
);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
},
|
|
511
|
+
},
|
|
512
|
+
});
|
|
513
|
+
if (!modified) return null;
|
|
514
|
+
deadCodeElimination(ast, refIdents);
|
|
515
|
+
if (knownExportedIdents.size > 0) {
|
|
516
|
+
const warningMessage = createNotExportableMessage(opts.filename, knownExportedIdents);
|
|
517
|
+
console.warn(warningMessage);
|
|
518
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
519
|
+
const warningTemplate = template.statement(
|
|
520
|
+
`console.warn(${JSON.stringify(warningMessage)})`,
|
|
521
|
+
)();
|
|
522
|
+
ast.program.body.unshift(warningTemplate);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
if (hmrAdded) {
|
|
526
|
+
const signatureSource = getRouteHmrSignatureSource(ast, opts.filename);
|
|
527
|
+
ast.program.body.push(
|
|
528
|
+
...createRouteHmrStatement(stableRouteOptionKeys, {
|
|
529
|
+
hmrStyle: opts.hmrStyle ?? 'vite',
|
|
530
|
+
targetFramework: opts.targetFramework,
|
|
531
|
+
routeId: opts.hmrRouteId,
|
|
532
|
+
routeSignature: hashRouteHmrSignature(signatureSource),
|
|
533
|
+
}),
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
const result = generateFromAst(ast, {
|
|
537
|
+
sourceMaps: true,
|
|
538
|
+
sourceFileName: opts.filename,
|
|
539
|
+
filename: opts.filename,
|
|
540
|
+
});
|
|
541
|
+
if (result.map) result.map.sourcesContent = [opts.code];
|
|
542
|
+
return result;
|
|
543
|
+
}
|
|
544
|
+
function compileCodeSplitVirtualRoute(opts) {
|
|
545
|
+
const ast = parseAst(opts);
|
|
546
|
+
const refIdents = findReferencedIdentifiers(ast);
|
|
547
|
+
if (opts.sharedBindings && opts.sharedBindings.size > 0)
|
|
548
|
+
removeSharedDeclarations(ast, opts.sharedBindings);
|
|
549
|
+
const intendedSplitNodes = new Set(opts.splitTargets);
|
|
550
|
+
const knownExportedIdents = /* @__PURE__ */ new Set();
|
|
551
|
+
babel.traverse(ast, {
|
|
552
|
+
Program: {
|
|
553
|
+
enter(programPath) {
|
|
554
|
+
const trackedNodesToSplitByType = {
|
|
555
|
+
component: void 0,
|
|
556
|
+
loader: void 0,
|
|
557
|
+
pendingComponent: void 0,
|
|
558
|
+
errorComponent: void 0,
|
|
559
|
+
notFoundComponent: void 0,
|
|
560
|
+
};
|
|
561
|
+
programPath.traverse({
|
|
562
|
+
CallExpression: (path) => {
|
|
563
|
+
if (!t.isIdentifier(path.node.callee)) return;
|
|
564
|
+
if (!splittableCreateRouteFns.includes(path.node.callee.name)) return;
|
|
565
|
+
function babelHandleVirtual(options) {
|
|
566
|
+
if (t.isObjectExpression(options)) {
|
|
567
|
+
options.properties.forEach((prop) => {
|
|
568
|
+
if (t.isObjectProperty(prop))
|
|
569
|
+
KNOWN_SPLIT_ROUTE_IDENTS.forEach((splitType) => {
|
|
570
|
+
if (getObjectPropertyKeyName(prop) !== splitType) return;
|
|
571
|
+
const value = prop.value;
|
|
572
|
+
if (t.isIdentifier(value) && value.name === 'undefined') return;
|
|
573
|
+
let isExported = false;
|
|
574
|
+
if (t.isIdentifier(value)) {
|
|
575
|
+
isExported = hasExport(ast, value);
|
|
576
|
+
if (isExported) knownExportedIdents.add(value.name);
|
|
577
|
+
}
|
|
578
|
+
if (isExported && t.isIdentifier(value)) removeExports(ast, value);
|
|
579
|
+
else {
|
|
580
|
+
const meta = SPLIT_NODES_CONFIG.get(splitType);
|
|
581
|
+
trackedNodesToSplitByType[splitType] = {
|
|
582
|
+
node: prop.value,
|
|
583
|
+
meta,
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
});
|
|
588
|
+
options.properties = [];
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
if (t.isCallExpression(path.parentPath.node))
|
|
592
|
+
babelHandleVirtual(resolveIdentifier(path, path.parentPath.node.arguments[0]));
|
|
593
|
+
else if (t.isVariableDeclarator(path.parentPath.node)) {
|
|
594
|
+
const caller = resolveIdentifier(path, path.parentPath.node.init);
|
|
595
|
+
if (t.isCallExpression(caller))
|
|
596
|
+
babelHandleVirtual(resolveIdentifier(path, caller.arguments[0]));
|
|
597
|
+
}
|
|
598
|
+
},
|
|
599
|
+
});
|
|
600
|
+
intendedSplitNodes.forEach((SPLIT_TYPE) => {
|
|
601
|
+
const splitKey = trackedNodesToSplitByType[SPLIT_TYPE];
|
|
602
|
+
if (!splitKey) return;
|
|
603
|
+
let splitNode = splitKey.node;
|
|
604
|
+
const splitMeta = {
|
|
605
|
+
...splitKey.meta,
|
|
606
|
+
shouldRemoveNode: true,
|
|
607
|
+
};
|
|
608
|
+
let originalIdentName;
|
|
609
|
+
if (t.isIdentifier(splitNode)) originalIdentName = splitNode.name;
|
|
610
|
+
while (t.isIdentifier(splitNode))
|
|
611
|
+
splitNode = programPath.scope.getBinding(splitNode.name)?.path.node;
|
|
612
|
+
if (splitNode)
|
|
613
|
+
if (t.isFunctionDeclaration(splitNode)) {
|
|
614
|
+
if (!splitNode.id)
|
|
615
|
+
throw new Error(
|
|
616
|
+
`Function declaration for "${SPLIT_TYPE}" must have an identifier.`,
|
|
617
|
+
);
|
|
618
|
+
splitMeta.shouldRemoveNode = false;
|
|
619
|
+
splitMeta.localExporterIdent = splitNode.id.name;
|
|
620
|
+
} else if (t.isFunctionExpression(splitNode) || t.isArrowFunctionExpression(splitNode))
|
|
621
|
+
programPath.pushContainer(
|
|
622
|
+
'body',
|
|
623
|
+
t.variableDeclaration('const', [
|
|
624
|
+
t.variableDeclarator(t.identifier(splitMeta.localExporterIdent), splitNode),
|
|
625
|
+
]),
|
|
626
|
+
);
|
|
627
|
+
else if (t.isImportSpecifier(splitNode) || t.isImportDefaultSpecifier(splitNode))
|
|
628
|
+
programPath.pushContainer(
|
|
629
|
+
'body',
|
|
630
|
+
t.variableDeclaration('const', [
|
|
631
|
+
t.variableDeclarator(t.identifier(splitMeta.localExporterIdent), splitNode.local),
|
|
632
|
+
]),
|
|
633
|
+
);
|
|
634
|
+
else if (t.isVariableDeclarator(splitNode))
|
|
635
|
+
if (t.isIdentifier(splitNode.id)) {
|
|
636
|
+
splitMeta.localExporterIdent = splitNode.id.name;
|
|
637
|
+
splitMeta.shouldRemoveNode = false;
|
|
638
|
+
} else if (t.isObjectPattern(splitNode.id)) {
|
|
639
|
+
if (originalIdentName) splitMeta.localExporterIdent = originalIdentName;
|
|
640
|
+
splitMeta.shouldRemoveNode = false;
|
|
641
|
+
} else throw new Error(`Unexpected splitNode type ☝️: ${splitNode.type}`);
|
|
642
|
+
else if (t.isCallExpression(splitNode)) {
|
|
643
|
+
const outputSplitNodeCode = generateFromAst(splitNode).code;
|
|
644
|
+
const splitNodeAst = babel.parse(outputSplitNodeCode);
|
|
645
|
+
if (!splitNodeAst)
|
|
646
|
+
throw new Error(
|
|
647
|
+
`Failed to parse the generated code for "${SPLIT_TYPE}" in the node type "${splitNode.type}"`,
|
|
648
|
+
);
|
|
649
|
+
const statement = splitNodeAst.program.body[0];
|
|
650
|
+
if (!statement)
|
|
651
|
+
throw new Error(
|
|
652
|
+
`Failed to parse the generated code for "${SPLIT_TYPE}" in the node type "${splitNode.type}" as no statement was found in the program body`,
|
|
653
|
+
);
|
|
654
|
+
if (t.isExpressionStatement(statement)) {
|
|
655
|
+
const expression = statement.expression;
|
|
656
|
+
programPath.pushContainer(
|
|
657
|
+
'body',
|
|
658
|
+
t.variableDeclaration('const', [
|
|
659
|
+
t.variableDeclarator(t.identifier(splitMeta.localExporterIdent), expression),
|
|
660
|
+
]),
|
|
661
|
+
);
|
|
662
|
+
} else
|
|
663
|
+
throw new Error(
|
|
664
|
+
`Unexpected expression type encounter for "${SPLIT_TYPE}" in the node type "${splitNode.type}"`,
|
|
665
|
+
);
|
|
666
|
+
} else if (t.isConditionalExpression(splitNode))
|
|
667
|
+
programPath.pushContainer(
|
|
668
|
+
'body',
|
|
669
|
+
t.variableDeclaration('const', [
|
|
670
|
+
t.variableDeclarator(t.identifier(splitMeta.localExporterIdent), splitNode),
|
|
671
|
+
]),
|
|
672
|
+
);
|
|
673
|
+
else if (t.isTSAsExpression(splitNode)) {
|
|
674
|
+
splitNode = splitNode.expression;
|
|
675
|
+
programPath.pushContainer(
|
|
676
|
+
'body',
|
|
677
|
+
t.variableDeclaration('const', [
|
|
678
|
+
t.variableDeclarator(t.identifier(splitMeta.localExporterIdent), splitNode),
|
|
679
|
+
]),
|
|
680
|
+
);
|
|
681
|
+
} else if (t.isBooleanLiteral(splitNode)) return;
|
|
682
|
+
else if (t.isNullLiteral(splitNode)) return;
|
|
683
|
+
else {
|
|
684
|
+
console.info('Unexpected splitNode type:', splitNode);
|
|
685
|
+
throw new Error(`Unexpected splitNode type ☝️: ${splitNode.type}`);
|
|
686
|
+
}
|
|
687
|
+
if (splitMeta.shouldRemoveNode)
|
|
688
|
+
programPath.node.body = programPath.node.body.filter((node) => {
|
|
689
|
+
return node !== splitNode;
|
|
690
|
+
});
|
|
691
|
+
for (const plugin of opts.compilerPlugins ?? []) {
|
|
692
|
+
const pluginExporterIdent = plugin.onExportSplitRouteProperty?.({
|
|
693
|
+
programPath,
|
|
694
|
+
splitNodeMeta: splitMeta,
|
|
695
|
+
localExporterIdent: splitMeta.localExporterIdent,
|
|
696
|
+
});
|
|
697
|
+
if (pluginExporterIdent) splitMeta.localExporterIdent = pluginExporterIdent.name;
|
|
698
|
+
}
|
|
699
|
+
programPath.pushContainer('body', [
|
|
700
|
+
t.exportNamedDeclaration(null, [
|
|
701
|
+
t.exportSpecifier(
|
|
702
|
+
t.identifier(splitMeta.localExporterIdent),
|
|
703
|
+
t.identifier(splitMeta.exporterIdent),
|
|
704
|
+
),
|
|
705
|
+
]),
|
|
706
|
+
]);
|
|
707
|
+
});
|
|
708
|
+
programPath.traverse({
|
|
709
|
+
ExportNamedDeclaration(path) {
|
|
710
|
+
if (path.node.declaration) {
|
|
711
|
+
if (t.isVariableDeclaration(path.node.declaration)) {
|
|
712
|
+
const specifiers = path.node.declaration.declarations.flatMap((decl) => {
|
|
713
|
+
if (t.isIdentifier(decl.id))
|
|
714
|
+
return [
|
|
715
|
+
t.importSpecifier(t.identifier(decl.id.name), t.identifier(decl.id.name)),
|
|
716
|
+
];
|
|
717
|
+
if (t.isObjectPattern(decl.id))
|
|
718
|
+
return collectIdentifiersFromPattern(decl.id).map((name) =>
|
|
719
|
+
t.importSpecifier(t.identifier(name), t.identifier(name)),
|
|
720
|
+
);
|
|
721
|
+
if (t.isArrayPattern(decl.id))
|
|
722
|
+
return collectIdentifiersFromPattern(decl.id).map((name) =>
|
|
723
|
+
t.importSpecifier(t.identifier(name), t.identifier(name)),
|
|
724
|
+
);
|
|
725
|
+
return [];
|
|
726
|
+
});
|
|
727
|
+
if (specifiers.length === 0) {
|
|
728
|
+
path.remove();
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
const importDecl = t.importDeclaration(
|
|
732
|
+
specifiers,
|
|
733
|
+
t.stringLiteral(removeSplitSearchParamFromFilename(opts.filename)),
|
|
734
|
+
);
|
|
735
|
+
path.replaceWith(importDecl);
|
|
736
|
+
path.traverse({
|
|
737
|
+
Identifier(identPath) {
|
|
738
|
+
if (identPath.parentPath.isImportSpecifier() && identPath.key === 'local')
|
|
739
|
+
refIdents.add(identPath);
|
|
740
|
+
},
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
},
|
|
745
|
+
});
|
|
746
|
+
if (opts.sharedBindings && opts.sharedBindings.size > 0) {
|
|
747
|
+
const sharedImportSpecifiers = [...opts.sharedBindings].map((name) =>
|
|
748
|
+
t.importSpecifier(t.identifier(name), t.identifier(name)),
|
|
749
|
+
);
|
|
750
|
+
const sharedModuleUrl = addSharedSearchParamToFilename(
|
|
751
|
+
removeSplitSearchParamFromFilename(opts.filename),
|
|
752
|
+
);
|
|
753
|
+
const [sharedImportPath] = programPath.unshiftContainer(
|
|
754
|
+
'body',
|
|
755
|
+
t.importDeclaration(sharedImportSpecifiers, t.stringLiteral(sharedModuleUrl)),
|
|
756
|
+
);
|
|
757
|
+
sharedImportPath.traverse({
|
|
758
|
+
Identifier(identPath) {
|
|
759
|
+
if (identPath.parentPath.isImportSpecifier() && identPath.key === 'local')
|
|
760
|
+
refIdents.add(identPath);
|
|
761
|
+
},
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
},
|
|
765
|
+
},
|
|
766
|
+
});
|
|
767
|
+
deadCodeElimination(ast, refIdents);
|
|
768
|
+
stripUnreferencedTopLevelExpressionStatements(ast);
|
|
769
|
+
if (ast.program.body.length === 0) ast.program.directives = [];
|
|
770
|
+
const result = generateFromAst(ast, {
|
|
771
|
+
sourceMaps: true,
|
|
772
|
+
sourceFileName: opts.filename,
|
|
773
|
+
filename: opts.filename,
|
|
774
|
+
});
|
|
775
|
+
if (result.map) result.map.sourcesContent = [opts.code];
|
|
776
|
+
return result;
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Compile the shared virtual module (`?tsr-shared=1`).
|
|
780
|
+
* Keeps only shared binding declarations, their transitive dependencies,
|
|
781
|
+
* and imports they need. Exports all shared bindings.
|
|
782
|
+
*/
|
|
783
|
+
function compileCodeSplitSharedRoute(opts) {
|
|
784
|
+
const ast = parseAst(opts);
|
|
785
|
+
const refIdents = findReferencedIdentifiers(ast);
|
|
786
|
+
const localBindings = /* @__PURE__ */ new Set();
|
|
787
|
+
for (const node of ast.program.body) collectLocalBindingsFromStatement(node, localBindings);
|
|
788
|
+
localBindings.delete('Route');
|
|
789
|
+
const depGraph = buildDependencyGraph(buildDeclarationMap(ast), localBindings);
|
|
790
|
+
const keepBindings = new Set(opts.sharedBindings);
|
|
791
|
+
keepBindings.delete('Route');
|
|
792
|
+
expandTransitively(keepBindings, depGraph);
|
|
793
|
+
retainModuleLevelDeclarations(ast, keepBindings);
|
|
794
|
+
unwrapExportedDeclarations(ast);
|
|
795
|
+
const exportSpecifiers = [...opts.sharedBindings]
|
|
796
|
+
.sort((a, b) => a.localeCompare(b))
|
|
797
|
+
.map((name) => t.exportSpecifier(t.identifier(name), t.identifier(name)));
|
|
798
|
+
if (exportSpecifiers.length > 0) {
|
|
799
|
+
const exportDecl = t.exportNamedDeclaration(null, exportSpecifiers);
|
|
800
|
+
ast.program.body.push(exportDecl);
|
|
801
|
+
babel.traverse(ast, {
|
|
802
|
+
Program(programPath) {
|
|
803
|
+
const bodyPaths = programPath.get('body');
|
|
804
|
+
const last = bodyPaths[bodyPaths.length - 1];
|
|
805
|
+
if (last && last.isExportNamedDeclaration())
|
|
806
|
+
last.traverse({
|
|
807
|
+
Identifier(identPath) {
|
|
808
|
+
if (identPath.parentPath.isExportSpecifier() && identPath.key === 'local')
|
|
809
|
+
refIdents.add(identPath);
|
|
810
|
+
},
|
|
811
|
+
});
|
|
812
|
+
programPath.stop();
|
|
813
|
+
},
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
deadCodeElimination(ast, refIdents);
|
|
817
|
+
if (ast.program.body.length === 0) ast.program.directives = [];
|
|
818
|
+
const result = generateFromAst(ast, {
|
|
819
|
+
sourceMaps: true,
|
|
820
|
+
sourceFileName: opts.filename,
|
|
821
|
+
filename: opts.filename,
|
|
822
|
+
});
|
|
823
|
+
if (result.map) result.map.sourcesContent = [opts.code];
|
|
824
|
+
return result;
|
|
825
|
+
}
|
|
826
|
+
/**
|
|
827
|
+
* This function should read get the options from by searching for the key `codeSplitGroupings`
|
|
828
|
+
* on createFileRoute and return it's values if it exists, else return undefined
|
|
829
|
+
*/
|
|
830
|
+
function detectCodeSplitGroupingsFromRoute(opts) {
|
|
831
|
+
const ast = parseAst(opts);
|
|
832
|
+
let codeSplitGroupings = void 0;
|
|
833
|
+
babel.traverse(ast, {
|
|
834
|
+
Program: {
|
|
835
|
+
enter(programPath) {
|
|
836
|
+
programPath.traverse({
|
|
837
|
+
CallExpression(path) {
|
|
838
|
+
if (!t.isIdentifier(path.node.callee)) return;
|
|
839
|
+
if (
|
|
840
|
+
!(
|
|
841
|
+
path.node.callee.name === 'createRoute' ||
|
|
842
|
+
path.node.callee.name === 'createFileRoute'
|
|
843
|
+
)
|
|
844
|
+
)
|
|
845
|
+
return;
|
|
846
|
+
function babelHandleSplittingGroups(routeOptions) {
|
|
847
|
+
if (t.isObjectExpression(routeOptions))
|
|
848
|
+
routeOptions.properties.forEach((prop) => {
|
|
849
|
+
if (t.isObjectProperty(prop)) {
|
|
850
|
+
if (getObjectPropertyKeyName(prop) === 'codeSplitGroupings') {
|
|
851
|
+
const value = prop.value;
|
|
852
|
+
if (t.isArrayExpression(value))
|
|
853
|
+
codeSplitGroupings = value.elements.map((group) => {
|
|
854
|
+
if (t.isArrayExpression(group))
|
|
855
|
+
return group.elements.map((node) => {
|
|
856
|
+
if (!t.isStringLiteral(node))
|
|
857
|
+
throw new Error(
|
|
858
|
+
'You must provide a string literal for the codeSplitGroupings',
|
|
859
|
+
);
|
|
860
|
+
return node.value;
|
|
861
|
+
});
|
|
862
|
+
throw new Error(
|
|
863
|
+
'You must provide arrays with codeSplitGroupings options.',
|
|
864
|
+
);
|
|
865
|
+
});
|
|
866
|
+
else
|
|
867
|
+
throw new Error(
|
|
868
|
+
'You must provide an array of arrays for the codeSplitGroupings.',
|
|
869
|
+
);
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
if (t.isCallExpression(path.parentPath.node))
|
|
875
|
+
babelHandleSplittingGroups(
|
|
876
|
+
resolveIdentifier(path, path.parentPath.node.arguments[0]),
|
|
877
|
+
);
|
|
878
|
+
else if (t.isVariableDeclarator(path.parentPath.node)) {
|
|
879
|
+
const caller = resolveIdentifier(path, path.parentPath.node.init);
|
|
880
|
+
if (t.isCallExpression(caller))
|
|
881
|
+
babelHandleSplittingGroups(resolveIdentifier(path, caller.arguments[0]));
|
|
882
|
+
}
|
|
883
|
+
},
|
|
884
|
+
});
|
|
885
|
+
},
|
|
886
|
+
},
|
|
887
|
+
});
|
|
888
|
+
return { groupings: codeSplitGroupings };
|
|
889
|
+
}
|
|
890
|
+
function createNotExportableMessage(filename, idents) {
|
|
891
|
+
const list = Array.from(idents).map((d) => `- ${d}`);
|
|
892
|
+
return [
|
|
893
|
+
`[tanstack-router] These exports from "${filename}" will not be code-split and will increase your bundle size:`,
|
|
894
|
+
...list,
|
|
895
|
+
'For the best optimization, these items should either have their export statements removed, or be imported from another location that is not a route file.',
|
|
896
|
+
].join('\n');
|
|
897
|
+
}
|
|
898
|
+
function getImportSpecifierAndPathFromLocalName(programPath, name) {
|
|
899
|
+
let specifier = null;
|
|
900
|
+
let path = null;
|
|
901
|
+
programPath.traverse({
|
|
902
|
+
ImportDeclaration(importPath) {
|
|
903
|
+
const found = importPath.node.specifiers.find(
|
|
904
|
+
(targetSpecifier) => targetSpecifier.local.name === name,
|
|
905
|
+
);
|
|
906
|
+
if (found) {
|
|
907
|
+
specifier = found;
|
|
908
|
+
path = importPath.node.source.value;
|
|
909
|
+
}
|
|
910
|
+
},
|
|
911
|
+
});
|
|
912
|
+
return {
|
|
913
|
+
specifier,
|
|
914
|
+
path,
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
function resolveIdentifier(path, node) {
|
|
918
|
+
if (t.isIdentifier(node)) {
|
|
919
|
+
const binding = path.scope.getBinding(node.name);
|
|
920
|
+
if (binding) {
|
|
921
|
+
const declarator = binding.path.node;
|
|
922
|
+
if (t.isObjectExpression(declarator.init)) return declarator.init;
|
|
923
|
+
else if (t.isFunctionDeclaration(declarator.init)) return declarator.init;
|
|
924
|
+
}
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
return node;
|
|
928
|
+
}
|
|
929
|
+
function removeIdentifierLiteral(path, node) {
|
|
930
|
+
const binding = path.scope.getBinding(node.name);
|
|
931
|
+
if (binding) {
|
|
932
|
+
if (t.isVariableDeclarator(binding.path.node) && t.isObjectPattern(binding.path.node.id)) {
|
|
933
|
+
const objectPattern = binding.path.node.id;
|
|
934
|
+
objectPattern.properties = objectPattern.properties.filter((prop) => {
|
|
935
|
+
if (!t.isObjectProperty(prop)) return true;
|
|
936
|
+
if (t.isIdentifier(prop.value) && prop.value.name === node.name) return false;
|
|
937
|
+
if (
|
|
938
|
+
t.isAssignmentPattern(prop.value) &&
|
|
939
|
+
t.isIdentifier(prop.value.left) &&
|
|
940
|
+
prop.value.left.name === node.name
|
|
941
|
+
)
|
|
942
|
+
return false;
|
|
943
|
+
return true;
|
|
944
|
+
});
|
|
945
|
+
if (objectPattern.properties.length === 0) binding.path.remove();
|
|
946
|
+
return;
|
|
947
|
+
}
|
|
948
|
+
binding.path.remove();
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
function hasExport(ast, node) {
|
|
952
|
+
let found = false;
|
|
953
|
+
babel.traverse(ast, {
|
|
954
|
+
ExportNamedDeclaration(path) {
|
|
955
|
+
if (path.node.declaration) {
|
|
956
|
+
if (t.isVariableDeclaration(path.node.declaration))
|
|
957
|
+
path.node.declaration.declarations.forEach((decl) => {
|
|
958
|
+
if (t.isVariableDeclarator(decl)) {
|
|
959
|
+
if (t.isIdentifier(decl.id)) {
|
|
960
|
+
if (decl.id.name === node.name) found = true;
|
|
961
|
+
} else if (t.isObjectPattern(decl.id) || t.isArrayPattern(decl.id)) {
|
|
962
|
+
if (collectIdentifiersFromPattern(decl.id).includes(node.name)) found = true;
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
});
|
|
966
|
+
if (t.isFunctionDeclaration(path.node.declaration)) {
|
|
967
|
+
if (t.isIdentifier(path.node.declaration.id)) {
|
|
968
|
+
if (path.node.declaration.id.name === node.name) found = true;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
},
|
|
973
|
+
ExportDefaultDeclaration(path) {
|
|
974
|
+
if (t.isIdentifier(path.node.declaration)) {
|
|
975
|
+
if (path.node.declaration.name === node.name) found = true;
|
|
976
|
+
}
|
|
977
|
+
if (t.isFunctionDeclaration(path.node.declaration)) {
|
|
978
|
+
if (t.isIdentifier(path.node.declaration.id)) {
|
|
979
|
+
if (path.node.declaration.id.name === node.name) found = true;
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
},
|
|
983
|
+
});
|
|
984
|
+
return found;
|
|
985
|
+
}
|
|
986
|
+
function removeExports(ast, node) {
|
|
987
|
+
let removed = false;
|
|
988
|
+
babel.traverse(ast, {
|
|
989
|
+
ExportNamedDeclaration(path) {
|
|
990
|
+
if (path.node.declaration) {
|
|
991
|
+
if (t.isVariableDeclaration(path.node.declaration))
|
|
992
|
+
path.node.declaration.declarations.forEach((decl) => {
|
|
993
|
+
if (t.isVariableDeclarator(decl)) {
|
|
994
|
+
if (t.isIdentifier(decl.id)) {
|
|
995
|
+
if (decl.id.name === node.name) {
|
|
996
|
+
path.remove();
|
|
997
|
+
removed = true;
|
|
998
|
+
}
|
|
999
|
+
} else if (t.isObjectPattern(decl.id) || t.isArrayPattern(decl.id)) {
|
|
1000
|
+
if (collectIdentifiersFromPattern(decl.id).includes(node.name)) {
|
|
1001
|
+
path.remove();
|
|
1002
|
+
removed = true;
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
});
|
|
1007
|
+
else if (t.isFunctionDeclaration(path.node.declaration)) {
|
|
1008
|
+
if (t.isIdentifier(path.node.declaration.id)) {
|
|
1009
|
+
if (path.node.declaration.id.name === node.name) {
|
|
1010
|
+
path.remove();
|
|
1011
|
+
removed = true;
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
},
|
|
1017
|
+
ExportDefaultDeclaration(path) {
|
|
1018
|
+
if (t.isIdentifier(path.node.declaration)) {
|
|
1019
|
+
if (path.node.declaration.name === node.name) {
|
|
1020
|
+
path.remove();
|
|
1021
|
+
removed = true;
|
|
1022
|
+
}
|
|
1023
|
+
} else if (t.isFunctionDeclaration(path.node.declaration)) {
|
|
1024
|
+
if (t.isIdentifier(path.node.declaration.id)) {
|
|
1025
|
+
if (path.node.declaration.id.name === node.name) {
|
|
1026
|
+
path.remove();
|
|
1027
|
+
removed = true;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
},
|
|
1032
|
+
});
|
|
1033
|
+
return removed;
|
|
1034
|
+
}
|
|
1035
|
+
//#endregion
|
|
1036
|
+
export {
|
|
1037
|
+
compileCodeSplitReferenceRoute,
|
|
1038
|
+
compileCodeSplitSharedRoute,
|
|
1039
|
+
compileCodeSplitVirtualRoute,
|
|
1040
|
+
computeSharedBindings,
|
|
1041
|
+
detectCodeSplitGroupingsFromRoute,
|
|
1042
|
+
};
|