@tanstack/start-plugin-core 1.121.21 → 1.121.23
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/dist/cjs/plugin.cjs +12 -2
- package/dist/cjs/plugin.cjs.map +1 -1
- package/dist/cjs/start-manifest-plugin/plugin.cjs +77 -72
- package/dist/cjs/start-manifest-plugin/plugin.cjs.map +1 -1
- package/dist/cjs/start-manifest-plugin/plugin.d.cts +3 -1
- package/dist/cjs/start-router-plugin/generator-plugins/routes-manifest-plugin.cjs +2 -2
- package/dist/cjs/start-router-plugin/generator-plugins/routes-manifest-plugin.cjs.map +1 -1
- package/dist/esm/plugin.js +12 -2
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/start-manifest-plugin/plugin.d.ts +3 -1
- package/dist/esm/start-manifest-plugin/plugin.js +77 -72
- package/dist/esm/start-manifest-plugin/plugin.js.map +1 -1
- package/dist/esm/start-router-plugin/generator-plugins/routes-manifest-plugin.js +2 -2
- package/dist/esm/start-router-plugin/generator-plugins/routes-manifest-plugin.js.map +1 -1
- package/package.json +7 -7
- package/src/global.d.ts +2 -0
- package/src/plugin.ts +10 -0
- package/src/start-manifest-plugin/plugin.ts +93 -99
- package/src/start-router-plugin/generator-plugins/routes-manifest-plugin.ts +2 -2
package/dist/cjs/plugin.cjs
CHANGED
|
@@ -182,9 +182,19 @@ function TanStackStartVitePluginCore(opts, startConfig) {
|
|
|
182
182
|
}
|
|
183
183
|
}),
|
|
184
184
|
plugin$2.loadEnvPlugin(startConfig),
|
|
185
|
-
plugin$3.startManifestPlugin(
|
|
185
|
+
plugin$3.startManifestPlugin(),
|
|
186
186
|
plugin$4.devServerPlugin(),
|
|
187
|
-
plugin$5.nitroPlugin(startConfig, () => ssrBundle)
|
|
187
|
+
plugin$5.nitroPlugin(startConfig, () => ssrBundle),
|
|
188
|
+
{
|
|
189
|
+
name: "tanstack-start:core:capture-client-bundle",
|
|
190
|
+
applyToEnvironment(e) {
|
|
191
|
+
return e.config.consumer === "client";
|
|
192
|
+
},
|
|
193
|
+
enforce: "post",
|
|
194
|
+
generateBundle(_options, bundle) {
|
|
195
|
+
globalThis.TSS_CLIENT_BUNDLE = bundle;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
188
198
|
];
|
|
189
199
|
}
|
|
190
200
|
function defineReplaceEnv(key, value) {
|
package/dist/cjs/plugin.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.cjs","sources":["../../src/plugin.ts"],"sourcesContent":["import path from 'node:path'\nimport { createNitro } from 'nitropack'\nimport { trimPathRight } from '@tanstack/router-core'\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { TanStackServerFnPluginEnv } from '@tanstack/server-functions-plugin'\nimport * as vite from 'vite'\nimport { createTanStackConfig } from './schema'\nimport { nitroPlugin } from './nitro-plugin/plugin'\nimport { startManifestPlugin } from './start-manifest-plugin/plugin'\nimport { startCompilerPlugin } from './start-compiler-plugin'\nimport {\n CLIENT_DIST_DIR,\n SSR_ENTRY_FILE,\n VITE_ENVIRONMENT_NAMES,\n} from './constants'\nimport { tanStackStartRouter } from './start-router-plugin/plugin'\nimport { loadEnvPlugin } from './load-env-plugin/plugin'\nimport { devServerPlugin } from './dev-server-plugin/plugin'\nimport { resolveVirtualEntriesPlugin } from './resolve-virtual-entries-plugin/plugin'\nimport type { createTanStackStartOptionsSchema } from './schema'\nimport type { PluginOption, Rollup } from 'vite'\nimport type { z } from 'zod'\nimport type { CompileStartFrameworkOptions } from './compilers'\n\nexport type TanStackStartInputConfig = z.input<\n ReturnType<typeof createTanStackStartOptionsSchema>\n>\n\nconst defaultConfig = createTanStackConfig()\nexport function getTanStackStartOptions(opts?: TanStackStartInputConfig) {\n return defaultConfig.parse(opts)\n}\n\nexport type TanStackStartOutputConfig = ReturnType<\n typeof getTanStackStartOptions\n>\n\nexport interface TanStackStartVitePluginCoreOptions {\n framework: CompileStartFrameworkOptions\n getVirtualServerRootHandler: (ctx: {\n routerFilepath: string\n serverEntryFilepath: string\n }) => string\n getVirtualServerEntry: (ctx: { routerFilepath: string }) => string\n getVirtualClientEntry: (ctx: { routerFilepath: string }) => string\n}\n// this needs to live outside of the TanStackStartVitePluginCore since it will be invoked multiple times by vite\nlet ssrBundle: Rollup.OutputBundle\n\nexport function TanStackStartVitePluginCore(\n opts: TanStackStartVitePluginCoreOptions,\n startConfig: TanStackStartOutputConfig,\n): Array<PluginOption> {\n return [\n tanStackStartRouter({\n ...startConfig.tsr,\n target: opts.framework,\n autoCodeSplitting: true,\n }),\n resolveVirtualEntriesPlugin(opts, startConfig),\n {\n name: 'tanstack-start-core:config-client',\n async config(viteConfig) {\n const viteAppBase = trimPathRight(viteConfig.base || '/')\n globalThis.TSS_APP_BASE = viteAppBase\n\n const nitroOutputPublicDir = await (async () => {\n // Create a dummy nitro app to get the resolved public output path\n const dummyNitroApp = await createNitro({\n preset: startConfig.target,\n compatibilityDate: '2024-12-01',\n })\n\n const nitroOutputPublicDir = dummyNitroApp.options.output.publicDir\n await dummyNitroApp.close()\n\n return nitroOutputPublicDir\n })()\n\n const getClientEntryPath = (startConfig: TanStackStartOutputConfig) => {\n // when the user specifies a custom client entry path, we need to resolve it\n // relative to the root of the project, keeping in mind that if not specified\n // it will be /~start/default-client-entry which is a virtual path\n // that is resolved by vite to the actual client entry path\n const entry = startConfig.clientEntryPath.startsWith(\n '/~start/default-client-entry',\n )\n ? startConfig.clientEntryPath\n : vite.normalizePath(\n path.join(\n '/@fs',\n path.resolve(startConfig.root, startConfig.clientEntryPath),\n ),\n )\n\n return entry\n }\n\n return {\n base: viteAppBase,\n environments: {\n [VITE_ENVIRONMENT_NAMES.client]: {\n consumer: 'client',\n build: {\n manifest: true,\n rollupOptions: {\n input: {\n main: getClientEntryPath(startConfig),\n },\n output: {\n dir: path.resolve(startConfig.root, CLIENT_DIST_DIR),\n },\n // TODO: this should be removed\n external: ['node:fs', 'node:path', 'node:os', 'node:crypto'],\n },\n },\n },\n [VITE_ENVIRONMENT_NAMES.server]: {\n consumer: 'server',\n build: {\n ssr: true,\n // we don't write to the file system as the below 'capture-output' plugin will\n // capture the output and write it to the virtual file system\n write: false,\n copyPublicDir: false,\n rollupOptions: {\n output: {\n entryFileNames: SSR_ENTRY_FILE,\n },\n plugins: [\n {\n name: 'capture-output',\n generateBundle(_options, bundle) {\n // TODO: can this hook be called more than once?\n ssrBundle = bundle\n },\n },\n ],\n },\n commonjsOptions: {\n include: [/node_modules/],\n },\n },\n },\n },\n resolve: {\n noExternal: [\n '@tanstack/start-client',\n '@tanstack/start-client-core',\n '@tanstack/start-server',\n '@tanstack/start-server-core',\n '@tanstack/start-server-functions-fetcher',\n '@tanstack/start-server-functions-client',\n '@tanstack/start-server-functions-server',\n '@tanstack/start-router-manifest',\n '@tanstack/start-config',\n '@tanstack/server-functions-plugin',\n 'nitropack',\n '@tanstack/**start**',\n ...Object.values(VIRTUAL_MODULES),\n ],\n },\n optimizeDeps: {\n exclude: [...Object.values(VIRTUAL_MODULES)],\n },\n /* prettier-ignore */\n define: {\n // define is an esbuild function that replaces the any instances of given keys with the given values\n // i.e: __FRAMEWORK_NAME__ can be replaced with JSON.stringify(\"TanStack Start\")\n // This is not the same as injecting environment variables.\n\n ...defineReplaceEnv('TSS_CLIENT_ENTRY', getClientEntryPath(startConfig)), // This is consumed by the router-manifest, where the entry point is imported after the dev refresh runtime is resolved\n ...defineReplaceEnv('TSS_SERVER_FN_BASE', startConfig.serverFns.base),\n ...defineReplaceEnv('TSS_OUTPUT_PUBLIC_DIR', nitroOutputPublicDir),\n ...defineReplaceEnv('TSS_APP_BASE', viteAppBase)\n },\n }\n },\n },\n // N.B. TanStackStartCompilerPlugin must be before the TanStackServerFnPluginEnv\n startCompilerPlugin(opts.framework, {\n client: { envName: VITE_ENVIRONMENT_NAMES.client },\n server: { envName: VITE_ENVIRONMENT_NAMES.server },\n }),\n TanStackServerFnPluginEnv({\n // This is the ID that will be available to look up and import\n // our server function manifest and resolve its module\n manifestVirtualImportId: VIRTUAL_MODULES.serverFnManifest,\n manifestOutputFilename: path.join(\n '.tanstack',\n 'start',\n 'build',\n 'server',\n 'server-functions-manifest.json',\n ),\n client: {\n getRuntimeCode: () =>\n `import { createClientRpc } from '@tanstack/${opts.framework}-start/server-functions-client'`,\n replacer: (d) =>\n `createClientRpc('${d.functionId}', '${startConfig.serverFns.base}')`,\n envName: VITE_ENVIRONMENT_NAMES.client,\n },\n server: {\n getRuntimeCode: () =>\n `import { createServerRpc } from '@tanstack/${opts.framework}-start/server-functions-server'`,\n replacer: (d) =>\n `createServerRpc('${d.functionId}', '${startConfig.serverFns.base}', ${d.fn})`,\n envName: VITE_ENVIRONMENT_NAMES.server,\n },\n importer: (fn) => {\n const serverEnv = (globalThis as any).viteDevServer.environments[\n VITE_ENVIRONMENT_NAMES.server\n ]\n if (!serverEnv) {\n throw new Error(`'ssr' vite dev environment not found`)\n }\n return serverEnv.runner.import(fn.extractedFilename)\n },\n }),\n loadEnvPlugin(startConfig),\n startManifestPlugin(startConfig),\n devServerPlugin(),\n nitroPlugin(startConfig, () => ssrBundle),\n ]\n}\n\nfunction defineReplaceEnv<TKey extends string, TValue extends string>(\n key: TKey,\n value: TValue,\n): { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue } {\n return {\n [`process.env.${key}`]: JSON.stringify(value),\n [`import.meta.env.${key}`]: JSON.stringify(value),\n } as { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue }\n}\n"],"names":["createTanStackConfig","tanStackStartRouter","resolveVirtualEntriesPlugin","trimPathRight","createNitro","nitroOutputPublicDir","startConfig","vite","VITE_ENVIRONMENT_NAMES","CLIENT_DIST_DIR","SSR_ENTRY_FILE","VIRTUAL_MODULES","startCompilerPlugin","TanStackServerFnPluginEnv","loadEnvPlugin","startManifestPlugin","devServerPlugin","nitroPlugin"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BsBA,OAAqB,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A,uBAAuB,OAAO;AAAA,IAAA,CAClD;AAAA,IACDK,gDAA0B;AAAA;AAAA;AAAA,MAGxB,yBAAyBF,gBAAgB,gBAAA;AAAA,MACzC,wBAAwB,KAAK;AAAA,QAC3B;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,MACA,QAAQ;AAAA,QACN,gBAAgB,MACd,8CAA8C,KAAK,SAAS;AAAA,QAC9D,UAAU,CAAC,MACT,oBAAoB,EAAE,UAAU,OAAO,YAAY,UAAU,IAAI;AAAA,QACnE,SAASH,UAAAA,uBAAuB;AAAA,MAClC;AAAA,MACA,QAAQ;AAAA,QACN,gBAAgB,MACd,8CAA8C,KAAK,SAAS;AAAA,QAC9D,UAAU,CAAC,MACT,oBAAoB,EAAE,UAAU,OAAO,YAAY,UAAU,IAAI,MAAM,EAAE,EAAE;AAAA,QAC7E,SAASA,UAAAA,uBAAuB;AAAA,MAClC;AAAA,MACA,UAAU,CAAC,OAAO;AAChB,cAAM,YAAa,WAAmB,cAAc,aAClDA,UAAAA,uBAAuB,MACzB;AACA,YAAI,CAAC,WAAW;AACR,gBAAA,IAAI,MAAM,sCAAsC;AAAA,QAAA;AAExD,eAAO,UAAU,OAAO,OAAO,GAAG,iBAAiB;AAAA,MAAA;AAAA,IACrD,CACD;AAAA,IACDM,SAAAA,cAAc,WAAW;AAAA,IACzBC,SAAAA,oBAAoB,WAAW;AAAA,IAC/BC,yBAAgB;AAAA,IAChBC,SAAA,YAAY,aAAa,MAAM,SAAS;AAAA,EAC1C;AACF;AAEA,SAAS,iBACP,KACA,OACsE;AAC/D,SAAA;AAAA,IACL,CAAC,eAAe,GAAG,EAAE,GAAG,KAAK,UAAU,KAAK;AAAA,IAC5C,CAAC,mBAAmB,GAAG,EAAE,GAAG,KAAK,UAAU,KAAK;AAAA,EAClD;AACF;;"}
|
|
1
|
+
{"version":3,"file":"plugin.cjs","sources":["../../src/plugin.ts"],"sourcesContent":["import path from 'node:path'\nimport { createNitro } from 'nitropack'\nimport { trimPathRight } from '@tanstack/router-core'\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { TanStackServerFnPluginEnv } from '@tanstack/server-functions-plugin'\nimport * as vite from 'vite'\nimport { createTanStackConfig } from './schema'\nimport { nitroPlugin } from './nitro-plugin/plugin'\nimport { startManifestPlugin } from './start-manifest-plugin/plugin'\nimport { startCompilerPlugin } from './start-compiler-plugin'\nimport {\n CLIENT_DIST_DIR,\n SSR_ENTRY_FILE,\n VITE_ENVIRONMENT_NAMES,\n} from './constants'\nimport { tanStackStartRouter } from './start-router-plugin/plugin'\nimport { loadEnvPlugin } from './load-env-plugin/plugin'\nimport { devServerPlugin } from './dev-server-plugin/plugin'\nimport { resolveVirtualEntriesPlugin } from './resolve-virtual-entries-plugin/plugin'\nimport type { createTanStackStartOptionsSchema } from './schema'\nimport type { PluginOption, Rollup } from 'vite'\nimport type { z } from 'zod'\nimport type { CompileStartFrameworkOptions } from './compilers'\n\nexport type TanStackStartInputConfig = z.input<\n ReturnType<typeof createTanStackStartOptionsSchema>\n>\n\nconst defaultConfig = createTanStackConfig()\nexport function getTanStackStartOptions(opts?: TanStackStartInputConfig) {\n return defaultConfig.parse(opts)\n}\n\nexport type TanStackStartOutputConfig = ReturnType<\n typeof getTanStackStartOptions\n>\n\nexport interface TanStackStartVitePluginCoreOptions {\n framework: CompileStartFrameworkOptions\n getVirtualServerRootHandler: (ctx: {\n routerFilepath: string\n serverEntryFilepath: string\n }) => string\n getVirtualServerEntry: (ctx: { routerFilepath: string }) => string\n getVirtualClientEntry: (ctx: { routerFilepath: string }) => string\n}\n// this needs to live outside of the TanStackStartVitePluginCore since it will be invoked multiple times by vite\nlet ssrBundle: Rollup.OutputBundle\n\nexport function TanStackStartVitePluginCore(\n opts: TanStackStartVitePluginCoreOptions,\n startConfig: TanStackStartOutputConfig,\n): Array<PluginOption> {\n return [\n tanStackStartRouter({\n ...startConfig.tsr,\n target: opts.framework,\n autoCodeSplitting: true,\n }),\n resolveVirtualEntriesPlugin(opts, startConfig),\n {\n name: 'tanstack-start-core:config-client',\n async config(viteConfig) {\n const viteAppBase = trimPathRight(viteConfig.base || '/')\n globalThis.TSS_APP_BASE = viteAppBase\n\n const nitroOutputPublicDir = await (async () => {\n // Create a dummy nitro app to get the resolved public output path\n const dummyNitroApp = await createNitro({\n preset: startConfig.target,\n compatibilityDate: '2024-12-01',\n })\n\n const nitroOutputPublicDir = dummyNitroApp.options.output.publicDir\n await dummyNitroApp.close()\n\n return nitroOutputPublicDir\n })()\n\n const getClientEntryPath = (startConfig: TanStackStartOutputConfig) => {\n // when the user specifies a custom client entry path, we need to resolve it\n // relative to the root of the project, keeping in mind that if not specified\n // it will be /~start/default-client-entry which is a virtual path\n // that is resolved by vite to the actual client entry path\n const entry = startConfig.clientEntryPath.startsWith(\n '/~start/default-client-entry',\n )\n ? startConfig.clientEntryPath\n : vite.normalizePath(\n path.join(\n '/@fs',\n path.resolve(startConfig.root, startConfig.clientEntryPath),\n ),\n )\n\n return entry\n }\n\n return {\n base: viteAppBase,\n environments: {\n [VITE_ENVIRONMENT_NAMES.client]: {\n consumer: 'client',\n build: {\n manifest: true,\n rollupOptions: {\n input: {\n main: getClientEntryPath(startConfig),\n },\n output: {\n dir: path.resolve(startConfig.root, CLIENT_DIST_DIR),\n },\n // TODO: this should be removed\n external: ['node:fs', 'node:path', 'node:os', 'node:crypto'],\n },\n },\n },\n [VITE_ENVIRONMENT_NAMES.server]: {\n consumer: 'server',\n build: {\n ssr: true,\n // we don't write to the file system as the below 'capture-output' plugin will\n // capture the output and write it to the virtual file system\n write: false,\n copyPublicDir: false,\n rollupOptions: {\n output: {\n entryFileNames: SSR_ENTRY_FILE,\n },\n plugins: [\n {\n name: 'capture-output',\n generateBundle(_options, bundle) {\n // TODO: can this hook be called more than once?\n ssrBundle = bundle\n },\n },\n ],\n },\n commonjsOptions: {\n include: [/node_modules/],\n },\n },\n },\n },\n resolve: {\n noExternal: [\n '@tanstack/start-client',\n '@tanstack/start-client-core',\n '@tanstack/start-server',\n '@tanstack/start-server-core',\n '@tanstack/start-server-functions-fetcher',\n '@tanstack/start-server-functions-client',\n '@tanstack/start-server-functions-server',\n '@tanstack/start-router-manifest',\n '@tanstack/start-config',\n '@tanstack/server-functions-plugin',\n 'nitropack',\n '@tanstack/**start**',\n ...Object.values(VIRTUAL_MODULES),\n ],\n },\n optimizeDeps: {\n exclude: [...Object.values(VIRTUAL_MODULES)],\n },\n /* prettier-ignore */\n define: {\n // define is an esbuild function that replaces the any instances of given keys with the given values\n // i.e: __FRAMEWORK_NAME__ can be replaced with JSON.stringify(\"TanStack Start\")\n // This is not the same as injecting environment variables.\n\n ...defineReplaceEnv('TSS_CLIENT_ENTRY', getClientEntryPath(startConfig)), // This is consumed by the router-manifest, where the entry point is imported after the dev refresh runtime is resolved\n ...defineReplaceEnv('TSS_SERVER_FN_BASE', startConfig.serverFns.base),\n ...defineReplaceEnv('TSS_OUTPUT_PUBLIC_DIR', nitroOutputPublicDir),\n ...defineReplaceEnv('TSS_APP_BASE', viteAppBase)\n },\n }\n },\n },\n // N.B. TanStackStartCompilerPlugin must be before the TanStackServerFnPluginEnv\n startCompilerPlugin(opts.framework, {\n client: { envName: VITE_ENVIRONMENT_NAMES.client },\n server: { envName: VITE_ENVIRONMENT_NAMES.server },\n }),\n TanStackServerFnPluginEnv({\n // This is the ID that will be available to look up and import\n // our server function manifest and resolve its module\n manifestVirtualImportId: VIRTUAL_MODULES.serverFnManifest,\n manifestOutputFilename: path.join(\n '.tanstack',\n 'start',\n 'build',\n 'server',\n 'server-functions-manifest.json',\n ),\n client: {\n getRuntimeCode: () =>\n `import { createClientRpc } from '@tanstack/${opts.framework}-start/server-functions-client'`,\n replacer: (d) =>\n `createClientRpc('${d.functionId}', '${startConfig.serverFns.base}')`,\n envName: VITE_ENVIRONMENT_NAMES.client,\n },\n server: {\n getRuntimeCode: () =>\n `import { createServerRpc } from '@tanstack/${opts.framework}-start/server-functions-server'`,\n replacer: (d) =>\n `createServerRpc('${d.functionId}', '${startConfig.serverFns.base}', ${d.fn})`,\n envName: VITE_ENVIRONMENT_NAMES.server,\n },\n importer: (fn) => {\n const serverEnv = (globalThis as any).viteDevServer.environments[\n VITE_ENVIRONMENT_NAMES.server\n ]\n if (!serverEnv) {\n throw new Error(`'ssr' vite dev environment not found`)\n }\n return serverEnv.runner.import(fn.extractedFilename)\n },\n }),\n loadEnvPlugin(startConfig),\n startManifestPlugin(startConfig),\n devServerPlugin(),\n nitroPlugin(startConfig, () => ssrBundle),\n {\n name: 'tanstack-start:core:capture-client-bundle',\n applyToEnvironment(e) {\n return e.config.consumer === 'client'\n },\n enforce: 'post',\n generateBundle(_options, bundle) {\n globalThis.TSS_CLIENT_BUNDLE = bundle\n },\n },\n ]\n}\n\nfunction defineReplaceEnv<TKey extends string, TValue extends string>(\n key: TKey,\n value: TValue,\n): { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue } {\n return {\n [`process.env.${key}`]: JSON.stringify(value),\n [`import.meta.env.${key}`]: JSON.stringify(value),\n } as { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue }\n}\n"],"names":["createTanStackConfig","tanStackStartRouter","resolveVirtualEntriesPlugin","trimPathRight","createNitro","nitroOutputPublicDir","startConfig","vite","VITE_ENVIRONMENT_NAMES","CLIENT_DIST_DIR","SSR_ENTRY_FILE","VIRTUAL_MODULES","startCompilerPlugin","TanStackServerFnPluginEnv","loadEnvPlugin","startManifestPlugin","devServerPlugin","nitroPlugin"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BsBA,OAAqB,qBAAA;AAmB3C,IAAI;AAEY,SAAA,4BACd,MACA,aACqB;AACd,SAAA;AAAA,IACLC,2BAAoB;AAAA,MAClB,GAAG,YAAY;AAAA,MACf,QAAQ,KAAK;AAAA,MACb,mBAAmB;AAAA,IAAA,CACpB;AAAA,IACDC,SAAA,4BAA4B,MAAM,WAAW;AAAA,IAC7C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AAAA,QACN,gBAAgB,MACd,8CAA8C,KAAK,SAAS;AAAA,QAC9D,UAAU,CAAC,MACT,oBAAoB,EAAE,UAAU,OAAO,YAAY,UAAU,IAAI;AAAA,QACnE,SAASH,UAAAA,uBAAuB;AAAA,MAClC;AAAA,MACA,QAAQ;AAAA,QACN,gBAAgB,MACd,8CAA8C,KAAK,SAAS;AAAA,QAC9D,UAAU,CAAC,MACT,oBAAoB,EAAE,UAAU,OAAO,YAAY,UAAU,IAAI,MAAM,EAAE,EAAE;AAAA,QAC7E,SAASA,UAAAA,uBAAuB;AAAA,MAClC;AAAA,MACA,UAAU,CAAC,OAAO;AAChB,cAAM,YAAa,WAAmB,cAAc,aAClDA,UAAAA,uBAAuB,MACzB;AACA,YAAI,CAAC,WAAW;AACR,gBAAA,IAAI,MAAM,sCAAsC;AAAA,QAAA;AAExD,eAAO,UAAU,OAAO,OAAO,GAAG,iBAAiB;AAAA,MAAA;AAAA,IACrD,CACD;AAAA,IACDM,SAAAA,cAAc,WAAW;AAAA,IACzBC,SAAAA,oBAA+B;AAAA,IAC/BC,yBAAgB;AAAA,IAChBC,qBAAY,aAAa,MAAM,SAAS;AAAA,IACxC;AAAA,MACE,MAAM;AAAA,MACN,mBAAmB,GAAG;AACb,eAAA,EAAE,OAAO,aAAa;AAAA,MAC/B;AAAA,MACA,SAAS;AAAA,MACT,eAAe,UAAU,QAAQ;AAC/B,mBAAW,oBAAoB;AAAA,MAAA;AAAA,IACjC;AAAA,EAEJ;AACF;AAEA,SAAS,iBACP,KACA,OACsE;AAC/D,SAAA;AAAA,IACL,CAAC,eAAe,GAAG,EAAE,GAAG,KAAK,UAAU,KAAK;AAAA,IAC5C,CAAC,mBAAmB,GAAG,EAAE,GAAG,KAAK,UAAU,KAAK;AAAA,EAClD;AACF;;"}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const node_fs = require("node:fs");
|
|
4
3
|
const path = require("node:path");
|
|
5
4
|
const ufo = require("ufo");
|
|
6
5
|
const routerCore = require("@tanstack/router-core");
|
|
7
6
|
const startServerCore = require("@tanstack/start-server-core");
|
|
7
|
+
const routerPlugin = require("@tanstack/router-plugin");
|
|
8
8
|
const utils = require("../utils.cjs");
|
|
9
|
-
const
|
|
10
|
-
|
|
9
|
+
const getCSSRecursively = (chunk, chunksByFileName, basePath) => {
|
|
10
|
+
var _a;
|
|
11
11
|
const result = [];
|
|
12
|
-
for (const cssFile of
|
|
12
|
+
for (const cssFile of ((_a = chunk.viteMetadata) == null ? void 0 : _a.importedCss) ?? []) {
|
|
13
13
|
result.push({
|
|
14
14
|
tag: "link",
|
|
15
15
|
attrs: {
|
|
@@ -19,11 +19,11 @@ const getCSSRecursively = (file, filesByRouteFilePath, basePath) => {
|
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
21
|
}
|
|
22
|
-
for (const
|
|
23
|
-
const
|
|
24
|
-
if (
|
|
22
|
+
for (const importedFileName of chunk.imports) {
|
|
23
|
+
const importedChunk = chunksByFileName.get(importedFileName);
|
|
24
|
+
if (importedChunk) {
|
|
25
25
|
result.push(
|
|
26
|
-
...getCSSRecursively(
|
|
26
|
+
...getCSSRecursively(importedChunk, chunksByFileName, basePath)
|
|
27
27
|
);
|
|
28
28
|
}
|
|
29
29
|
}
|
|
@@ -38,9 +38,6 @@ function startManifestPlugin(opts) {
|
|
|
38
38
|
configResolved(resolvedConfig) {
|
|
39
39
|
config = resolvedConfig;
|
|
40
40
|
},
|
|
41
|
-
// configEnvironment(env, envConfig) {
|
|
42
|
-
// config = envConfig.
|
|
43
|
-
// },
|
|
44
41
|
resolveId: {
|
|
45
42
|
filter: { id: new RegExp(startServerCore.VIRTUAL_MODULES.startManifest) },
|
|
46
43
|
handler(id) {
|
|
@@ -55,7 +52,6 @@ function startManifestPlugin(opts) {
|
|
|
55
52
|
id: new RegExp(resolvedModuleId)
|
|
56
53
|
},
|
|
57
54
|
handler(id) {
|
|
58
|
-
var _a;
|
|
59
55
|
if (id === resolvedModuleId) {
|
|
60
56
|
if (this.environment.config.consumer !== "server") {
|
|
61
57
|
return `export default {}`;
|
|
@@ -66,77 +62,85 @@ function startManifestPlugin(opts) {
|
|
|
66
62
|
})`;
|
|
67
63
|
}
|
|
68
64
|
const APP_BASE = globalThis.TSS_APP_BASE;
|
|
69
|
-
const clientViteManifestPath = path.resolve(
|
|
70
|
-
opts.root,
|
|
71
|
-
constants.CLIENT_DIST_DIR,
|
|
72
|
-
".vite",
|
|
73
|
-
"manifest.json"
|
|
74
|
-
);
|
|
75
|
-
let viteManifest;
|
|
76
|
-
try {
|
|
77
|
-
viteManifest = JSON.parse(
|
|
78
|
-
node_fs.readFileSync(clientViteManifestPath, "utf-8")
|
|
79
|
-
);
|
|
80
|
-
} catch (err) {
|
|
81
|
-
console.error(err);
|
|
82
|
-
throw new Error(
|
|
83
|
-
`Could not find the production client vite manifest at '${clientViteManifestPath}'!`
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
65
|
const routeTreeRoutes = globalThis.TSS_ROUTES_MANIFEST.routes;
|
|
87
66
|
let entryFile;
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
67
|
+
const clientBundle = globalThis.TSS_CLIENT_BUNDLE;
|
|
68
|
+
const chunksByFileName = /* @__PURE__ */ new Map();
|
|
69
|
+
const routeChunks = {};
|
|
70
|
+
for (const bundleEntry of Object.values(clientBundle)) {
|
|
71
|
+
if (bundleEntry.type === "chunk") {
|
|
72
|
+
chunksByFileName.set(bundleEntry.fileName, bundleEntry);
|
|
73
|
+
if (bundleEntry.isEntry) {
|
|
74
|
+
if (entryFile) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
`multiple entries detected: ${entryFile.fileName} ${bundleEntry.fileName}`
|
|
96
77
|
);
|
|
97
78
|
}
|
|
98
|
-
entryFile =
|
|
79
|
+
entryFile = bundleEntry;
|
|
99
80
|
}
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
81
|
+
const routePieces = bundleEntry.moduleIds.flatMap((m) => {
|
|
82
|
+
const [id2, query] = m.split("?");
|
|
83
|
+
if (id2 === void 0) {
|
|
84
|
+
throw new Error("expected id to be defined");
|
|
85
|
+
}
|
|
86
|
+
if (query === void 0) {
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
const searchParams = new URLSearchParams(query);
|
|
90
|
+
const split = searchParams.get(routerPlugin.tsrSplit);
|
|
91
|
+
if (split !== null) {
|
|
92
|
+
return {
|
|
93
|
+
id: id2,
|
|
94
|
+
split
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
return [];
|
|
114
98
|
});
|
|
115
|
-
if (
|
|
116
|
-
|
|
99
|
+
if (routePieces.length > 0) {
|
|
100
|
+
routePieces.forEach((r) => {
|
|
101
|
+
let array = routeChunks[r.id];
|
|
102
|
+
if (array === void 0) {
|
|
103
|
+
array = [];
|
|
104
|
+
routeChunks[r.id] = array;
|
|
105
|
+
}
|
|
106
|
+
array.push(bundleEntry);
|
|
107
|
+
});
|
|
117
108
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
);
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
Object.entries(routeTreeRoutes).forEach(([routeId, v]) => {
|
|
112
|
+
if (!v.filePath) {
|
|
113
|
+
throw new Error(`expected filePath to be set for ${routeId}`);
|
|
114
|
+
}
|
|
115
|
+
const chunks = routeChunks[v.filePath];
|
|
116
|
+
if (chunks) {
|
|
117
|
+
chunks.forEach((chunk) => {
|
|
118
|
+
const preloads = chunk.imports.map((d) => {
|
|
119
|
+
const assetPath = ufo.joinURL(APP_BASE, d);
|
|
120
|
+
return assetPath;
|
|
121
|
+
});
|
|
122
|
+
preloads.unshift(path.join(APP_BASE, chunk.fileName));
|
|
123
|
+
const cssAssetsList = getCSSRecursively(
|
|
124
|
+
chunk,
|
|
125
|
+
chunksByFileName,
|
|
126
|
+
APP_BASE
|
|
127
|
+
);
|
|
128
|
+
routeTreeRoutes[routeId] = {
|
|
129
|
+
...v,
|
|
130
|
+
assets: [...v.assets || [], ...cssAssetsList],
|
|
131
|
+
preloads: [...v.preloads || [], ...preloads]
|
|
132
|
+
};
|
|
133
|
+
});
|
|
128
134
|
}
|
|
129
135
|
});
|
|
130
136
|
if (entryFile) {
|
|
131
137
|
routeTreeRoutes[routerCore.rootRouteId].preloads = [
|
|
132
|
-
ufo.joinURL(APP_BASE, entryFile.
|
|
133
|
-
...
|
|
134
|
-
(d) => ufo.joinURL(APP_BASE, viteManifest[d].file)
|
|
135
|
-
)) || []
|
|
138
|
+
ufo.joinURL(APP_BASE, entryFile.fileName),
|
|
139
|
+
...entryFile.imports.map((d) => ufo.joinURL(APP_BASE, d))
|
|
136
140
|
];
|
|
137
141
|
const entryCssAssetsList = getCSSRecursively(
|
|
138
142
|
entryFile,
|
|
139
|
-
|
|
143
|
+
chunksByFileName,
|
|
140
144
|
APP_BASE
|
|
141
145
|
);
|
|
142
146
|
routeTreeRoutes[routerCore.rootRouteId].assets = [
|
|
@@ -145,15 +149,15 @@ function startManifestPlugin(opts) {
|
|
|
145
149
|
{
|
|
146
150
|
tag: "script",
|
|
147
151
|
attrs: {
|
|
148
|
-
src: ufo.joinURL(APP_BASE, entryFile.
|
|
152
|
+
src: ufo.joinURL(APP_BASE, entryFile.fileName),
|
|
149
153
|
type: "module"
|
|
150
154
|
}
|
|
151
155
|
}
|
|
152
156
|
];
|
|
153
157
|
}
|
|
154
158
|
const recurseRoute = (route, seenPreloads = {}) => {
|
|
155
|
-
var
|
|
156
|
-
route.preloads = (
|
|
159
|
+
var _a;
|
|
160
|
+
route.preloads = (_a = route.preloads) == null ? void 0 : _a.filter((preload) => {
|
|
157
161
|
if (seenPreloads[preload]) {
|
|
158
162
|
return false;
|
|
159
163
|
}
|
|
@@ -178,5 +182,6 @@ function startManifestPlugin(opts) {
|
|
|
178
182
|
}
|
|
179
183
|
};
|
|
180
184
|
}
|
|
185
|
+
exports.getCSSRecursively = getCSSRecursively;
|
|
181
186
|
exports.startManifestPlugin = startManifestPlugin;
|
|
182
187
|
//# sourceMappingURL=plugin.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.cjs","sources":["../../../src/start-manifest-plugin/plugin.ts"],"sourcesContent":["import { readFileSync } from 'node:fs'\nimport path from 'node:path'\nimport { joinURL } from 'ufo'\nimport { rootRouteId } from '@tanstack/router-core'\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { resolveViteId } from '../utils'\nimport { CLIENT_DIST_DIR } from '../constants'\nimport type {\n PluginOption,\n ResolvedConfig,\n Manifest as ViteManifest,\n ManifestChunk as ViteManifestChunk,\n} from 'vite'\nimport type { RouterManagedTag } from '@tanstack/router-core'\nimport type { TanStackStartOutputConfig } from '../plugin'\n\nconst getCSSRecursively = (\n file: ViteManifestChunk,\n filesByRouteFilePath: ViteManifest,\n basePath: string,\n) => {\n const result: Array<RouterManagedTag> = []\n\n // Get all css imports from the file\n for (const cssFile of file.css ?? []) {\n result.push({\n tag: 'link',\n attrs: {\n rel: 'stylesheet',\n href: joinURL(basePath, cssFile),\n type: 'text/css',\n },\n })\n }\n\n // Recursively get CSS from imports\n for (const imp of file.imports ?? []) {\n const importInfo = filesByRouteFilePath[imp]\n if (importInfo) {\n result.push(\n ...getCSSRecursively(importInfo, filesByRouteFilePath, basePath),\n )\n }\n }\n\n return result\n}\n\nconst resolvedModuleId = resolveViteId(VIRTUAL_MODULES.startManifest)\nexport function startManifestPlugin(\n opts: TanStackStartOutputConfig,\n): PluginOption {\n let config: ResolvedConfig\n\n return {\n name: 'tanstack-start:start-manifest-plugin',\n enforce: 'pre',\n\n configResolved(resolvedConfig) {\n config = resolvedConfig\n },\n // configEnvironment(env, envConfig) {\n // config = envConfig.\n // },\n resolveId: {\n filter: { id: new RegExp(VIRTUAL_MODULES.startManifest) },\n handler(id) {\n if (id === VIRTUAL_MODULES.startManifest) {\n return resolvedModuleId\n }\n return undefined\n },\n },\n load: {\n filter: {\n id: new RegExp(resolvedModuleId),\n },\n handler(id) {\n if (id === resolvedModuleId) {\n if (this.environment.config.consumer !== 'server') {\n // this will ultimately fail the build if the plugin is used outside the server environment\n // TODO: do we need special handling for `serve`?\n return `export default {}`\n }\n\n // If we're in development, return a dummy manifest\n if (config.command === 'serve') {\n return `export const tsrStartManifest = () => ({\n routes: {}\n })`\n }\n\n // This is the basepath for the application\n const APP_BASE = globalThis.TSS_APP_BASE\n\n const clientViteManifestPath = path.resolve(\n opts.root,\n CLIENT_DIST_DIR,\n '.vite',\n 'manifest.json',\n )\n\n let viteManifest: ViteManifest\n try {\n viteManifest = JSON.parse(\n readFileSync(clientViteManifestPath, 'utf-8'),\n )\n } catch (err) {\n console.error(err)\n throw new Error(\n `Could not find the production client vite manifest at '${clientViteManifestPath}'!`,\n )\n }\n\n // This the manifest pulled from the generated route tree and later used by the Router.\n // i.e what's located in `src/generatedRouteTree.gen.ts`\n const routeTreeRoutes = globalThis.TSS_ROUTES_MANIFEST.routes\n\n // This is where hydration will start, from when the SSR'd page reaches the browser.\n // By default, this'd be the virtual entry of `/~start/default-client-entry.tsx`, unless a custom entry is provided.\n let entryFile: ViteManifestChunk | undefined\n\n const filesByRouteFilePath: ViteManifest = Object.fromEntries(\n Object.entries(viteManifest).map(([k, v]) => {\n if (v.isEntry) {\n if (entryFile !== undefined) {\n console.error(\n `multiple entries detected`,\n entryFile.file,\n v.file,\n )\n }\n entryFile = v\n }\n\n const rPath = k.split('?')[0]\n\n return [rPath, v]\n }, {}),\n )\n\n const routesDirectoryFromRoot = path.relative(\n opts.root,\n opts.tsr.routesDirectory,\n )\n\n // Add preloads to the routes from the vite manifest\n Object.entries(routeTreeRoutes).forEach(([routeId, v]) => {\n const file =\n filesByRouteFilePath[\n path.posix.join(routesDirectoryFromRoot, v.filePath as string)\n ]\n\n if (file) {\n // Map the relevant imports to their route paths,\n // so that it can be imported in the browser.\n const preloads = (file.imports ?? []).map((d) => {\n const assetPath = joinURL(APP_BASE, viteManifest[d]!.file)\n return assetPath\n })\n\n // Since this is the most important JS entry for the route,\n // it should be moved to the front of the preloads so that\n // it has the best chance of being loaded first.\n if (file.file) {\n preloads.unshift(path.join(APP_BASE, file.file))\n }\n\n const cssAssetsList = getCSSRecursively(\n file,\n filesByRouteFilePath,\n APP_BASE,\n )\n\n routeTreeRoutes[routeId] = {\n ...v,\n assets: [...(v.assets || []), ...cssAssetsList],\n preloads,\n }\n }\n })\n\n if (entryFile) {\n routeTreeRoutes[rootRouteId]!.preloads = [\n joinURL(APP_BASE, entryFile.file),\n ...(entryFile.imports?.map((d) =>\n joinURL(APP_BASE, viteManifest[d]!.file),\n ) || []),\n ]\n\n // Gather all the CSS files from the entry file in\n // the `css` key and add them to the root route\n const entryCssAssetsList = getCSSRecursively(\n entryFile,\n filesByRouteFilePath,\n APP_BASE,\n )\n\n routeTreeRoutes[rootRouteId]!.assets = [\n ...(routeTreeRoutes[rootRouteId]!.assets || []),\n ...entryCssAssetsList,\n {\n tag: 'script',\n attrs: {\n src: joinURL(APP_BASE, entryFile.file),\n type: 'module',\n },\n },\n ]\n }\n\n const recurseRoute = (\n route: {\n preloads?: Array<string>\n children?: Array<any>\n },\n seenPreloads = {} as Record<string, true>,\n ) => {\n route.preloads = route.preloads?.filter((preload) => {\n if (seenPreloads[preload]) {\n return false\n }\n seenPreloads[preload] = true\n return true\n })\n\n if (route.children) {\n route.children.forEach((child) => {\n const childRoute = routeTreeRoutes[child]!\n recurseRoute(childRoute, { ...seenPreloads })\n })\n }\n }\n\n // @ts-expect-error\n recurseRoute(routeTreeRoutes[rootRouteId])\n\n const routesManifest = {\n routes: routeTreeRoutes,\n }\n\n return `export const tsrStartManifest = () => (${JSON.stringify(routesManifest)})`\n }\n\n return undefined\n },\n },\n }\n}\n"],"names":["joinURL","resolveViteId","VIRTUAL_MODULES","CLIENT_DIST_DIR","readFileSync","rootRouteId","_a"],"mappings":";;;;;;;;;AAgBA,MAAM,oBAAoB,CACxB,MACA,sBACA,aACG;AACH,QAAM,SAAkC,CAAC;AAGzC,aAAW,WAAW,KAAK,OAAO,CAAA,GAAI;AACpC,WAAO,KAAK;AAAA,MACV,KAAK;AAAA,MACL,OAAO;AAAA,QACL,KAAK;AAAA,QACL,MAAMA,IAAAA,QAAQ,UAAU,OAAO;AAAA,QAC/B,MAAM;AAAA,MAAA;AAAA,IACR,CACD;AAAA,EAAA;AAIH,aAAW,OAAO,KAAK,WAAW,CAAA,GAAI;AAC9B,UAAA,aAAa,qBAAqB,GAAG;AAC3C,QAAI,YAAY;AACP,aAAA;AAAA,QACL,GAAG,kBAAkB,YAAY,sBAAsB,QAAQ;AAAA,MACjE;AAAA,IAAA;AAAA,EACF;AAGK,SAAA;AACT;AAEA,MAAM,mBAAmBC,MAAAA,cAAcC,gBAAA,gBAAgB,aAAa;AAC7D,SAAS,oBACd,MACc;AACV,MAAA;AAEG,SAAA;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IAET,eAAe,gBAAgB;AACpB,eAAA;AAAA,IACX;AAAA;AAAA;AAAA;AAAA,IAIA,WAAW;AAAA,MACT,QAAQ,EAAE,IAAI,IAAI,OAAOA,gBAAA,gBAAgB,aAAa,EAAE;AAAA,MACxD,QAAQ,IAAI;AACN,YAAA,OAAOA,gCAAgB,eAAe;AACjC,iBAAA;AAAA,QAAA;AAEF,eAAA;AAAA,MAAA;AAAA,IAEX;AAAA,IACA,MAAM;AAAA,MACJ,QAAQ;AAAA,QACN,IAAI,IAAI,OAAO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,cAAA;AAET,2BAAa,OAAO,IAAI;AACjB,qBAAA;AAAA,YAAA;AAGT,gBAAI,MAAM,UAAU;AACZ,oBAAA,SAAS,QAAQ,CAAC,UAAU;AAC1B,sBAAA,aAAa,gBAAgB,KAAK;AACxC,6BAAa,YAAY,EAAE,GAAG,cAAc;AAAA,cAAA,CAC7C;AAAA,YAAA;AAAA,UAEL;AAGa,uBAAA,gBAAgBD,WAAAA,WAAW,CAAC;AAEzC,gBAAM,iBAAiB;AAAA,YACrB,QAAQ;AAAA,UACV;AAEA,iBAAO,0CAA0C,KAAK,UAAU,cAAc,CAAC;AAAA,QAAA;AAG1E,eAAA;AAAA,MAAA;AAAA,IACT;AAAA,EAEJ;AACF;;"}
|
|
1
|
+
{"version":3,"file":"plugin.cjs","sources":["../../../src/start-manifest-plugin/plugin.ts"],"sourcesContent":["import path from 'node:path'\nimport { joinURL } from 'ufo'\nimport { rootRouteId } from '@tanstack/router-core'\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { tsrSplit } from '@tanstack/router-plugin'\nimport { resolveViteId } from '../utils'\nimport type { PluginOption, ResolvedConfig, Rollup } from 'vite'\nimport type { RouterManagedTag } from '@tanstack/router-core'\nimport type { TanStackStartOutputConfig } from '../plugin'\n\nexport const getCSSRecursively = (\n chunk: Rollup.OutputChunk,\n chunksByFileName: Map<string, Rollup.OutputChunk>,\n basePath: string,\n) => {\n const result: Array<RouterManagedTag> = []\n\n // Get all css imports from the file\n for (const cssFile of chunk.viteMetadata?.importedCss ?? []) {\n result.push({\n tag: 'link',\n attrs: {\n rel: 'stylesheet',\n href: joinURL(basePath, cssFile),\n type: 'text/css',\n },\n })\n }\n\n // Recursively get CSS from imports\n for (const importedFileName of chunk.imports) {\n const importedChunk = chunksByFileName.get(importedFileName)\n if (importedChunk) {\n result.push(\n ...getCSSRecursively(importedChunk, chunksByFileName, basePath),\n )\n }\n }\n\n return result\n}\n\nconst resolvedModuleId = resolveViteId(VIRTUAL_MODULES.startManifest)\nexport function startManifestPlugin(\n opts: TanStackStartOutputConfig,\n): PluginOption {\n let config: ResolvedConfig\n\n return {\n name: 'tanstack-start:start-manifest-plugin',\n enforce: 'pre',\n\n configResolved(resolvedConfig) {\n config = resolvedConfig\n },\n resolveId: {\n filter: { id: new RegExp(VIRTUAL_MODULES.startManifest) },\n handler(id) {\n if (id === VIRTUAL_MODULES.startManifest) {\n return resolvedModuleId\n }\n return undefined\n },\n },\n load: {\n filter: {\n id: new RegExp(resolvedModuleId),\n },\n handler(id) {\n if (id === resolvedModuleId) {\n if (this.environment.config.consumer !== 'server') {\n // this will ultimately fail the build if the plugin is used outside the server environment\n // TODO: do we need special handling for `serve`?\n return `export default {}`\n }\n\n // If we're in development, return a dummy manifest\n if (config.command === 'serve') {\n return `export const tsrStartManifest = () => ({\n routes: {}\n })`\n }\n\n // This is the basepath for the application\n const APP_BASE = globalThis.TSS_APP_BASE\n\n // This the manifest pulled from the generated route tree and later used by the Router.\n // i.e what's located in `src/routeTree.gen.ts`\n const routeTreeRoutes = globalThis.TSS_ROUTES_MANIFEST.routes\n\n // This is where hydration will start, from when the SSR'd page reaches the browser.\n // By default, this'd be the virtual entry of `/~start/default-client-entry.tsx`, unless a custom entry is provided.\n let entryFile: Rollup.OutputChunk | undefined\n\n const clientBundle = globalThis.TSS_CLIENT_BUNDLE\n const chunksByFileName = new Map<string, Rollup.OutputChunk>()\n\n const routeChunks: Record<\n string /** fullPath of route file **/,\n Array<Rollup.OutputChunk>\n > = {}\n for (const bundleEntry of Object.values(clientBundle)) {\n if (bundleEntry.type === 'chunk') {\n chunksByFileName.set(bundleEntry.fileName, bundleEntry)\n if (bundleEntry.isEntry) {\n if (entryFile) {\n throw new Error(\n `multiple entries detected: ${entryFile.fileName} ${bundleEntry.fileName}`,\n )\n }\n entryFile = bundleEntry\n }\n const routePieces = bundleEntry.moduleIds.flatMap((m) => {\n const [id, query] = m.split('?')\n if (id === undefined) {\n throw new Error('expected id to be defined')\n }\n if (query === undefined) {\n return []\n }\n const searchParams = new URLSearchParams(query)\n const split = searchParams.get(tsrSplit)\n\n if (split !== null) {\n return {\n id,\n split,\n }\n }\n return []\n })\n if (routePieces.length > 0) {\n routePieces.forEach((r) => {\n let array = routeChunks[r.id]\n if (array === undefined) {\n array = []\n routeChunks[r.id] = array\n }\n array.push(bundleEntry)\n })\n }\n }\n }\n\n // Add preloads to the routes from the vite manifest\n Object.entries(routeTreeRoutes).forEach(([routeId, v]) => {\n if (!v.filePath) {\n throw new Error(`expected filePath to be set for ${routeId}`)\n }\n const chunks = routeChunks[v.filePath]\n if (chunks) {\n chunks.forEach((chunk) => {\n // Map the relevant imports to their route paths,\n // so that it can be imported in the browser.\n const preloads = chunk.imports.map((d) => {\n const assetPath = joinURL(APP_BASE, d)\n return assetPath\n })\n\n // Since this is the most important JS entry for the route,\n // it should be moved to the front of the preloads so that\n // it has the best chance of being loaded first.\n preloads.unshift(path.join(APP_BASE, chunk.fileName))\n\n const cssAssetsList = getCSSRecursively(\n chunk,\n chunksByFileName,\n APP_BASE,\n )\n\n routeTreeRoutes[routeId] = {\n ...v,\n assets: [...(v.assets || []), ...cssAssetsList],\n preloads: [...(v.preloads || []), ...preloads],\n }\n })\n }\n })\n\n if (entryFile) {\n routeTreeRoutes[rootRouteId]!.preloads = [\n joinURL(APP_BASE, entryFile.fileName),\n ...entryFile.imports.map((d) => joinURL(APP_BASE, d)),\n ]\n\n // Gather all the CSS files from the entry file in\n // the `css` key and add them to the root route\n const entryCssAssetsList = getCSSRecursively(\n entryFile,\n chunksByFileName,\n APP_BASE,\n )\n\n routeTreeRoutes[rootRouteId]!.assets = [\n ...(routeTreeRoutes[rootRouteId]!.assets || []),\n ...entryCssAssetsList,\n {\n tag: 'script',\n attrs: {\n src: joinURL(APP_BASE, entryFile.fileName),\n type: 'module',\n },\n },\n ]\n }\n\n const recurseRoute = (\n route: {\n preloads?: Array<string>\n children?: Array<any>\n },\n seenPreloads = {} as Record<string, true>,\n ) => {\n route.preloads = route.preloads?.filter((preload) => {\n if (seenPreloads[preload]) {\n return false\n }\n seenPreloads[preload] = true\n return true\n })\n\n if (route.children) {\n route.children.forEach((child) => {\n const childRoute = routeTreeRoutes[child]!\n recurseRoute(childRoute, { ...seenPreloads })\n })\n }\n }\n\n recurseRoute(routeTreeRoutes[rootRouteId]!)\n\n const routesManifest = {\n routes: routeTreeRoutes,\n }\n\n return `export const tsrStartManifest = () => (${JSON.stringify(routesManifest)})`\n }\n\n return undefined\n },\n },\n }\n}\n"],"names":["joinURL","resolveViteId","VIRTUAL_MODULES","id","tsrSplit","rootRouteId"],"mappings":";;;;;;;;AAUO,MAAM,oBAAoB,CAC/B,OACA,kBACA,aACG;;AACH,QAAM,SAAkC,CAAC;AAGzC,aAAW,aAAW,WAAM,iBAAN,mBAAoB,gBAAe,CAAA,GAAI;AAC3D,WAAO,KAAK;AAAA,MACV,KAAK;AAAA,MACL,OAAO;AAAA,QACL,KAAK;AAAA,QACL,MAAMA,IAAAA,QAAQ,UAAU,OAAO;AAAA,QAC/B,MAAM;AAAA,MAAA;AAAA,IACR,CACD;AAAA,EAAA;AAIQ,aAAA,oBAAoB,MAAM,SAAS;AACtC,UAAA,gBAAgB,iBAAiB,IAAI,gBAAgB;AAC3D,QAAI,eAAe;AACV,aAAA;AAAA,QACL,GAAG,kBAAkB,eAAe,kBAAkB,QAAQ;AAAA,MAChE;AAAA,IAAA;AAAA,EACF;AAGK,SAAA;AACT;AAEA,MAAM,mBAAmBC,MAAAA,cAAcC,gBAAA,gBAAgB,aAAa;AAC7D,SAAS,oBACd,MACc;AACV,MAAA;AAEG,SAAA;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IAET,eAAe,gBAAgB;AACpB,eAAA;AAAA,IACX;AAAA,IACA,WAAW;AAAA,MACT,QAAQ,EAAE,IAAI,IAAI,OAAOA,gBAAA,gBAAgB,aAAa,EAAE;AAAA,MACxD,QAAQ,IAAI;AACN,YAAA,OAAOA,gCAAgB,eAAe;AACjC,iBA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;AAAA,cACA;AAAA,YACF;AAEgB,4BAAAK,WAAAA,WAAW,EAAG,SAAS;AAAA,cACrC,GAAI,gBAAgBA,WAAAA,WAAW,EAAG,UAAU,CAAC;AAAA,cAC7C,GAAG;AAAA,cACH;AAAA,gBACE,KAAK;AAAA,gBACL,OAAO;AAAA,kBACL,KAAKL,IAAA,QAAQ,UAAU,UAAU,QAAQ;AAAA,kBACzC,MAAM;AAAA,gBAAA;AAAA,cACR;AAAA,YAEJ;AAAA,UAAA;AAGF,gBAAM,eAAe,CACnB,OAIA,eAAe,CAAA,MACZ;;AACH,kBAAM,YAAW,WAAM,aAAN,mBAAgB,OAAO,CAAC,YAAY;AAC/C,kBAAA,aAAa,OAAO,GAAG;AAClB,uBAAA;AAAA,cAAA;AAET,2BAAa,OAAO,IAAI;AACjB,qBAAA;AAAA,YAAA;AAGT,gBAAI,MAAM,UAAU;AACZ,oBAAA,SAAS,QAAQ,CAAC,UAAU;AAC1B,sBAAA,aAAa,gBAAgB,KAAK;AACxC,6BAAa,YAAY,EAAE,GAAG,cAAc;AAAA,cAAA,CAC7C;AAAA,YAAA;AAAA,UAEL;AAEa,uBAAA,gBAAgBK,WAAAA,WAAW,CAAE;AAE1C,gBAAM,iBAAiB;AAAA,YACrB,QAAQ;AAAA,UACV;AAEA,iBAAO,0CAA0C,KAAK,UAAU,cAAc,CAAC;AAAA,QAAA;AAG1E,eAAA;AAAA,MAAA;AAAA,IACT;AAAA,EAEJ;AACF;;;"}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
import { PluginOption } from 'vite';
|
|
1
|
+
import { PluginOption, Rollup } from 'vite';
|
|
2
|
+
import { RouterManagedTag } from '@tanstack/router-core';
|
|
2
3
|
import { TanStackStartOutputConfig } from '../plugin.cjs';
|
|
4
|
+
export declare const getCSSRecursively: (chunk: Rollup.OutputChunk, chunksByFileName: Map<string, Rollup.OutputChunk>, basePath: string) => RouterManagedTag[];
|
|
3
5
|
export declare function startManifestPlugin(opts: TanStackStartOutputConfig): PluginOption;
|
|
@@ -13,7 +13,7 @@ function routesManifestPlugin() {
|
|
|
13
13
|
}
|
|
14
14
|
const routesManifest = {
|
|
15
15
|
[routerCore.rootRouteId]: {
|
|
16
|
-
filePath: rootRouteNode.
|
|
16
|
+
filePath: rootRouteNode.fullPath,
|
|
17
17
|
children: routeTree.acc.routeTree.map((d) => d.routePath)
|
|
18
18
|
},
|
|
19
19
|
...Object.fromEntries(
|
|
@@ -23,7 +23,7 @@ function routesManifestPlugin() {
|
|
|
23
23
|
return [
|
|
24
24
|
filePathId,
|
|
25
25
|
{
|
|
26
|
-
filePath: d.
|
|
26
|
+
filePath: d.fullPath,
|
|
27
27
|
parent: ((_a = d.parent) == null ? void 0 : _a.routePath) ? d.parent.routePath : void 0,
|
|
28
28
|
children: (_b = d.children) == null ? void 0 : _b.map((childRoute) => childRoute.routePath)
|
|
29
29
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routes-manifest-plugin.cjs","sources":["../../../../src/start-router-plugin/generator-plugins/routes-manifest-plugin.ts"],"sourcesContent":["import { rootRouteId } from '@tanstack/router-core'\n\nimport type { GeneratorPlugin } from '@tanstack/router-generator'\n\n/**\n * this plugin builds the routes manifest and stores it on globalThis\n * so that it can be accessed later (e.g. from a vite plugin)\n */\nexport function routesManifestPlugin(): GeneratorPlugin {\n return {\n name: 'routes-manifest-plugin',\n onRouteTreesChanged: ({ routeTrees, rootRouteNode }) => {\n const routeTree = routeTrees.find((tree) => tree.exportName === 'Route')\n if (!routeTree) {\n throw new Error(\n 'No route tree found with export name \"Route\". Please ensure your routes are correctly defined.',\n )\n }\n const routesManifest = {\n [rootRouteId]: {\n filePath: rootRouteNode.
|
|
1
|
+
{"version":3,"file":"routes-manifest-plugin.cjs","sources":["../../../../src/start-router-plugin/generator-plugins/routes-manifest-plugin.ts"],"sourcesContent":["import { rootRouteId } from '@tanstack/router-core'\n\nimport type { GeneratorPlugin } from '@tanstack/router-generator'\n\n/**\n * this plugin builds the routes manifest and stores it on globalThis\n * so that it can be accessed later (e.g. from a vite plugin)\n */\nexport function routesManifestPlugin(): GeneratorPlugin {\n return {\n name: 'routes-manifest-plugin',\n onRouteTreesChanged: ({ routeTrees, rootRouteNode }) => {\n const routeTree = routeTrees.find((tree) => tree.exportName === 'Route')\n if (!routeTree) {\n throw new Error(\n 'No route tree found with export name \"Route\". Please ensure your routes are correctly defined.',\n )\n }\n const routesManifest = {\n [rootRouteId]: {\n filePath: rootRouteNode.fullPath,\n children: routeTree.acc.routeTree.map((d) => d.routePath),\n },\n ...Object.fromEntries(\n routeTree.acc.routeNodes.map((d) => {\n const filePathId = d.routePath\n\n return [\n filePathId,\n {\n filePath: d.fullPath,\n parent: d.parent?.routePath ? d.parent.routePath : undefined,\n children: d.children?.map((childRoute) => childRoute.routePath),\n },\n ]\n }),\n ),\n }\n\n globalThis.TSS_ROUTES_MANIFEST = { routes: routesManifest }\n },\n }\n}\n"],"names":["rootRouteId"],"mappings":";;;AAQO,SAAS,uBAAwC;AAC/C,SAAA;AAAA,IACL,MAAM;AAAA,IACN,qBAAqB,CAAC,EAAE,YAAY,oBAAoB;AACtD,YAAM,YAAY,WAAW,KAAK,CAAC,SAAS,KAAK,eAAe,OAAO;AACvE,UAAI,CAAC,WAAW;AACd,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MAAA;AAEF,YAAM,iBAAiB;AAAA,QACrB,CAACA,sBAAW,GAAG;AAAA,UACb,UAAU,cAAc;AAAA,UACxB,UAAU,UAAU,IAAI,UAAU,IAAI,CAAC,MAAM,EAAE,SAAS;AAAA,QAC1D;AAAA,QACA,GAAG,OAAO;AAAA,UACR,UAAU,IAAI,WAAW,IAAI,CAAC,MAAM;;AAClC,kBAAM,aAAa,EAAE;AAEd,mBAAA;AAAA,cACL;AAAA,cACA;AAAA,gBACE,UAAU,EAAE;AAAA,gBACZ,UAAQ,OAAE,WAAF,mBAAU,aAAY,EAAE,OAAO,YAAY;AAAA,gBACnD,WAAU,OAAE,aAAF,mBAAY,IAAI,CAAC,eAAe,WAAW;AAAA,cAAS;AAAA,YAElE;AAAA,UACD,CAAA;AAAA,QAAA;AAAA,MAEL;AAEW,iBAAA,sBAAsB,EAAE,QAAQ,eAAe;AAAA,IAAA;AAAA,EAE9D;AACF;;"}
|
package/dist/esm/plugin.js
CHANGED
|
@@ -163,9 +163,19 @@ function TanStackStartVitePluginCore(opts, startConfig) {
|
|
|
163
163
|
}
|
|
164
164
|
}),
|
|
165
165
|
loadEnvPlugin(startConfig),
|
|
166
|
-
startManifestPlugin(
|
|
166
|
+
startManifestPlugin(),
|
|
167
167
|
devServerPlugin(),
|
|
168
|
-
nitroPlugin(startConfig, () => ssrBundle)
|
|
168
|
+
nitroPlugin(startConfig, () => ssrBundle),
|
|
169
|
+
{
|
|
170
|
+
name: "tanstack-start:core:capture-client-bundle",
|
|
171
|
+
applyToEnvironment(e) {
|
|
172
|
+
return e.config.consumer === "client";
|
|
173
|
+
},
|
|
174
|
+
enforce: "post",
|
|
175
|
+
generateBundle(_options, bundle) {
|
|
176
|
+
globalThis.TSS_CLIENT_BUNDLE = bundle;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
169
179
|
];
|
|
170
180
|
}
|
|
171
181
|
function defineReplaceEnv(key, value) {
|
package/dist/esm/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sources":["../../src/plugin.ts"],"sourcesContent":["import path from 'node:path'\nimport { createNitro } from 'nitropack'\nimport { trimPathRight } from '@tanstack/router-core'\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { TanStackServerFnPluginEnv } from '@tanstack/server-functions-plugin'\nimport * as vite from 'vite'\nimport { createTanStackConfig } from './schema'\nimport { nitroPlugin } from './nitro-plugin/plugin'\nimport { startManifestPlugin } from './start-manifest-plugin/plugin'\nimport { startCompilerPlugin } from './start-compiler-plugin'\nimport {\n CLIENT_DIST_DIR,\n SSR_ENTRY_FILE,\n VITE_ENVIRONMENT_NAMES,\n} from './constants'\nimport { tanStackStartRouter } from './start-router-plugin/plugin'\nimport { loadEnvPlugin } from './load-env-plugin/plugin'\nimport { devServerPlugin } from './dev-server-plugin/plugin'\nimport { resolveVirtualEntriesPlugin } from './resolve-virtual-entries-plugin/plugin'\nimport type { createTanStackStartOptionsSchema } from './schema'\nimport type { PluginOption, Rollup } from 'vite'\nimport type { z } from 'zod'\nimport type { CompileStartFrameworkOptions } from './compilers'\n\nexport type TanStackStartInputConfig = z.input<\n ReturnType<typeof createTanStackStartOptionsSchema>\n>\n\nconst defaultConfig = createTanStackConfig()\nexport function getTanStackStartOptions(opts?: TanStackStartInputConfig) {\n return defaultConfig.parse(opts)\n}\n\nexport type TanStackStartOutputConfig = ReturnType<\n typeof getTanStackStartOptions\n>\n\nexport interface TanStackStartVitePluginCoreOptions {\n framework: CompileStartFrameworkOptions\n getVirtualServerRootHandler: (ctx: {\n routerFilepath: string\n serverEntryFilepath: string\n }) => string\n getVirtualServerEntry: (ctx: { routerFilepath: string }) => string\n getVirtualClientEntry: (ctx: { routerFilepath: string }) => string\n}\n// this needs to live outside of the TanStackStartVitePluginCore since it will be invoked multiple times by vite\nlet ssrBundle: Rollup.OutputBundle\n\nexport function TanStackStartVitePluginCore(\n opts: TanStackStartVitePluginCoreOptions,\n startConfig: TanStackStartOutputConfig,\n): Array<PluginOption> {\n return [\n tanStackStartRouter({\n ...startConfig.tsr,\n target: opts.framework,\n autoCodeSplitting: true,\n }),\n resolveVirtualEntriesPlugin(opts, startConfig),\n {\n name: 'tanstack-start-core:config-client',\n async config(viteConfig) {\n const viteAppBase = trimPathRight(viteConfig.base || '/')\n globalThis.TSS_APP_BASE = viteAppBase\n\n const nitroOutputPublicDir = await (async () => {\n // Create a dummy nitro app to get the resolved public output path\n const dummyNitroApp = await createNitro({\n preset: startConfig.target,\n compatibilityDate: '2024-12-01',\n })\n\n const nitroOutputPublicDir = dummyNitroApp.options.output.publicDir\n await dummyNitroApp.close()\n\n return nitroOutputPublicDir\n })()\n\n const getClientEntryPath = (startConfig: TanStackStartOutputConfig) => {\n // when the user specifies a custom client entry path, we need to resolve it\n // relative to the root of the project, keeping in mind that if not specified\n // it will be /~start/default-client-entry which is a virtual path\n // that is resolved by vite to the actual client entry path\n const entry = startConfig.clientEntryPath.startsWith(\n '/~start/default-client-entry',\n )\n ? startConfig.clientEntryPath\n : vite.normalizePath(\n path.join(\n '/@fs',\n path.resolve(startConfig.root, startConfig.clientEntryPath),\n ),\n )\n\n return entry\n }\n\n return {\n base: viteAppBase,\n environments: {\n [VITE_ENVIRONMENT_NAMES.client]: {\n consumer: 'client',\n build: {\n manifest: true,\n rollupOptions: {\n input: {\n main: getClientEntryPath(startConfig),\n },\n output: {\n dir: path.resolve(startConfig.root, CLIENT_DIST_DIR),\n },\n // TODO: this should be removed\n external: ['node:fs', 'node:path', 'node:os', 'node:crypto'],\n },\n },\n },\n [VITE_ENVIRONMENT_NAMES.server]: {\n consumer: 'server',\n build: {\n ssr: true,\n // we don't write to the file system as the below 'capture-output' plugin will\n // capture the output and write it to the virtual file system\n write: false,\n copyPublicDir: false,\n rollupOptions: {\n output: {\n entryFileNames: SSR_ENTRY_FILE,\n },\n plugins: [\n {\n name: 'capture-output',\n generateBundle(_options, bundle) {\n // TODO: can this hook be called more than once?\n ssrBundle = bundle\n },\n },\n ],\n },\n commonjsOptions: {\n include: [/node_modules/],\n },\n },\n },\n },\n resolve: {\n noExternal: [\n '@tanstack/start-client',\n '@tanstack/start-client-core',\n '@tanstack/start-server',\n '@tanstack/start-server-core',\n '@tanstack/start-server-functions-fetcher',\n '@tanstack/start-server-functions-client',\n '@tanstack/start-server-functions-server',\n '@tanstack/start-router-manifest',\n '@tanstack/start-config',\n '@tanstack/server-functions-plugin',\n 'nitropack',\n '@tanstack/**start**',\n ...Object.values(VIRTUAL_MODULES),\n ],\n },\n optimizeDeps: {\n exclude: [...Object.values(VIRTUAL_MODULES)],\n },\n /* prettier-ignore */\n define: {\n // define is an esbuild function that replaces the any instances of given keys with the given values\n // i.e: __FRAMEWORK_NAME__ can be replaced with JSON.stringify(\"TanStack Start\")\n // This is not the same as injecting environment variables.\n\n ...defineReplaceEnv('TSS_CLIENT_ENTRY', getClientEntryPath(startConfig)), // This is consumed by the router-manifest, where the entry point is imported after the dev refresh runtime is resolved\n ...defineReplaceEnv('TSS_SERVER_FN_BASE', startConfig.serverFns.base),\n ...defineReplaceEnv('TSS_OUTPUT_PUBLIC_DIR', nitroOutputPublicDir),\n ...defineReplaceEnv('TSS_APP_BASE', viteAppBase)\n },\n }\n },\n },\n // N.B. TanStackStartCompilerPlugin must be before the TanStackServerFnPluginEnv\n startCompilerPlugin(opts.framework, {\n client: { envName: VITE_ENVIRONMENT_NAMES.client },\n server: { envName: VITE_ENVIRONMENT_NAMES.server },\n }),\n TanStackServerFnPluginEnv({\n // This is the ID that will be available to look up and import\n // our server function manifest and resolve its module\n manifestVirtualImportId: VIRTUAL_MODULES.serverFnManifest,\n manifestOutputFilename: path.join(\n '.tanstack',\n 'start',\n 'build',\n 'server',\n 'server-functions-manifest.json',\n ),\n client: {\n getRuntimeCode: () =>\n `import { createClientRpc } from '@tanstack/${opts.framework}-start/server-functions-client'`,\n replacer: (d) =>\n `createClientRpc('${d.functionId}', '${startConfig.serverFns.base}')`,\n envName: VITE_ENVIRONMENT_NAMES.client,\n },\n server: {\n getRuntimeCode: () =>\n `import { createServerRpc } from '@tanstack/${opts.framework}-start/server-functions-server'`,\n replacer: (d) =>\n `createServerRpc('${d.functionId}', '${startConfig.serverFns.base}', ${d.fn})`,\n envName: VITE_ENVIRONMENT_NAMES.server,\n },\n importer: (fn) => {\n const serverEnv = (globalThis as any).viteDevServer.environments[\n VITE_ENVIRONMENT_NAMES.server\n ]\n if (!serverEnv) {\n throw new Error(`'ssr' vite dev environment not found`)\n }\n return serverEnv.runner.import(fn.extractedFilename)\n },\n }),\n loadEnvPlugin(startConfig),\n startManifestPlugin(startConfig),\n devServerPlugin(),\n nitroPlugin(startConfig, () => ssrBundle),\n ]\n}\n\nfunction defineReplaceEnv<TKey extends string, TValue extends string>(\n key: TKey,\n value: TValue,\n): { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue } {\n return {\n [`process.env.${key}`]: JSON.stringify(value),\n [`import.meta.env.${key}`]: JSON.stringify(value),\n } as { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue }\n}\n"],"names":["nitroOutputPublicDir","startConfig"],"mappings":";;;;;;;;;;;;;;;AA4BsB,qBAAqB;AAmB3C,IAAI;AAEY,SAAA,4BACd,MACA,aACqB;AACd,SAAA;AAAA,IACL,oBAAoB;AAAA,MAClB,GAAG,YAAY;AAAA,MACf,QAAQ,KAAK;AAAA,MACb,mBAAmB;AAAA,IAAA,CACpB;AAAA,IACD,4BAA4B,MAAM,WAAW;AAAA,IAC7C;AAAA,MACE,MAAM;AAAA,MACN,MAAM,OAAO,YAAY;AACvB,cAAM,cAAc,cAAc,WAAW,QAAQ,GAAG;AACxD,mBAAW,eAAe;AAEpB,cAAA,uBAAuB,OAAO,YAAY;AAExC,gBAAA,gBAAgB,MAAM,YAAY;AAAA,YACtC,QAAQ,YAAY;AAAA,YACpB,mBAAmB;AAAA,UAAA,CACpB;AAEKA,gBAAAA,wBAAuB,cAAc,QAAQ,OAAO;AAC1D,gBAAM,cAAc,MAAM;AAEnBA,iBAAAA;AAAAA,QAAAA,GACN;AAEG,cAAA,qBAAqB,CAACC,iBAA2C;AAK/D,gBAAA,QAAQA,aAAY,gBAAgB;AAAA,YACxC;AAAA,UAAA,IAEEA,aAAY,kBACZ,KAAK;AAAA,YACH,KAAK;AAAA,cACH;AAAA,cACA,KAAK,QAAQA,aAAY,MAAMA,aAAY,eAAe;AAAA,YAAA;AAAA,UAE9D;AAEG,iBAAA;AAAA,QACT;AAEO,eAAA;AAAA,UACL,MAAM;AAAA,UACN,cAAc;AAAA,YACZ,CAAC,uBAAuB,MAAM,GAAG;AAAA,cAC/B,UAA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UAAU,KAAK;AAAA,IAC5C,CAAC,mBAAmB,GAAG,EAAE,GAAG,KAAK,UAAU,KAAK;AAAA,EAClD;AACF;"}
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["../../src/plugin.ts"],"sourcesContent":["import path from 'node:path'\nimport { createNitro } from 'nitropack'\nimport { trimPathRight } from '@tanstack/router-core'\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { TanStackServerFnPluginEnv } from '@tanstack/server-functions-plugin'\nimport * as vite from 'vite'\nimport { createTanStackConfig } from './schema'\nimport { nitroPlugin } from './nitro-plugin/plugin'\nimport { startManifestPlugin } from './start-manifest-plugin/plugin'\nimport { startCompilerPlugin } from './start-compiler-plugin'\nimport {\n CLIENT_DIST_DIR,\n SSR_ENTRY_FILE,\n VITE_ENVIRONMENT_NAMES,\n} from './constants'\nimport { tanStackStartRouter } from './start-router-plugin/plugin'\nimport { loadEnvPlugin } from './load-env-plugin/plugin'\nimport { devServerPlugin } from './dev-server-plugin/plugin'\nimport { resolveVirtualEntriesPlugin } from './resolve-virtual-entries-plugin/plugin'\nimport type { createTanStackStartOptionsSchema } from './schema'\nimport type { PluginOption, Rollup } from 'vite'\nimport type { z } from 'zod'\nimport type { CompileStartFrameworkOptions } from './compilers'\n\nexport type TanStackStartInputConfig = z.input<\n ReturnType<typeof createTanStackStartOptionsSchema>\n>\n\nconst defaultConfig = createTanStackConfig()\nexport function getTanStackStartOptions(opts?: TanStackStartInputConfig) {\n return defaultConfig.parse(opts)\n}\n\nexport type TanStackStartOutputConfig = ReturnType<\n typeof getTanStackStartOptions\n>\n\nexport interface TanStackStartVitePluginCoreOptions {\n framework: CompileStartFrameworkOptions\n getVirtualServerRootHandler: (ctx: {\n routerFilepath: string\n serverEntryFilepath: string\n }) => string\n getVirtualServerEntry: (ctx: { routerFilepath: string }) => string\n getVirtualClientEntry: (ctx: { routerFilepath: string }) => string\n}\n// this needs to live outside of the TanStackStartVitePluginCore since it will be invoked multiple times by vite\nlet ssrBundle: Rollup.OutputBundle\n\nexport function TanStackStartVitePluginCore(\n opts: TanStackStartVitePluginCoreOptions,\n startConfig: TanStackStartOutputConfig,\n): Array<PluginOption> {\n return [\n tanStackStartRouter({\n ...startConfig.tsr,\n target: opts.framework,\n autoCodeSplitting: true,\n }),\n resolveVirtualEntriesPlugin(opts, startConfig),\n {\n name: 'tanstack-start-core:config-client',\n async config(viteConfig) {\n const viteAppBase = trimPathRight(viteConfig.base || '/')\n globalThis.TSS_APP_BASE = viteAppBase\n\n const nitroOutputPublicDir = await (async () => {\n // Create a dummy nitro app to get the resolved public output path\n const dummyNitroApp = await createNitro({\n preset: startConfig.target,\n compatibilityDate: '2024-12-01',\n })\n\n const nitroOutputPublicDir = dummyNitroApp.options.output.publicDir\n await dummyNitroApp.close()\n\n return nitroOutputPublicDir\n })()\n\n const getClientEntryPath = (startConfig: TanStackStartOutputConfig) => {\n // when the user specifies a custom client entry path, we need to resolve it\n // relative to the root of the project, keeping in mind that if not specified\n // it will be /~start/default-client-entry which is a virtual path\n // that is resolved by vite to the actual client entry path\n const entry = startConfig.clientEntryPath.startsWith(\n '/~start/default-client-entry',\n )\n ? startConfig.clientEntryPath\n : vite.normalizePath(\n path.join(\n '/@fs',\n path.resolve(startConfig.root, startConfig.clientEntryPath),\n ),\n )\n\n return entry\n }\n\n return {\n base: viteAppBase,\n environments: {\n [VITE_ENVIRONMENT_NAMES.client]: {\n consumer: 'client',\n build: {\n manifest: true,\n rollupOptions: {\n input: {\n main: getClientEntryPath(startConfig),\n },\n output: {\n dir: path.resolve(startConfig.root, CLIENT_DIST_DIR),\n },\n // TODO: this should be removed\n external: ['node:fs', 'node:path', 'node:os', 'node:crypto'],\n },\n },\n },\n [VITE_ENVIRONMENT_NAMES.server]: {\n consumer: 'server',\n build: {\n ssr: true,\n // we don't write to the file system as the below 'capture-output' plugin will\n // capture the output and write it to the virtual file system\n write: false,\n copyPublicDir: false,\n rollupOptions: {\n output: {\n entryFileNames: SSR_ENTRY_FILE,\n },\n plugins: [\n {\n name: 'capture-output',\n generateBundle(_options, bundle) {\n // TODO: can this hook be called more than once?\n ssrBundle = bundle\n },\n },\n ],\n },\n commonjsOptions: {\n include: [/node_modules/],\n },\n },\n },\n },\n resolve: {\n noExternal: [\n '@tanstack/start-client',\n '@tanstack/start-client-core',\n '@tanstack/start-server',\n '@tanstack/start-server-core',\n '@tanstack/start-server-functions-fetcher',\n '@tanstack/start-server-functions-client',\n '@tanstack/start-server-functions-server',\n '@tanstack/start-router-manifest',\n '@tanstack/start-config',\n '@tanstack/server-functions-plugin',\n 'nitropack',\n '@tanstack/**start**',\n ...Object.values(VIRTUAL_MODULES),\n ],\n },\n optimizeDeps: {\n exclude: [...Object.values(VIRTUAL_MODULES)],\n },\n /* prettier-ignore */\n define: {\n // define is an esbuild function that replaces the any instances of given keys with the given values\n // i.e: __FRAMEWORK_NAME__ can be replaced with JSON.stringify(\"TanStack Start\")\n // This is not the same as injecting environment variables.\n\n ...defineReplaceEnv('TSS_CLIENT_ENTRY', getClientEntryPath(startConfig)), // This is consumed by the router-manifest, where the entry point is imported after the dev refresh runtime is resolved\n ...defineReplaceEnv('TSS_SERVER_FN_BASE', startConfig.serverFns.base),\n ...defineReplaceEnv('TSS_OUTPUT_PUBLIC_DIR', nitroOutputPublicDir),\n ...defineReplaceEnv('TSS_APP_BASE', viteAppBase)\n },\n }\n },\n },\n // N.B. TanStackStartCompilerPlugin must be before the TanStackServerFnPluginEnv\n startCompilerPlugin(opts.framework, {\n client: { envName: VITE_ENVIRONMENT_NAMES.client },\n server: { envName: VITE_ENVIRONMENT_NAMES.server },\n }),\n TanStackServerFnPluginEnv({\n // This is the ID that will be available to look up and import\n // our server function manifest and resolve its module\n manifestVirtualImportId: VIRTUAL_MODULES.serverFnManifest,\n manifestOutputFilename: path.join(\n '.tanstack',\n 'start',\n 'build',\n 'server',\n 'server-functions-manifest.json',\n ),\n client: {\n getRuntimeCode: () =>\n `import { createClientRpc } from '@tanstack/${opts.framework}-start/server-functions-client'`,\n replacer: (d) =>\n `createClientRpc('${d.functionId}', '${startConfig.serverFns.base}')`,\n envName: VITE_ENVIRONMENT_NAMES.client,\n },\n server: {\n getRuntimeCode: () =>\n `import { createServerRpc } from '@tanstack/${opts.framework}-start/server-functions-server'`,\n replacer: (d) =>\n `createServerRpc('${d.functionId}', '${startConfig.serverFns.base}', ${d.fn})`,\n envName: VITE_ENVIRONMENT_NAMES.server,\n },\n importer: (fn) => {\n const serverEnv = (globalThis as any).viteDevServer.environments[\n VITE_ENVIRONMENT_NAMES.server\n ]\n if (!serverEnv) {\n throw new Error(`'ssr' vite dev environment not found`)\n }\n return serverEnv.runner.import(fn.extractedFilename)\n },\n }),\n loadEnvPlugin(startConfig),\n startManifestPlugin(startConfig),\n devServerPlugin(),\n nitroPlugin(startConfig, () => ssrBundle),\n {\n name: 'tanstack-start:core:capture-client-bundle',\n applyToEnvironment(e) {\n return e.config.consumer === 'client'\n },\n enforce: 'post',\n generateBundle(_options, bundle) {\n globalThis.TSS_CLIENT_BUNDLE = bundle\n },\n },\n ]\n}\n\nfunction defineReplaceEnv<TKey extends string, TValue extends string>(\n key: TKey,\n value: TValue,\n): { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue } {\n return {\n [`process.env.${key}`]: JSON.stringify(value),\n [`import.meta.env.${key}`]: JSON.stringify(value),\n } as { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue }\n}\n"],"names":["nitroOutputPublicDir","startConfig"],"mappings":";;;;;;;;;;;;;;;AA4BsB,qBAAqB;AAmB3C,IAAI;AAEY,SAAA,4BACd,MACA,aACqB;AACd,SAAA;AAAA,IACL,oBAAoB;AAAA,MAClB,GAAG,YAAY;AAAA,MACf,QAAQ,KAAK;AAAA,MACb,mBAAmB;AAAA,IAAA,CACpB;AAAA,IACD,4BAA4B,MAAM,WAAW;AAAA,IAC7C;AAAA,MACE,MAAM;AAAA,MACN,MAAM,OAAO,YAAY;AACvB,cAAM,cAAc,cAA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A,MACA,QAAQ;AAAA,QACN,gBAAgB,MACd,8CAA8C,KAAK,SAAS;AAAA,QAC9D,UAAU,CAAC,MACT,oBAAoB,EAAE,UAAU,OAAO,YAAY,UAAU,IAAI,MAAM,EAAE,EAAE;AAAA,QAC7E,SAAS,uBAAuB;AAAA,MAClC;AAAA,MACA,UAAU,CAAC,OAAO;AAChB,cAAM,YAAa,WAAmB,cAAc,aAClD,uBAAuB,MACzB;AACA,YAAI,CAAC,WAAW;AACR,gBAAA,IAAI,MAAM,sCAAsC;AAAA,QAAA;AAExD,eAAO,UAAU,OAAO,OAAO,GAAG,iBAAiB;AAAA,MAAA;AAAA,IACrD,CACD;AAAA,IACD,cAAc,WAAW;AAAA,IACzB,oBAA+B;AAAA,IAC/B,gBAAgB;AAAA,IAChB,YAAY,aAAa,MAAM,SAAS;AAAA,IACxC;AAAA,MACE,MAAM;AAAA,MACN,mBAAmB,GAAG;AACb,eAAA,EAAE,OAAO,aAAa;AAAA,MAC/B;AAAA,MACA,SAAS;AAAA,MACT,eAAe,UAAU,QAAQ;AAC/B,mBAAW,oBAAoB;AAAA,MAAA;AAAA,IACjC;AAAA,EAEJ;AACF;AAEA,SAAS,iBACP,KACA,OACsE;AAC/D,SAAA;AAAA,IACL,CAAC,eAAe,GAAG,EAAE,GAAG,KAAK,UAAU,KAAK;AAAA,IAC5C,CAAC,mBAAmB,GAAG,EAAE,GAAG,KAAK,UAAU,KAAK;AAAA,EAClD;AACF;"}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
import { PluginOption } from 'vite';
|
|
1
|
+
import { PluginOption, Rollup } from 'vite';
|
|
2
|
+
import { RouterManagedTag } from '@tanstack/router-core';
|
|
2
3
|
import { TanStackStartOutputConfig } from '../plugin.js';
|
|
4
|
+
export declare const getCSSRecursively: (chunk: Rollup.OutputChunk, chunksByFileName: Map<string, Rollup.OutputChunk>, basePath: string) => RouterManagedTag[];
|
|
3
5
|
export declare function startManifestPlugin(opts: TanStackStartOutputConfig): PluginOption;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
2
1
|
import path from "node:path";
|
|
3
2
|
import { joinURL } from "ufo";
|
|
4
3
|
import { rootRouteId } from "@tanstack/router-core";
|
|
5
4
|
import { VIRTUAL_MODULES } from "@tanstack/start-server-core";
|
|
5
|
+
import { tsrSplit } from "@tanstack/router-plugin";
|
|
6
6
|
import { resolveViteId } from "../utils.js";
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
const getCSSRecursively = (chunk, chunksByFileName, basePath) => {
|
|
8
|
+
var _a;
|
|
9
9
|
const result = [];
|
|
10
|
-
for (const cssFile of
|
|
10
|
+
for (const cssFile of ((_a = chunk.viteMetadata) == null ? void 0 : _a.importedCss) ?? []) {
|
|
11
11
|
result.push({
|
|
12
12
|
tag: "link",
|
|
13
13
|
attrs: {
|
|
@@ -17,11 +17,11 @@ const getCSSRecursively = (file, filesByRouteFilePath, basePath) => {
|
|
|
17
17
|
}
|
|
18
18
|
});
|
|
19
19
|
}
|
|
20
|
-
for (const
|
|
21
|
-
const
|
|
22
|
-
if (
|
|
20
|
+
for (const importedFileName of chunk.imports) {
|
|
21
|
+
const importedChunk = chunksByFileName.get(importedFileName);
|
|
22
|
+
if (importedChunk) {
|
|
23
23
|
result.push(
|
|
24
|
-
...getCSSRecursively(
|
|
24
|
+
...getCSSRecursively(importedChunk, chunksByFileName, basePath)
|
|
25
25
|
);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -36,9 +36,6 @@ function startManifestPlugin(opts) {
|
|
|
36
36
|
configResolved(resolvedConfig) {
|
|
37
37
|
config = resolvedConfig;
|
|
38
38
|
},
|
|
39
|
-
// configEnvironment(env, envConfig) {
|
|
40
|
-
// config = envConfig.
|
|
41
|
-
// },
|
|
42
39
|
resolveId: {
|
|
43
40
|
filter: { id: new RegExp(VIRTUAL_MODULES.startManifest) },
|
|
44
41
|
handler(id) {
|
|
@@ -53,7 +50,6 @@ function startManifestPlugin(opts) {
|
|
|
53
50
|
id: new RegExp(resolvedModuleId)
|
|
54
51
|
},
|
|
55
52
|
handler(id) {
|
|
56
|
-
var _a;
|
|
57
53
|
if (id === resolvedModuleId) {
|
|
58
54
|
if (this.environment.config.consumer !== "server") {
|
|
59
55
|
return `export default {}`;
|
|
@@ -64,77 +60,85 @@ function startManifestPlugin(opts) {
|
|
|
64
60
|
})`;
|
|
65
61
|
}
|
|
66
62
|
const APP_BASE = globalThis.TSS_APP_BASE;
|
|
67
|
-
const clientViteManifestPath = path.resolve(
|
|
68
|
-
opts.root,
|
|
69
|
-
CLIENT_DIST_DIR,
|
|
70
|
-
".vite",
|
|
71
|
-
"manifest.json"
|
|
72
|
-
);
|
|
73
|
-
let viteManifest;
|
|
74
|
-
try {
|
|
75
|
-
viteManifest = JSON.parse(
|
|
76
|
-
readFileSync(clientViteManifestPath, "utf-8")
|
|
77
|
-
);
|
|
78
|
-
} catch (err) {
|
|
79
|
-
console.error(err);
|
|
80
|
-
throw new Error(
|
|
81
|
-
`Could not find the production client vite manifest at '${clientViteManifestPath}'!`
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
63
|
const routeTreeRoutes = globalThis.TSS_ROUTES_MANIFEST.routes;
|
|
85
64
|
let entryFile;
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
65
|
+
const clientBundle = globalThis.TSS_CLIENT_BUNDLE;
|
|
66
|
+
const chunksByFileName = /* @__PURE__ */ new Map();
|
|
67
|
+
const routeChunks = {};
|
|
68
|
+
for (const bundleEntry of Object.values(clientBundle)) {
|
|
69
|
+
if (bundleEntry.type === "chunk") {
|
|
70
|
+
chunksByFileName.set(bundleEntry.fileName, bundleEntry);
|
|
71
|
+
if (bundleEntry.isEntry) {
|
|
72
|
+
if (entryFile) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
`multiple entries detected: ${entryFile.fileName} ${bundleEntry.fileName}`
|
|
94
75
|
);
|
|
95
76
|
}
|
|
96
|
-
entryFile =
|
|
77
|
+
entryFile = bundleEntry;
|
|
97
78
|
}
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
79
|
+
const routePieces = bundleEntry.moduleIds.flatMap((m) => {
|
|
80
|
+
const [id2, query] = m.split("?");
|
|
81
|
+
if (id2 === void 0) {
|
|
82
|
+
throw new Error("expected id to be defined");
|
|
83
|
+
}
|
|
84
|
+
if (query === void 0) {
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
const searchParams = new URLSearchParams(query);
|
|
88
|
+
const split = searchParams.get(tsrSplit);
|
|
89
|
+
if (split !== null) {
|
|
90
|
+
return {
|
|
91
|
+
id: id2,
|
|
92
|
+
split
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return [];
|
|
112
96
|
});
|
|
113
|
-
if (
|
|
114
|
-
|
|
97
|
+
if (routePieces.length > 0) {
|
|
98
|
+
routePieces.forEach((r) => {
|
|
99
|
+
let array = routeChunks[r.id];
|
|
100
|
+
if (array === void 0) {
|
|
101
|
+
array = [];
|
|
102
|
+
routeChunks[r.id] = array;
|
|
103
|
+
}
|
|
104
|
+
array.push(bundleEntry);
|
|
105
|
+
});
|
|
115
106
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
Object.entries(routeTreeRoutes).forEach(([routeId, v]) => {
|
|
110
|
+
if (!v.filePath) {
|
|
111
|
+
throw new Error(`expected filePath to be set for ${routeId}`);
|
|
112
|
+
}
|
|
113
|
+
const chunks = routeChunks[v.filePath];
|
|
114
|
+
if (chunks) {
|
|
115
|
+
chunks.forEach((chunk) => {
|
|
116
|
+
const preloads = chunk.imports.map((d) => {
|
|
117
|
+
const assetPath = joinURL(APP_BASE, d);
|
|
118
|
+
return assetPath;
|
|
119
|
+
});
|
|
120
|
+
preloads.unshift(path.join(APP_BASE, chunk.fileName));
|
|
121
|
+
const cssAssetsList = getCSSRecursively(
|
|
122
|
+
chunk,
|
|
123
|
+
chunksByFileName,
|
|
124
|
+
APP_BASE
|
|
125
|
+
);
|
|
126
|
+
routeTreeRoutes[routeId] = {
|
|
127
|
+
...v,
|
|
128
|
+
assets: [...v.assets || [], ...cssAssetsList],
|
|
129
|
+
preloads: [...v.preloads || [], ...preloads]
|
|
130
|
+
};
|
|
131
|
+
});
|
|
126
132
|
}
|
|
127
133
|
});
|
|
128
134
|
if (entryFile) {
|
|
129
135
|
routeTreeRoutes[rootRouteId].preloads = [
|
|
130
|
-
joinURL(APP_BASE, entryFile.
|
|
131
|
-
...
|
|
132
|
-
(d) => joinURL(APP_BASE, viteManifest[d].file)
|
|
133
|
-
)) || []
|
|
136
|
+
joinURL(APP_BASE, entryFile.fileName),
|
|
137
|
+
...entryFile.imports.map((d) => joinURL(APP_BASE, d))
|
|
134
138
|
];
|
|
135
139
|
const entryCssAssetsList = getCSSRecursively(
|
|
136
140
|
entryFile,
|
|
137
|
-
|
|
141
|
+
chunksByFileName,
|
|
138
142
|
APP_BASE
|
|
139
143
|
);
|
|
140
144
|
routeTreeRoutes[rootRouteId].assets = [
|
|
@@ -143,15 +147,15 @@ function startManifestPlugin(opts) {
|
|
|
143
147
|
{
|
|
144
148
|
tag: "script",
|
|
145
149
|
attrs: {
|
|
146
|
-
src: joinURL(APP_BASE, entryFile.
|
|
150
|
+
src: joinURL(APP_BASE, entryFile.fileName),
|
|
147
151
|
type: "module"
|
|
148
152
|
}
|
|
149
153
|
}
|
|
150
154
|
];
|
|
151
155
|
}
|
|
152
156
|
const recurseRoute = (route, seenPreloads = {}) => {
|
|
153
|
-
var
|
|
154
|
-
route.preloads = (
|
|
157
|
+
var _a;
|
|
158
|
+
route.preloads = (_a = route.preloads) == null ? void 0 : _a.filter((preload) => {
|
|
155
159
|
if (seenPreloads[preload]) {
|
|
156
160
|
return false;
|
|
157
161
|
}
|
|
@@ -177,6 +181,7 @@ function startManifestPlugin(opts) {
|
|
|
177
181
|
};
|
|
178
182
|
}
|
|
179
183
|
export {
|
|
184
|
+
getCSSRecursively,
|
|
180
185
|
startManifestPlugin
|
|
181
186
|
};
|
|
182
187
|
//# sourceMappingURL=plugin.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sources":["../../../src/start-manifest-plugin/plugin.ts"],"sourcesContent":["import { readFileSync } from 'node:fs'\nimport path from 'node:path'\nimport { joinURL } from 'ufo'\nimport { rootRouteId } from '@tanstack/router-core'\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { resolveViteId } from '../utils'\nimport { CLIENT_DIST_DIR } from '../constants'\nimport type {\n PluginOption,\n ResolvedConfig,\n Manifest as ViteManifest,\n ManifestChunk as ViteManifestChunk,\n} from 'vite'\nimport type { RouterManagedTag } from '@tanstack/router-core'\nimport type { TanStackStartOutputConfig } from '../plugin'\n\nconst getCSSRecursively = (\n file: ViteManifestChunk,\n filesByRouteFilePath: ViteManifest,\n basePath: string,\n) => {\n const result: Array<RouterManagedTag> = []\n\n // Get all css imports from the file\n for (const cssFile of file.css ?? []) {\n result.push({\n tag: 'link',\n attrs: {\n rel: 'stylesheet',\n href: joinURL(basePath, cssFile),\n type: 'text/css',\n },\n })\n }\n\n // Recursively get CSS from imports\n for (const imp of file.imports ?? []) {\n const importInfo = filesByRouteFilePath[imp]\n if (importInfo) {\n result.push(\n ...getCSSRecursively(importInfo, filesByRouteFilePath, basePath),\n )\n }\n }\n\n return result\n}\n\nconst resolvedModuleId = resolveViteId(VIRTUAL_MODULES.startManifest)\nexport function startManifestPlugin(\n opts: TanStackStartOutputConfig,\n): PluginOption {\n let config: ResolvedConfig\n\n return {\n name: 'tanstack-start:start-manifest-plugin',\n enforce: 'pre',\n\n configResolved(resolvedConfig) {\n config = resolvedConfig\n },\n // configEnvironment(env, envConfig) {\n // config = envConfig.\n // },\n resolveId: {\n filter: { id: new RegExp(VIRTUAL_MODULES.startManifest) },\n handler(id) {\n if (id === VIRTUAL_MODULES.startManifest) {\n return resolvedModuleId\n }\n return undefined\n },\n },\n load: {\n filter: {\n id: new RegExp(resolvedModuleId),\n },\n handler(id) {\n if (id === resolvedModuleId) {\n if (this.environment.config.consumer !== 'server') {\n // this will ultimately fail the build if the plugin is used outside the server environment\n // TODO: do we need special handling for `serve`?\n return `export default {}`\n }\n\n // If we're in development, return a dummy manifest\n if (config.command === 'serve') {\n return `export const tsrStartManifest = () => ({\n routes: {}\n })`\n }\n\n // This is the basepath for the application\n const APP_BASE = globalThis.TSS_APP_BASE\n\n const clientViteManifestPath = path.resolve(\n opts.root,\n CLIENT_DIST_DIR,\n '.vite',\n 'manifest.json',\n )\n\n let viteManifest: ViteManifest\n try {\n viteManifest = JSON.parse(\n readFileSync(clientViteManifestPath, 'utf-8'),\n )\n } catch (err) {\n console.error(err)\n throw new Error(\n `Could not find the production client vite manifest at '${clientViteManifestPath}'!`,\n )\n }\n\n // This the manifest pulled from the generated route tree and later used by the Router.\n // i.e what's located in `src/generatedRouteTree.gen.ts`\n const routeTreeRoutes = globalThis.TSS_ROUTES_MANIFEST.routes\n\n // This is where hydration will start, from when the SSR'd page reaches the browser.\n // By default, this'd be the virtual entry of `/~start/default-client-entry.tsx`, unless a custom entry is provided.\n let entryFile: ViteManifestChunk | undefined\n\n const filesByRouteFilePath: ViteManifest = Object.fromEntries(\n Object.entries(viteManifest).map(([k, v]) => {\n if (v.isEntry) {\n if (entryFile !== undefined) {\n console.error(\n `multiple entries detected`,\n entryFile.file,\n v.file,\n )\n }\n entryFile = v\n }\n\n const rPath = k.split('?')[0]\n\n return [rPath, v]\n }, {}),\n )\n\n const routesDirectoryFromRoot = path.relative(\n opts.root,\n opts.tsr.routesDirectory,\n )\n\n // Add preloads to the routes from the vite manifest\n Object.entries(routeTreeRoutes).forEach(([routeId, v]) => {\n const file =\n filesByRouteFilePath[\n path.posix.join(routesDirectoryFromRoot, v.filePath as string)\n ]\n\n if (file) {\n // Map the relevant imports to their route paths,\n // so that it can be imported in the browser.\n const preloads = (file.imports ?? []).map((d) => {\n const assetPath = joinURL(APP_BASE, viteManifest[d]!.file)\n return assetPath\n })\n\n // Since this is the most important JS entry for the route,\n // it should be moved to the front of the preloads so that\n // it has the best chance of being loaded first.\n if (file.file) {\n preloads.unshift(path.join(APP_BASE, file.file))\n }\n\n const cssAssetsList = getCSSRecursively(\n file,\n filesByRouteFilePath,\n APP_BASE,\n )\n\n routeTreeRoutes[routeId] = {\n ...v,\n assets: [...(v.assets || []), ...cssAssetsList],\n preloads,\n }\n }\n })\n\n if (entryFile) {\n routeTreeRoutes[rootRouteId]!.preloads = [\n joinURL(APP_BASE, entryFile.file),\n ...(entryFile.imports?.map((d) =>\n joinURL(APP_BASE, viteManifest[d]!.file),\n ) || []),\n ]\n\n // Gather all the CSS files from the entry file in\n // the `css` key and add them to the root route\n const entryCssAssetsList = getCSSRecursively(\n entryFile,\n filesByRouteFilePath,\n APP_BASE,\n )\n\n routeTreeRoutes[rootRouteId]!.assets = [\n ...(routeTreeRoutes[rootRouteId]!.assets || []),\n ...entryCssAssetsList,\n {\n tag: 'script',\n attrs: {\n src: joinURL(APP_BASE, entryFile.file),\n type: 'module',\n },\n },\n ]\n }\n\n const recurseRoute = (\n route: {\n preloads?: Array<string>\n children?: Array<any>\n },\n seenPreloads = {} as Record<string, true>,\n ) => {\n route.preloads = route.preloads?.filter((preload) => {\n if (seenPreloads[preload]) {\n return false\n }\n seenPreloads[preload] = true\n return true\n })\n\n if (route.children) {\n route.children.forEach((child) => {\n const childRoute = routeTreeRoutes[child]!\n recurseRoute(childRoute, { ...seenPreloads })\n })\n }\n }\n\n // @ts-expect-error\n recurseRoute(routeTreeRoutes[rootRouteId])\n\n const routesManifest = {\n routes: routeTreeRoutes,\n }\n\n return `export const tsrStartManifest = () => (${JSON.stringify(routesManifest)})`\n }\n\n return undefined\n },\n },\n }\n}\n"],"names":["_a"],"mappings":";;;;;;;AAgBA,MAAM,oBAAoB,CACxB,MACA,sBACA,aACG;AACH,QAAM,SAAkC,CAAC;AAGzC,aAAW,WAAW,KAAK,OAAO,CAAA,GAAI;AACpC,WAAO,KAAK;AAAA,MACV,KAAK;AAAA,MACL,OAAO;AAAA,QACL,KAAK;AAAA,QACL,MAAM,QAAQ,UAAU,OAAO;AAAA,QAC/B,MAAM;AAAA,MAAA;AAAA,IACR,CACD;AAAA,EAAA;AAIH,aAAW,OAAO,KAAK,WAAW,CAAA,GAAI;AAC9B,UAAA,aAAa,qBAAqB,GAAG;AAC3C,QAAI,YAAY;AACP,aAAA;AAAA,QACL,GAAG,kBAAkB,YAAY,sBAAsB,QAAQ;AAAA,MACjE;AAAA,IAAA;AAAA,EACF;AAGK,SAAA;AACT;AAEA,MAAM,mBAAmB,cAAc,gBAAgB,aAAa;AAC7D,SAAS,oBACd,MACc;AACV,MAAA;AAEG,SAAA;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IAET,eAAe,gBAAgB;AACpB,eAAA;AAAA,IACX;AAAA;AAAA;AAAA;AAAA,IAIA,WAAW;AAAA,MACT,QAAQ,EAAE,IAAI,IAAI,OAAO,gBAAgB,aAAa,EAAE;AAAA,MACxD,QAAQ,IAAI;AACN,YAAA,OAAO,gBAAgB,eAAe;AACjC,iBAAA;AAAA,QAAA;AAEF,eAAA;AAAA,MAAA;AAAA,IAEX;AAAA,IACA,MAAM;AAAA,MACJ,QAAQ;AAAA,QACN,IAAI,IAAI,OAAO,gBAAgB;AAAA,MACjC;AAAA,MACA,QAAQ,IAAI;;AACV,YAAI,OAAO,kBAAkB;AAC3B,cAAI,KAAK,YAAY,OAAO,aAAa,UAAU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GAAG,cAAc;AAAA,cAAA,CAC7C;AAAA,YAAA;AAAA,UAEL;AAGa,uBAAA,gBAAgB,WAAW,CAAC;AAEzC,gBAAM,iBAAiB;AAAA,YACrB,QAAQ;AAAA,UACV;AAEA,iBAAO,0CAA0C,KAAK,UAAU,cAAc,CAAC;AAAA,QAAA;AAG1E,eAAA;AAAA,MAAA;AAAA,IACT;AAAA,EAEJ;AACF;"}
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["../../../src/start-manifest-plugin/plugin.ts"],"sourcesContent":["import path from 'node:path'\nimport { joinURL } from 'ufo'\nimport { rootRouteId } from '@tanstack/router-core'\nimport { VIRTUAL_MODULES } from '@tanstack/start-server-core'\nimport { tsrSplit } from '@tanstack/router-plugin'\nimport { resolveViteId } from '../utils'\nimport type { PluginOption, ResolvedConfig, Rollup } from 'vite'\nimport type { RouterManagedTag } from '@tanstack/router-core'\nimport type { TanStackStartOutputConfig } from '../plugin'\n\nexport const getCSSRecursively = (\n chunk: Rollup.OutputChunk,\n chunksByFileName: Map<string, Rollup.OutputChunk>,\n basePath: string,\n) => {\n const result: Array<RouterManagedTag> = []\n\n // Get all css imports from the file\n for (const cssFile of chunk.viteMetadata?.importedCss ?? []) {\n result.push({\n tag: 'link',\n attrs: {\n rel: 'stylesheet',\n href: joinURL(basePath, cssFile),\n type: 'text/css',\n },\n })\n }\n\n // Recursively get CSS from imports\n for (const importedFileName of chunk.imports) {\n const importedChunk = chunksByFileName.get(importedFileName)\n if (importedChunk) {\n result.push(\n ...getCSSRecursively(importedChunk, chunksByFileName, basePath),\n )\n }\n }\n\n return result\n}\n\nconst resolvedModuleId = resolveViteId(VIRTUAL_MODULES.startManifest)\nexport function startManifestPlugin(\n opts: TanStackStartOutputConfig,\n): PluginOption {\n let config: ResolvedConfig\n\n return {\n name: 'tanstack-start:start-manifest-plugin',\n enforce: 'pre',\n\n configResolved(resolvedConfig) {\n config = resolvedConfig\n },\n resolveId: {\n filter: { id: new RegExp(VIRTUAL_MODULES.startManifest) },\n handler(id) {\n if (id === VIRTUAL_MODULES.startManifest) {\n return resolvedModuleId\n }\n return undefined\n },\n },\n load: {\n filter: {\n id: new RegExp(resolvedModuleId),\n },\n handler(id) {\n if (id === resolvedModuleId) {\n if (this.environment.config.consumer !== 'server') {\n // this will ultimately fail the build if the plugin is used outside the server environment\n // TODO: do we need special handling for `serve`?\n return `export default {}`\n }\n\n // If we're in development, return a dummy manifest\n if (config.command === 'serve') {\n return `export const tsrStartManifest = () => ({\n routes: {}\n })`\n }\n\n // This is the basepath for the application\n const APP_BASE = globalThis.TSS_APP_BASE\n\n // This the manifest pulled from the generated route tree and later used by the Router.\n // i.e what's located in `src/routeTree.gen.ts`\n const routeTreeRoutes = globalThis.TSS_ROUTES_MANIFEST.routes\n\n // This is where hydration will start, from when the SSR'd page reaches the browser.\n // By default, this'd be the virtual entry of `/~start/default-client-entry.tsx`, unless a custom entry is provided.\n let entryFile: Rollup.OutputChunk | undefined\n\n const clientBundle = globalThis.TSS_CLIENT_BUNDLE\n const chunksByFileName = new Map<string, Rollup.OutputChunk>()\n\n const routeChunks: Record<\n string /** fullPath of route file **/,\n Array<Rollup.OutputChunk>\n > = {}\n for (const bundleEntry of Object.values(clientBundle)) {\n if (bundleEntry.type === 'chunk') {\n chunksByFileName.set(bundleEntry.fileName, bundleEntry)\n if (bundleEntry.isEntry) {\n if (entryFile) {\n throw new Error(\n `multiple entries detected: ${entryFile.fileName} ${bundleEntry.fileName}`,\n )\n }\n entryFile = bundleEntry\n }\n const routePieces = bundleEntry.moduleIds.flatMap((m) => {\n const [id, query] = m.split('?')\n if (id === undefined) {\n throw new Error('expected id to be defined')\n }\n if (query === undefined) {\n return []\n }\n const searchParams = new URLSearchParams(query)\n const split = searchParams.get(tsrSplit)\n\n if (split !== null) {\n return {\n id,\n split,\n }\n }\n return []\n })\n if (routePieces.length > 0) {\n routePieces.forEach((r) => {\n let array = routeChunks[r.id]\n if (array === undefined) {\n array = []\n routeChunks[r.id] = array\n }\n array.push(bundleEntry)\n })\n }\n }\n }\n\n // Add preloads to the routes from the vite manifest\n Object.entries(routeTreeRoutes).forEach(([routeId, v]) => {\n if (!v.filePath) {\n throw new Error(`expected filePath to be set for ${routeId}`)\n }\n const chunks = routeChunks[v.filePath]\n if (chunks) {\n chunks.forEach((chunk) => {\n // Map the relevant imports to their route paths,\n // so that it can be imported in the browser.\n const preloads = chunk.imports.map((d) => {\n const assetPath = joinURL(APP_BASE, d)\n return assetPath\n })\n\n // Since this is the most important JS entry for the route,\n // it should be moved to the front of the preloads so that\n // it has the best chance of being loaded first.\n preloads.unshift(path.join(APP_BASE, chunk.fileName))\n\n const cssAssetsList = getCSSRecursively(\n chunk,\n chunksByFileName,\n APP_BASE,\n )\n\n routeTreeRoutes[routeId] = {\n ...v,\n assets: [...(v.assets || []), ...cssAssetsList],\n preloads: [...(v.preloads || []), ...preloads],\n }\n })\n }\n })\n\n if (entryFile) {\n routeTreeRoutes[rootRouteId]!.preloads = [\n joinURL(APP_BASE, entryFile.fileName),\n ...entryFile.imports.map((d) => joinURL(APP_BASE, d)),\n ]\n\n // Gather all the CSS files from the entry file in\n // the `css` key and add them to the root route\n const entryCssAssetsList = getCSSRecursively(\n entryFile,\n chunksByFileName,\n APP_BASE,\n )\n\n routeTreeRoutes[rootRouteId]!.assets = [\n ...(routeTreeRoutes[rootRouteId]!.assets || []),\n ...entryCssAssetsList,\n {\n tag: 'script',\n attrs: {\n src: joinURL(APP_BASE, entryFile.fileName),\n type: 'module',\n },\n },\n ]\n }\n\n const recurseRoute = (\n route: {\n preloads?: Array<string>\n children?: Array<any>\n },\n seenPreloads = {} as Record<string, true>,\n ) => {\n route.preloads = route.preloads?.filter((preload) => {\n if (seenPreloads[preload]) {\n return false\n }\n seenPreloads[preload] = true\n return true\n })\n\n if (route.children) {\n route.children.forEach((child) => {\n const childRoute = routeTreeRoutes[child]!\n recurseRoute(childRoute, { ...seenPreloads })\n })\n }\n }\n\n recurseRoute(routeTreeRoutes[rootRouteId]!)\n\n const routesManifest = {\n routes: routeTreeRoutes,\n }\n\n return `export const tsrStartManifest = () => (${JSON.stringify(routesManifest)})`\n }\n\n return undefined\n },\n },\n }\n}\n"],"names":["id"],"mappings":";;;;;;AAUO,MAAM,oBAAoB,CAC/B,OACA,kBACA,aACG;;AACH,QAAM,SAAkC,CAAC;AAGzC,aAAW,aAAW,WAAM,iBAAN,mBAAoB,gBAAe,CAAA,GAAI;AAC3D,WAAO,KAAK;AAAA,MACV,KAAK;AAAA,MACL,OAAO;AAAA,QACL,KAAK;AAAA,QACL,MAAM,QAAQ,UAAU,OAAO;AAAA,QAC/B,MAAM;AAAA,MAAA;AAAA,IACR,CACD;AAAA,EAAA;AAIQ,aAAA,oBAAoB,MAAM,SAAS;AACtC,UAAA,gBAAgB,iBAAiB,IAAI,gBAAgB;AAC3D,QAAI,eAAe;AACV,aAAA;AAAA,QACL,GAAG,kBAAkB,eAAe,kBAAkB,QAAQ;AAAA,MAChE;AAAA,IAAA;AAAA,EACF;AAGK,SAAA;AACT;AAEA,MAAM,mBAAmB,cAAc,gBAAgB,aAAa;AAC7D,SAAS,oBACd,MACc;AACV,MAAA;AAEG,SAAA;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IAET,eAAe,gBAAgB;AACpB,eAAA;AAAA,IACX;AAAA,IACA,WAAW;AAAA,MACT,QAAQ,EAAE,IAAI,IAAI,OAAO,gBAAgB,aAAa,EAAE;AAAA,MACxD,QAAQ,IAAI;AACN,YAAA,OAAO,gBAAgB,eAAe;AACjC,iBAAA;AAAA,QAAA;AAEF,eAAA;AAAA,MAAA;AAAA,IAEX;AAAA,IACA,MAAM;AAAA,MACJ,QAAQ;AAAA,QACN,IAAI,IAAI,OAAO,gBAAgB;AAAA,MACjC;AAAA,MACA,QAAQ,IAAI;AACV,YAAI,OAAO,kBAAkB;AAC3B,cAAI,KAAK,YAAY,OAAO,aAAa,UAAU;AAG1C,mBAAA;AAAA,UAAA;AAIL,cAAA,OAAO,YA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CACnB,OAIA,eAAe,CAAA,MACZ;;AACH,kBAAM,YAAW,WAAM,aAAN,mBAAgB,OAAO,CAAC,YAAY;AAC/C,kBAAA,aAAa,OAAO,GAAG;AAClB,uBAAA;AAAA,cAAA;AAET,2BAAa,OAAO,IAAI;AACjB,qBAAA;AAAA,YAAA;AAGT,gBAAI,MAAM,UAAU;AACZ,oBAAA,SAAS,QAAQ,CAAC,UAAU;AAC1B,sBAAA,aAAa,gBAAgB,KAAK;AACxC,6BAAa,YAAY,EAAE,GAAG,cAAc;AAAA,cAAA,CAC7C;AAAA,YAAA;AAAA,UAEL;AAEa,uBAAA,gBAAgB,WAAW,CAAE;AAE1C,gBAAM,iBAAiB;AAAA,YACrB,QAAQ;AAAA,UACV;AAEA,iBAAO,0CAA0C,KAAK,UAAU,cAAc,CAAC;AAAA,QAAA;AAG1E,eAAA;AAAA,MAAA;AAAA,IACT;AAAA,EAEJ;AACF;"}
|
|
@@ -11,7 +11,7 @@ function routesManifestPlugin() {
|
|
|
11
11
|
}
|
|
12
12
|
const routesManifest = {
|
|
13
13
|
[rootRouteId]: {
|
|
14
|
-
filePath: rootRouteNode.
|
|
14
|
+
filePath: rootRouteNode.fullPath,
|
|
15
15
|
children: routeTree.acc.routeTree.map((d) => d.routePath)
|
|
16
16
|
},
|
|
17
17
|
...Object.fromEntries(
|
|
@@ -21,7 +21,7 @@ function routesManifestPlugin() {
|
|
|
21
21
|
return [
|
|
22
22
|
filePathId,
|
|
23
23
|
{
|
|
24
|
-
filePath: d.
|
|
24
|
+
filePath: d.fullPath,
|
|
25
25
|
parent: ((_a = d.parent) == null ? void 0 : _a.routePath) ? d.parent.routePath : void 0,
|
|
26
26
|
children: (_b = d.children) == null ? void 0 : _b.map((childRoute) => childRoute.routePath)
|
|
27
27
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routes-manifest-plugin.js","sources":["../../../../src/start-router-plugin/generator-plugins/routes-manifest-plugin.ts"],"sourcesContent":["import { rootRouteId } from '@tanstack/router-core'\n\nimport type { GeneratorPlugin } from '@tanstack/router-generator'\n\n/**\n * this plugin builds the routes manifest and stores it on globalThis\n * so that it can be accessed later (e.g. from a vite plugin)\n */\nexport function routesManifestPlugin(): GeneratorPlugin {\n return {\n name: 'routes-manifest-plugin',\n onRouteTreesChanged: ({ routeTrees, rootRouteNode }) => {\n const routeTree = routeTrees.find((tree) => tree.exportName === 'Route')\n if (!routeTree) {\n throw new Error(\n 'No route tree found with export name \"Route\". Please ensure your routes are correctly defined.',\n )\n }\n const routesManifest = {\n [rootRouteId]: {\n filePath: rootRouteNode.
|
|
1
|
+
{"version":3,"file":"routes-manifest-plugin.js","sources":["../../../../src/start-router-plugin/generator-plugins/routes-manifest-plugin.ts"],"sourcesContent":["import { rootRouteId } from '@tanstack/router-core'\n\nimport type { GeneratorPlugin } from '@tanstack/router-generator'\n\n/**\n * this plugin builds the routes manifest and stores it on globalThis\n * so that it can be accessed later (e.g. from a vite plugin)\n */\nexport function routesManifestPlugin(): GeneratorPlugin {\n return {\n name: 'routes-manifest-plugin',\n onRouteTreesChanged: ({ routeTrees, rootRouteNode }) => {\n const routeTree = routeTrees.find((tree) => tree.exportName === 'Route')\n if (!routeTree) {\n throw new Error(\n 'No route tree found with export name \"Route\". Please ensure your routes are correctly defined.',\n )\n }\n const routesManifest = {\n [rootRouteId]: {\n filePath: rootRouteNode.fullPath,\n children: routeTree.acc.routeTree.map((d) => d.routePath),\n },\n ...Object.fromEntries(\n routeTree.acc.routeNodes.map((d) => {\n const filePathId = d.routePath\n\n return [\n filePathId,\n {\n filePath: d.fullPath,\n parent: d.parent?.routePath ? d.parent.routePath : undefined,\n children: d.children?.map((childRoute) => childRoute.routePath),\n },\n ]\n }),\n ),\n }\n\n globalThis.TSS_ROUTES_MANIFEST = { routes: routesManifest }\n },\n }\n}\n"],"names":[],"mappings":";AAQO,SAAS,uBAAwC;AAC/C,SAAA;AAAA,IACL,MAAM;AAAA,IACN,qBAAqB,CAAC,EAAE,YAAY,oBAAoB;AACtD,YAAM,YAAY,WAAW,KAAK,CAAC,SAAS,KAAK,eAAe,OAAO;AACvE,UAAI,CAAC,WAAW;AACd,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MAAA;AAEF,YAAM,iBAAiB;AAAA,QACrB,CAAC,WAAW,GAAG;AAAA,UACb,UAAU,cAAc;AAAA,UACxB,UAAU,UAAU,IAAI,UAAU,IAAI,CAAC,MAAM,EAAE,SAAS;AAAA,QAC1D;AAAA,QACA,GAAG,OAAO;AAAA,UACR,UAAU,IAAI,WAAW,IAAI,CAAC,MAAM;;AAClC,kBAAM,aAAa,EAAE;AAEd,mBAAA;AAAA,cACL;AAAA,cACA;AAAA,gBACE,UAAU,EAAE;AAAA,gBACZ,UAAQ,OAAE,WAAF,mBAAU,aAAY,EAAE,OAAO,YAAY;AAAA,gBACnD,WAAU,OAAE,aAAF,mBAAY,IAAI,CAAC,eAAe,WAAW;AAAA,cAAS;AAAA,YAElE;AAAA,UACD,CAAA;AAAA,QAAA;AAAA,MAEL;AAEW,iBAAA,sBAAsB,EAAE,QAAQ,eAAe;AAAA,IAAA;AAAA,EAE9D;AACF;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/start-plugin-core",
|
|
3
|
-
"version": "1.121.
|
|
3
|
+
"version": "1.121.23",
|
|
4
4
|
"description": "Modern and scalable routing for React applications",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -63,12 +63,12 @@
|
|
|
63
63
|
"ufo": "^1.5.4",
|
|
64
64
|
"xmlbuilder2": "^3.1.1",
|
|
65
65
|
"zod": "^3.24.2",
|
|
66
|
-
"@tanstack/router-core": "
|
|
67
|
-
"@tanstack/router-
|
|
68
|
-
"@tanstack/router-
|
|
69
|
-
"@tanstack/
|
|
70
|
-
"@tanstack/start-server-core": "
|
|
71
|
-
"@tanstack/
|
|
66
|
+
"@tanstack/router-core": "1.121.21",
|
|
67
|
+
"@tanstack/router-generator": "1.121.21",
|
|
68
|
+
"@tanstack/router-plugin": "1.121.22",
|
|
69
|
+
"@tanstack/router-utils": "1.121.21",
|
|
70
|
+
"@tanstack/start-server-core": "1.121.23",
|
|
71
|
+
"@tanstack/server-functions-plugin": "1.121.21"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"vite": "^6.0.0"
|
package/src/global.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { Manifest } from '@tanstack/router-core'
|
|
2
|
+
import type { Rollup } from 'vite'
|
|
2
3
|
|
|
3
4
|
/* eslint-disable no-var */
|
|
4
5
|
declare global {
|
|
5
6
|
var TSS_APP_BASE: string
|
|
6
7
|
var TSS_ROUTES_MANIFEST: Manifest
|
|
8
|
+
var TSS_CLIENT_BUNDLE: Rollup.OutputBundle
|
|
7
9
|
}
|
|
8
10
|
export {}
|
package/src/plugin.ts
CHANGED
|
@@ -221,6 +221,16 @@ export function TanStackStartVitePluginCore(
|
|
|
221
221
|
startManifestPlugin(startConfig),
|
|
222
222
|
devServerPlugin(),
|
|
223
223
|
nitroPlugin(startConfig, () => ssrBundle),
|
|
224
|
+
{
|
|
225
|
+
name: 'tanstack-start:core:capture-client-bundle',
|
|
226
|
+
applyToEnvironment(e) {
|
|
227
|
+
return e.config.consumer === 'client'
|
|
228
|
+
},
|
|
229
|
+
enforce: 'post',
|
|
230
|
+
generateBundle(_options, bundle) {
|
|
231
|
+
globalThis.TSS_CLIENT_BUNDLE = bundle
|
|
232
|
+
},
|
|
233
|
+
},
|
|
224
234
|
]
|
|
225
235
|
}
|
|
226
236
|
|
|
@@ -1,28 +1,22 @@
|
|
|
1
|
-
import { readFileSync } from 'node:fs'
|
|
2
1
|
import path from 'node:path'
|
|
3
2
|
import { joinURL } from 'ufo'
|
|
4
3
|
import { rootRouteId } from '@tanstack/router-core'
|
|
5
4
|
import { VIRTUAL_MODULES } from '@tanstack/start-server-core'
|
|
5
|
+
import { tsrSplit } from '@tanstack/router-plugin'
|
|
6
6
|
import { resolveViteId } from '../utils'
|
|
7
|
-
import {
|
|
8
|
-
import type {
|
|
9
|
-
PluginOption,
|
|
10
|
-
ResolvedConfig,
|
|
11
|
-
Manifest as ViteManifest,
|
|
12
|
-
ManifestChunk as ViteManifestChunk,
|
|
13
|
-
} from 'vite'
|
|
7
|
+
import type { PluginOption, ResolvedConfig, Rollup } from 'vite'
|
|
14
8
|
import type { RouterManagedTag } from '@tanstack/router-core'
|
|
15
9
|
import type { TanStackStartOutputConfig } from '../plugin'
|
|
16
10
|
|
|
17
|
-
const getCSSRecursively = (
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
export const getCSSRecursively = (
|
|
12
|
+
chunk: Rollup.OutputChunk,
|
|
13
|
+
chunksByFileName: Map<string, Rollup.OutputChunk>,
|
|
20
14
|
basePath: string,
|
|
21
15
|
) => {
|
|
22
16
|
const result: Array<RouterManagedTag> = []
|
|
23
17
|
|
|
24
18
|
// Get all css imports from the file
|
|
25
|
-
for (const cssFile of
|
|
19
|
+
for (const cssFile of chunk.viteMetadata?.importedCss ?? []) {
|
|
26
20
|
result.push({
|
|
27
21
|
tag: 'link',
|
|
28
22
|
attrs: {
|
|
@@ -34,11 +28,11 @@ const getCSSRecursively = (
|
|
|
34
28
|
}
|
|
35
29
|
|
|
36
30
|
// Recursively get CSS from imports
|
|
37
|
-
for (const
|
|
38
|
-
const
|
|
39
|
-
if (
|
|
31
|
+
for (const importedFileName of chunk.imports) {
|
|
32
|
+
const importedChunk = chunksByFileName.get(importedFileName)
|
|
33
|
+
if (importedChunk) {
|
|
40
34
|
result.push(
|
|
41
|
-
...getCSSRecursively(
|
|
35
|
+
...getCSSRecursively(importedChunk, chunksByFileName, basePath),
|
|
42
36
|
)
|
|
43
37
|
}
|
|
44
38
|
}
|
|
@@ -59,9 +53,6 @@ export function startManifestPlugin(
|
|
|
59
53
|
configResolved(resolvedConfig) {
|
|
60
54
|
config = resolvedConfig
|
|
61
55
|
},
|
|
62
|
-
// configEnvironment(env, envConfig) {
|
|
63
|
-
// config = envConfig.
|
|
64
|
-
// },
|
|
65
56
|
resolveId: {
|
|
66
57
|
filter: { id: new RegExp(VIRTUAL_MODULES.startManifest) },
|
|
67
58
|
handler(id) {
|
|
@@ -93,106 +84,110 @@ export function startManifestPlugin(
|
|
|
93
84
|
// This is the basepath for the application
|
|
94
85
|
const APP_BASE = globalThis.TSS_APP_BASE
|
|
95
86
|
|
|
96
|
-
const clientViteManifestPath = path.resolve(
|
|
97
|
-
opts.root,
|
|
98
|
-
CLIENT_DIST_DIR,
|
|
99
|
-
'.vite',
|
|
100
|
-
'manifest.json',
|
|
101
|
-
)
|
|
102
|
-
|
|
103
|
-
let viteManifest: ViteManifest
|
|
104
|
-
try {
|
|
105
|
-
viteManifest = JSON.parse(
|
|
106
|
-
readFileSync(clientViteManifestPath, 'utf-8'),
|
|
107
|
-
)
|
|
108
|
-
} catch (err) {
|
|
109
|
-
console.error(err)
|
|
110
|
-
throw new Error(
|
|
111
|
-
`Could not find the production client vite manifest at '${clientViteManifestPath}'!`,
|
|
112
|
-
)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
87
|
// This the manifest pulled from the generated route tree and later used by the Router.
|
|
116
|
-
// i.e what's located in `src/
|
|
88
|
+
// i.e what's located in `src/routeTree.gen.ts`
|
|
117
89
|
const routeTreeRoutes = globalThis.TSS_ROUTES_MANIFEST.routes
|
|
118
90
|
|
|
119
91
|
// This is where hydration will start, from when the SSR'd page reaches the browser.
|
|
120
92
|
// By default, this'd be the virtual entry of `/~start/default-client-entry.tsx`, unless a custom entry is provided.
|
|
121
|
-
let entryFile:
|
|
122
|
-
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
93
|
+
let entryFile: Rollup.OutputChunk | undefined
|
|
94
|
+
|
|
95
|
+
const clientBundle = globalThis.TSS_CLIENT_BUNDLE
|
|
96
|
+
const chunksByFileName = new Map<string, Rollup.OutputChunk>()
|
|
97
|
+
|
|
98
|
+
const routeChunks: Record<
|
|
99
|
+
string /** fullPath of route file **/,
|
|
100
|
+
Array<Rollup.OutputChunk>
|
|
101
|
+
> = {}
|
|
102
|
+
for (const bundleEntry of Object.values(clientBundle)) {
|
|
103
|
+
if (bundleEntry.type === 'chunk') {
|
|
104
|
+
chunksByFileName.set(bundleEntry.fileName, bundleEntry)
|
|
105
|
+
if (bundleEntry.isEntry) {
|
|
106
|
+
if (entryFile) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
`multiple entries detected: ${entryFile.fileName} ${bundleEntry.fileName}`,
|
|
131
109
|
)
|
|
132
110
|
}
|
|
133
|
-
entryFile =
|
|
111
|
+
entryFile = bundleEntry
|
|
134
112
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
113
|
+
const routePieces = bundleEntry.moduleIds.flatMap((m) => {
|
|
114
|
+
const [id, query] = m.split('?')
|
|
115
|
+
if (id === undefined) {
|
|
116
|
+
throw new Error('expected id to be defined')
|
|
117
|
+
}
|
|
118
|
+
if (query === undefined) {
|
|
119
|
+
return []
|
|
120
|
+
}
|
|
121
|
+
const searchParams = new URLSearchParams(query)
|
|
122
|
+
const split = searchParams.get(tsrSplit)
|
|
123
|
+
|
|
124
|
+
if (split !== null) {
|
|
125
|
+
return {
|
|
126
|
+
id,
|
|
127
|
+
split,
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return []
|
|
131
|
+
})
|
|
132
|
+
if (routePieces.length > 0) {
|
|
133
|
+
routePieces.forEach((r) => {
|
|
134
|
+
let array = routeChunks[r.id]
|
|
135
|
+
if (array === undefined) {
|
|
136
|
+
array = []
|
|
137
|
+
routeChunks[r.id] = array
|
|
138
|
+
}
|
|
139
|
+
array.push(bundleEntry)
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
146
144
|
|
|
147
145
|
// Add preloads to the routes from the vite manifest
|
|
148
146
|
Object.entries(routeTreeRoutes).forEach(([routeId, v]) => {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
147
|
+
if (!v.filePath) {
|
|
148
|
+
throw new Error(`expected filePath to be set for ${routeId}`)
|
|
149
|
+
}
|
|
150
|
+
const chunks = routeChunks[v.filePath]
|
|
151
|
+
if (chunks) {
|
|
152
|
+
chunks.forEach((chunk) => {
|
|
153
|
+
// Map the relevant imports to their route paths,
|
|
154
|
+
// so that it can be imported in the browser.
|
|
155
|
+
const preloads = chunk.imports.map((d) => {
|
|
156
|
+
const assetPath = joinURL(APP_BASE, d)
|
|
157
|
+
return assetPath
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
// Since this is the most important JS entry for the route,
|
|
161
|
+
// it should be moved to the front of the preloads so that
|
|
162
|
+
// it has the best chance of being loaded first.
|
|
163
|
+
preloads.unshift(path.join(APP_BASE, chunk.fileName))
|
|
164
|
+
|
|
165
|
+
const cssAssetsList = getCSSRecursively(
|
|
166
|
+
chunk,
|
|
167
|
+
chunksByFileName,
|
|
168
|
+
APP_BASE,
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
routeTreeRoutes[routeId] = {
|
|
172
|
+
...v,
|
|
173
|
+
assets: [...(v.assets || []), ...cssAssetsList],
|
|
174
|
+
preloads: [...(v.preloads || []), ...preloads],
|
|
175
|
+
}
|
|
160
176
|
})
|
|
161
|
-
|
|
162
|
-
// Since this is the most important JS entry for the route,
|
|
163
|
-
// it should be moved to the front of the preloads so that
|
|
164
|
-
// it has the best chance of being loaded first.
|
|
165
|
-
if (file.file) {
|
|
166
|
-
preloads.unshift(path.join(APP_BASE, file.file))
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const cssAssetsList = getCSSRecursively(
|
|
170
|
-
file,
|
|
171
|
-
filesByRouteFilePath,
|
|
172
|
-
APP_BASE,
|
|
173
|
-
)
|
|
174
|
-
|
|
175
|
-
routeTreeRoutes[routeId] = {
|
|
176
|
-
...v,
|
|
177
|
-
assets: [...(v.assets || []), ...cssAssetsList],
|
|
178
|
-
preloads,
|
|
179
|
-
}
|
|
180
177
|
}
|
|
181
178
|
})
|
|
182
179
|
|
|
183
180
|
if (entryFile) {
|
|
184
181
|
routeTreeRoutes[rootRouteId]!.preloads = [
|
|
185
|
-
joinURL(APP_BASE, entryFile.
|
|
186
|
-
...
|
|
187
|
-
joinURL(APP_BASE, viteManifest[d]!.file),
|
|
188
|
-
) || []),
|
|
182
|
+
joinURL(APP_BASE, entryFile.fileName),
|
|
183
|
+
...entryFile.imports.map((d) => joinURL(APP_BASE, d)),
|
|
189
184
|
]
|
|
190
185
|
|
|
191
186
|
// Gather all the CSS files from the entry file in
|
|
192
187
|
// the `css` key and add them to the root route
|
|
193
188
|
const entryCssAssetsList = getCSSRecursively(
|
|
194
189
|
entryFile,
|
|
195
|
-
|
|
190
|
+
chunksByFileName,
|
|
196
191
|
APP_BASE,
|
|
197
192
|
)
|
|
198
193
|
|
|
@@ -202,7 +197,7 @@ export function startManifestPlugin(
|
|
|
202
197
|
{
|
|
203
198
|
tag: 'script',
|
|
204
199
|
attrs: {
|
|
205
|
-
src: joinURL(APP_BASE, entryFile.
|
|
200
|
+
src: joinURL(APP_BASE, entryFile.fileName),
|
|
206
201
|
type: 'module',
|
|
207
202
|
},
|
|
208
203
|
},
|
|
@@ -232,8 +227,7 @@ export function startManifestPlugin(
|
|
|
232
227
|
}
|
|
233
228
|
}
|
|
234
229
|
|
|
235
|
-
|
|
236
|
-
recurseRoute(routeTreeRoutes[rootRouteId])
|
|
230
|
+
recurseRoute(routeTreeRoutes[rootRouteId]!)
|
|
237
231
|
|
|
238
232
|
const routesManifest = {
|
|
239
233
|
routes: routeTreeRoutes,
|
|
@@ -18,7 +18,7 @@ export function routesManifestPlugin(): GeneratorPlugin {
|
|
|
18
18
|
}
|
|
19
19
|
const routesManifest = {
|
|
20
20
|
[rootRouteId]: {
|
|
21
|
-
filePath: rootRouteNode.
|
|
21
|
+
filePath: rootRouteNode.fullPath,
|
|
22
22
|
children: routeTree.acc.routeTree.map((d) => d.routePath),
|
|
23
23
|
},
|
|
24
24
|
...Object.fromEntries(
|
|
@@ -28,7 +28,7 @@ export function routesManifestPlugin(): GeneratorPlugin {
|
|
|
28
28
|
return [
|
|
29
29
|
filePathId,
|
|
30
30
|
{
|
|
31
|
-
filePath: d.
|
|
31
|
+
filePath: d.fullPath,
|
|
32
32
|
parent: d.parent?.routePath ? d.parent.routePath : undefined,
|
|
33
33
|
children: d.children?.map((childRoute) => childRoute.routePath),
|
|
34
34
|
},
|