Skip to content

Commit 620b516

Browse files
authored
release: v2.4.4
2 parents 8445413 + 90e33b6 commit 620b516

File tree

9 files changed

+69
-79
lines changed

9 files changed

+69
-79
lines changed

.vitepress/config.js

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ module.exports = {
7474
text: 'English',
7575
link: 'https://vitejs.dev'
7676
},
77+
{
78+
text: '日本語',
79+
link: 'https://ja.vitejs.dev'
80+
}
7781
]
7882
}
7983
],

config/index.md

+18-35
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Vite 也直接支持 TS 配置文件。你可以在 `vite.config.ts` 中使用 `
5555
如果配置文件需要基于(`serve``build`)命令或者不同的 [模式](/guide/env-and-mode) 来决定选项,则可以选择导出这样一个函数:
5656

5757
```js
58-
export default ({ command, mode }) => {
58+
export default defineConfig(({ command, mode }) => {
5959
if (command === 'serve') {
6060
return {
6161
// serve 独有配置
@@ -65,15 +65,15 @@ export default ({ command, mode }) => {
6565
// build 独有配置
6666
}
6767
}
68-
}
68+
})
6969
```
7070

7171
### 异步配置 {#async-config}
7272

7373
如果配置需要调用一个异步函数,也可以转而导出一个异步函数:
7474

7575
```js
76-
export default async ({ command, mode }) => {
76+
export default defineConfig(async ({ command, mode }) => {
7777
const data = await asyncFunction()
7878
return {
7979
// 构建模式所需的特有配置
@@ -242,15 +242,15 @@ export default async ({ command, mode }) => {
242242
指定传递给 CSS 预处理器的选项。例如:
243243

244244
```js
245-
export default {
245+
export default defineConfig({
246246
css: {
247247
preprocessorOptions: {
248248
scss: {
249249
additionalData: `$injectedColor: orange;`
250250
}
251251
}
252252
}
253-
}
253+
})
254254
```
255255

256256
### json.namedExports {#json-namedexports}
@@ -276,24 +276,24 @@ export default async ({ command, mode }) => {
276276
`ESBuildOptions` 继承自 [ESbuild 转换选项](https://esbuild.github.io/api/#transform-api)。最常见的用例是自定义 JSX:
277277

278278
```js
279-
export default {
279+
export default defineConfig({
280280
esbuild: {
281281
jsxFactory: 'h',
282282
jsxFragment: 'Fragment'
283283
}
284-
}
284+
})
285285
```
286286

287287
默认情况下,ESbuild 会被应用在 `ts``jsx``tsx` 文件。你可以通过 `esbuild.include``esbuild.exclude` 对要处理的文件类型进行配置,这两个配置的类型应为 `string | RegExp | (string | RegExp)[]`
288288

289289
此外,你还可以通过 `esbuild.jsxInject` 来自动为每一个被 ESbuild 转换的文件注入 JSX helper。
290290

291291
```js
292-
export default {
292+
export default defineConfig({
293293
esbuild: {
294294
jsxInject: `import React from 'react'`
295295
}
296-
}
296+
})
297297
```
298298

299299
设置为 `false` 来禁用 ESbuild 转换。
@@ -374,11 +374,11 @@ export default async ({ command, mode }) => {
374374
**示例:**
375375

376376
```js
377-
export default {
377+
export default defineConfig({
378378
server: {
379379
open: '/docs/index.html'
380380
}
381-
}
381+
})
382382
```
383383

384384
### server.proxy {#server-proxy}
@@ -392,7 +392,7 @@ export default async ({ command, mode }) => {
392392
**示例:**
393393

394394
```js
395-
export default {
395+
export default defineConfig({
396396
server: {
397397
proxy: {
398398
// 字符串简写写法
@@ -419,7 +419,7 @@ export default async ({ command, mode }) => {
419419
}
420420
}
421421
}
422-
}
422+
})
423423
```
424424

425425
### server.cors {#server-cors}
@@ -514,14 +514,14 @@ createServer()
514514
接受一个路径作为自定义工作区的 root 目录。可以是绝对路径或是相对于 [项目 root 目录](/guide/#index-html-and-project-root) 的相对路径。示例如下:
515515

516516
```js
517-
export default {
517+
export default defineConfig({
518518
server: {
519519
fs: {
520520
// 可以为项目根目录的上一级提供服务
521521
allow: ['..']
522522
}
523523
}
524-
}
524+
})
525525
```
526526

527527
## 构建选项 {#build-options}
@@ -543,23 +543,6 @@ createServer()
543543

544544
注意:如果代码包含不能被 `esbuild` 安全地编译的特性,那么构建将会失败。查看 [esbuild 文档](https://esbuild.github.io/content-types/#javascript) 获取更多细节。
545545

546-
### build.polyfillDynamicImport {#build-polyfilldynamicimport}
547-
548-
- **Type:** `boolean`
549-
- **Default:** `false`
550-
551-
设置是否自动注入 [动态导入 polyfill](https://github.com/GoogleChromeLabs/dynamic-import-polyfill)
552-
553-
如果设置为 `true`,该 polyfill 会被自动注入到每一个 `index.html` 入口的代理模块中。若将本次构建使用 `build.rollupOptions.input` 配置为使用非 html 的自定义入口,则需要在你的自定义的入口手动导入该 polyfill:
554-
555-
```js
556-
import 'vite/dynamic-import-polyfill'
557-
```
558-
559-
当使用 [`@vitejs/plugin-legacy`](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy) 时,该插件会将本选项自动配置为 `true`
560-
561-
注意:本 polyfill **无法**应用到 [库模式](/guide/build#library-mode) 中。如果你需要支持没有原生动态导入功能的浏览器,你可能需要避免在你的库中使用本功能。
562-
563546
### build.outDir {#build-outdir}
564547

565548
- **类型:** `string`
@@ -599,7 +582,7 @@ createServer()
599582
- **类型:** `boolean | 'inline'` | 'hidden'`
600583
- **默认:** `false`
601584
602-
构建后是否生成 source map 文件。
585+
构建后是否生成 source map 文件。如果为 `true`,将会创建一个独立的 source map 文件。如果为 `'inline'`,source map 将作为一个 data URI 附加在输出文件中。`'hidden'` 的工作原理与 `'true'` 相似,只是 bundle 文件中相应的注释将不被保留。
603586
604587
### build.rollupOptions {#build-rollupoptions}
605588
@@ -621,10 +604,10 @@ createServer()
621604
622605
### build.lib {#build-lib}
623606
624-
- **类型:** `{ entry: string, name?: string, formats?: ('es' | 'cjs' | 'umd' | 'iife')[], fileName?: string }`
607+
- **类型:** `{ entry: string, name?: string, formats?: ('es' | 'cjs' | 'umd' | 'iife')[], fileName?: string | ((format: ModuleFormat) => string) }`
625608
- **相关内容:** [库模式](/guide/build#library-mode)
626609
627-
构建为库。`entry` 是必须的因为库不能使用 HTML 作为入口。`name` 则是暴露的全局变量,在 `formats` 包含 `'umd'``'iife'` 时是必须的。默认 `formats``['es', 'umd']``fileName` 是输出的包文件名,默认 `fileName``package.json``name` 选项。
610+
构建为库。`entry` 是必须的因为库不能使用 HTML 作为入口。`name` 则是暴露的全局变量,在 `formats` 包含 `'umd'``'iife'` 时是必须的。默认 `formats``['es', 'umd']``fileName` 是输出的包文件名,默认 `fileName``package.json``name` 选项,同时,它还可以被定义为参数为 `format` 的函数
628611
629612
### build.manifest {#build-manifest}
630613

guide/api-javascript.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ Vite 的 JavaScript API 是完全类型化的,我们推荐使用 TypeScript
44

55
## `createServer` {#createserver}
66

7-
**类型签名**
7+
**类型签名**
88

99
```ts
1010
async function createServer(inlineConfig?: InlineConfig): Promise<ViteDevServer>
1111
```
1212

13-
**使用示例**
13+
**使用示例**
1414

1515
```js
1616
const { createServer } = require('vite')
@@ -119,15 +119,15 @@ interface ViteDevServer {
119119

120120
## `build`
121121

122-
**类型校验**
122+
**类型校验**
123123

124124
```ts
125125
async function build(
126126
inlineConfig?: InlineConfig
127127
): Promise<RollupOutput | RollupOutput[]>
128128
```
129129

130-
**使用示例**
130+
**使用示例**
131131

132132
```js
133133
const path = require('path')
@@ -148,12 +148,12 @@ const { build } = require('vite')
148148

149149
## `resolveConfig` {#resolveconfig}
150150

151-
**类型校验**
151+
**类型校验**
152152

153153
```ts
154154
async function resolveConfig(
155155
inlineConfig: InlineConfig,
156156
command: 'build' | 'serve',
157157
defaultMode?: string
158158
): Promise<ResolvedConfig>
159-
```
159+
```

guide/api-plugin.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ Vite 插件扩展了设计出色的 Rollup 接口,带有一些 Vite 独有的
3434
import vitePlugin from 'vite-plugin-feature'
3535
import rollupPlugin from 'rollup-plugin-feature'
3636

37-
export default {
37+
export default defineConfig({
3838
plugins: [vitePlugin(), rollupPlugin()]
39-
}
39+
})
4040
```
4141

4242
假值的插件将被忽略,可以用来轻松地启用或停用插件。
@@ -55,11 +55,12 @@ export default function framework(config) {
5555

5656
```js
5757
// vite.config.js
58+
import { defineConfig } from 'vite'
5859
import framework from 'vite-plugin-framework'
5960

60-
export default {
61+
export default defineConfig({
6162
plugins: [framework()]
62-
}
63+
})
6364
```
6465

6566
## 简单示例 {#simple-examples}
@@ -429,16 +430,17 @@ function myPlugin() {
429430
```js
430431
// vite.config.js
431432
import example from 'rollup-plugin-example'
433+
import { defineConfig } from 'vite'
432434
433-
export default {
435+
export default defineConfig({
434436
plugins: [
435437
{
436438
...example(),
437439
enforce: 'post',
438440
apply: 'build'
439441
}
440442
]
441-
}
443+
})
442444
```
443445

444446
查看 [Vite Rollup 插件](https://vite-rollup-plugins.patak.dev) 获取兼容的官方 Rollup 插件列表及其使用指南。

guide/backend-integration.md

+2-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
```js
1010
// vite.config.js
11-
export default {
11+
export default defineConfig({
1212
build: {
1313
// 在 outDir 中生成 manifest.json
1414
manifest: true,
@@ -17,14 +17,7 @@
1717
input: '/path/to/main.js'
1818
}
1919
}
20-
}
21-
```
22-
23-
如果你使用了 [`@vitejs/plugin-legacy`](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy) 插件或者手动开启 [`build.dynamicImportPolyfill` 选项](/config/#build-polyfilldynamicimport),请记得将 [dynamic import polyfill](/config/#build-polyfilldynamicimport) 添加到入口处,因为它并不会被自动注入:
24-
25-
```js
26-
// 添加到你应用入口文件的开头
27-
import 'vite/dynamic-import-polyfill'
20+
})
2821
```
2922

3023
2. 在开发环境中,在服务器的 HTML 模板中注入以下内容(用正在运行的本地 URL 替换 `http://localhost:3000`):

guide/build.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ defaults and supports es6-module and supports es6-module-dynamic-import, not ope
3232

3333
```js
3434
// vite.config.js
35-
module.exports = {
35+
module.exports = defineConfig({
3636
build: {
3737
rollupOptions: {
3838
// https://rollupjs.org/guide/en/#big-list-of-options
3939
}
4040
}
41-
}
41+
})
4242
```
4343

4444
例如,你可以使用仅在构建期间应用的插件来指定多个 Rollup 输出。
@@ -49,13 +49,13 @@ module.exports = {
4949

5050
```js
5151
// vite.config.js
52-
module.exports = {
52+
module.exports = defineConfig({
5353
build: {
5454
watch: {
5555
// https://rollupjs.org/guide/en/#watch-options
5656
}
5757
}
58-
}
58+
})
5959
```
6060

6161
## 多页面应用模式 {#multi-page-app}
@@ -79,8 +79,9 @@ module.exports = {
7979
```js
8080
// vite.config.js
8181
const { resolve } = require('path')
82+
const { defineConfig } = require('vite')
8283

83-
module.exports = {
84+
module.exports = defineConfig({
8485
build: {
8586
rollupOptions: {
8687
input: {
@@ -89,7 +90,7 @@ module.exports = {
8990
}
9091
}
9192
}
92-
}
93+
})
9394
```
9495

9596
如果你指定了另一个根目录,请记住,在解析输入路径时,`__dirname` 的值将仍然是 vite.config.js 文件所在的目录。因此,你需要把对应入口文件的 `root` 的路径添加到 `resolve` 的参数中。
@@ -103,12 +104,14 @@ module.exports = {
103104
```js
104105
// vite.config.js
105106
const path = require('path')
107+
const { defineConfig } = require('vite')
106108

107-
module.exports = {
109+
module.exports = defineConfig({
108110
build: {
109111
lib: {
110112
entry: path.resolve(__dirname, 'lib/main.js'),
111-
name: 'MyLib'
113+
name: 'MyLib',
114+
fileName: (format) => `my-lib.${format}.js`
112115
},
113116
rollupOptions: {
114117
// 确保外部化处理那些你不想打包进库的依赖
@@ -121,7 +124,7 @@ module.exports = {
121124
}
122125
}
123126
}
124-
}
127+
})
125128
```
126129

127130
使用如上配置运行 `vite build` 时,将会使用一套面向库的 Rollup 预设,并且将为该库提供两种构建格式:`es``umd` (可在 `build.lib` 中配置):

0 commit comments

Comments
 (0)