forked from rstackjs/rsbuild-plugin-react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassic-mode.ts
More file actions
258 lines (246 loc) · 7.02 KB
/
Copy pathclassic-mode.ts
File metadata and controls
258 lines (246 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { readFileSync } from 'node:fs';
import type { RsbuildEntryDescription, RsbuildPluginAPI } from '@rsbuild/core';
import type { RouteConfigEntry } from '@react-router/dev/routes';
import { resolve } from 'pathe';
import {
getBuildManifest,
getRoutesByServerBundleId,
} from './build-manifest.js';
import { BUILD_CLIENT_ROUTE_QUERY_STRING } from './constants.js';
import {
DEV_HMR_RUNTIME_MODULE_ID,
type DevHmrPlanOptions,
} from './dev-hmr.js';
import {
createReactRouterDevRuntimeController,
type ReactRouterDevRuntimeController,
} from './dev-runtime-controller.js';
import { generateWithProps } from './plugin-utils.js';
import { resolvePrerenderPaths } from './prerender.js';
import { generateServerBuild } from './server-utils.js';
import {
createReactRouterServerBuildPlan,
type ReactRouterServerBundleEntry,
} from './server-build-plan.js';
import {
getRouteChunkEntryName,
getRouteChunkModuleId,
getRouteEntryBaseName,
routeChunkExportNames,
} from './route-chunks.js';
import type { Config } from './react-router-config.js';
import type { Route } from './types.js';
type CreateClassicWebRouteEntriesOptions = {
appDirectory: string;
isBuild: boolean;
routes: Record<string, Route>;
splitRouteModules: boolean;
};
type CreateClassicVirtualModulesOptions = {
allowedActionOrigins: string[] | undefined;
appDirectory: string;
assetsBuildDirectory: string;
basename: string;
devHmrRuntimeModule?: string;
entryServerPath: string;
future: Config['future'];
isSpaMode: boolean;
prerenderPaths: string[];
publicPath: string;
routeDiscovery: Config['routeDiscovery'];
routes: Record<string, Route>;
routesByServerBundleId: Record<string, Record<string, Route>>;
ssr: boolean;
};
type CreateClassicBuildArtifactsOptions = {
api: RsbuildPluginAPI;
defaultEntryName: string;
isBuild: boolean;
prerenderConfig: Config['prerender'];
reactRouterConfig: Required<
Pick<
Config,
'appDirectory' | 'buildDirectory' | 'serverBuildFile' | 'future'
>
> &
Pick<Config, 'serverBundles'>;
routeConfig: RouteConfigEntry[];
routes: Record<string, Route>;
rootDirectory: string;
ssr: boolean;
devHmr?: DevHmrPlanOptions;
};
export type ClassicBuildArtifacts = {
buildManifest: Awaited<ReturnType<typeof getBuildManifest>>;
devRuntime: ReactRouterDevRuntimeController;
prerenderPaths: string[];
routesByServerBundleId: Record<string, Record<string, Route>>;
serverBundleEntries: ReactRouterServerBundleEntry[];
};
export const createClassicBuildArtifacts = async ({
api,
defaultEntryName,
isBuild,
prerenderConfig,
reactRouterConfig,
routeConfig,
routes,
rootDirectory,
ssr,
devHmr,
}: CreateClassicBuildArtifactsOptions): Promise<ClassicBuildArtifacts> => {
const buildManifest = await getBuildManifest({
reactRouterConfig,
routes,
rootDirectory,
});
const routesByServerBundleId = getRoutesByServerBundleId(
buildManifest,
routes
);
const serverBuildPlan = createReactRouterServerBuildPlan({
routesByServerBundleId,
serverBuildFile: reactRouterConfig.serverBuildFile,
defaultEntryName,
});
const prerenderPaths = await resolvePrerenderPaths(
prerenderConfig,
ssr,
routeConfig,
{
logWarning: true,
warn: message => api.logger.warn(message),
}
);
return {
buildManifest,
devRuntime: createReactRouterDevRuntimeController({
api,
isBuild,
buildPlan: serverBuildPlan,
clientPatchesRouteMetadata: devHmr?.isEnabled,
onNodeRebuildCommitted: devHmr?.onNodeRebuildCommitted,
}),
prerenderPaths,
routesByServerBundleId,
serverBundleEntries: serverBuildPlan.serverBundleEntries,
};
};
export const createClassicWebRouteEntries = ({
appDirectory,
isBuild,
routes,
splitRouteModules,
}: CreateClassicWebRouteEntriesOptions): {
manifestChunkNames: Set<string>;
webRouteEntries: Record<string, RsbuildEntryDescription>;
} => {
const manifestChunkNames = new Set<string>(['entry.client']);
const webRouteEntries = Object.values(routes).reduce(
(acc, route) => {
const entryName = getRouteEntryBaseName(route, appDirectory);
const routeFilePath = resolve(appDirectory, route.file);
manifestChunkNames.add(entryName);
acc[entryName] = {
import: `${routeFilePath}${BUILD_CLIENT_ROUTE_QUERY_STRING}`,
html: false,
};
if (isBuild && splitRouteModules && route.id !== 'root') {
let source: string;
try {
source = readFileSync(routeFilePath, 'utf8');
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
return acc;
}
throw error;
}
for (const exportName of routeChunkExportNames) {
if (!source.includes(exportName)) {
continue;
}
const chunkEntryName = getRouteChunkEntryName(
route,
exportName,
appDirectory
);
manifestChunkNames.add(chunkEntryName);
acc[chunkEntryName] = {
import: getRouteChunkModuleId(routeFilePath, exportName),
html: false,
};
}
}
return acc;
},
{} as Record<string, RsbuildEntryDescription>
);
return {
manifestChunkNames,
webRouteEntries,
};
};
export const createClassicVirtualModules = ({
allowedActionOrigins,
appDirectory,
assetsBuildDirectory,
basename,
devHmrRuntimeModule,
entryServerPath,
future,
isSpaMode,
prerenderPaths,
publicPath,
routeDiscovery,
routes,
routesByServerBundleId,
ssr,
}: CreateClassicVirtualModulesOptions): Record<string, string> => {
const serverBuildOptions = {
entryServerPath,
assetsBuildDirectory,
basename,
appDirectory,
ssr,
isSpaMode,
future,
allowedActionOrigins,
prerender: prerenderPaths,
routeDiscovery,
publicPath,
};
const bundleVirtualModules = Object.fromEntries(
Object.entries(routesByServerBundleId).map(([bundleId, bundleRoutes]) => [
`virtual/react-router/server-build-${bundleId}`,
generateServerBuild(bundleRoutes, {
...serverBuildOptions,
serverManifestId: `virtual/react-router/server-manifest-${bundleId}`,
}),
])
);
const bundleManifestModules = Object.fromEntries(
Object.entries(routesByServerBundleId)
.filter(
([, bundleRoutes]) =>
bundleRoutes && Object.keys(bundleRoutes).length > 0
)
.map(([bundleId]) => [
`virtual/react-router/server-manifest-${bundleId}`,
'export default {};',
])
);
return {
'virtual/react-router/browser-manifest': 'export default {};',
'virtual/react-router/server-manifest': 'export default {};',
'virtual/react-router/server-build': generateServerBuild(
routes,
serverBuildOptions
),
...bundleVirtualModules,
...bundleManifestModules,
'virtual/react-router/with-props': generateWithProps(),
...(devHmrRuntimeModule !== undefined
? { [DEV_HMR_RUNTIME_MODULE_ID]: devHmrRuntimeModule }
: {}),
};
};