From c38e7f1890ba5bc97ddacee91ea196bcfc7652e6 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Wed, 10 Aug 2022 22:06:02 +0800 Subject: [PATCH] Remove dev server during build (#4234) --- .changeset/silly-chairs-pretend.md | 5 ++ packages/astro/src/core/build/index.ts | 53 ++++++---------------- packages/astro/src/core/build/page-data.ts | 6 --- packages/astro/src/core/errors.ts | 9 ++-- 4 files changed, 26 insertions(+), 47 deletions(-) create mode 100644 .changeset/silly-chairs-pretend.md diff --git a/.changeset/silly-chairs-pretend.md b/.changeset/silly-chairs-pretend.md new file mode 100644 index 000000000..0b0175cc7 --- /dev/null +++ b/.changeset/silly-chairs-pretend.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Remove dev server during build diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index 782f6652e..154a77a7a 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -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,24 +115,18 @@ class AstroBuilder { colors.dim(`Completed in ${getTimeStat(this.timer.init, performance.now())}.`) ); - try { - await staticBuild({ - allPages, - astroConfig: this.config, - logging: this.logging, - manifest: this.manifest, - mode: this.mode, - origin: this.origin, - pageNames, - routeCache: this.routeCache, - 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; - } + await staticBuild({ + allPages, + astroConfig: this.config, + logging: this.logging, + manifest: this.manifest, + mode: this.mode, + origin: this.origin, + pageNames, + routeCache: this.routeCache, + viteConfig, + buildConfig, + }); // 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); } } diff --git a/packages/astro/src/core/build/page-data.ts b/packages/astro/src/core/build/page-data.ts index 97c6a2915..5de8804c4 100644 --- a/packages/astro/src/core/build/page-data.ts +++ b/packages/astro/src/core/build/page-data.ts @@ -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 { diff --git a/packages/astro/src/core/errors.ts b/packages/astro/src/core/errors.ts index 13eec5cb8..f7ddd1f9f 100644 --- a/packages/astro/src/core/errors.ts +++ b/packages/astro/src/core/errors.ts @@ -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.') {