improve build perf (#2772)
This commit is contained in:
parent
2c4fd919fa
commit
b4d34e2d2c
10 changed files with 37 additions and 20 deletions
5
.changeset/tough-cherries-bathe.md
Normal file
5
.changeset/tough-cherries-bathe.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Improve build performance, especially on large sites
|
5
.changeset/tough-dragons-sniff.md
Normal file
5
.changeset/tough-dragons-sniff.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Surface vite warnings to the user
|
|
@ -65,7 +65,7 @@ class AstroBuilder {
|
||||||
{
|
{
|
||||||
mode: this.mode,
|
mode: this.mode,
|
||||||
server: {
|
server: {
|
||||||
hmr: { overlay: false },
|
hmr: false,
|
||||||
middlewareMode: 'ssr',
|
middlewareMode: 'ssr',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -47,7 +47,7 @@ export async function build(opts: ScanBasedBuildOptions) {
|
||||||
const internals = createBuildInternals();
|
const internals = createBuildInternals();
|
||||||
|
|
||||||
return await vite.build({
|
return await vite.build({
|
||||||
logLevel: 'error',
|
logLevel: 'warn',
|
||||||
mode: 'production',
|
mode: 'production',
|
||||||
build: {
|
build: {
|
||||||
emptyOutDir: true,
|
emptyOutDir: true,
|
||||||
|
|
|
@ -35,7 +35,11 @@ export interface StaticBuildOptions {
|
||||||
viteConfig: ViteConfigWithSSR;
|
viteConfig: ViteConfigWithSSR;
|
||||||
}
|
}
|
||||||
|
|
||||||
const MAX_CONCURRENT_RENDERS = 10;
|
// Render is usually compute, which Node.js can't parallelize well.
|
||||||
|
// In real world testing, dropping from 10->1 showed a notiable perf
|
||||||
|
// improvement. In the future, we can revisit a smarter parallel
|
||||||
|
// system, possibly one that parallelizes if async IO is detected.
|
||||||
|
const MAX_CONCURRENT_RENDERS = 1;
|
||||||
|
|
||||||
const STATUS_CODE_PAGES = new Set(['/404', '/500']);
|
const STATUS_CODE_PAGES = new Set(['/404', '/500']);
|
||||||
|
|
||||||
|
@ -161,8 +165,9 @@ export async function staticBuild(opts: StaticBuildOptions) {
|
||||||
// condition, so we are doing it ourselves
|
// condition, so we are doing it ourselves
|
||||||
emptyDir(astroConfig.dist, new Set('.git'));
|
emptyDir(astroConfig.dist, new Set('.git'));
|
||||||
|
|
||||||
// Run the SSR build and client build in parallel
|
// Build your project (SSR application code, assets, client JS, etc.)
|
||||||
const [ssrResult] = (await Promise.all([ssrBuild(opts, internals, pageInput), clientBuild(opts, internals, jsInput)])) as RollupOutput[];
|
const ssrResult = await ssrBuild(opts, internals, pageInput) as RollupOutput;
|
||||||
|
await clientBuild(opts, internals, jsInput);
|
||||||
|
|
||||||
// SSG mode, generate pages.
|
// SSG mode, generate pages.
|
||||||
if (staticMode) {
|
if (staticMode) {
|
||||||
|
@ -181,13 +186,12 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
|
||||||
const out = ssr ? getServerRoot(astroConfig) : getOutRoot(astroConfig);
|
const out = ssr ? getServerRoot(astroConfig) : getOutRoot(astroConfig);
|
||||||
|
|
||||||
return await vite.build({
|
return await vite.build({
|
||||||
logLevel: 'error',
|
logLevel: 'warn',
|
||||||
mode: 'production',
|
mode: 'production',
|
||||||
build: {
|
build: {
|
||||||
...viteConfig.build,
|
...viteConfig.build,
|
||||||
emptyOutDir: false,
|
emptyOutDir: false,
|
||||||
manifest: ssr,
|
manifest: ssr,
|
||||||
minify: false,
|
|
||||||
outDir: fileURLToPath(out),
|
outDir: fileURLToPath(out),
|
||||||
ssr: true,
|
ssr: true,
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
|
@ -199,7 +203,12 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
|
||||||
assetFileNames: 'assets/[name].[hash][extname]',
|
assetFileNames: 'assets/[name].[hash][extname]',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
target: 'esnext', // must match an esbuild target
|
// must match an esbuild target
|
||||||
|
target: 'esnext',
|
||||||
|
// improve build performance
|
||||||
|
minify: false,
|
||||||
|
polyfillModulePreload: false,
|
||||||
|
reportCompressedSize: false,
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
vitePluginNewBuild(input, internals, 'mjs'),
|
vitePluginNewBuild(input, internals, 'mjs'),
|
||||||
|
@ -226,9 +235,8 @@ async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals,
|
||||||
}
|
}
|
||||||
|
|
||||||
const out = astroConfig.buildOptions.experimentalSsr ? getClientRoot(astroConfig) : getOutRoot(astroConfig);
|
const out = astroConfig.buildOptions.experimentalSsr ? getClientRoot(astroConfig) : getOutRoot(astroConfig);
|
||||||
|
|
||||||
return await vite.build({
|
return await vite.build({
|
||||||
logLevel: 'error',
|
logLevel: 'warn',
|
||||||
mode: 'production',
|
mode: 'production',
|
||||||
build: {
|
build: {
|
||||||
emptyOutDir: false,
|
emptyOutDir: false,
|
||||||
|
@ -295,13 +303,11 @@ async function generatePages(result: RollupOutput, opts: StaticBuildOptions, int
|
||||||
// Get renderers to be shared for each page generation.
|
// Get renderers to be shared for each page generation.
|
||||||
const renderers = await collectRenderers(opts);
|
const renderers = await collectRenderers(opts);
|
||||||
|
|
||||||
const generationPromises = [];
|
|
||||||
for (let output of result.output) {
|
for (let output of result.output) {
|
||||||
if (chunkIsPage(opts.astroConfig, output, internals)) {
|
if (chunkIsPage(opts.astroConfig, output, internals)) {
|
||||||
generationPromises.push(generatePage(output as OutputChunk, opts, internals, facadeIdToPageDataMap, renderers));
|
await (generatePage(output as OutputChunk, opts, internals, facadeIdToPageDataMap, renderers));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await Promise.all(generationPromises);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function generatePage(output: OutputChunk, opts: StaticBuildOptions, internals: BuildInternals, facadeIdToPageDataMap: Map<string, PageBuildData>, renderers: Renderer[]) {
|
async function generatePage(output: OutputChunk, opts: StaticBuildOptions, internals: BuildInternals, facadeIdToPageDataMap: Map<string, PageBuildData>, renderers: Renderer[]) {
|
||||||
|
|
|
@ -49,7 +49,7 @@ export async function createVite(inlineConfig: ViteConfigWithSSR, { astroConfig,
|
||||||
let viteConfig: ViteConfigWithSSR = {
|
let viteConfig: ViteConfigWithSSR = {
|
||||||
cacheDir: fileURLToPath(new URL('./node_modules/.vite/', astroConfig.projectRoot)), // using local caches allows Astro to be used in monorepos, etc.
|
cacheDir: fileURLToPath(new URL('./node_modules/.vite/', astroConfig.projectRoot)), // using local caches allows Astro to be used in monorepos, etc.
|
||||||
clearScreen: false, // we want to control the output, not Vite
|
clearScreen: false, // we want to control the output, not Vite
|
||||||
logLevel: 'error', // log errors only
|
logLevel: 'warn', // log warnings and errors only
|
||||||
optimizeDeps: {
|
optimizeDeps: {
|
||||||
entries: ['src/**/*'], // Try and scan a user’s project (won’t catch everything),
|
entries: ['src/**/*'], // Try and scan a user’s project (won’t catch everything),
|
||||||
},
|
},
|
||||||
|
|
|
@ -5,6 +5,7 @@ import type { LogOptions } from '../core/logger.js';
|
||||||
import esbuild from 'esbuild';
|
import esbuild from 'esbuild';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
import slash from 'slash';
|
import slash from 'slash';
|
||||||
|
import fs from 'fs';
|
||||||
import { getViteTransform, TransformHook } from './styles.js';
|
import { getViteTransform, TransformHook } from './styles.js';
|
||||||
import { parseAstroRequest } from './query.js';
|
import { parseAstroRequest } from './query.js';
|
||||||
import { cachedCompilation } from './compile.js';
|
import { cachedCompilation } from './compile.js';
|
||||||
|
@ -124,13 +125,11 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
|
||||||
},
|
|
||||||
async transform(source, id, opts) {
|
|
||||||
if (!id.endsWith('.astro')) {
|
if (!id.endsWith('.astro')) {
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const source = await fs.promises.readFile(id, {encoding: 'utf-8'});
|
||||||
try {
|
try {
|
||||||
const transformResult = await cachedCompilation(config, id, source, viteTransform, { ssr: Boolean(opts?.ssr) });
|
const transformResult = await cachedCompilation(config, id, source, viteTransform, { ssr: Boolean(opts?.ssr) });
|
||||||
|
|
||||||
|
@ -140,6 +139,8 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
|
||||||
loader: 'ts',
|
loader: 'ts',
|
||||||
sourcemap: 'external',
|
sourcemap: 'external',
|
||||||
sourcefile: id,
|
sourcefile: id,
|
||||||
|
// Pass relevant Vite options, if needed:
|
||||||
|
define: config.vite.define,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Signal to Vite that we accept HMR updates
|
// Signal to Vite that we accept HMR updates
|
||||||
|
|
|
@ -5,7 +5,7 @@ import Meta from '../components/Meta.astro';
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>My App</title>
|
<title>My App</title>
|
||||||
<link rel="styleshseet" href={Astro.resolve('../styles/global.css')}>
|
<link rel="styleshseet" href="/styles/global.css">
|
||||||
<Meta />
|
<Meta />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>My App</title>
|
<title>My App</title>
|
||||||
<link rel="styleshseet" href={Astro.resolve('../styles/global.css')}>
|
<link rel="styleshseet" href="/styles/global.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue