You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: guide/api-vite-runtime.md
+50-50
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,18 @@
1
-
# Vite Runtime API
1
+
# Vite 运行时 API {#vite-runtime-api}
2
2
3
-
:::warning Low-level API
4
-
This API was introduced in Vite 5.1 as an experimental feature. It was added to [gather feedback](https://github.com/vitejs/vite/discussions/15774). There will probably be breaking changes to it in Vite 5.2, so make sure to pin the Vite version to `~5.1.0` when using it. This is a low-level API meant for library and framework authors. If your goal is to create an application, make sure to check out the higher-level SSR plugins and tools at [Awesome Vite SSR section](https://github.com/vitejs/awesome-vite#ssr)first.
3
+
:::warning 低级别 API
4
+
这个 API 在 Vite 5.1 中作为一个实验性特性引入。它被添加以 [收集反馈](https://github.com/vitejs/vite/discussions/15774)。在Vite 5.2 中,它可能会有破坏性的变化,所以在使用它时,请确保将 Vite 版本固定在 `~5.1.0`。这是一个面向库和框架作者的低级别 API。如果你的目标是开发应用,请确保首先查看 [Vite SSR 精选板块](https://github.com/vitejs/awesome-vite#ssr)的高级 SSR 插件和工具。
5
5
:::
6
6
7
-
The "Vite Runtime" is a tool that allows running any code by processing it with Vite plugins first. It is different from `server.ssrLoadModule`because the runtime implementation is decoupled from the server. This allows library and framework authors to implement their own layer of communication between the server and the runtime.
7
+
"Vite 运行时" 是一个工具,它允许首先用 Vite 插件处理任何代码后运行。它与 `server.ssrLoadModule`不同,因为运行时实现是从服务器解耦的。这允许库和框架作者实现他们自己的服务器和运行时之间的通信层。
8
8
9
-
One of the goals of this feature is to provide a customizable API to process and run the code. Vite provides enough tools to use Vite Runtime out of the box, but users can build upon it if their needs do not align with Vite's built-in implementation.
9
+
这个特性的一个目标是提供一个可定制的API来处理和运行代码。Vite 提供了足够的工具来开箱即用 Vite 运行时,但如果用户的需求与 Vite 的内置实现不一致,他们可以在其基础上进行构建。
10
10
11
-
All APIs can be imported from `vite/runtime`unless stated otherwise.
11
+
除非另有说明,所有API都可以从 `vite/runtime`导入。
12
12
13
13
## `ViteRuntime`
14
14
15
-
**Type Signature:**
15
+
**类型签名:**
16
16
17
17
```ts
18
18
exportclassViteRuntime {
@@ -22,42 +22,42 @@ export class ViteRuntime {
22
22
privatedebug?:ViteRuntimeDebugger,
23
23
) {}
24
24
/**
25
-
* URL to execute. Accepts file path, server path, or id relative to the root.
The`ViteRuntime`class requires `root`and`fetchModule`options when initiated. Vite exposes `ssrFetchModule` on the [`server`](/guide/api-javascript)instance for easier integration with Vite SSR. Vite also exports `fetchModule`from its main entry point - it doesn't make any assumptions about how the code is running unlike `ssrFetchModule` that expects the code to run using `new Function`. This can be seen in source maps that these functions return.
54
+
当你初始化`ViteRuntime`类时,需要 `root`和`fetchModule`这两个选项。Vite 在 [`server`](/guide/api-javascript)实例中公开了 `ssrFetchModule`,以便更方便地与 Vite SSR 集成。Vite 主入口也导出了 `fetchModule`- 它不会假设代码的运行方式,这与期望代码通过 `new Function` 运行的 `ssrFetchModule` 是不同的,这一点可以从这些函数返回的 sourcemap 中看出。
55
55
56
-
Runner in `ViteRuntime`is responsible for executing the code. Vite exports`ESModulesRunner` out of the box, it uses `new AsyncFunction`to run the code. You can provide your own implementation if your JavaScript runtime doesn't support unsafe evaluation.
The two main methods that runtime exposes are `executeUrl`and`executeEntrypoint`. The only difference between them is that all modules executed by `executeEntrypoint` will be reexecuted if HMR triggers `full-reload`event. Be aware that Vite Runtime doesn't update`exports`object when this happens (it overrides it), you would need to run `executeUrl`or get the module from `moduleCache`again if you rely on having the latest `exports` object.
Vite exports`ESModulesRunner` that implements this interface by default. It uses `new AsyncFunction`to run code, so if the code has inlined source map it should contain an [offset of 2 lines](https://tc39.es/ecma262/#sec-createdynamicfunction)to accommodate for new lines added. This is done automatically by `server.ssrFetchModule`. If your runner implementation doesn't have this constraint, you should use `fetchModule` (exported from `vite`) directly.
This interface defines how HMR communication is established. Vite exports`ServerHMRConnector` from the main entry point to support HMR during Vite SSR. The `isReady` and `send` methods are usually called when the custom event is triggered (like, `import.meta.hot.send("my-event")`).
173
+
这个接口定义了如何建立热模块替换(HMR)的通信。Vite 从主入口处导出`ServerHMRConnector`,以在 Vite SSR 期间支持 HMR。当自定义事件被触发时(例如,`import.meta.hot.send("my-event")`),通常会调用 `isReady` 和 `send` 方法。
174
174
175
-
`onUpdate` is called only once when the new runtime is initiated. It passed down a method that should be called when connection triggers the HMR event. The implementation depends on the type of connection (as an example, it can be `WebSocket`/`EventEmitter`/`MessageChannel`), but it usually looks something like this:
The callback is queued and it will wait for the current update to be resolved before processing the next update. Unlike the browser implementation, HMR updates in Vite Runtime wait until all listeners (like, `vite:beforeUpdate`/`vite:beforeFullReload`) are finished before updating the modules.
0 commit comments