Refactor ViteConfigWithSSR type (#4952)
This commit is contained in:
parent
16688162b4
commit
5bcd76e3ab
7 changed files with 20 additions and 20 deletions
5
.changeset/moody-gifts-press.md
Normal file
5
.changeset/moody-gifts-press.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Refactor ViteConfigWithSSR type
|
|
@ -16,7 +16,6 @@ import type { SerializedSSRManifest } from '../core/app/types';
|
||||||
import type { PageBuildData } from '../core/build/types';
|
import type { PageBuildData } from '../core/build/types';
|
||||||
import type { AstroConfigSchema } from '../core/config';
|
import type { AstroConfigSchema } from '../core/config';
|
||||||
import type { AstroCookies } from '../core/cookies';
|
import type { AstroCookies } from '../core/cookies';
|
||||||
import type { ViteConfigWithSSR } from '../core/create-vite';
|
|
||||||
import type { AstroComponentFactory, Metadata } from '../runtime/server';
|
import type { AstroComponentFactory, Metadata } from '../runtime/server';
|
||||||
export type {
|
export type {
|
||||||
MarkdownHeading,
|
MarkdownHeading,
|
||||||
|
@ -1161,10 +1160,10 @@ export interface AstroIntegration {
|
||||||
'astro:build:ssr'?: (options: { manifest: SerializedSSRManifest }) => void | Promise<void>;
|
'astro:build:ssr'?: (options: { manifest: SerializedSSRManifest }) => void | Promise<void>;
|
||||||
'astro:build:start'?: (options: { buildConfig: BuildConfig }) => void | Promise<void>;
|
'astro:build:start'?: (options: { buildConfig: BuildConfig }) => void | Promise<void>;
|
||||||
'astro:build:setup'?: (options: {
|
'astro:build:setup'?: (options: {
|
||||||
vite: ViteConfigWithSSR;
|
vite: vite.InlineConfig;
|
||||||
pages: Map<string, PageBuildData>;
|
pages: Map<string, PageBuildData>;
|
||||||
target: 'client' | 'server';
|
target: 'client' | 'server';
|
||||||
updateConfig: (newConfig: ViteConfigWithSSR) => void;
|
updateConfig: (newConfig: vite.InlineConfig) => void;
|
||||||
}) => void | Promise<void>;
|
}) => void | Promise<void>;
|
||||||
'astro:build:generated'?: (options: { dir: URL }) => void | Promise<void>;
|
'astro:build:generated'?: (options: { dir: URL }) => void | Promise<void>;
|
||||||
'astro:build:done'?: (options: {
|
'astro:build:done'?: (options: {
|
||||||
|
|
|
@ -5,13 +5,14 @@ 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,
|
||||||
runHookConfigDone,
|
runHookConfigDone,
|
||||||
runHookConfigSetup,
|
runHookConfigSetup,
|
||||||
} from '../../integrations/index.js';
|
} from '../../integrations/index.js';
|
||||||
import { createVite, ViteConfigWithSSR } from '../create-vite.js';
|
import { createVite } from '../create-vite.js';
|
||||||
import { fixViteErrorMessage } from '../errors.js';
|
import { fixViteErrorMessage } from '../errors.js';
|
||||||
import { debug, info, levels, timerMessage } from '../logger/core.js';
|
import { debug, info, levels, timerMessage } from '../logger/core.js';
|
||||||
import { apply as applyPolyfill } from '../polyfill.js';
|
import { apply as applyPolyfill } from '../polyfill.js';
|
||||||
|
@ -84,7 +85,7 @@ class AstroBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 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({ viteConfig }: { viteConfig: ViteConfigWithSSR }) {
|
private async build({ viteConfig }: { viteConfig: vite.InlineConfig }) {
|
||||||
const buildConfig: BuildConfig = {
|
const buildConfig: BuildConfig = {
|
||||||
client: new URL('./client/', this.settings.config.outDir),
|
client: new URL('./client/', this.settings.config.outDir),
|
||||||
server: new URL('./server/', this.settings.config.outDir),
|
server: new URL('./server/', this.settings.config.outDir),
|
||||||
|
|
|
@ -9,7 +9,6 @@ import { prependForwardSlash } from '../../core/path.js';
|
||||||
import { emptyDir, isModeServerWithNoAdapter, removeDir } from '../../core/util.js';
|
import { emptyDir, isModeServerWithNoAdapter, removeDir } from '../../core/util.js';
|
||||||
import { runHookBuildSetup } from '../../integrations/index.js';
|
import { runHookBuildSetup } from '../../integrations/index.js';
|
||||||
import { PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
|
import { PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
|
||||||
import type { ViteConfigWithSSR } from '../create-vite';
|
|
||||||
import { info } from '../logger/core.js';
|
import { info } from '../logger/core.js';
|
||||||
import { getOutDirWithinCwd } from './common.js';
|
import { getOutDirWithinCwd } from './common.js';
|
||||||
import { generatePages } from './generate.js';
|
import { generatePages } from './generate.js';
|
||||||
|
@ -114,7 +113,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
|
||||||
const ssr = settings.config.output === 'server';
|
const ssr = settings.config.output === 'server';
|
||||||
const out = ssr ? opts.buildConfig.server : getOutDirWithinCwd(settings.config.outDir);
|
const out = ssr ? opts.buildConfig.server : getOutDirWithinCwd(settings.config.outDir);
|
||||||
|
|
||||||
const viteBuildConfig: ViteConfigWithSSR = {
|
const viteBuildConfig: vite.InlineConfig = {
|
||||||
...viteConfig,
|
...viteConfig,
|
||||||
mode: viteConfig.mode || 'production',
|
mode: viteConfig.mode || 'production',
|
||||||
logLevel: opts.viteConfig.logLevel ?? 'error',
|
logLevel: opts.viteConfig.logLevel ?? 'error',
|
||||||
|
@ -192,7 +191,7 @@ async function clientBuild(
|
||||||
|
|
||||||
info(opts.logging, null, `\n${bgGreen(black(' building client '))}`);
|
info(opts.logging, null, `\n${bgGreen(black(' building client '))}`);
|
||||||
|
|
||||||
const viteBuildConfig = {
|
const viteBuildConfig: vite.InlineConfig = {
|
||||||
...viteConfig,
|
...viteConfig,
|
||||||
mode: viteConfig.mode || 'production',
|
mode: viteConfig.mode || 'production',
|
||||||
logLevel: 'info',
|
logLevel: 'info',
|
||||||
|
@ -226,7 +225,7 @@ async function clientBuild(
|
||||||
],
|
],
|
||||||
envPrefix: 'PUBLIC_',
|
envPrefix: 'PUBLIC_',
|
||||||
base: settings.config.base,
|
base: settings.config.base,
|
||||||
} as ViteConfigWithSSR;
|
} ;
|
||||||
|
|
||||||
await runHookBuildSetup({
|
await runHookBuildSetup({
|
||||||
config: settings.config,
|
config: settings.config,
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import type { InlineConfig } from 'vite';
|
||||||
import type {
|
import type {
|
||||||
AstroSettings,
|
AstroSettings,
|
||||||
BuildConfig,
|
BuildConfig,
|
||||||
|
@ -7,7 +8,6 @@ import type {
|
||||||
RuntimeMode,
|
RuntimeMode,
|
||||||
SSRLoadedRenderer,
|
SSRLoadedRenderer,
|
||||||
} from '../../@types/astro';
|
} from '../../@types/astro';
|
||||||
import type { ViteConfigWithSSR } from '../create-vite';
|
|
||||||
import type { LogOptions } from '../logger/core';
|
import type { LogOptions } from '../logger/core';
|
||||||
import type { RouteCache } from '../render/route-cache';
|
import type { RouteCache } from '../render/route-cache';
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ export interface StaticBuildOptions {
|
||||||
origin: string;
|
origin: string;
|
||||||
pageNames: string[];
|
pageNames: string[];
|
||||||
routeCache: RouteCache;
|
routeCache: RouteCache;
|
||||||
viteConfig: ViteConfigWithSSR;
|
viteConfig: InlineConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SingleFileBuiltModule {
|
export interface SingleFileBuiltModule {
|
||||||
|
|
|
@ -21,9 +21,6 @@ import astroScriptsPageSSRPlugin from '../vite-plugin-scripts/page-ssr.js';
|
||||||
import { createCustomViteLogger } from './errors.js';
|
import { createCustomViteLogger } from './errors.js';
|
||||||
import { resolveDependency } from './util.js';
|
import { resolveDependency } from './util.js';
|
||||||
|
|
||||||
// note: ssr is still an experimental API hence the type omission from `vite`
|
|
||||||
export type ViteConfigWithSSR = vite.InlineConfig & { ssr?: vite.SSROptions };
|
|
||||||
|
|
||||||
interface CreateViteOptions {
|
interface CreateViteOptions {
|
||||||
settings: AstroSettings;
|
settings: AstroSettings;
|
||||||
logging: LogOptions;
|
logging: LogOptions;
|
||||||
|
@ -58,12 +55,12 @@ function getSsrNoExternalDeps(projectRoot: URL): string[] {
|
||||||
|
|
||||||
/** Return a common starting point for all Vite actions */
|
/** Return a common starting point for all Vite actions */
|
||||||
export async function createVite(
|
export async function createVite(
|
||||||
commandConfig: ViteConfigWithSSR,
|
commandConfig: vite.InlineConfig,
|
||||||
{ settings, logging, mode }: CreateViteOptions
|
{ settings, logging, mode }: CreateViteOptions
|
||||||
): Promise<ViteConfigWithSSR> {
|
): Promise<vite.InlineConfig> {
|
||||||
const thirdPartyAstroPackages = await getAstroPackages(settings);
|
const thirdPartyAstroPackages = await getAstroPackages(settings);
|
||||||
// Start with the Vite configuration that Astro core needs
|
// Start with the Vite configuration that Astro core needs
|
||||||
const commonConfig: ViteConfigWithSSR = {
|
const commonConfig: vite.InlineConfig = {
|
||||||
cacheDir: fileURLToPath(new URL('./node_modules/.vite/', settings.config.root)), // using local caches allows Astro to be used in monorepos, etc.
|
cacheDir: fileURLToPath(new URL('./node_modules/.vite/', settings.config.root)), // using local caches allows Astro to be used in monorepos, etc.
|
||||||
clearScreen: false, // we want to control the output, not Vite
|
clearScreen: false, // we want to control the output, not Vite
|
||||||
logLevel: 'warn', // log warnings and errors only
|
logLevel: 'warn', // log warnings and errors only
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { bold } from 'kleur/colors';
|
import { bold } from 'kleur/colors';
|
||||||
import type { AddressInfo } from 'net';
|
import type { AddressInfo } from 'net';
|
||||||
import type { ViteDevServer } from 'vite';
|
import type { InlineConfig, ViteDevServer } from 'vite';
|
||||||
import {
|
import {
|
||||||
AstroConfig,
|
AstroConfig,
|
||||||
AstroRenderer,
|
AstroRenderer,
|
||||||
|
@ -12,7 +12,6 @@ import {
|
||||||
import type { SerializedSSRManifest } from '../core/app/types';
|
import type { SerializedSSRManifest } from '../core/app/types';
|
||||||
import type { PageBuildData } from '../core/build/types';
|
import type { PageBuildData } from '../core/build/types';
|
||||||
import { mergeConfig } from '../core/config/config.js';
|
import { mergeConfig } from '../core/config/config.js';
|
||||||
import type { ViteConfigWithSSR } from '../core/create-vite.js';
|
|
||||||
import { info, LogOptions } from '../core/logger/core.js';
|
import { info, LogOptions } from '../core/logger/core.js';
|
||||||
|
|
||||||
async function withTakingALongTimeMsg<T>({
|
async function withTakingALongTimeMsg<T>({
|
||||||
|
@ -224,7 +223,7 @@ export async function runHookBuildSetup({
|
||||||
logging,
|
logging,
|
||||||
}: {
|
}: {
|
||||||
config: AstroConfig;
|
config: AstroConfig;
|
||||||
vite: ViteConfigWithSSR;
|
vite: InlineConfig;
|
||||||
pages: Map<string, PageBuildData>;
|
pages: Map<string, PageBuildData>;
|
||||||
target: 'server' | 'client';
|
target: 'server' | 'client';
|
||||||
logging: LogOptions;
|
logging: LogOptions;
|
||||||
|
|
Loading…
Add table
Reference in a new issue