@octanejs/tanstack-start 0.1.28 → 0.1.30

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.
Files changed (38) hide show
  1. package/README.md +19 -1
  2. package/THIRD_PARTY_NOTICES.md +6 -5
  3. package/package.json +18 -6
  4. package/src/client-only-server-strip-loader.js +7 -0
  5. package/src/client-only-server-strip.js +34 -25
  6. package/src/internal/README.md +7 -7
  7. package/src/internal/start-plugin-core/rsbuild/import-protection.d.ts +27 -0
  8. package/src/internal/start-plugin-core/rsbuild/import-protection.js +1193 -0
  9. package/src/internal/start-plugin-core/rsbuild/index.d.ts +9 -0
  10. package/src/internal/start-plugin-core/rsbuild/index.js +3 -0
  11. package/src/internal/start-plugin-core/rsbuild/normalized-client-build.d.ts +20 -0
  12. package/src/internal/start-plugin-core/rsbuild/normalized-client-build.js +261 -0
  13. package/src/internal/start-plugin-core/rsbuild/planning.d.ts +56 -0
  14. package/src/internal/start-plugin-core/rsbuild/planning.js +173 -0
  15. package/src/internal/start-plugin-core/rsbuild/plugin.d.ts +7 -0
  16. package/src/internal/start-plugin-core/rsbuild/plugin.js +504 -0
  17. package/src/internal/start-plugin-core/rsbuild/post-build.d.ts +10 -0
  18. package/src/internal/start-plugin-core/rsbuild/post-build.js +59 -0
  19. package/src/internal/start-plugin-core/rsbuild/schema.d.ts +2441 -0
  20. package/src/internal/start-plugin-core/rsbuild/schema.js +28 -0
  21. package/src/internal/start-plugin-core/rsbuild/server-middleware.d.ts +32 -0
  22. package/src/internal/start-plugin-core/rsbuild/server-middleware.js +139 -0
  23. package/src/internal/start-plugin-core/rsbuild/start-compiler-host.d.ts +36 -0
  24. package/src/internal/start-plugin-core/rsbuild/start-compiler-host.js +322 -0
  25. package/src/internal/start-plugin-core/rsbuild/start-compiler-metadata-loader.d.ts +10 -0
  26. package/src/internal/start-plugin-core/rsbuild/start-compiler-metadata-loader.js +12 -0
  27. package/src/internal/start-plugin-core/rsbuild/start-compiler-metadata.d.ts +14 -0
  28. package/src/internal/start-plugin-core/rsbuild/start-compiler-metadata.js +5 -0
  29. package/src/internal/start-plugin-core/rsbuild/start-router-plugin.d.ts +19 -0
  30. package/src/internal/start-plugin-core/rsbuild/start-router-plugin.js +69 -0
  31. package/src/internal/start-plugin-core/rsbuild/swc-rsc.d.ts +17 -0
  32. package/src/internal/start-plugin-core/rsbuild/swc-rsc.js +118 -0
  33. package/src/internal/start-plugin-core/rsbuild/types.d.ts +17 -0
  34. package/src/internal/start-plugin-core/rsbuild/types.js +0 -0
  35. package/src/internal/start-plugin-core/rsbuild/virtual-modules.d.ts +60 -0
  36. package/src/internal/start-plugin-core/rsbuild/virtual-modules.js +359 -0
  37. package/src/plugin-rsbuild.d.ts +25 -0
  38. package/src/plugin-rsbuild.js +79 -0
