Skip to content
Merged

d2m #1547

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
]
}

Expand Down
5 changes: 1 addition & 4 deletions guide/api-environment-frameworks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 客户端,
Expand Down
10 changes: 2 additions & 8 deletions guide/api-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ async function createServer(inlineConfig?: InlineConfig): Promise<ViteDevServer>
**使用示例:**

```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,
},
Expand Down Expand Up @@ -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: {
Expand Down
21 changes: 6 additions & 15 deletions guide/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -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。这确保了与开发服务器的工作方式保持一致的结构。

Expand All @@ -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',
Expand All @@ -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',
},
Expand Down
1 change: 0 additions & 1 deletion guide/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)。
Expand Down
6 changes: 1 addition & 5 deletions guide/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand All @@ -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',
)

Expand Down