Skip to content

Commit b15e138

Browse files
docs(en): merge docs-cn/sync-docs into docs-cn/dev @ 3d72c63
2 parents 3ef2db1 + 32b8d38 commit b15e138

File tree

5 files changed

+112
-9
lines changed

5 files changed

+112
-9
lines changed

config/index.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export default defineConfig(({ command, mode }) => {
151151

152152
定义全局常量替换方式。其中每项在开发环境下会被定义在全局,而在构建时被静态替换。
153153

154-
- `2.0.0-beta.70` 版本开始,字符串值将直接作为一个表达式,所以如果定义为了一个字符串常量,它需要被显式地引用(例如:通过 `JSON.stringify`
154+
- 为了与 [esbuild 的行为](https://esbuild.github.io/api/#define)保持一致,表达式必须为一个 JSON 对象(null、boolean、number、string、数组或对象),亦或是一个单独的标识符
155155

156156
- 替换只会在匹配到周围是单词边界(`\b`)时执行。
157157

@@ -323,6 +323,14 @@ export default defineConfig(({ command, mode }) => {
323323
})
324324
```
325325

326+
### css.devSourcemap
327+
328+
- **实验性**
329+
- **类型:** `boolean`
330+
- **默认:** `false`
331+
332+
在开发过程中是否启用 sourcemap。
333+
326334
### json.namedExports {#json-namedexports}
327335

328336
- **类型:** `boolean`
@@ -538,14 +546,12 @@ export default defineConfig(({ command, mode }) => {
538546

539547
### server.hmr {#server-hmr}
540548

541-
- **类型:** `boolean | { protocol?: string, host?: string, port?: number | false, path?: string, timeout?: number, overlay?: boolean, clientPort?: number, server?: Server }`
549+
- **类型:** `boolean | { protocol?: string, host?: string, port?: number, path?: string, timeout?: number, overlay?: boolean, clientPort?: number, server?: Server }`
542550

543551
禁用或配置 HMR 连接(用于 HMR websocket 必须使用不同的 http 服务器地址的情况)。
544552

545553
设置 `server.hmr.overlay``false` 可以禁用开发服务器错误的屏蔽。
546554

547-
当连接到某个域名而不需要端口时,可以设置 `server.hmr.port``false`
548-
549555
`clientPort` 是一个高级选项,只在客户端的情况下覆盖端口,这允许你为 websocket 提供不同的端口,而并非在客户端代码中查找。如果需要在 dev-server 情况下使用 SSL 代理,这非常有用。
550556

551557
当使用 `server.middlewareMode``server.https` 时,你需将 `server.hmr.server` 指定为你 HTTP(S) 的服务器,这将通过你的服务器来处理 HMR 的安全连接请求。这在使用自签证书或想通过网络在某端口暴露 Vite 的情况下,非常有用。
@@ -961,9 +967,9 @@ export default defineConfig({
961967

962968
- **类型:** `string | string[]`
963969

964-
默认情况下,Vite 会抓取你的 `index.html` 来检测需要预构建的依赖项。如果指定了 `build.rollupOptions.input`,Vite 将转而去抓取这些入口点。
970+
默认情况下,Vite 会抓取你的 `index.html` 来检测需要预构建的依赖项(忽略了`node_modules``build.outDir``__tests__``coverage`。如果指定了 `build.rollupOptions.input`,Vite 将转而去抓取这些入口点。
965971

966-
如果这两者都不合你意,则可以使用此选项指定自定义条目——该值需要遵循 [fast-glob 模式](https://github.com/mrmlnc/fast-glob#basic-syntax) ,或者是相对于 Vite 项目根的模式数组。这将覆盖掉默认条目推断
972+
如果这两者都不合你意,则可以使用此选项指定自定义条目——该值需要遵循 [fast-glob 模式](https://github.com/mrmlnc/fast-glob#basic-syntax) ,或者是相对于 Vite 项目根目录的匹配模式数组。当显式声明了 `optimizeDeps.entries` 时默认只有 `node_modules``build.outDir` 文件夹会被忽略。如果还需忽略其他文件夹,你可以在模式列表中使用以 `!` 为前缀的、用来匹配忽略项的模式
967973

968974
### optimizeDeps.exclude {#optimizedeps-exclude}
969975

guide/api-hmr.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,12 @@ if (import.meta.hot) {
122122
- `'vite:beforePrune'` 当不再需要的模块即将被剔除时
123123
- `'vite:error'` 当发生错误时(例如,语法错误)
124124
125-
自定义 HMR 事件可以由插件发送。更多细节详见 [handleHotUpdate](./api-plugin#handleHotUpdate)。
125+
自定义 HMR 事件可以由插件发送。更多细节详见 [handleHotUpdate](./api-plugin#handleHotUpdate)。
126+
127+
## `hot.send(event, data)`
128+
129+
发送自定义事件到 Vite 开发服务器。
130+
131+
如果在连接前调用,数据会先被缓存、等到连接建立好后再发送。
132+
133+
查看 [客户端与服务器的数据交互](/guide/api-plugin.html#client-server-communication) 一节获取更多细节。

guide/api-plugin.md

+84
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,87 @@ import { normalizePath } from 'vite'
492492
normalizePath('foo\\bar') // 'foo/bar'
493493
normalizePath('foo/bar') // 'foo/bar'
494494
```
495+
496+
## Client-server Communication
497+
498+
Since Vite 2.9, we provide some utilities for plugins to help handle the communication with clients.
499+
500+
### Server to Client
501+
502+
On the plugin side, we could use `server.ws.send` to boardcast events to all the clients:
503+
504+
```js
505+
// vite.config.js
506+
export default defineConfig({
507+
plugins: [
508+
{
509+
// ...
510+
configureServer(server) {
511+
server.ws.send('my:greetings', { msg: 'hello' })
512+
}
513+
}
514+
]
515+
})
516+
```
517+
518+
::: tip NOTE
519+
We recommend **alway prefixing** your event names to avoid collisions with other plugins.
520+
:::
521+
522+
On the client side, use [`hot.on`](/guide/api-hmr.html#hot-on-event-cb) to listen to the events:
523+
524+
```ts
525+
// client side
526+
if (import.meta.hot) {
527+
import.meta.hot.on('my:greetings', (data) => {
528+
console.log(data.msg) // hello
529+
})
530+
}
531+
```
532+
533+
### Client to Server
534+
535+
To send events from the client to the server, we can use [`hot.send`](/guide/api-hmr.html#hot-send-event-payload):
536+
537+
```ts
538+
// client side
539+
if (import.meta.hot) {
540+
import.meta.hot.send('my:from-client', { msg: 'Hey!' })
541+
}
542+
```
543+
544+
Then use `server.ws.on` and listen to the events on the server side:
545+
546+
```js
547+
// vite.config.js
548+
export default defineConfig({
549+
plugins: [
550+
{
551+
// ...
552+
configureServer(server) {
553+
server.ws.on('my:from-client', (data, client) => {
554+
console.log('Message from client:', data.msg) // Hey!
555+
// reply only to the client (if needed)
556+
client.send('my:ack', { msg: 'Hi! I got your message!' })
557+
})
558+
}
559+
}
560+
]
561+
})
562+
```
563+
564+
### TypeScript for Custom Events
565+
566+
It is possible to type custom events by extending the `CustomEventMap` interface:
567+
568+
```ts
569+
// events.d.ts
570+
import 'vite/types/customEvent'
571+
572+
declare module 'vite/types/customEvent' {
573+
interface CustomEventMap {
574+
'custom:foo': { msg: string }
575+
// 'event-key': payload
576+
}
577+
}
578+
```

guide/assets.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ function getImageUrl(name) {
103103
}
104104
```
105105
106-
在生产构建时,Vite 才会进行必要的转换保证 URL 在打包和资源哈希后仍指向正确的地址。
106+
在生产构建时,Vite 才会进行必要的转换保证 URL 在打包和资源哈希后仍指向正确的地址。然而,这个 URL 字符串必须是静态的,这样才好分析。否则代码将被原样保留、因而在 `build.target` 不支持 `import.meta.url` 时会导致运行时错误。
107+
108+
```js
109+
// Vite 不会转换这个
110+
const imgUrl = new URL(imagePath, import.meta.url).href
111+
```
107112
108113
::: warning 注意:无法在 SSR 中使用
109114
如果你正在以服务端渲染模式使用 Vite 则此模式不支持,因为 `import.meta.url` 在浏览器和 Node.js 中有不同的语义。服务端的产物也无法预先确定客户端主机 URL。

guide/env-and-mode.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Vite 使用 [dotenv](https://github.com/motdotla/dotenv) 从你的 [环境目录
3737

3838
一份用于指定模式的文件(例如 `.env.production`)会比通用形式的优先级更高(例如 `.env`)。
3939

40-
另外,Vite 执行时已经存在的环境变量有最高的优先级,不会被 `.env` 类文件覆盖。
40+
另外,Vite 执行时已经存在的环境变量有最高的优先级,不会被 `.env` 类文件覆盖。例如当运行 `VITE_SOME_KEY=123 vite build` 的时候。
4141

4242
`.env` 类文件会在 Vite 启动一开始时被加载,而改动会在重启服务器后生效。
4343
:::

0 commit comments

Comments
 (0)