0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

Handle Vite 5 changes (#9005)

This commit is contained in:
Bjorn Lu 2023-11-06 23:13:57 +08:00 committed by GitHub
parent 0f5b054281
commit c5a7993266
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 7 deletions

View file

@ -54,7 +54,7 @@ export default async function createStaticPreviewServer(
null,
msg.serverStart({
startupTime: performance.now() - startServerTime,
resolvedUrls: previewServer.resolvedUrls,
resolvedUrls: previewServer.resolvedUrls ?? { local: [], network: [] },
host: settings.config.server.host,
base: settings.config.base,
})
@ -72,7 +72,9 @@ export default async function createStaticPreviewServer(
host: getResolvedHostForHttpServer(settings.config.server.host),
port: settings.config.server.port,
closed,
server: previewServer.httpServer,
// In Vite 5, `httpServer` may be a `Http2SecureServer`, but we know we are only starting a HTTP server
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
server: previewServer.httpServer as http.Server,
stop: async () => {
await new Promise((resolve, reject) => {
previewServer.httpServer.destroy((err) => (err ? reject(err) : resolve(undefined)));

View file

@ -1,11 +1,13 @@
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import type { Plugin } from 'vite';
import { version } from 'vite';
import type { Plugin, Connect } from 'vite';
import type { AstroSettings } from '../../@types/astro.js';
import { notFoundTemplate, subpathNotUsedTemplate } from '../../template/4xx.js';
import { stripBase } from './util.js';
const HAS_FILE_EXTENSION_REGEXP = /^.*\.[^\\]+$/;
const IS_VITE_5 = version.startsWith('5.');
export function vitePluginAstroPreview(settings: AstroSettings): Plugin {
const { base, outDir, trailingSlash } = settings.config;
@ -50,7 +52,7 @@ export function vitePluginAstroPreview(settings: AstroSettings): Plugin {
});
return () => {
server.middlewares.use((req, res) => {
const fourOhFourMiddleware: Connect.NextHandleFunction = (req, res) => {
const errorPagePath = fileURLToPath(outDir + '/404.html');
if (fs.existsSync(errorPagePath)) {
res.statusCode = 404;
@ -61,7 +63,19 @@ export function vitePluginAstroPreview(settings: AstroSettings): Plugin {
res.statusCode = 404;
res.end(notFoundTemplate(pathname, 'Not Found'));
}
});
};
// Vite 5 has its own 404 middleware, we replace it with ours instead.
if (IS_VITE_5) {
for (const middleware of server.middlewares.stack) {
// This hardcoded name will not break between Vite versions
if ((middleware.handle as Connect.HandleFunction).name === 'vite404Middleware') {
middleware.handle = fourOhFourMiddleware;
}
}
} else {
server.middlewares.use(fourOhFourMiddleware);
}
};
},
};

View file

@ -50,7 +50,9 @@ export default function astroScriptsPlugin({ settings }: { settings: AstroSettin
},
buildStart() {
const hasHydrationScripts = settings.scripts.some((s) => s.stage === 'before-hydration');
if (hasHydrationScripts && env?.command === 'build' && !env?.ssrBuild) {
// @ts-expect-error Vite 5 renamed `ssrBuild` to `isSsrBuild`
const isSsrBuild = env?.ssrBuild || env?.isSsrBuild;
if (hasHydrationScripts && env?.command === 'build' && !isSsrBuild) {
this.emitFile({
type: 'chunk',
id: BEFORE_HYDRATION_SCRIPT_ID,

View file

@ -1,7 +1,12 @@
import { expect } from 'chai';
import { version } from 'vite';
import { loadFixture } from './test-utils.js';
describe('Preview Routing', () => {
const IS_VITE_5 = version.startsWith('5.');
// Skip in Vite 5 as it changes how HTML files are served. We may want to review aligning
// trailingSlash and build.format to avoid potential footguns in Astro 4
(IS_VITE_5 ? describe.skip : describe)('Preview Routing', function () {
describe('build format: directory', () => {
describe('Subpath without trailing slash and trailingSlash: never', () => {
/** @type {import('./test-utils').Fixture} */