Remove dev server during build (#4234)

This commit is contained in:
Bjorn Lu 2022-08-10 22:06:02 +08:00 committed by GitHub
parent dcc2480d9d
commit c38e7f1890
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 47 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Remove dev server during build

View file

@ -5,7 +5,6 @@ import type { LogOptions } from '../logger/core';
import fs from 'fs'; import fs from 'fs';
import * as colors from 'kleur/colors'; import * as colors from 'kleur/colors';
import { performance } from 'perf_hooks'; import { performance } from 'perf_hooks';
import * as vite from 'vite';
import { import {
runHookBuildDone, runHookBuildDone,
runHookBuildStart, runHookBuildStart,
@ -18,7 +17,6 @@ import { debug, info, levels, timerMessage } from '../logger/core.js';
import { apply as applyPolyfill } from '../polyfill.js'; import { apply as applyPolyfill } from '../polyfill.js';
import { RouteCache } from '../render/route-cache.js'; import { RouteCache } from '../render/route-cache.js';
import { createRouteManifest } from '../routing/index.js'; import { createRouteManifest } from '../routing/index.js';
import { createSafeError } from '../util.js';
import { collectPagesData } from './page-data.js'; import { collectPagesData } from './page-data.js';
import { staticBuild } from './static-build.js'; import { staticBuild } from './static-build.js';
import { getTimeStat } from './util.js'; import { getTimeStat } from './util.js';
@ -64,7 +62,6 @@ class AstroBuilder {
debug('build', 'Initial setup...'); debug('build', 'Initial setup...');
const { logging } = this; const { logging } = this;
this.timer.init = performance.now(); this.timer.init = performance.now();
this.timer.viteStart = performance.now();
this.config = await runHookConfigSetup({ config: this.config, command: 'build' }); this.config = await runHookConfigSetup({ config: this.config, command: 'build' });
this.manifest = createRouteManifest({ config: this.config }, this.logging); this.manifest = createRouteManifest({ config: this.config }, this.logging);
@ -80,20 +77,11 @@ class AstroBuilder {
{ astroConfig: this.config, logging, mode: 'build' } { astroConfig: this.config, logging, mode: 'build' }
); );
await runHookConfigDone({ config: this.config }); await runHookConfigDone({ config: this.config });
const viteServer = await vite.createServer(viteConfig); return { viteConfig };
debug('build', timerMessage('Vite started', this.timer.viteStart));
return { viteConfig, viteServer };
} }
/** Run the build logic. build() is marked private because usage should go through ".run()" */ /** Run the build logic. build() is marked private because usage should go through ".run()" */
private async build({ private async build({ viteConfig }: { viteConfig: ViteConfigWithSSR }) {
viteConfig,
viteServer,
}: {
viteConfig: ViteConfigWithSSR;
viteServer: vite.ViteDevServer;
}) {
const { origin } = this;
const buildConfig: BuildConfig = { const buildConfig: BuildConfig = {
client: new URL('./client/', this.config.outDir), client: new URL('./client/', this.config.outDir),
server: new URL('./server/', this.config.outDir), server: new URL('./server/', this.config.outDir),
@ -111,10 +99,6 @@ class AstroBuilder {
astroConfig: this.config, astroConfig: this.config,
logging: this.logging, logging: this.logging,
manifest: this.manifest, manifest: this.manifest,
origin,
routeCache: this.routeCache,
viteServer,
ssr: this.config.output === 'server',
}); });
debug('build', timerMessage('All pages loaded', this.timer.loadStart)); debug('build', timerMessage('All pages loaded', this.timer.loadStart));
@ -131,7 +115,6 @@ class AstroBuilder {
colors.dim(`Completed in ${getTimeStat(this.timer.init, performance.now())}.`) colors.dim(`Completed in ${getTimeStat(this.timer.init, performance.now())}.`)
); );
try {
await staticBuild({ await staticBuild({
allPages, allPages,
astroConfig: this.config, astroConfig: this.config,
@ -144,11 +127,6 @@ class AstroBuilder {
viteConfig, viteConfig,
buildConfig, buildConfig,
}); });
} catch (err: unknown) {
// If the build doesn't complete, still shutdown the Vite server so the process doesn't hang.
await viteServer.close();
throw err;
}
// Write any additionally generated assets to disk. // Write any additionally generated assets to disk.
this.timer.assetsStart = performance.now(); this.timer.assetsStart = performance.now();
@ -162,7 +140,6 @@ class AstroBuilder {
debug('build', timerMessage('Additional assets copied', this.timer.assetsStart)); debug('build', timerMessage('Additional assets copied', this.timer.assetsStart));
// You're done! Time to clean up. // You're done! Time to clean up.
await viteServer.close();
await runHookBuildDone({ await runHookBuildDone({
config: this.config, config: this.config,
buildConfig, buildConfig,
@ -186,7 +163,7 @@ class AstroBuilder {
try { try {
await this.build(setupData); await this.build(setupData);
} catch (_err) { } catch (_err) {
throw fixViteErrorMessage(createSafeError(_err), setupData.viteServer); throw fixViteErrorMessage(_err);
} }
} }

View file

@ -1,4 +1,3 @@
import type { ViteDevServer } from 'vite';
import type { AstroConfig, ManifestData } from '../../@types/astro'; import type { AstroConfig, ManifestData } from '../../@types/astro';
import type { LogOptions } from '../logger/core'; import type { LogOptions } from '../logger/core';
import { info } from '../logger/core.js'; import { info } from '../logger/core.js';
@ -6,16 +5,11 @@ import type { AllPagesData } from './types';
import * as colors from 'kleur/colors'; import * as colors from 'kleur/colors';
import { debug } from '../logger/core.js'; import { debug } from '../logger/core.js';
import { RouteCache } from '../render/route-cache.js';
export interface CollectPagesDataOptions { export interface CollectPagesDataOptions {
astroConfig: AstroConfig; astroConfig: AstroConfig;
logging: LogOptions; logging: LogOptions;
manifest: ManifestData; manifest: ManifestData;
origin: string;
routeCache: RouteCache;
viteServer: ViteDevServer;
ssr: boolean;
} }
export interface CollectPagesDataResult { export interface CollectPagesDataResult {

View file

@ -42,11 +42,14 @@ export function cleanErrorStack(stack: string) {
.join('\n'); .join('\n');
} }
/** Update the error message to correct any vite-isms that we don't want to expose to the user. */ /**
export function fixViteErrorMessage(_err: unknown, server: ViteDevServer, filePath?: URL) { * Update the error message to correct any vite-isms that we don't want to expose to the user.
* The `server` is required if the error may come from `server.ssrLoadModule()`.
*/
export function fixViteErrorMessage(_err: unknown, server?: ViteDevServer, filePath?: URL) {
const err = createSafeError(_err); const err = createSafeError(_err);
// Vite will give you better stacktraces, using sourcemaps. // Vite will give you better stacktraces, using sourcemaps.
server.ssrFixStacktrace(err); server?.ssrFixStacktrace(err);
// Fix: Astro.glob() compiles to import.meta.glob() by the time Vite sees it, // Fix: Astro.glob() compiles to import.meta.glob() by the time Vite sees it,
// so we need to update this error message in case it originally came from Astro.glob(). // so we need to update this error message in case it originally came from Astro.glob().
if (err.message === 'import.meta.glob() can only accept string literals.') { if (err.message === 'import.meta.glob() can only accept string literals.') {