@@ -0,0 +1,504 @@
1
+ import { escapeRegExp, normalizePath } from '../utils.js';
2
+ import { createServerFnBasePath, normalizePublicBase } from '../planning.js';
3
+ import {
4
+ applyResolvedBaseAndOutput,
5
+ applyResolvedRouterBasepath,
6
+ createStartConfigContext,
7
+ } from '../config-context.js';
8
+ import {
9
+ RSBUILD_CLIENT_ASSETS_DIR,
10
+ RSBUILD_ENVIRONMENT_NAMES,
11
+ RSBUILD_RSC_LAYERS,
12
+ createRsbuildEnvironmentPlan,
13
+ createRsbuildResolvedEntryAliases,
14
+ resolveRsbuildOutputDirectory,
15
+ } from './planning.js';
16
+ import { parseStartConfig, rsbuildClientOutputSchema } from './schema.js';
17
+ import { registerStartCompilerTransforms } from './start-compiler-host.js';
18
+ import { registerImportProtection } from './import-protection.js';
19
+ import { START_MANIFEST_PLACEHOLDER, registerVirtualModules } from './virtual-modules.js';
20
+ import { createServerSetup } from './server-middleware.js';
21
+ import { registerClientBuildCapture } from './normalized-client-build.js';
22
+ import { registerRouterPlugins } from './start-router-plugin.js';
23
+ import { postBuildWithRsbuild } from './post-build.js';
24
+ import { enableSwcReactServerComponents } from './swc-rsc.js';
25
+ import { dirname, join, resolve } from 'node:path';
26
+ import { existsSync, readdirSync, realpathSync, statSync } from 'node:fs';
27
+ import { fileURLToPath } from 'node:url';
28
+ import { joinURL } from 'ufo';
29
+ //#region src/rsbuild/plugin.ts
30
+ var currentDir = dirname(fileURLToPath(import.meta.url));
31
+ var isInsideRouterMonoRepo = (() => {
32
+ const candidate = resolve(currentDir, '../../../../');
33
+ return candidate.endsWith('/packages') || candidate.endsWith('\\packages');
34
+ })();
35
+ function tanStackStartRsbuild(corePluginOpts, startPluginOpts = {}) {
36
+ const rscOpts = corePluginOpts.rsc;
37
+ const rscEnabled = Boolean(rscOpts);
38
+ const configContext = createStartConfigContext({
39
+ corePluginOpts,
40
+ startPluginOpts,
41
+ parseConfig: parseStartConfig,
42
+ });
43
+ const { getConfig, resolvedStartConfig } = configContext;
44
+ const serverFnProviderEnv = corePluginOpts.providerEnvironmentName;
45
+ const ssrIsProvider = corePluginOpts.ssrIsProvider;
46
+ const scriptFormat = rsbuildClientOutputSchema.parse(
47
+ startPluginOpts.rsbuild?.client?.output ?? 'module',
48
+ );
49
+ let rscPlugins;
50
+ let devServerRef = null;
51
+ const serverFnsById = {};
52
+ let updateServerFnResolver;
53
+ return {
54
+ name: 'tanstack-start-rsbuild',
55
+ setup(api) {
56
+ const startCompilerEnvironments = [
57
+ {
58
+ name: RSBUILD_ENVIRONMENT_NAMES.client,
59
+ type: 'client',
60
+ },
61
+ {
62
+ name: RSBUILD_ENVIRONMENT_NAMES.server,
63
+ type: 'server',
64
+ },
65
+ ...(serverFnProviderEnv !== RSBUILD_ENVIRONMENT_NAMES.server && !rscEnabled
66
+ ? [
67
+ {
68
+ name: serverFnProviderEnv,
69
+ type: 'server',
70
+ },
71
+ ]
72
+ : []),
73
+ ];
74
+ const startCompilerServerEnvironmentNames = startCompilerEnvironments
75
+ .filter((env) => env.type === 'server')
76
+ .map((env) => env.name);
77
+ api.modifyRsbuildConfig((rsbuildConfig, { mergeRsbuildConfig }) => {
78
+ const root = typeof rsbuildConfig.root === 'string' ? rsbuildConfig.root : process.cwd();
79
+ const serverBase = rsbuildConfig.server?.base;
80
+ const assetPrefix = rsbuildConfig.output?.assetPrefix;
81
+ const publicBase = normalizePublicBase(
82
+ typeof serverBase === 'string'
83
+ ? serverBase
84
+ : typeof assetPrefix === 'string' && assetPrefix !== 'auto'
85
+ ? assetPrefix
86
+ : void 0,
87
+ );
88
+ const rootDistPath = rsbuildConfig.output?.distPath;
89
+ const clientDistPath =
90
+ rsbuildConfig.environments?.[RSBUILD_ENVIRONMENT_NAMES.client]?.output?.distPath;
91
+ const serverDistPath =
92
+ rsbuildConfig.environments?.[RSBUILD_ENVIRONMENT_NAMES.server]?.output?.distPath;
93
+ applyResolvedBaseAndOutput({
94
+ resolvedStartConfig,
95
+ root,
96
+ publicBase,
97
+ clientOutputDirectory: resolveRsbuildOutputDirectory({
98
+ distPath: clientDistPath,
99
+ rootDistPath,
100
+ fallback: 'dist/client',
101
+ subdirectory: 'client',
102
+ }),
103
+ serverOutputDirectory: resolveRsbuildOutputDirectory({
104
+ distPath: serverDistPath,
105
+ rootDistPath,
106
+ fallback: 'dist/server',
107
+ subdirectory: 'server',
108
+ }),
109
+ });
110
+ const { startConfig } = getConfig();
111
+ const routerBasepath = applyResolvedRouterBasepath({
112
+ resolvedStartConfig,
113
+ startConfig,
114
+ });
115
+ const resolvedEntryPlan = configContext.resolveEntries();
116
+ const isDev = api.context.action === 'dev';
117
+ const isPreview = api.context.action === 'preview';
118
+ const environmentPlan = createRsbuildEnvironmentPlan({
119
+ root,
120
+ entryAliases: createRsbuildResolvedEntryAliases({
121
+ entryPaths: resolvedEntryPlan.entryPaths,
122
+ }),
123
+ clientOutputDirectory: resolvedStartConfig.outputDirectories.client,
124
+ serverOutputDirectory: resolvedStartConfig.outputDirectories.server,
125
+ publicBase: resolvedStartConfig.basePaths.publicBase,
126
+ serverFnProviderEnv,
127
+ environmentOverrides: corePluginOpts.rsbuild?.environments,
128
+ scriptFormat,
129
+ rsc: rscOpts,
130
+ dev: isDev,
131
+ });
132
+ const serverFnBase = createServerFnBasePath({
133
+ routerBasepath,
134
+ serverFnBase: startConfig.serverFns.base,
135
+ });
136
+ const inlineCssEnabled = !isDev && startConfig.server.build.inlineCss.enabled;
137
+ return mergeRsbuildConfig(rsbuildConfig, {
138
+ source: {
139
+ ...(rscEnabled ? { include: [{ not: /[\\/]core-js[\\/]/ }] } : {}),
140
+ define: {
141
+ 'process.env.TSS_SERVER_FN_BASE': JSON.stringify(serverFnBase),
142
+ 'import.meta.env.TSS_SERVER_FN_BASE': JSON.stringify(serverFnBase),
143
+ 'process.env.TSS_ROUTER_BASEPATH': JSON.stringify(routerBasepath),
144
+ 'import.meta.env.TSS_ROUTER_BASEPATH': JSON.stringify(routerBasepath),
145
+ 'process.env.TSS_DEV_SERVER': JSON.stringify(isDev ? 'true' : 'false'),
146
+ 'import.meta.env.TSS_DEV_SERVER': JSON.stringify(isDev ? 'true' : 'false'),
147
+ 'process.env.TSS_DEV_SSR_STYLES_ENABLED': JSON.stringify('false'),
148
+ 'import.meta.env.TSS_DEV_SSR_STYLES_ENABLED': JSON.stringify('false'),
149
+ 'process.env.TSS_DEV_SSR_STYLES_BASEPATH': JSON.stringify(
150
+ resolvedStartConfig.basePaths.publicBase,
151
+ ),
152
+ 'import.meta.env.TSS_DEV_SSR_STYLES_BASEPATH': JSON.stringify(
153
+ resolvedStartConfig.basePaths.publicBase,
154
+ ),
155
+ 'process.env.TSS_INLINE_CSS_ENABLED': JSON.stringify(
156
+ inlineCssEnabled ? 'true' : 'false',
157
+ ),
158
+ 'import.meta.env.TSS_INLINE_CSS_ENABLED': JSON.stringify(
159
+ inlineCssEnabled ? 'true' : 'false',
160
+ ),
161
+ 'process.env.TSS_DISABLE_CSRF_MIDDLEWARE_WARNING': JSON.stringify(
162
+ startConfig.serverFns.disableCsrfMiddlewareWarning ? 'true' : 'false',
163
+ ),
164
+ 'import.meta.env.TSS_DISABLE_CSRF_MIDDLEWARE_WARNING': JSON.stringify(
165
+ startConfig.serverFns.disableCsrfMiddlewareWarning ? 'true' : 'false',
166
+ ),
167
+ },
168
+ },
169
+ server: {
170
+ ...(rsbuildConfig.server?.printUrls === void 0 ||
171
+ rsbuildConfig.server.printUrls === true
172
+ ? { printUrls: ({ urls }) => urls }
173
+ : {}),
174
+ compress: false,
175
+ htmlFallback: false,
176
+ ...(isPreview ||
177
+ (isDev && startPluginOpts.rsbuild?.installDevServerMiddleware !== false)
178
+ ? {
179
+ setup: createServerSetup({
180
+ serverFnBasePath: serverFnBase,
181
+ serverOutputDirectory: resolvedStartConfig.outputDirectories.server,
182
+ publicBase: resolvedStartConfig.basePaths.publicBase,
183
+ }),
184
+ }
185
+ : {}),
186
+ },
187
+ ...(isDev
188
+ ? {
189
+ dev: {
190
+ lazyCompilation: false,
191
+ ...(rscEnabled ? { liveReload: false } : {}),
192
+ },
193
+ }
194
+ : {}),
195
+ environments: environmentPlan.environments,
196
+ resolve: { alias: environmentPlan.alias },
197
+ });
198
+ });
199
+ registerStartCompilerTransforms(api, {
200
+ framework: corePluginOpts.framework,
201
+ root: () => resolvedStartConfig.root || process.cwd(),
202
+ environments: startCompilerEnvironments,
203
+ providerEnvName: serverFnProviderEnv,
204
+ generateFunctionId: startPluginOpts.serverFns?.generateFunctionId,
205
+ compilerTransforms: corePluginOpts.compilerTransforms,
206
+ serverFnProviderModuleDirectives: corePluginOpts.serverFnProviderModuleDirectives,
207
+ serverFnsById,
208
+ onServerFnsByIdChange: () => {
209
+ updateServerFnResolver?.();
210
+ },
211
+ });
212
+ registerImportProtection(api, {
213
+ getConfig,
214
+ framework: corePluginOpts.framework,
215
+ environments: startCompilerEnvironments,
216
+ });
217
+ const virtualModuleState = registerVirtualModules(api, {
218
+ root: resolvedStartConfig.root || process.cwd(),
219
+ getConfig,
220
+ serverFnsById,
221
+ providerEnvName: serverFnProviderEnv,
222
+ ssrIsProvider,
223
+ serializationAdapters: corePluginOpts.serializationAdapters,
224
+ getDevClientEntryUrl: (publicBase) =>
225
+ joinURL(publicBase, RSBUILD_CLIENT_ASSETS_DIR, 'js/index.js'),
226
+ rscEnabled,
227
+ scriptFormat,
228
+ });
229
+ updateServerFnResolver = virtualModuleState.updateServerFnResolver;
230
+ if (!rscEnabled)
231
+ api.modifyRspackConfig((config, utils) => {
232
+ if (!startCompilerServerEnvironmentNames.includes(utils.environment.name)) return;
233
+ config.plugins.push({
234
+ apply(compiler) {
235
+ compiler.hooks.finishMake.tapPromise(
236
+ {
237
+ name: 'TanStackStartServerFnResolverRebuild',
238
+ stage: -10,
239
+ },
240
+ async (compilation) => {
241
+ virtualModuleState.updateServerFnResolver();
242
+ await rebuildModulesContaining(
243
+ compilation,
244
+ virtualModuleState.serverFnResolverPath,
245
+ );
246
+ },
247
+ );
248
+ },
249
+ });
250
+ });
251
+ const { getClientBuild } = registerClientBuildCapture(api);
252
+ if (api.context.action !== 'dev') {
253
+ const normalizedManifestPath = normalizePath(virtualModuleState.manifestPath);
254
+ const matchesManifestPath = (id) => normalizePath(id) === normalizedManifestPath;
255
+ api.transform(
256
+ {
257
+ test: (id) => matchesManifestPath(id),
258
+ environments: [RSBUILD_ENVIRONMENT_NAMES.server],
259
+ },
260
+ ({ code }) => {
261
+ const clientBuild = getClientBuild();
262
+ if (clientBuild) return virtualModuleState.generateManifestContent(clientBuild);
263
+ if (!rscEnabled)
264
+ throw new Error(
265
+ 'TanStack Start could not generate the rsbuild server manifest before the client build completed',
266
+ );
267
+ return code;
268
+ },
269
+ );
270
+ }
271
+ registerRouterPlugins(api, {
272
+ getConfig,
273
+ corePluginOpts,
274
+ startPluginOpts,
275
+ });
276
+ if (isInsideRouterMonoRepo && api.context.action === 'dev')
277
+ api.modifyRspackConfig((config) => {
278
+ const workspaceDistRealpaths = resolveWorkspacePackageDistRealpaths();
279
+ if (workspaceDistRealpaths.length === 0) return;
280
+ const workspaceDistIgnored = new RegExp(
281
+ workspaceDistRealpaths.map((path) => `^${escapeRegExp(path)}(?:[\\\\/]|$)`).join('|'),
282
+ );
283
+ const ignored = config.watchOptions?.ignored;
284
+ config.watchOptions = {
285
+ ...(config.watchOptions ?? {}),
286
+ ignored:
287
+ ignored == null
288
+ ? new RegExp(`${defaultRspackWatchIgnored.source}|${workspaceDistIgnored.source}`)
289
+ : typeof ignored === 'string'
290
+ ? [ignored, ...workspaceDistRealpaths]
291
+ : Array.isArray(ignored)
292
+ ? [...ignored, ...workspaceDistRealpaths]
293
+ : new RegExp(`${ignored.source}|${workspaceDistIgnored.source}`),
294
+ };
295
+ });
296
+ if (rscEnabled) {
297
+ api.modifyRspackConfig((config, utils) => {
298
+ const envName = utils.environment.name;
299
+ const isServerEnv = envName === RSBUILD_ENVIRONMENT_NAMES.server;
300
+ const isClientEnv = envName === RSBUILD_ENVIRONMENT_NAMES.client;
301
+ if (!rscPlugins) rscPlugins = utils.rspack.experiments.rsc.createPlugins();
302
+ if (isServerEnv) {
303
+ const moduleRules = (config.module.rules ??= []);
304
+ const root = resolvedStartConfig.root || process.cwd();
305
+ moduleRules.push({
306
+ resourceQuery: /(?:^|[?&])tss-serverfn-split(?:&|$)/,
307
+ layer: RSBUILD_RSC_LAYERS.rsc,
308
+ resolve: { conditionNames: ['react-server', '...'] },
309
+ });
310
+ moduleRules.push({
311
+ issuerLayer: RSBUILD_RSC_LAYERS.rsc,
312
+ resourceQuery: { not: [/(?:^|[?&])tsr-split(?:=|&|$)/] },
313
+ resolve: { conditionNames: ['react-server', '...'] },
314
+ });
315
+ seedResolveModules(config, [`${root}/node_modules`, 'node_modules']);
316
+ config.plugins.push(
317
+ new rscPlugins.ServerPlugin({
318
+ cssLink: {
319
+ precedence: false,
320
+ props: { 'data-rsc-css-href': '' },
321
+ },
322
+ onServerComponentChanges: () => {
323
+ devServerRef?.sockWrite('custom', { event: 'rsc:update' });
324
+ },
325
+ }),
326
+ );
327
+ config.plugins.push({
328
+ apply(compiler) {
329
+ compiler.hooks.finishMake.tapPromise(
330
+ {
331
+ name: 'TanStackStartRscServerFnResolverRebuild',
332
+ stage: -10,
333
+ },
334
+ async (compilation) => {
335
+ const resolverContent = virtualModuleState.generateCurrentResolverContent(true);
336
+ virtualModuleState.tryUpdateServerFnResolver(resolverContent);
337
+ await rebuildModulesContaining(
338
+ compilation,
339
+ virtualModuleState.serverFnResolverPath,
340
+ );
341
+ },
342
+ );
343
+ },
344
+ });
345
+ if (api.context.action !== 'dev')
346
+ config.plugins.push({
347
+ apply(compiler) {
348
+ compiler.hooks.finishMake.tapPromise(
349
+ {
350
+ name: 'TanStackStartRscManifestRebuild',
351
+ stage: 10,
352
+ },
353
+ async (compilation) => {
354
+ const clientBuild = getClientBuild();
355
+ if (!clientBuild) return;
356
+ virtualModuleState.updateManifest(clientBuild);
357
+ await rebuildModulesContaining(compilation, virtualModuleState.manifestPath);
358
+ },
359
+ );
360
+ },
361
+ });
362
+ }
363
+ if (isClientEnv) config.plugins.push(new rscPlugins.ClientPlugin());
364
+ if (isServerEnv) enableSwcReactServerComponents(config, 'rsc-subtree');
365
+ else if (isClientEnv) enableSwcReactServerComponents(config, 'all');
366
+ });
367
+ if (api.context.action === 'dev')
368
+ api.onBeforeStartDevServer(({ server }) => {
369
+ devServerRef = server;
370
+ });
371
+ }
372
+ if (!rscEnabled)
373
+ api.onAfterCreateCompiler(({ compiler }) => {
374
+ if ('compilers' in compiler)
375
+ for (const environmentName of startCompilerServerEnvironmentNames) {
376
+ const serverCompiler = compiler.compilers.find((c) => c.name === environmentName);
377
+ if (serverCompiler) {
378
+ const dependencies = [RSBUILD_ENVIRONMENT_NAMES.client];
379
+ if (
380
+ environmentName === RSBUILD_ENVIRONMENT_NAMES.server &&
381
+ serverFnProviderEnv !== RSBUILD_ENVIRONMENT_NAMES.server
382
+ )
383
+ dependencies.push(serverFnProviderEnv);
384
+ compiler.setDependencies(serverCompiler, dependencies);
385
+ }
386
+ }
387
+ });
388
+ if (api.context.action !== 'dev' && rscEnabled) {
389
+ const manifestPlaceholderLiteral = JSON.stringify(START_MANIFEST_PLACEHOLDER);
390
+ api.modifyRspackConfig((config, utils) => {
391
+ if (utils.environment.name !== RSBUILD_ENVIRONMENT_NAMES.server) return;
392
+ config.plugins.push({
393
+ apply(compiler) {
394
+ compiler.hooks.compilation.tap('TanStackStartManifestReplace', (compilation) => {
395
+ compilation.hooks.processAssets.tap(
396
+ {
397
+ name: 'TanStackStartManifestReplace',
398
+ stage: utils.rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
399
+ },
400
+ () => {
401
+ let assetsWithPlaceholder;
402
+ for (const asset of compilation.getAssets()) {
403
+ if (!asset.name.endsWith('.js')) continue;
404
+ const sourceStr = String(asset.source.source());
405
+ if (!sourceStr.includes(manifestPlaceholderLiteral)) continue;
406
+ if (!assetsWithPlaceholder) assetsWithPlaceholder = [];
407
+ assetsWithPlaceholder.push({
408
+ asset,
409
+ sourceStr,
410
+ });
411
+ }
412
+ if (!assetsWithPlaceholder) return;
413
+ const clientBuild = getClientBuild();
414
+ if (!clientBuild)
415
+ throw new Error(
416
+ 'TanStack Start could not replace the rsbuild RSC server manifest placeholder because the client build was unavailable',
417
+ );
418
+ const manifestValueLiteral =
419
+ virtualModuleState.generateManifestValueLiteral(clientBuild);
420
+ for (const { asset, sourceStr } of assetsWithPlaceholder)
421
+ compilation.updateAsset(
422
+ asset.name,
423
+ new utils.rspack.sources.RawSource(
424
+ sourceStr.replace(manifestPlaceholderLiteral, manifestValueLiteral),
425
+ ),
426
+ );
427
+ },
428
+ );
429
+ });
430
+ },
431
+ });
432
+ });
433
+ }
434
+ api.onAfterEnvironmentCompile(({ environment }) => {
435
+ if (environment.name !== RSBUILD_ENVIRONMENT_NAMES.client) return;
436
+ virtualModuleState.updateServerFnResolver();
437
+ const clientBuild = getClientBuild();
438
+ if (clientBuild) virtualModuleState.updateManifest(clientBuild);
439
+ });
440
+ if (api.context.action === 'build')
441
+ api.onAfterBuild(async () => {
442
+ const { startConfig } = getConfig();
443
+ await postBuildWithRsbuild({
444
+ startConfig,
445
+ clientOutputDirectory: resolvedStartConfig.outputDirectories.client,
446
+ serverOutputDirectory: resolvedStartConfig.outputDirectories.server,
447
+ });
448
+ });
449
+ },
450
+ };
451
+ }
452
+ var defaultRspackWatchIgnored = /[\\/](?:\.git|node_modules)[\\/]/;
453
+ function seedResolveModules(config, entries) {
454
+ const resolveModules = (config.resolve.modules ??= []);
455
+ for (const entry of entries) if (!resolveModules.includes(entry)) resolveModules.push(entry);
456
+ }
457
+ function rebuildModulesContaining(compilation, identifierFragment) {
458
+ const rebuilds = [];
459
+ for (const mod of compilation.modules) {
460
+ if (!mod.identifier().includes(identifierFragment)) continue;
461
+ rebuilds.push(
462
+ new Promise((resolve, reject) => {
463
+ compilation.rebuildModule(mod, (err) => {
464
+ if (err) reject(err);
465
+ else resolve();
466
+ });
467
+ }),
468
+ );
469
+ }
470
+ return rebuilds.length === 0 ? Promise.resolve() : Promise.all(rebuilds).then(() => void 0);
471
+ }
472
+ /**
473
+ * Return the realpath of every packages/<name>/dist directory in the
474
+ * TanStack Router monorepo. Only meaningful when called from inside the
475
+ * monorepo — in user apps, callers should guard with
476
+ * `isInsideRouterMonoRepo` before invoking this.
477
+ */
478
+ function resolveWorkspacePackageDistRealpaths() {
479
+ const packagesDir = resolve(currentDir, '../../../../');
480
+ if (!existsSync(packagesDir)) return [];
481
+ let entries;
482
+ try {
483
+ entries = readdirSync(packagesDir);
484
+ } catch {
485
+ return [];
486
+ }
487
+ const dists = [];
488
+ for (const entry of entries) {
489
+ const distPath = join(packagesDir, entry, 'dist');
490
+ try {
491
+ if (!statSync(distPath).isDirectory()) continue;
492
+ } catch {
493
+ continue;
494
+ }
495
+ try {
496
+ dists.push(realpathSync(distPath));
497
+ } catch {
498
+ dists.push(distPath);
499
+ }
500
+ }
501
+ return dists;
502
+ }
503
+ //#endregion
504
+ export { tanStackStartRsbuild };
@@ -0,0 +1,10 @@
1
+ import { TanStackStartOutputConfig } from '../schema.js';
2
+ export declare function postBuildWithRsbuild({
3
+ startConfig,
4
+ clientOutputDirectory,
5
+ serverOutputDirectory,
6
+ }: {
7
+ startConfig: TanStackStartOutputConfig;
8
+ clientOutputDirectory: string;
9
+ serverOutputDirectory: string;
10
+ }): Promise<void>;
@@ -0,0 +1,59 @@
1
+ import { postBuild } from '../post-build.js';
2
+ import { prerender } from '../prerender.js';
3
+ import { join } from 'pathe';
4
+ //#region src/rsbuild/post-build.ts
5
+ async function postBuildWithRsbuild({ startConfig, clientOutputDirectory, serverOutputDirectory }) {
6
+ await postBuild({
7
+ startConfig,
8
+ adapter: {
9
+ getClientOutputDirectory() {
10
+ return clientOutputDirectory;
11
+ },
12
+ prerender(startConfig) {
13
+ return prerender({
14
+ startConfig,
15
+ handler: createRsbuildPrerenderHandler({
16
+ clientOutputDirectory,
17
+ serverOutputDirectory,
18
+ }),
19
+ });
20
+ },
21
+ },
22
+ });
23
+ }
24
+ function createRsbuildPrerenderHandler({ clientOutputDirectory, serverOutputDirectory }) {
25
+ process.env.TSS_PRERENDERING = 'true';
26
+ process.env.TSS_CLIENT_OUTPUT_DIR = clientOutputDirectory;
27
+ let requestHandlerPromise;
28
+ return {
29
+ getClientOutputDirectory() {
30
+ return clientOutputDirectory;
31
+ },
32
+ async request(path, options) {
33
+ const requestHandler = await getRequestHandler();
34
+ const url = new URL(path, 'http://localhost');
35
+ return requestHandler(
36
+ new Request(url, {
37
+ ...options,
38
+ redirect: 'manual',
39
+ }),
40
+ );
41
+ },
42
+ };
43
+ function getRequestHandler() {
44
+ if (!requestHandlerPromise) requestHandlerPromise = loadRequestHandler(serverOutputDirectory);
45
+ return requestHandlerPromise;
46
+ }
47
+ }
48
+ async function loadRequestHandler(serverOutputDirectory) {
49
+ const { pathToFileURL } = await import('node:url');
50
+ const serverEntryUrl = pathToFileURL(join(serverOutputDirectory, 'index.js')).toString();
51
+ const handler = (await import(serverEntryUrl)).default;
52
+ if (typeof handler === 'function') return handler;
53
+ if (handler && typeof handler.fetch === 'function') return (request) => handler.fetch(request);
54
+ throw new Error(
55
+ `Unable to resolve a request handler from Rsbuild server bundle at ${serverEntryUrl}`,
56
+ );
57
+ }
58
+ //#endregion
59
+ export { postBuildWithRsbuild };