Skip to content

Commit 423f6c0

Browse files
authored
chore(start-plugin-core): reorganize and centralize the plugin orchestration (#4266)
1 parent 6d50013 commit 423f6c0

File tree

12 files changed

+121
-103
lines changed

12 files changed

+121
-103
lines changed

packages/start-plugin-core/src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ export const VITE_ENVIRONMENT_NAMES = {
44
server: 'ssr',
55
client: 'client',
66
} as const
7+
8+
export const CLIENT_DIST_DIR = '.tanstack-start/build/client-dist'
9+
export const SSR_ENTRY_FILE = 'ssr.mjs'

packages/start-plugin-core/src/nitro/dev-server-plugin.ts renamed to packages/start-plugin-core/src/dev-server-plugin/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createEvent, getHeader, sendWebResponse } from 'h3'
22
import { isRunnableDevEnvironment } from 'vite'
3-
import { extractHtmlScripts } from '../extractHtmlScripts'
43
import { VITE_ENVIRONMENT_NAMES } from '../constants'
4+
import { extractHtmlScripts } from './extract-html-scripts'
55
import type { Connect, DevEnvironment, Plugin, ViteDevServer } from 'vite'
66

77
declare global {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as vite from 'vite'
2+
import type { TanStackStartOutputConfig } from '../plugin'
3+
4+
export function loadEnvPlugin(
5+
startOpts: TanStackStartOutputConfig,
6+
): vite.Plugin {
7+
return {
8+
name: 'tanstack-vite-plugin-nitro-load-env',
9+
enforce: 'pre',
10+
config(userConfig, envConfig) {
11+
Object.assign(
12+
process.env,
13+
vite.loadEnv(envConfig.mode, userConfig.root ?? startOpts.root, ''),
14+
)
15+
},
16+
}
17+
}

packages/start-plugin-core/src/build-sitemap.ts renamed to packages/start-plugin-core/src/nitro-plugin/build-sitemap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { writeFileSync } from 'node:fs'
22
import path from 'node:path'
33
import { create } from 'xmlbuilder2'
4-
import { createLogger } from './utils'
5-
import type { TanStackStartOutputConfig } from './plugin'
4+
import { createLogger } from '../utils'
5+
import type { TanStackStartOutputConfig } from '../plugin'
66
import type { XMLBuilder } from 'xmlbuilder2/lib/interfaces'
77

88
export type SitemapUrl = {

packages/start-plugin-core/src/nitro/nitro-plugin.ts renamed to packages/start-plugin-core/src/nitro-plugin/plugin.ts

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import path from 'node:path'
22
import { rmSync } from 'node:fs'
33
import { build, copyPublicAssets, createNitro, prepare } from 'nitropack'
44
import { dirname, resolve } from 'pathe'
5-
import { loadEnv } from 'vite'
6-
import { clientDistDir, ssrEntryFile } from '../plugin'
7-
import { prerender } from '../prerender'
8-
import { VITE_ENVIRONMENT_NAMES } from '../constants'
9-
import { buildSitemap } from '../build-sitemap'
10-
import { devServerPlugin } from './dev-server-plugin'
5+
import {
6+
CLIENT_DIST_DIR,
7+
SSR_ENTRY_FILE,
8+
VITE_ENVIRONMENT_NAMES,
9+
} from '../constants'
10+
import { buildSitemap } from './build-sitemap'
11+
import { prerender } from './prerender'
1112
import type {
1213
EnvironmentOptions,
1314
PluginOption,
@@ -17,28 +18,13 @@ import type {
1718
import type { Nitro, NitroConfig } from 'nitropack'
1819
import type { TanStackStartOutputConfig } from '../plugin'
1920

20-
function setupLoadEnv(startOpts: TanStackStartOutputConfig): PluginOption {
21-
return {
22-
name: 'tanstack-vite-plugin-nitro-load-env',
23-
enforce: 'pre',
24-
config(userConfig, envConfig) {
25-
Object.assign(
26-
process.env,
27-
loadEnv(envConfig.mode, userConfig.root ?? startOpts.root, ''),
28-
)
29-
},
30-
}
31-
}
32-
3321
export function nitroPlugin(
3422
options: TanStackStartOutputConfig,
3523
getSsrBundle: () => Rollup.OutputBundle,
3624
): Array<PluginOption> {
3725
const buildPreset =
3826
process.env['START_TARGET'] ?? (options.target as string | undefined)
3927
return [
40-
setupLoadEnv(options),
41-
devServerPlugin(),
4228
{
4329
name: 'tanstack-vite-plugin-nitro',
4430
configEnvironment(name) {
@@ -77,7 +63,7 @@ export function nitroPlugin(
7763

7864
// Build the client bundle
7965
// i.e client entry file with `hydrateRoot(...)`
80-
const clientOutputDir = resolve(options.root, clientDistDir)
66+
const clientOutputDir = resolve(options.root, CLIENT_DIST_DIR)
8167
rmSync(clientOutputDir, { recursive: true, force: true })
8268
await builder.build(client)
8369

@@ -93,7 +79,7 @@ export function nitroPlugin(
9379
baseURL: globalThis.TSS_APP_BASE,
9480
publicAssets: [
9581
{
96-
dir: path.resolve(options.root, clientDistDir),
82+
dir: path.resolve(options.root, CLIENT_DIST_DIR),
9783
baseURL: '/',
9884
maxAge: 31536000, // 1 year
9985
},
@@ -102,7 +88,7 @@ export function nitroPlugin(
10288
generateTsConfig: false,
10389
},
10490
prerender: undefined,
105-
renderer: ssrEntryFile,
91+
renderer: SSR_ENTRY_FILE,
10692
plugins: [], // Nitro's plugins
10793
appConfigFiles: [],
10894
scanDirs: [],

packages/start-plugin-core/src/prerender.ts renamed to packages/start-plugin-core/src/nitro-plugin/prerender.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import path from 'node:path'
44
import { getRollupConfig } from 'nitropack/rollup'
55
import { build as buildNitro, createNitro } from 'nitropack'
66
import { joinURL, withBase, withoutBase } from 'ufo'
7+
import { VITE_ENVIRONMENT_NAMES } from '../constants'
8+
import { createLogger } from '../utils'
79
import { Queue } from './queue'
8-
import { VITE_ENVIRONMENT_NAMES } from './constants'
9-
import { createLogger } from './utils'
1010
import type { ViteBuilder } from 'vite'
1111
import type { $Fetch, Nitro } from 'nitropack'
12-
import type { TanStackStartOutputConfig } from './plugin'
13-
import type { Page } from './schema'
12+
import type { TanStackStartOutputConfig } from '../plugin'
13+
import type { Page } from '../schema'
1414

1515
export async function prerender({
1616
options,

packages/start-plugin-core/src/plugin.ts

Lines changed: 17 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ import { tanstackRouter } from '@tanstack/router-plugin/vite'
55
import { TanStackServerFnPluginEnv } from '@tanstack/server-functions-plugin'
66
import * as vite from 'vite'
77
import { createTanStackConfig } from './schema'
8-
import { nitroPlugin } from './nitro/nitro-plugin'
9-
import { startManifestPlugin } from './routesManifestPlugin'
10-
import { TanStackStartCompilerPlugin } from './start-compiler-plugin'
11-
import { VITE_ENVIRONMENT_NAMES } from './constants'
8+
import { nitroPlugin } from './nitro-plugin/plugin'
9+
import { startRoutesManifestPlugin } from './start-routes-manifest-plugin/plugin'
10+
import { startCompilerPlugin } from './start-compiler-plugin'
11+
import {
12+
CLIENT_DIST_DIR,
13+
SSR_ENTRY_FILE,
14+
VITE_ENVIRONMENT_NAMES,
15+
} from './constants'
1216
import { TanStackStartServerRoutesVite } from './start-server-routes-plugin/plugin'
17+
import { loadEnvPlugin } from './load-env-plugin/plugin'
18+
import { devServerPlugin } from './dev-server-plugin/plugin'
19+
import { resolveVirtualEntriesPlugin } from './resolve-virtual-entries-plugin/plugin'
1320
import type { createTanStackStartOptionsSchema } from './schema'
1421
import type { PluginOption, Rollup } from 'vite'
1522
import type { z } from 'zod'
@@ -33,9 +40,6 @@ declare global {
3340
var TSS_APP_BASE: string
3441
}
3542

36-
export const clientDistDir = '.tanstack-start/build/client-dist'
37-
export const ssrEntryFile = 'ssr.mjs'
38-
3943
export interface TanStackStartVitePluginCoreOptions {
4044
framework: CompileStartFrameworkOptions
4145
getVirtualServerRootHandler: (ctx: {
@@ -111,7 +115,7 @@ export function TanStackStartVitePluginCore(
111115
main: getClientEntryPath(startConfig),
112116
},
113117
output: {
114-
dir: path.resolve(startConfig.root, clientDistDir),
118+
dir: path.resolve(startConfig.root, CLIENT_DIST_DIR),
115119
},
116120
// TODO: this should be removed
117121
external: ['node:fs', 'node:path', 'node:os', 'node:crypto'],
@@ -128,7 +132,7 @@ export function TanStackStartVitePluginCore(
128132
copyPublicDir: false,
129133
rollupOptions: {
130134
output: {
131-
entryFileNames: ssrEntryFile,
135+
entryFileNames: SSR_ENTRY_FILE,
132136
},
133137
plugins: [
134138
{
@@ -179,7 +183,7 @@ export function TanStackStartVitePluginCore(
179183
},
180184
},
181185
// N.B. TanStackStartCompilerPlugin must be before the TanStackServerFnPluginEnv
182-
TanStackStartCompilerPlugin(opts.framework, {
186+
startCompilerPlugin(opts.framework, {
183187
client: { envName: VITE_ENVIRONMENT_NAMES.client },
184188
server: { envName: VITE_ENVIRONMENT_NAMES.server },
185189
}),
@@ -213,7 +217,9 @@ export function TanStackStartVitePluginCore(
213217
return serverEnv.runner.import(fn.extractedFilename)
214218
},
215219
}),
216-
startManifestPlugin(startConfig),
220+
loadEnvPlugin(startConfig),
221+
startRoutesManifestPlugin(startConfig),
222+
devServerPlugin(),
217223
nitroPlugin(startConfig, () => ssrBundle),
218224
TanStackStartServerRoutesVite({
219225
...startConfig.tsr,
@@ -222,63 +228,6 @@ export function TanStackStartVitePluginCore(
222228
]
223229
}
224230

225-
function resolveVirtualEntriesPlugin(
226-
opts: TanStackStartVitePluginCoreOptions,
227-
startConfig: TanStackStartOutputConfig,
228-
): PluginOption {
229-
let resolvedConfig: vite.ResolvedConfig
230-
231-
const modules = new Set<string>([
232-
'/~start/server-entry',
233-
'/~start/default-server-entry',
234-
'/~start/default-client-entry',
235-
])
236-
237-
return {
238-
name: 'tanstack-start-core:resolve-virtual-entries',
239-
configResolved(config) {
240-
resolvedConfig = config
241-
},
242-
resolveId(id) {
243-
if (modules.has(id)) {
244-
return `${id}.tsx`
245-
}
246-
247-
return undefined
248-
},
249-
load(id) {
250-
const routerFilepath = vite.normalizePath(
251-
path.resolve(startConfig.root, startConfig.tsr.srcDirectory, 'router'),
252-
)
253-
254-
if (id === '/~start/server-entry.tsx') {
255-
const ssrEntryFilepath = startConfig.serverEntryPath.startsWith(
256-
'/~start/default-server-entry',
257-
)
258-
? startConfig.serverEntryPath
259-
: vite.normalizePath(
260-
path.resolve(resolvedConfig.root, startConfig.serverEntryPath),
261-
)
262-
263-
return opts.getVirtualServerRootHandler({
264-
routerFilepath,
265-
serverEntryFilepath: ssrEntryFilepath,
266-
})
267-
}
268-
269-
if (id === '/~start/default-client-entry.tsx') {
270-
return opts.getVirtualClientEntry({ routerFilepath })
271-
}
272-
273-
if (id === '/~start/default-server-entry.tsx') {
274-
return opts.getVirtualServerEntry({ routerFilepath })
275-
}
276-
277-
return undefined
278-
},
279-
}
280-
}
281-
282231
function defineReplaceEnv<TKey extends string, TValue extends string>(
283232
key: TKey,
284233
value: TValue,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import path from 'node:path'
2+
import * as vite from 'vite'
3+
import type {
4+
TanStackStartOutputConfig,
5+
TanStackStartVitePluginCoreOptions,
6+
} from '../plugin'
7+
8+
export function resolveVirtualEntriesPlugin(
9+
opts: TanStackStartVitePluginCoreOptions,
10+
startConfig: TanStackStartOutputConfig,
11+
): vite.Plugin {
12+
let resolvedConfig: vite.ResolvedConfig
13+
14+
const modules = new Set<string>([
15+
'/~start/server-entry',
16+
'/~start/default-server-entry',
17+
'/~start/default-client-entry',
18+
])
19+
20+
return {
21+
name: 'tanstack-start-core:resolve-virtual-entries',
22+
configResolved(config) {
23+
resolvedConfig = config
24+
},
25+
resolveId(id) {
26+
if (modules.has(id)) {
27+
return `${id}.tsx`
28+
}
29+
30+
return undefined
31+
},
32+
load(id) {
33+
const routerFilepath = vite.normalizePath(
34+
path.resolve(startConfig.root, startConfig.tsr.srcDirectory, 'router'),
35+
)
36+
37+
if (id === '/~start/server-entry.tsx') {
38+
const ssrEntryFilepath = startConfig.serverEntryPath.startsWith(
39+
'/~start/default-server-entry',
40+
)
41+
? startConfig.serverEntryPath
42+
: vite.normalizePath(
43+
path.resolve(resolvedConfig.root, startConfig.serverEntryPath),
44+
)
45+
46+
return opts.getVirtualServerRootHandler({
47+
routerFilepath,
48+
serverEntryFilepath: ssrEntryFilepath,
49+
})
50+
}
51+
52+
if (id === '/~start/default-client-entry.tsx') {
53+
return opts.getVirtualClientEntry({ routerFilepath })
54+
}
55+
56+
if (id === '/~start/default-server-entry.tsx') {
57+
return opts.getVirtualServerEntry({ routerFilepath })
58+
}
59+
60+
return undefined
61+
},
62+
}
63+
}

packages/start-plugin-core/src/start-compiler-plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const transformFuncs = [
2424

2525
const tokenRegex = new RegExp(transformFuncs.join('|'))
2626

27-
export function TanStackStartCompilerPlugin(
27+
export function startCompilerPlugin(
2828
framework: CompileStartFrameworkOptions,
2929
inputOpts?: {
3030
client?: {

packages/start-plugin-core/src/routesManifestPlugin.ts renamed to packages/start-plugin-core/src/start-routes-manifest-plugin/plugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { mkdirSync, readFileSync, rmSync, writeFile } from 'node:fs'
22
import path from 'node:path'
33
import { joinURL } from 'ufo'
44
import { rootRouteId } from '@tanstack/router-core'
5-
import { resolveViteId } from './utils'
5+
import { resolveViteId } from '../utils'
66
import type {
77
PluginOption,
88
ResolvedConfig,
99
Manifest as ViteManifest,
1010
ManifestChunk as ViteManifestChunk,
1111
} from 'vite'
1212
import type { Manifest, RouterManagedTag } from '@tanstack/router-core'
13-
import type { TanStackStartOutputConfig } from './plugin'
13+
import type { TanStackStartOutputConfig } from '../plugin'
1414

1515
const getCSSRecursively = (
1616
file: ViteManifestChunk,
@@ -44,7 +44,7 @@ const getCSSRecursively = (
4444
return result
4545
}
4646

47-
export function startManifestPlugin(
47+
export function startRoutesManifestPlugin(
4848
opts: TanStackStartOutputConfig,
4949
): PluginOption {
5050
let config: ResolvedConfig

0 commit comments

Comments
 (0)