-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathbuild.ts
225 lines (203 loc) · 5.28 KB
/
build.ts
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
import type * as Vite from "vite";
import colors from "picocolors";
import { loadConfig } from "../config/config";
import {
type EnvironmentName,
type EnvironmentBuildContext,
resolveViteConfig,
extractPluginContext,
cleanBuildDirectory,
cleanViteManifests,
getEnvironmentOptionsResolvers,
resolveEnvironmentsOptions,
getServerEnvironmentKeys,
} from "./plugin";
import invariant from "../invariant";
import { preloadVite, getVite } from "./vite";
export interface ViteBuildOptions {
assetsInlineLimit?: number;
clearScreen?: boolean;
config?: string;
emptyOutDir?: boolean;
force?: boolean;
logLevel?: Vite.LogLevel;
minify?: Vite.BuildOptions["minify"];
mode?: string;
profile?: boolean;
sourcemapClient?: boolean | "inline" | "hidden";
sourcemapServer?: boolean | "inline" | "hidden";
}
export async function build(root: string, viteBuildOptions: ViteBuildOptions) {
// Ensure Vite's ESM build is preloaded at the start of the process
// so it can be accessed synchronously via `getVite`
await preloadVite();
let vite = getVite();
let configResult = await loadConfig({ rootDirectory: root });
if (!configResult.ok) {
throw new Error(configResult.error);
}
let config = configResult.value;
let unstable_viteEnvironmentApi = config.future.unstable_viteEnvironmentApi;
let viteMajor = parseInt(vite.version.split(".")[0], 10);
if (unstable_viteEnvironmentApi && viteMajor === 5) {
throw new Error(
"The future.unstable_viteEnvironmentApi option is not supported in Vite 5"
);
}
return await (unstable_viteEnvironmentApi
? viteAppBuild(root, viteBuildOptions)
: viteBuild(root, viteBuildOptions));
}
async function viteAppBuild(
root: string,
{
assetsInlineLimit,
clearScreen,
config: configFile,
emptyOutDir,
force,
logLevel,
minify,
mode,
sourcemapClient,
sourcemapServer,
}: ViteBuildOptions
) {
let vite = getVite();
let builder = await vite.createBuilder({
root,
mode,
configFile,
build: {
assetsInlineLimit,
emptyOutDir,
minify,
},
optimizeDeps: { force },
clearScreen,
logLevel,
plugins: [
{
name: "react-router:cli-config",
configEnvironment(name) {
if (sourcemapClient && name === "client") {
return {
build: {
sourcemap: sourcemapClient,
},
};
}
if (sourcemapServer && name !== "client") {
return {
build: {
sourcemap: sourcemapServer,
},
};
}
},
configResolved(config) {
let hasReactRouterPlugin = config.plugins.find(
(plugin) => plugin.name === "react-router"
);
if (!hasReactRouterPlugin) {
throw new Error(
"React Router Vite plugin not found in Vite config"
);
}
},
},
],
});
await builder.buildApp();
}
async function viteBuild(
root: string,
{
assetsInlineLimit,
clearScreen,
config: configFile,
emptyOutDir,
force,
logLevel,
minify,
mode,
sourcemapClient,
sourcemapServer,
}: ViteBuildOptions
) {
let viteUserConfig: Vite.UserConfig = {};
let viteConfig = await resolveViteConfig({
configFile,
mode,
root,
plugins: [
{
name: "react-router:extract-vite-user-config",
config(config) {
viteUserConfig = config;
},
},
],
});
let ctx = extractPluginContext(viteConfig);
if (!ctx) {
console.error(
colors.red("React Router Vite plugin not found in Vite config")
);
process.exit(1);
}
async function buildEnvironment(environmentName: EnvironmentName) {
let vite = getVite();
let ssr = environmentName !== "client";
let resolveOptions = environmentOptionsResolvers[environmentName];
invariant(resolveOptions);
let environmentBuildContext: EnvironmentBuildContext = {
name: environmentName,
resolveOptions,
};
await vite.build({
root,
mode,
configFile,
build: {
assetsInlineLimit,
emptyOutDir,
minify,
ssr,
sourcemap: ssr ? sourcemapServer : sourcemapClient,
},
optimizeDeps: { force },
clearScreen,
logLevel,
...{
__reactRouterPluginContext: ctx,
__reactRouterEnvironmentBuildContext: environmentBuildContext,
},
});
}
let { reactRouterConfig, buildManifest } = ctx;
invariant(buildManifest, "Expected build manifest to be present");
let environmentOptionsResolvers = await getEnvironmentOptionsResolvers(
ctx,
"build"
);
let environmentsOptions = resolveEnvironmentsOptions(
environmentOptionsResolvers,
{ viteUserConfig }
);
await cleanBuildDirectory(viteConfig, ctx);
// Run the Vite client build first
await buildEnvironment("client");
// Then run Vite SSR builds in parallel
let serverEnvironmentNames = getServerEnvironmentKeys(
ctx,
environmentOptionsResolvers
);
await Promise.all(serverEnvironmentNames.map(buildEnvironment));
await cleanViteManifests(environmentsOptions, ctx);
await reactRouterConfig.buildEnd?.({
buildManifest,
reactRouterConfig,
viteConfig,
});
}