Skip to content

Commit 068d727

Browse files
docs(en): merge docs-cn/sync-docs into docs-cn/dev @ 836c818
2 parents 7006065 + 787ec22 commit 068d727

File tree

4 files changed

+72
-12
lines changed

4 files changed

+72
-12
lines changed

config/build-options.md

+39-5
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,55 @@
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` 函数对依赖项列表及其路径进行细粒度控制。它期望接收一个 `resolvemodulepreloaddependciesfn` 类型的函数:
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+
- **Type:** `boolean`
64+
- **Default:** `true`
65+
- **Deprecated** use `build.modulePreload.polyfill` instead
66+
67+
Whether to automatically inject a [module preload polyfill](https://guybedford.com/es-module-preloading-integrity#modulepreload-polyfill).
68+
3569
## build.outDir {#build-outdir}
3670
3771
- **类型:** `string`

guide/api-plugin.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,21 @@ export default defineConfig({
594594
595595
```ts
596596
// events.d.ts
597-
import 'vite/types/customEvent'
597+
import 'vite'
598+
import 'vite/client/types'
598599
599-
declare module 'vite/types/customEvent' {
600-
interface CustomEventMap {
601-
'custom:foo': { msg: string }
602-
// 'event-key': payload
603-
}
600+
interface MyCustomEventMap {
601+
'custom:foo': { msg: string }
602+
// 'event-key': payload
603+
}
604+
605+
// extend interface for server-side
606+
declare module 'vite' {
607+
interface CustomEventMap extends MyCustomEventMap {}
608+
}
609+
610+
// extend interface for client-side
611+
declare module 'vite/client/types' {
612+
interface CustomEventMap extends MyCustomEventMap {}
604613
}
605614
```

guide/static-deploy.md

+4-1
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,6 +76,9 @@ $ npm run preview
7676
# 进入构建文件夹
7777
cd dist
7878
79+
# 放置 .nojekyll 以绕过 Jekyll 的处理。
80+
echo > .nojekyll
81+
7982
# 如果你要部署到自定义域名
8083
# echo 'www.example.com' > CNAME
8184

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)