Skip to content

Commit b37edc6

Browse files
release v3.2.0
2 parents acc06a8 + 25068b8 commit b37edc6

10 files changed

+139
-16
lines changed

.vitepress/config.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@ export default defineConfig({
2121
['meta', { property: 'og:description', content: ogDescription }],
2222
['meta', { name: 'twitter:card', content: 'summary_large_image' }],
2323
['meta', { name: 'twitter:site', content: '@vite_js' }],
24-
['meta', { name: 'theme-color', content: '#646cff' }]
24+
['meta', { name: 'theme-color', content: '#646cff' }],
25+
[
26+
'script',
27+
{
28+
src: 'https://cdn.usefathom.com/script.js',
29+
'data-site': 'CBDFBSLI',
30+
'data-spa': 'auto',
31+
defer: ''
32+
}
33+
]
2534
],
2635

2736
vue: {

config/build-options.md

+50-8
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,62 @@
1010

1111
另一个特殊值是 “esnext” —— 即假设有原生动态导入支持,并且将会转译得尽可能小:
1212

13-
- 如果 [`build.minify`](#build-minify) 选项为 `'terser'` `'esnext'` 将会强制降级为 `'es2019'`
13+
- 如果 [`build.minify`](#build-minify) 选项为 `'terser'``'esnext'` 将会强制降级为 `'es2019'`
1414
- 其他情况下将完全不会执行转译。
1515

1616
转换过程将会由 esbuild 执行,并且此值应该是一个合法的 [esbuild 目标选项](https://esbuild.github.io/api/#target)。自定义目标也可以是一个 ES 版本(例如:`es2015`)、一个浏览器版本(例如:`chrome58`)或是多个目标组成的一个数组。
1717

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

20-
## build.polyfillModulePreload {#build-polyfillmodulepreload}
20+
## build.modulePreload {#build-modulepreload}
2121

22-
- **类型:** `boolean`
22+
- **类型:** `boolean | { polyfill?: boolean, resolveDependencies?: ResolveModulePreloadDependenciesFn }`
2323
- **默认值:** `true`
2424

25-
用于决定是否自动注入 [module preload 的 polyfill](https://guybedford.com/es-module-preloading-integrity#modulepreload-polyfill).
26-
27-
如果设置为 `true`,此 polyfill 会被自动注入到每个 `index.html` 入口的 proxy 模块中。如果是通过 `build.rollupOptions.input` 将构建配置为使用非 html 的自定义入口,那么则需要在你自定义入口中手动引入 polyfill:
25+
默认情况下,一个 [模块预加载 polyfill](https://guybedford.com/es-module-preloading-integrity#modulepreload-polyfill) 会被自动注入。该 polyfill 会自动注入到每个 `index.html` 入口的的代理模块中。如果构建通过 `build.rollupOptions.input` 被配置为了使用非 HTML 入口的形式,那么必须要在你的自定义入口中手动引入该 polyfill:
2826

2927
```js
3028
import 'vite/modulepreload-polyfill'
3129
```
3230

3331
注意:此 polyfill **不适用于** [Library 模式](/guide/build#library-mode)。如果你需要支持不支持动态引入的浏览器,你应该避免在你的库中使用此选项。
3432

33+
此 polyfill 可以通过 `{ polyfill: false }` 来禁用。
34+
35+
每个动态导入要预加载的块列表将由 Vite 计算。默认情况下,在载入这些依赖时,会使用一个包含 `base` 的绝对路径。如果 `base` 是相对路径(`''` 或者 './'),解析时则会使用 `import.meta.url`,以避免出现依赖于最终部署基路径的绝对路径。
36+
37+
目前有一个实验性功能支持使用 `resolveDependencies` 函数对依赖项列表及其路径进行细粒度控制。它期望接收一个 `ResolveModulePreloadDependenciesFn` 类型的函数:
38+
39+
```ts
40+
type ResolveModulePreloadDependenciesFn = (
41+
url: string,
42+
deps: string[],
43+
context: {
44+
importer: string
45+
}
46+
) => (string | { runtime?: string })[]
47+
```
48+
49+
`resolveDependencies` 函数将为每个动态导入调用,同时带着一个它所依赖的 chunk 列表。并且它还会为每个在入口 HTML 文件中导入的 chunk 调用。 可以返回一个新的依赖关系数组,可能被过滤后变少了,也可能有更多依赖注入进来了,同时它们的路径也被修改过。`deps` 路径是相对于 `build.outDir` 的。若在注入该模块到 HTML head 时使用 `new URL(dep, import.meta.url)` 获取绝对路径,则对于 `hostType === 'js'`,允许返回一个相对于 `hostId` 的路径。
50+
51+
```js
52+
modulePreload: {
53+
resolveDependencies: (filename, deps, { hostId, hostType }) => {
54+
return deps.filter(condition)
55+
}
56+
}
57+
```
58+
59+
解析得到的依赖路径可以再在之后使用 [`experimental.renderBuiltUrl`](../guide/build.md#advanced-base-options) 更改。
60+
61+
## build.polyfillModulePreload {#build-polyfillmodulepreload}
62+
63+
- **类型:** `boolean`
64+
- **默认:** `true`
65+
- **已废弃** 请使用 `build.modulePreload.polyfill` 替代
66+
67+
是否自动注入一个 [模块预加载 polyfill](https://guybedford.com/es-module-preloading-integrity#modulepreload-polyfill)。
68+
3569
## build.outDir {#build-outdir}
3670
3771
- **类型:** `string`
@@ -111,10 +145,10 @@ Git LFS 占位符会自动排除在内联之外,因为它们不包含它们所
111145
112146
## build.lib {#build-lib}
113147
114-
- **类型:** `{ entry: string, name?: string, formats?: ('es' | 'cjs' | 'umd' | 'iife')[], fileName?: string | ((format: ModuleFormat) => string) }`
148+
- **类型:** `{ entry: string | string[] | { [entryAlias: string]: string }, name?: string, formats?: ('es' | 'cjs' | 'umd' | 'iife')[], fileName?: string | ((format: ModuleFormat, entryName: string) => string) }`
115149
- **相关内容:** [库模式](/guide/build#library-mode)
116150
117-
构建为库。`entry` 是必须的因为库不能使用 HTML 作为入口。`name` 则是暴露的全局变量, `formats` 包含 `'umd'``'iife'` 时是必须的。默认 `formats``['es', 'umd']` `fileName` 是输出的包文件名,默认 `fileName``package.json``name` 选项,同时,它还可以被定义为参数为 `format` 的函数。
151+
构建为库。`entry` 是必需的,因为库不能使用 HTML 作为入口。`name` 则是暴露的全局变量,并且在 `formats` 包含 `'umd'``'iife'` 时是必需的。默认 `formats``['es', 'umd']`,如果使用了多个配置入口,则是 `['es', 'cjs']``fileName` 是输出的包文件名,默认 `fileName``package.json``name` 选项,同时,它还可以被定义为参数为 `format``entryAlias` 的函数。
118152
119153
## build.manifest {#build-manifest}
120154
@@ -175,6 +209,14 @@ npm add -D terser
175209
176210
默认情况下,若 `outDir``root` 目录下,则 Vite 会在构建时清空该目录。若 `outDir` 在根目录之外则会抛出一个警告避免意外删除掉重要的文件。可以设置该选项来关闭这个警告。该功能也可以通过命令行参数 `--emptyOutDir` 来使用。
177211
212+
## build.copyPublicDir {#build-copypublicdir}
213+
214+
- **实验性特性**
215+
- **类型:** `boolean`
216+
- **默认:** `true`
217+
218+
默认情况下,Vite 会在构建阶段将 `publicDir` 目录中的所有文件复制到 `outDir` 目录中。可以通过设置该选项为 `false` 来禁用该行为。
219+
178220
## build.reportCompressedSize {#build-reportcompressedsize}
179221
180222
- **类型:** `boolean`

config/server-options.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,11 @@ export default defineConfig({
297297
## server.fs.deny {#server-fs-deny}
298298

299299
- **类型:** `string[]`
300+
- **类型:** `['.env', '.env.*', '*.{pem,crt}']`
300301

301302
用于限制 Vite 开发服务器提供敏感文件的黑名单。
302303

303-
默认为 `['.env', '.env.*', '*.{pem,crt}']`
304+
默认为 `['.env', '.env.*', '*.{pem,crt}']`这会比 [`server.fs.allow`](#server-fs-allow) 选项的优先级更高。同时还支持 [picomatch patterns](https://github.com/micromatch/picomatch#globbing-features)
304305

305306
## server.origin {#server-origin}
306307

config/shared-options.md

+10
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ Vite 有一个“允许的情景”列表,并且会匹配列表中第一个情
158158

159159
`package.json` 中,在解析包的入口点时尝试的字段列表。注意:这比从 `exports` 字段解析的情景导出优先级低:如果一个入口点从 `exports` 成功解析,`resolve.mainFields` 将被忽略。
160160

161+
## resolve.browserField {#resolve-browserfield}
162+
163+
- **类型:** `boolean`
164+
- **默认:** `true`
165+
- **已废弃**
166+
167+
是否启用对 `browser` 字段的解析。
168+
169+
在未来,`resolve.mainFields` 的默认值会变成 `['browser', 'module', 'jsnext:main', 'jsnext']` 而这个选项将被移除。
170+
161171
## resolve.extensions {#resolve-extensions}
162172

163173
- **类型:** `string[]`

guide/api-hmr.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,19 @@ if (import.meta.hot) {
125125
126126
## `hot.invalidate()` {#hot-invalidate}
127127
128-
现在调用 `import.meta.hot.invalidate()` 只是重新加载页面。
128+
一个接收自身的模块可以在运行时意识到它不能处理 HMR 更新,因此需要将更新强制传递给导入者。通过调用 `import.meta.hot.invalidate()`,HMR 服务将使调用方的导入失效,就像调用方不是接收自身的一样。
129+
130+
请注意,你应该总是调用 `import.meta.hot.accept`,即使你打算随后立即调用 `invalidate`,否则 HMR 客户端将不会监听未来对接收自身模块的更改。为了清楚地表达你的意图,我们建议在 `accept` 回调中调用 `invalidate`,例如:
131+
132+
133+
```js
134+
import.meta.hot.accept((module) => {
135+
// 你可以使用新的模块实例来决定是否使其失效。
136+
if (cannotHandleUpdate(module)) {
137+
import.meta.hot.invalidate()
138+
}
139+
})
140+
```
129141
130142
## `hot.on(event, cb)` {#hot-onevent-cb}
131143
@@ -136,6 +148,7 @@ if (import.meta.hot) {
136148
- `'vite:beforeUpdate'` 当更新即将被应用时(例如,一个模块将被替换)
137149
- `'vite:beforeFullReload'` 当完整的重载即将发生时
138150
- `'vite:beforePrune'` 当不再需要的模块即将被剔除时
151+
- `'vite:invalidate'` 当使用 `import.meta.hot.invalidate()` 使一个模块失效时
139152
- `'vite:error'` 当发生错误时(例如,语法错误)
140153
141154
自定义 HMR 事件可以由插件发送。更多细节详见 [handleHotUpdate](./api-plugin#handleHotUpdate)。

guide/api-javascript.md

+5
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ interface ViteDevServer {
117117
* 解决 ssr 错误堆栈信息
118118
*/
119119
ssrFixStacktrace(e: Error): void
120+
/**
121+
* 触发模块图中某个模块的 HMR。你可以使用 `server.moduleGraph`
122+
* API 来检索要重新加载的模块。如果 `hmr``false`,则不进行任何操作
123+
*/
124+
reloadModule(module: ModuleNode): Promise<void>
120125
/**
121126
* 启动服务器
122127
*/

guide/build.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ import { defineConfig } from 'vite'
128128
export default defineConfig({
129129
build: {
130130
lib: {
131+
// Could also be a dictionary or array of multiple entry points
131132
entry: resolve(__dirname, 'lib/main.js'),
132133
name: 'MyLib',
133134
// the proper extensions will be added
@@ -183,8 +184,30 @@ dist/my-lib.umd.cjs 0.30 KiB / gzip: 0.16 KiB
183184
}
184185
```
185186

187+
或者,如果暴露了多个入口起点:
188+
189+
```json
190+
{
191+
"name": "my-lib",
192+
"type": "module",
193+
"files": ["dist"],
194+
"main": "./dist/my-lib.cjs",
195+
"module": "./dist/my-lib.mjs",
196+
"exports": {
197+
".": {
198+
"import": "./dist/my-lib.mjs",
199+
"require": "./dist/my-lib.cjs"
200+
},
201+
"./secondary": {
202+
"import": "./dist/secondary.mjs",
203+
"require": "./dist/secondary.cjs"
204+
}
205+
}
206+
}
207+
```
208+
186209
::: tip 注意
187-
如果 `package.json` 不包含 `"type": "module"`,Vite 会生成不同的文件后缀名以兼容 Node.js。`.js` 会变为 `.mjs``.cjs` 会变为 `.js`.
210+
如果 `package.json` 不包含 `"type": "module"`,Vite 会生成不同的文件后缀名以兼容 Node.js。`.js` 会变为 `.mjs``.cjs` 会变为 `.js`
188211
:::
189212

190213
::: tip 环境变量

guide/features.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export type { T }
5757

5858
#### `useDefineForClassFields`
5959

60-
从 Vite v2.5.0 开始,如果 TypeScript 的 target 是 `ESNext`此选项默认值则为 `true`。这与 [`tsc` v4.3.2 及以后版本的行为](https://github.com/microsoft/TypeScript/pull/42663) 一致。这也是标准的 ECMAScript 的运行时行为。
60+
从 Vite v2.5.0 开始,如果 TypeScript 的 target 是 `ES2022` 或更高,包括 `ESNext`则此选项默认值为 `true`。这与 [`tsc` v4.3.2 及以后版本的行为](https://github.com/microsoft/TypeScript/pull/42663) 一致。这也是标准的 ECMAScript 的运行时行为。
6161

6262
但对于那些习惯其他编程语言或旧版本 TypeScript 的开发者来说,这可能是违反直觉的。
6363
你可以参阅 [TypeScript 3.7 发布日志](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#the-usedefineforclassfields-flag-and-the-declare-property-modifier) 中了解更多关于如何兼容的信息。
@@ -71,10 +71,13 @@ export type { T }
7171
#### 影响构建结果的其他编译器选项 {#other-compiler-options-affecting-the-build-result}
7272

7373
- [`extends`](https://www.typescriptlang.org/tsconfig#extends)
74+
- [`alwaysStrict`](https://www.typescriptlang.org/tsconfig#alwaysStrict)
7475
- [`importsNotUsedAsValues`](https://www.typescriptlang.org/tsconfig#importsNotUsedAsValues)
75-
- [`preserveValueImports`](https://www.typescriptlang.org/tsconfig#preserveValueImports)
76+
- [`jsx`](https://www.typescriptlang.org/tsconfig#jsx)
7677
- [`jsxFactory`](https://www.typescriptlang.org/tsconfig#jsxFactory)
7778
- [`jsxFragmentFactory`](https://www.typescriptlang.org/tsconfig#jsxFragmentFactory)
79+
- [`jsxImportSource`](https://www.typescriptlang.org/tsconfig#jsxImportSource)
80+
- [`preserveValueImports`](https://www.typescriptlang.org/tsconfig#preserveValueImports)
7881

7982
如果你的代码库很难迁移到 `"isolatedModules": true`,或许你可以尝试通过第三方插件来解决,比如 [rollup-plugin-friendly-type-imports](https://www.npmjs.com/package/rollup-plugin-friendly-type-imports)。但是,这种方式不被 Vite 官方支持。
8083

guide/static-deploy.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ $ npm run preview
6464

6565
2. 在你的项目中,创建一个 `deploy.sh` 脚本,包含以下内容(注意高亮的行,按需使用),运行脚本来部署站点:
6666

67-
```bash{13,21,24}
67+
```bash{16,24,27}
6868
#!/usr/bin/env sh
6969
7070
# 发生错误时终止
@@ -76,11 +76,14 @@ $ npm run preview
7676
# 进入构建文件夹
7777
cd dist
7878
79+
# 放置 .nojekyll 以绕过 Jekyll 的处理。
80+
echo > .nojekyll
81+
7982
# 如果你要部署到自定义域名
8083
# echo 'www.example.com' > CNAME
8184
8285
git init
83-
git checkout -b main
86+
git checkout -B main
8487
git add -A
8588
git commit -m 'deploy'
8689

guide/troubleshooting.md

+14
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ import './Foo.js' // 应该为 './foo.js'
7676

7777
同时如果有依赖环,也会发生完全重载。要解决这个问题,请先尝试解决依赖循环。
7878

79+
## 构建 {#build}
80+
81+
### 构建产物因为 CORS 错误无法工作 {#built-file-does-not-work-because-of-cors-error}
82+
83+
如果导出的 HTML 文件是通过 `file` 协议打开的,那么其中的 script 将不会运行,且报告下列错误。
84+
85+
> Access to script at 'file:///foo/bar.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.
86+
87+
> Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///foo/bar.js. (Reason: CORS request not http).
88+
89+
请查看 [释因:CORS 请求不是 HTTP 请求 - HTTP | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSRequestNotHttp) 了解为什么会发生这种情况的更多信息。
90+
91+
你需要通过 `http` 协议访问该文件。最简单的办法就是使用 `npx vite preview`
92+
7993
## 其他 {#others}
8094

8195
### Syntax Error / Type Error {#syntax-error-type-error-happens}

0 commit comments

Comments
 (0)