diff --git a/.changeset/bright-forks-add.md b/.changeset/bright-forks-add.md new file mode 100644 index 000000000..d43484f04 --- /dev/null +++ b/.changeset/bright-forks-add.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Teardown compiler after Vite build to free up memory when rendering pages diff --git a/packages/astro/package.json b/packages/astro/package.json index 339bd8d5d..48edc2435 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -99,7 +99,7 @@ "test:e2e:match": "playwright test -g" }, "dependencies": { - "@astrojs/compiler": "^1.1.0", + "@astrojs/compiler": "^1.2.0", "@astrojs/language-server": "^0.28.3", "@astrojs/markdown-remark": "^2.0.1", "@astrojs/telemetry": "^2.0.1", diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index 0cec490bd..dce294487 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -203,7 +203,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) { case 'build': { const { default: build } = await import('../core/build/index.js'); - return await build(settings, { ...flags, logging, telemetry }); + return await build(settings, { ...flags, logging, telemetry, teardownCompiler: true }); } case 'check': { diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index 85a0b6536..2068bd78d 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -18,13 +18,19 @@ import { apply as applyPolyfill } from '../polyfill.js'; import { RouteCache } from '../render/route-cache.js'; import { createRouteManifest } from '../routing/index.js'; import { collectPagesData } from './page-data.js'; -import { staticBuild } from './static-build.js'; +import { staticBuild, viteBuild } from './static-build.js'; import { getTimeStat } from './util.js'; +import { StaticBuildOptions } from './types.js'; export interface BuildOptions { mode?: RuntimeMode; logging: LogOptions; telemetry: AstroTelemetry; + /** + * Teardown the compiler WASM instance after build. This can improve performance when + * building once, but may cause a performance hit if building multiple times in a row. + */ + teardownCompiler?: boolean; } /** `astro build` */ @@ -42,6 +48,7 @@ class AstroBuilder { private routeCache: RouteCache; private manifest: ManifestData; private timer: Record; + private teardownCompiler: boolean; constructor(settings: AstroSettings, options: BuildOptions) { if (options.mode) { @@ -49,6 +56,7 @@ class AstroBuilder { } this.settings = settings; this.logging = options.logging; + this.teardownCompiler = options.teardownCompiler ?? false; this.routeCache = new RouteCache(this.logging); this.origin = settings.config.site ? new URL(settings.config.site).origin @@ -126,7 +134,7 @@ class AstroBuilder { colors.dim(`Completed in ${getTimeStat(this.timer.init, performance.now())}.`) ); - await staticBuild({ + const opts: StaticBuildOptions = { allPages, settings: this.settings, logging: this.logging, @@ -135,9 +143,13 @@ class AstroBuilder { origin: this.origin, pageNames, routeCache: this.routeCache, + teardownCompiler: this.teardownCompiler, viteConfig, buildConfig, - }); + }; + + const { internals } = await viteBuild(opts); + await staticBuild(opts, internals); // Write any additionally generated assets to disk. this.timer.assetsStart = performance.now(); diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 2e39128f5..10b113e43 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -1,3 +1,4 @@ +import { teardown } from '@astrojs/compiler'; import * as eslexer from 'es-module-lexer'; import glob from 'fast-glob'; import fs from 'fs'; @@ -25,7 +26,7 @@ import { registerAllPlugins } from './plugins/index.js'; import type { PageBuildData, StaticBuildOptions } from './types'; import { getTimeStat } from './util.js'; -export async function staticBuild(opts: StaticBuildOptions) { +export async function viteBuild(opts: StaticBuildOptions) { const { allPages, settings } = opts; // Make sure we have an adapter before building @@ -98,6 +99,17 @@ export async function staticBuild(opts: StaticBuildOptions) { settings.timer.end('Client build'); + // Free up memory + internals.ssrEntryChunk = undefined; + if (opts.teardownCompiler) { + teardown(); + } + + return { internals }; +} + +export async function staticBuild(opts: StaticBuildOptions, internals: BuildInternals) { + const { settings } = opts; switch (settings.config.output) { case 'static': { settings.timer.start('Static generate'); diff --git a/packages/astro/src/core/build/types.ts b/packages/astro/src/core/build/types.ts index f34089bc1..02f5618d8 100644 --- a/packages/astro/src/core/build/types.ts +++ b/packages/astro/src/core/build/types.ts @@ -39,6 +39,7 @@ export interface StaticBuildOptions { pageNames: string[]; routeCache: RouteCache; viteConfig: InlineConfig; + teardownCompiler: boolean; } export interface SingleFileBuiltModule { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cd498df0f..6c8963294 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -395,7 +395,7 @@ importers: packages/astro: specifiers: - '@astrojs/compiler': ^1.1.0 + '@astrojs/compiler': ^1.2.0 '@astrojs/language-server': ^0.28.3 '@astrojs/markdown-remark': ^2.0.1 '@astrojs/telemetry': ^2.0.1 @@ -485,7 +485,7 @@ importers: yargs-parser: ^21.0.1 zod: ^3.17.3 dependencies: - '@astrojs/compiler': 1.1.0 + '@astrojs/compiler': 1.2.0 '@astrojs/language-server': 0.28.3 '@astrojs/markdown-remark': link:../markdown/remark '@astrojs/telemetry': link:../telemetry @@ -3956,8 +3956,8 @@ packages: /@astrojs/compiler/0.31.4: resolution: {integrity: sha512-6bBFeDTtPOn4jZaiD3p0f05MEGQL9pw2Zbfj546oFETNmjJFWO3nzHz6/m+P53calknCvyVzZ5YhoBLIvzn5iw==} - /@astrojs/compiler/1.1.0: - resolution: {integrity: sha512-C4kTwirys+HafufMqaxCbML2wqkGaXJM+5AekXh/v1IIOnMIdcEON9GBYsG6qa8aAmLhZ58aUZGPhzcA3Dx7Uw==} + /@astrojs/compiler/1.2.0: + resolution: {integrity: sha512-O8yPCyuq+PU9Fjht2tIW6WzSWiq8qDF1e8uAX2x+SOGFzKqOznp52UlDG2mSf+ekf0Z3R34sb64O7SgX+asTxg==} dev: false /@astrojs/language-server/0.28.3: