Refactor static build config merge (#4344)

This commit is contained in:
Bjorn Lu 2022-08-17 23:24:14 +08:00 committed by GitHub
parent 675b6d47f4
commit 500332a426
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 22 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Refactor static build config merge

View file

@ -116,19 +116,16 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
const out = ssr ? opts.buildConfig.server : astroConfig.outDir;
const viteBuildConfig: ViteConfigWithSSR = {
...viteConfig,
logLevel: opts.viteConfig.logLevel ?? 'error',
mode: 'production',
css: viteConfig.css,
optimizeDeps: {
include: [...(viteConfig.optimizeDeps?.include ?? [])],
exclude: [...(viteConfig.optimizeDeps?.exclude ?? [])],
},
build: {
...viteConfig.build,
emptyOutDir: false,
manifest: false,
outDir: fileURLToPath(out),
rollupOptions: {
...viteConfig.build?.rollupOptions,
input: [],
output: {
format: 'esm',
@ -138,7 +135,6 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
entryFileNames: opts.buildConfig.serverEntry,
},
},
ssr: true,
// must match an esbuild target
target: 'esnext',
@ -163,12 +159,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
vitePluginAnalyzer(internals),
],
publicDir: ssr ? false : viteConfig.publicDir,
root: viteConfig.root,
envPrefix: 'PUBLIC_',
server: viteConfig.server,
base: astroConfig.base,
ssr: viteConfig.ssr,
resolve: viteConfig.resolve,
};
await runHookBuildSetup({
@ -178,7 +169,6 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
target: 'server',
});
// TODO: use vite.mergeConfig() here?
return await vite.build(viteBuildConfig);
}
@ -202,23 +192,19 @@ async function clientBuild(
return null;
}
// TODO: use vite.mergeConfig() here?
info(opts.logging, null, `\n${bgGreen(black(' building client '))}`);
const viteBuildConfig = {
...viteConfig,
logLevel: 'info',
mode: 'production',
css: viteConfig.css,
optimizeDeps: {
include: [...(viteConfig.optimizeDeps?.include ?? [])],
exclude: [...(viteConfig.optimizeDeps?.exclude ?? [])],
},
build: {
...viteConfig.build,
emptyOutDir: false,
minify: 'esbuild',
outDir: fileURLToPath(out),
rollupOptions: {
...viteConfig.build?.rollupOptions,
input: Array.from(input),
output: {
format: 'esm',
@ -242,11 +228,7 @@ async function clientBuild(
}),
...(viteConfig.plugins || []),
],
publicDir: viteConfig.publicDir,
root: viteConfig.root,
envPrefix: 'PUBLIC_',
server: viteConfig.server,
base: astroConfig.base,
} as ViteConfigWithSSR;
await runHookBuildSetup({