Skip to content

Commit

Permalink
Remove dev server during build (#4234)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Aug 10, 2022
1 parent dcc2480 commit c38e7f1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-chairs-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Remove dev server during build
53 changes: 15 additions & 38 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -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);

Expand All @@ -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),
Expand All @@ -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));
Expand All @@ -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();
Expand All @@ -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,
Expand All @@ -186,7 +163,7 @@ class AstroBuilder {
try {
await this.build(setupData);
} catch (_err) {
throw fixViteErrorMessage(createSafeError(_err), setupData.viteServer);
throw fixViteErrorMessage(_err);
}
}

Expand Down
6 changes: 0 additions & 6 deletions packages/astro/src/core/build/page-data.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import type { ViteDevServer } from 'vite';
import type { AstroConfig, ManifestData } from '../../@types/astro';
import type { LogOptions } from '../logger/core';
import { info } from '../logger/core.js';
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 {
Expand Down
9 changes: 6 additions & 3 deletions packages/astro/src/core/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.') {
Expand Down

0 comments on commit c38e7f1

Please sign in to comment.