diff --git a/.vitepress/config.ts b/.vitepress/config.ts index d7450854..9dc9022f 100644 --- a/.vitepress/config.ts +++ b/.vitepress/config.ts @@ -43,9 +43,9 @@ function inlineScript(file: string): HeadConfig { 'script', {}, fs.readFileSync( - path.resolve(__dirname, `./inlined-scripts/${file}`), - 'utf-8' - ) + path.resolve(import.meta.dirname, `./inlined-scripts/${file}`), + 'utf-8', + ), ] } diff --git a/guide/api-environment-frameworks.md b/guide/api-environment-frameworks.md index cc9e261f..16411bcc 100644 --- a/guide/api-environment-frameworks.md +++ b/guide/api-environment-frameworks.md @@ -52,11 +52,8 @@ if (isRunnableDevEnvironment(server.environments.ssr)) { ```js import fs from 'node:fs' import path from 'node:path' -import { fileURLToPath } from 'node:url' import { createServer } from 'vite' -const __dirname = path.dirname(fileURLToPath(import.meta.url)) - const viteServer = await createServer({ server: { middlewareMode: true }, appType: 'custom', @@ -75,7 +72,7 @@ app.use('*', async (req, res, next) => { const url = req.originalUrl // 1. 读取 index.html - const indexHtmlPath = path.resolve(__dirname, 'index.html') + const indexHtmlPath = path.resolve(import.meta.dirname, 'index.html') let template = fs.readFileSync(indexHtmlPath, 'utf-8') // 2. 应用 Vite HTML 转换。这将注入 Vite HMR 客户端, diff --git a/guide/api-javascript.md b/guide/api-javascript.md index 6bda0e09..1edeba87 100644 --- a/guide/api-javascript.md +++ b/guide/api-javascript.md @@ -13,15 +13,12 @@ async function createServer(inlineConfig?: InlineConfig): Promise **使用示例:** ```ts twoslash -import { fileURLToPath } from 'node:url' import { createServer } from 'vite' -const __dirname = fileURLToPath(new URL('.', import.meta.url)) - const server = await createServer({ // 任何合法的用户配置选项,加上 `mode` 和 `configFile` configFile: false, - root: __dirname, + root: import.meta.dirname, server: { port: 1337, }, @@ -208,13 +205,10 @@ async function build( ```ts twoslash [vite.config.js] import path from 'node:path' -import { fileURLToPath } from 'node:url' import { build } from 'vite' -const __dirname = fileURLToPath(new URL('.', import.meta.url)) - await build({ - root: path.resolve(__dirname, './project'), + root: path.resolve(import.meta.dirname, './project'), base: '/foo/', build: { rollupOptions: { diff --git a/guide/build.md b/guide/build.md index 5d8197d2..fd774bcd 100644 --- a/guide/build.md +++ b/guide/build.md @@ -118,24 +118,21 @@ export default defineConfig({ ```js twoslash [vite.config.js] import { dirname, resolve } from 'node:path' -import { fileURLToPath } from 'node:url' import { defineConfig } from 'vite' -const __dirname = dirname(fileURLToPath(import.meta.url)) - export default defineConfig({ build: { rolldownOptions: { input: { - main: resolve(__dirname, 'index.html'), - nested: resolve(__dirname, 'nested/index.html'), + main: resolve(import.meta.dirname, 'index.html'), + nested: resolve(import.meta.dirname, 'nested/index.html'), }, }, }, }) ``` -如果你指定了另一个根目录,请记住,在解析输入路径时,`__dirname` 的值将仍然是 `vite.config.js` 文件所在的目录。因此,你需要把对应入口文件的 `root` 的路径添加到 `resolve` 的参数中。 +如果你指定了另一个根目录,请记住,在解析输入路径时,`import.meta.dirname` 的值将仍然是 `vite.config.js` 文件所在的目录。因此,你需要把对应入口文件的 `root` 的路径添加到 `resolve` 的参数中。 请注意,在 HTML 文件中,Vite 忽略了 `rolldownOptions.input` 对象中给定的入口名称,而是在生成 dist 文件夹中的 HTML 资源文件时,使用了文件已解析的路径 ID。这确保了与开发服务器的工作方式保持一致的结构。 @@ -149,15 +146,12 @@ export default defineConfig({ ```js twoslash [vite.config.js (单入口)] import { dirname, resolve } from 'node:path' -import { fileURLToPath } from 'node:url' import { defineConfig } from 'vite' -const __dirname = dirname(fileURLToPath(import.meta.url)) - export default defineConfig({ build: { lib: { - entry: resolve(__dirname, 'lib/main.js'), + entry: resolve(import.meta.dirname, 'lib/main.js'), name: 'MyLib', // 将添加适当的扩展名后缀 fileName: 'my-lib', @@ -180,17 +174,14 @@ export default defineConfig({ ```js twoslash [vite.config.js (多入口)] import { dirname, resolve } from 'node:path' -import { fileURLToPath } from 'node:url' import { defineConfig } from 'vite' -const __dirname = dirname(fileURLToPath(import.meta.url)) - export default defineConfig({ build: { lib: { entry: { - 'my-lib': resolve(__dirname, 'lib/main.js'), - secondary: resolve(__dirname, 'lib/secondary.js'), + 'my-lib': resolve(import.meta.dirname, 'lib/main.js'), + secondary: resolve(import.meta.dirname, 'lib/secondary.js'), }, name: 'MyLib', }, diff --git a/guide/migration.md b/guide/migration.md index 67c12a86..195b9639 100644 --- a/guide/migration.md +++ b/guide/migration.md @@ -342,7 +342,6 @@ const plugin = { 还有其他一些只影响少数用户的破坏性更改。 - **[TODO: 这将在稳定版发布前修复]** https://github.com/rolldown/rolldown/issues/5726 (affects nuxt, qwik) -- **[TODO: 这将在稳定版发布前修复]** 由于缺少预构建块输出功能([rolldown#4304](https://github.com/rolldown/rolldown/issues/4034)),旧版块现在作为资源文件而不是块文件输出。这意味着块相关选项不适用于旧版块,清单文件也不会将旧版块包含为块文件。 - **[TODO: 这将在稳定版发布前修复]** `@vite-ignore` 注释边缘情况 ([rolldown-vite#426](https://github.com/vitejs/rolldown-vite/issues/426)) - [Extglobs](https://github.com/micromatch/picomatch/blob/master/README.md#extglobs) 尚未得到支持 ([rolldown-vite#365](https://github.com/vitejs/rolldown-vite/issues/365)) - `define` 不共享对象引用:当你传递一个对象作为 `define` 的值时,每个变量都会有一个单独的对象副本。详见 [Oxc 转换器文档](https://oxc.rs/docs/guide/usage/transformer/global-variable-replacement#define)。 diff --git a/guide/ssr.md b/guide/ssr.md index c63ba07f..0baa33cb 100644 --- a/guide/ssr.md +++ b/guide/ssr.md @@ -68,12 +68,9 @@ if (import.meta.env.SSR) { ```js{15-18} twoslash [server.js] import fs from 'node:fs' import path from 'node:path' -import { fileURLToPath } from 'node:url' import express from 'express' import { createServer as createViteServer } from 'vite' -const __dirname = path.dirname(fileURLToPath(import.meta.url)) - async function createServer() { const app = express() @@ -111,7 +108,6 @@ createServer() // @noErrors import fs from 'node:fs' import path from 'node:path' -import { fileURLToPath } from 'node:url' /** @type {import('express').Express} */ var app @@ -125,7 +121,7 @@ app.use('*all', async (req, res, next) => { try { // 1. 读取 index.html let template = fs.readFileSync( - path.resolve(__dirname, 'index.html'), + path.resolve(import.meta.dirname, 'index.html'), 'utf-8', )