diff options
author | 2023-11-17 22:02:05 +0800 | |
---|---|---|
committer | 2023-11-17 22:02:05 +0800 | |
commit | 1c48ed286538ab9e354eca4e4dcd7c6385c96721 (patch) | |
tree | 0e240fda3dbc1202ecd0507773ad4d29084e936b /packages | |
parent | cd0878751857ba38a833fa77d81ed3a2f6998e2f (diff) | |
download | astro-1c48ed286538ab9e354eca4e4dcd7c6385c96721.tar.gz astro-1c48ed286538ab9e354eca4e4dcd7c6385c96721.tar.zst astro-1c48ed286538ab9e354eca4e4dcd7c6385c96721.zip |
Support Vite 5 (#9122)
Diffstat (limited to 'packages')
17 files changed, 70 insertions, 59 deletions
diff --git a/packages/astro/package.json b/packages/astro/package.json index 1cb40861c..020ef48f0 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -175,7 +175,7 @@ "tsconfck": "^3.0.0", "unist-util-visit": "^4.1.2", "vfile": "^5.3.7", - "vite": "^4.4.9", + "vite": "^5.0.0", "vitefu": "^0.2.4", "which-pm": "^2.1.1", "yargs-parser": "^21.1.1", @@ -222,7 +222,7 @@ "rehype-slug": "^5.0.1", "rehype-toc": "^3.0.2", "remark-code-titles": "^0.1.2", - "rollup": "^3.28.1", + "rollup": "^4.4.1", "sass": "^1.66.1", "srcset-parse": "^1.1.0", "unified": "^10.1.2" diff --git a/packages/astro/src/core/preview/static-preview-server.ts b/packages/astro/src/core/preview/static-preview-server.ts index 937ba1c99..68a700b6d 100644 --- a/packages/astro/src/core/preview/static-preview-server.ts +++ b/packages/astro/src/core/preview/static-preview-server.ts @@ -72,8 +72,6 @@ export default async function createStaticPreviewServer( host: getResolvedHostForHttpServer(settings.config.server.host), port: settings.config.server.port, closed, - // 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) => { diff --git a/packages/astro/src/core/preview/vite-plugin-astro-preview.ts b/packages/astro/src/core/preview/vite-plugin-astro-preview.ts index 7f9979275..aafd69cb4 100644 --- a/packages/astro/src/core/preview/vite-plugin-astro-preview.ts +++ b/packages/astro/src/core/preview/vite-plugin-astro-preview.ts @@ -1,13 +1,13 @@ import fs from 'node:fs'; +import type { IncomingMessage, ServerResponse } from 'node:http'; import { fileURLToPath } from 'node:url'; import type { Connect, Plugin } from 'vite'; -import { version } from 'vite'; import type { AstroSettings } from '../../@types/astro.js'; import { notFoundTemplate, subpathNotUsedTemplate } from '../../template/4xx.js'; +import { cleanUrl } from '../../vite-plugin-utils/index.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; @@ -24,8 +24,7 @@ export function vitePluginAstroPreview(settings: AstroSettings): Plugin { return; } - const strippedPathname = stripBase(req.url!, base); - const pathname = new URL(strippedPathname, 'https://a.b').pathname; + const pathname = cleanUrl(stripBase(req.url!, base)); const isRoot = pathname === '/'; // Validate trailingSlash @@ -53,29 +52,49 @@ export function vitePluginAstroPreview(settings: AstroSettings): Plugin { }); return () => { - const fourOhFourMiddleware: Connect.NextHandleFunction = (req, res) => { - const errorPagePath = fileURLToPath(outDir + '/404.html'); - if (fs.existsSync(errorPagePath)) { - res.statusCode = 404; - res.setHeader('Content-Type', 'text/html;charset=utf-8'); - res.end(fs.readFileSync(errorPagePath)); - } else { - const pathname = stripBase(req.url!, base); - res.statusCode = 404; - res.end(notFoundTemplate(pathname, 'Not Found')); - } - }; + // NOTE: the `base` is stripped from `req.url` for post middlewares - // 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; + server.middlewares.use((req, res, next) => { + const pathname = cleanUrl(req.url!); + + // Vite doesn't handle /foo/ if /foo.html exists, we handle it anyways + if (pathname.endsWith('/')) { + const pathnameWithoutSlash = pathname.slice(0, -1); + const htmlPath = fileURLToPath(outDir + pathnameWithoutSlash + '.html'); + if (fs.existsSync(htmlPath)) { + req.url = pathnameWithoutSlash + '.html'; + return next(); } } - } else { - server.middlewares.use(fourOhFourMiddleware); + // Vite doesn't handle /foo if /foo/index.html exists, we handle it anyways + else { + const htmlPath = fileURLToPath(outDir + pathname + '/index.html'); + if (fs.existsSync(htmlPath)) { + req.url = pathname + '/index.html'; + return next(); + } + } + + next(); + }); + + // Vite has its own 404 middleware, we replace it with ours instead. + for (const middleware of server.middlewares.stack) { + // This hardcoded name will not break between Vite versions + if ((middleware.handle as Connect.HandleFunction).name === 'vite404Middleware') { + // Fallback to 404 page if it exists + middleware.handle = (req: IncomingMessage, res: ServerResponse) => { + const errorPagePath = fileURLToPath(outDir + '/404.html'); + if (fs.existsSync(errorPagePath)) { + res.statusCode = 404; + res.setHeader('Content-Type', 'text/html;charset=utf-8'); + res.end(fs.readFileSync(errorPagePath)); + } else { + res.statusCode = 404; + res.end(notFoundTemplate(req.url!, 'Not Found')); + } + }; + } } }; }, diff --git a/packages/astro/src/vite-plugin-load-fallback/index.ts b/packages/astro/src/vite-plugin-load-fallback/index.ts index e11f317ca..80db39edd 100644 --- a/packages/astro/src/vite-plugin-load-fallback/index.ts +++ b/packages/astro/src/vite-plugin-load-fallback/index.ts @@ -2,6 +2,7 @@ import nodeFs from 'node:fs'; import npath from 'node:path'; import type * as vite from 'vite'; import { slash } from '../core/path.js'; +import { cleanUrl } from '../vite-plugin-utils/index.js'; type NodeFileSystemModule = typeof nodeFs; @@ -77,8 +78,3 @@ export default function loadFallbackPlugin({ }, ]; } - -const queryRE = /\?.*$/s; -const hashRE = /#.*$/s; - -const cleanUrl = (url: string): string => url.replace(hashRE, '').replace(queryRE, ''); diff --git a/packages/astro/src/vite-plugin-scripts/index.ts b/packages/astro/src/vite-plugin-scripts/index.ts index 0066b98f5..9b2848923 100644 --- a/packages/astro/src/vite-plugin-scripts/index.ts +++ b/packages/astro/src/vite-plugin-scripts/index.ts @@ -50,8 +50,7 @@ export default function astroScriptsPlugin({ settings }: { settings: AstroSettin }, buildStart() { const hasHydrationScripts = settings.scripts.some((s) => s.stage === 'before-hydration'); - // @ts-expect-error Vite 5 renamed `ssrBuild` to `isSsrBuild` - const isSsrBuild = env?.ssrBuild || env?.isSsrBuild; + const isSsrBuild = env?.isSsrBuild; if (hasHydrationScripts && env?.command === 'build' && !isSsrBuild) { this.emitFile({ type: 'chunk', diff --git a/packages/astro/src/vite-plugin-utils/index.ts b/packages/astro/src/vite-plugin-utils/index.ts index 51f0e6cc4..7bf9f092f 100644 --- a/packages/astro/src/vite-plugin-utils/index.ts +++ b/packages/astro/src/vite-plugin-utils/index.ts @@ -56,3 +56,8 @@ export function normalizeFilename(filename: string, root: URL) { } return removeLeadingForwardSlashWindows(filename); } + +const postfixRE = /[?#].*$/s; +export function cleanUrl(url: string): string { + return url.replace(postfixRE, ''); +} diff --git a/packages/astro/test/fixtures/integration-add-page-extension/astro.config.mjs b/packages/astro/test/fixtures/integration-add-page-extension/astro.config.mjs index 0a0a33697..4c52ae0da 100644 --- a/packages/astro/test/fixtures/integration-add-page-extension/astro.config.mjs +++ b/packages/astro/test/fixtures/integration-add-page-extension/astro.config.mjs @@ -1,4 +1,4 @@ -import { defineConfig } from 'rollup' +import { defineConfig } from 'astro/config' import test from './integration.js' export default defineConfig({ diff --git a/packages/astro/test/fixtures/integration-server-setup/astro.config.mjs b/packages/astro/test/fixtures/integration-server-setup/astro.config.mjs index 0a0a33697..4c52ae0da 100644 --- a/packages/astro/test/fixtures/integration-server-setup/astro.config.mjs +++ b/packages/astro/test/fixtures/integration-server-setup/astro.config.mjs @@ -1,4 +1,4 @@ -import { defineConfig } from 'rollup' +import { defineConfig } from 'astro/config' import test from './integration.js' export default defineConfig({ diff --git a/packages/astro/test/preview-routing.test.js b/packages/astro/test/preview-routing.test.js index 8a4653a72..9e986c47c 100644 --- a/packages/astro/test/preview-routing.test.js +++ b/packages/astro/test/preview-routing.test.js @@ -1,12 +1,7 @@ import { expect } from 'chai'; -import { version } from 'vite'; import { loadFixture } from './test-utils.js'; -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('Preview Routing', function () { describe('build format: directory', () => { describe('Subpath without trailing slash and trailingSlash: never', () => { /** @type {import('./test-utils').Fixture} */ diff --git a/packages/integrations/markdoc/package.json b/packages/integrations/markdoc/package.json index 18e6aca5e..19657879d 100644 --- a/packages/integrations/markdoc/package.json +++ b/packages/integrations/markdoc/package.json @@ -89,8 +89,7 @@ "devalue": "^4.3.2", "linkedom": "^0.15.1", "mocha": "^10.2.0", - "rollup": "^3.28.1", - "vite": "^4.4.9" + "vite": "^5.0.0" }, "engines": { "node": ">=18.14.1" diff --git a/packages/integrations/markdoc/src/content-entry-type.ts b/packages/integrations/markdoc/src/content-entry-type.ts index 07c5268e4..21ba7ab19 100644 --- a/packages/integrations/markdoc/src/content-entry-type.ts +++ b/packages/integrations/markdoc/src/content-entry-type.ts @@ -6,8 +6,7 @@ import matter from 'gray-matter'; import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; -import type * as rollup from 'rollup'; -import type { ErrorPayload as ViteErrorPayload } from 'vite'; +import type { ErrorPayload as ViteErrorPayload, Rollup } from 'vite'; import type { ComponentConfig } from './config.js'; import { htmlTokenTransform } from './html/transform/html-token-transform.js'; import type { MarkdocConfigResult } from './load-config.js'; @@ -174,7 +173,7 @@ function getEntryInfo({ fileUrl, contents }: { fileUrl: URL; contents: string }) async function emitOptimizedImages( nodeChildren: Node[], ctx: { - pluginContext: rollup.PluginContext; + pluginContext: Rollup.PluginContext; filePath: string; astroConfig: AstroConfig; } diff --git a/packages/integrations/mdx/package.json b/packages/integrations/mdx/package.json index f9e22d58c..20948fe61 100644 --- a/packages/integrations/mdx/package.json +++ b/packages/integrations/mdx/package.json @@ -75,7 +75,7 @@ "remark-shiki-twoslash": "^3.1.3", "remark-toc": "^8.0.1", "unified": "^10.1.2", - "vite": "^4.4.9" + "vite": "^5.0.0" }, "engines": { "node": ">=18.14.1" diff --git a/packages/integrations/react/package.json b/packages/integrations/react/package.json index 49d357a94..21e86e811 100644 --- a/packages/integrations/react/package.json +++ b/packages/integrations/react/package.json @@ -45,7 +45,7 @@ "dev": "astro-scripts dev \"src/**/*.ts\"" }, "dependencies": { - "@vitejs/plugin-react": "^4.0.4", + "@vitejs/plugin-react": "^4.2.0", "ultrahtml": "^1.3.0" }, "devDependencies": { @@ -57,7 +57,7 @@ "cheerio": "1.0.0-rc.12", "react": "^18.1.0", "react-dom": "^18.1.0", - "vite": "^4.4.9" + "vite": "^5.0.0" }, "peerDependencies": { "@types/react": "^17.0.50 || ^18.0.21", diff --git a/packages/integrations/svelte/package.json b/packages/integrations/svelte/package.json index 4c7e8bb63..a29a87ffb 100644 --- a/packages/integrations/svelte/package.json +++ b/packages/integrations/svelte/package.json @@ -42,18 +42,18 @@ "dev": "astro-scripts dev \"src/**/*.ts\"" }, "dependencies": { - "@sveltejs/vite-plugin-svelte": "^2.5.2", + "@sveltejs/vite-plugin-svelte": "^3.0.0", "svelte2tsx": "^0.6.20" }, "devDependencies": { "astro": "workspace:*", "astro-scripts": "workspace:*", "svelte": "^4.2.0", - "vite": "^4.4.9" + "vite": "^5.0.0" }, "peerDependencies": { "astro": "^3.0.0", - "svelte": "^3.55.0 || ^4.0.0 || ^5.0.0-next.1" + "svelte": "^4.0.0 || ^5.0.0-next.1" }, "engines": { "node": ">=18.14.1" diff --git a/packages/integrations/svelte/src/index.ts b/packages/integrations/svelte/src/index.ts index 695214223..b894ee623 100644 --- a/packages/integrations/svelte/src/index.ts +++ b/packages/integrations/svelte/src/index.ts @@ -73,6 +73,7 @@ async function getViteConfiguration({ } if (!resolvedOptions.preprocess && !(await svelteConfigHasPreprocess(root))) { + // @ts-expect-error there's a bug with the types where the first arg should be optional resolvedOptions.preprocess = vitePreprocess(); } diff --git a/packages/integrations/tailwind/package.json b/packages/integrations/tailwind/package.json index d093ffc61..02b3a5069 100644 --- a/packages/integrations/tailwind/package.json +++ b/packages/integrations/tailwind/package.json @@ -40,7 +40,7 @@ "astro": "workspace:*", "astro-scripts": "workspace:*", "tailwindcss": "^3.3.3", - "vite": "^4.4.9" + "vite": "^5.0.0" }, "peerDependencies": { "astro": "^3.0.0", diff --git a/packages/integrations/vue/package.json b/packages/integrations/vue/package.json index b8fa62d38..df29f8918 100644 --- a/packages/integrations/vue/package.json +++ b/packages/integrations/vue/package.json @@ -40,8 +40,8 @@ "test": "mocha --timeout 20000" }, "dependencies": { - "@vitejs/plugin-vue": "^4.3.3", - "@vitejs/plugin-vue-jsx": "^3.0.2", + "@vitejs/plugin-vue": "^4.5.0", + "@vitejs/plugin-vue-jsx": "^3.1.0", "@vue/babel-plugin-jsx": "^1.1.5", "@vue/compiler-sfc": "^3.3.4" }, @@ -50,10 +50,10 @@ "astro": "workspace:*", "astro-scripts": "workspace:*", "chai": "^4.3.7", - "linkedom": "^0.15.1", "cheerio": "1.0.0-rc.12", + "linkedom": "^0.15.1", "mocha": "^10.2.0", - "vite": "^4.4.9", + "vite": "^5.0.0", "vue": "^3.3.4" }, "peerDependencies": { |