0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

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 * as colors from 'kleur/colors';
import { performance } from 'perf_hooks';
import * as vite from 'vite';
import {
runHookBuildDone,
runHookBuildStart,
@ -18,7 +17,6 @@ import { debug, info, levels, timerMessage } from '../logger/core.js';
import { apply as applyPolyfill } from '../polyfill.js';
import { RouteCache } from '../render/route-cache.js';
import { createRouteManifest } from '../routing/index.js';
import { createSafeError } from '../util.js';
import { collectPagesData } from './page-data.js';
import { staticBuild } from './static-build.js';
import { getTimeStat } from './util.js';
@ -64,7 +62,6 @@ class AstroBuilder {
debug('build', 'Initial setup...');
const { logging } = this;
this.timer.init = performance.now();
this.timer.viteStart = performance.now();
this.config = await runHookConfigSetup({ config: this.config, command: 'build' });
this.manifest = createRouteManifest({ config: this.config }, this.logging);
@ -80,20 +77,11 @@ class AstroBuilder {
{ astroConfig: this.config, logging, mode: 'build' }
);
await runHookConfigDone({ config: this.config });
const viteServer = await vite.createServer(viteConfig);
debug('build', timerMessage('Vite started', this.timer.viteStart));
return { viteConfig, viteServer };
return { viteConfig };
}
/** Run the build logic. build() is marked private because usage should go through ".run()" */
private async build({
viteConfig,
viteServer,
}: {
viteConfig: ViteConfigWithSSR;
viteServer: vite.ViteDevServer;
}) {
const { origin } = this;
private async build({ viteConfig }: { viteConfig: ViteConfigWithSSR }) {
const buildConfig: BuildConfig = {
client: new URL('./client/', this.config.outDir),
server: new URL('./server/', this.config.outDir),
@ -111,10 +99,6 @@ class AstroBuilder {
astroConfig: this.config,
logging: this.logging,
manifest: this.manifest,
origin,
routeCache: this.routeCache,
viteServer,
ssr: this.config.output === 'server',
});
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())}.`)
);
try {
await staticBuild({
allPages,
astroConfig: this.config,
@ -144,11 +127,6 @@ class AstroBuilder {
viteConfig,
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.
this.timer.assetsStart = performance.now();
@ -162,7 +140,6 @@ class AstroBuilder {
debug('build', timerMessage('Additional assets copied', this.timer.assetsStart));
// You're done! Time to clean up.
await viteServer.close();
await runHookBuildDone({
config: this.config,
buildConfig,
@ -186,7 +163,7 @@ class AstroBuilder {
try {
await this.build(setupData);
} 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 { LogOptions } from '../logger/core';
import { info } from '../logger/core.js';
@ -6,16 +5,11 @@ import type { AllPagesData } from './types';
import * as colors from 'kleur/colors';
import { debug } from '../logger/core.js';
import { RouteCache } from '../render/route-cache.js';
export interface CollectPagesDataOptions {
astroConfig: AstroConfig;
logging: LogOptions;
manifest: ManifestData;
origin: string;
routeCache: RouteCache;
viteServer: ViteDevServer;
ssr: boolean;
}
export interface CollectPagesDataResult {

View file

@ -42,11 +42,14 @@ export function cleanErrorStack(stack: string) {
.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);
// 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,
// 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.') {