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-environment-frameworks.md
+54-54Lines changed: 54 additions & 54 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,19 @@
1
-
# Environment API for Frameworks
1
+
# フレームワーク向けの Environment API
2
2
3
-
:::warning Experimental
4
-
Initial work for this API was introduced in Vite 5.1 with the name "Vite Runtime API". This guide describes a revised API, renamed to Environment API. This API will be released in Vite 6 as experimental. You can already test it in the latest `[email protected]`version.
3
+
:::warning 実験的機能
4
+
この API の初期研究は、Vite 5.1 で「Vite ランタイム API」という名前で導入されました。このガイドでは、Environment API と改名された改訂版 API について説明します。この API は Vite 6 で実験的機能としてリリースされる予定です。すでに最新の `[email protected]`バージョンでテストできます。
5
5
6
-
Resources:
6
+
リソース:
7
7
8
-
-[Feedback discussion](https://github.com/vitejs/vite/discussions/16358) where we are gathering feedback about the new APIs.
9
-
-[Environment API PR](https://github.com/vitejs/vite/pull/16471) where the new API were implemented and reviewed.
8
+
-新しい API に関するフィードバックを収集する [Feedback discussion](https://github.com/vitejs/vite/discussions/16358)
9
+
-新しい API が実装され、レビューされる [Environment API PR](https://github.com/vitejs/vite/pull/16471)
10
10
11
-
Please share with us your feedback as you test the proposal.
11
+
この提案をテストする際には、ぜひフィードバックをお寄せください。
12
12
:::
13
13
14
-
## Environments andframeworks
14
+
## 環境とフレームワーク {#environments-and-frameworks}
15
15
16
-
The implicit `ssr`environment and other non-client environments use a `RunnableDevEnvironment`by default during dev. While this requires the runtime to be the same with the one the Vite server is running in, this works similarly with `ssrLoadModule`and allows frameworks to migrate and enable HMR for their SSR dev story. You can guard any runnable environment with an `isRunnableDevEnvironment`function.
16
+
暗黙的な `ssr`環境とその他の非クライアント環境では、開発中にデフォルトで `RunnableDevEnvironment`が使用されます。これには、Vite サーバーが実行しているのと同じランタイムが必要ですが、`ssrLoadModule`と同様に動作し、フレームワークが SSR 開発ストーリーの HMR を移行して有効にできるようにします。`isRunnableDevEnvironment`関数を使用して、実行可能な環境をすべて保護できます。
@@ -37,12 +37,12 @@ if (isRunnableDevEnvironment(server.environments.ssr)) {
37
37
```
38
38
39
39
:::warning
40
-
The `runner`is evaluated eagerly when it's accessed for the first time. Beware that Vite enables source map support when the `runner` is created by calling `process.setSourceMapsEnabled`or by overriding`Error.prepareStackTrace`if it's not available.
Given a Vite server configured in middleware mode as described by the [SSR setup guide](/guide/ssr#setting-up-the-dev-server), let's implement the SSR middleware using the environment API. Error handling is omitted.
45
+
[SSR セットアップガイド](/guide/ssr#setting-up-the-dev-server)で説明されているように、ミドルウェアモードに設定された Vite サーバーがあるとして、Environment API を使って SSR ミドルウェアを実装してみましょう。エラー処理は省略します。
46
46
47
47
```js
48
48
import { createServer } from'vite'
@@ -52,60 +52,60 @@ const server = await createServer({
52
52
appType:'custom',
53
53
environments: {
54
54
server: {
55
-
//by default, the modules are run in the same process as the vite dev server during dev
55
+
//デフォルトでは、開発中はモジュールは vite 開発サーバーと同じプロセスで実行されます
56
56
},
57
57
},
58
58
})
59
59
60
-
//You might need to cast this to RunnableDevEnvironment in TypeScript or use
61
-
//the "isRunnableDevEnvironment" function to guard the access to the runner
Since the `RunnableDevEnvironment`can only be used to run the code in the same runtime as the Vite server, it requires a runtime that can run the Vite Server (a runtime that is compatible with Node.js). This means that you will need to use the raw `DevEnvironment`to make it runtime agnostic.
95
+
`RunnableDevEnvironment`は Vite サーバーと同じランタイムでコードを実行する目的のみで使用できるため、Vite サーバーを実行できるランタイム(Node.js と互換性のあるランタイム)が必要です。つまり、ランタイムに依存しないようにするには、生の `DevEnvironment`を使用する必要があります。
96
96
97
-
:::info `FetchableDevEnvironment`proposal
97
+
:::info `FetchableDevEnvironment`プロポーザル
98
98
99
-
The initial proposal had a `run` method on the `DevEnvironment` class that would allow consumers to invoke an import on the runner side by using the `transport`option. During our testing we found out that the API was not universal enough to start recommending it. At the moment, we are looking for feedback on [the `FetchableDevEnvironment`proposal](https://github.com/vitejs/vite/discussions/18191).
99
+
当初の提案では、`DevEnvironment` クラスに `run` メソッドがあり、利用者は `transport`オプションを使用してランナー側でインポートを呼び出すことができました。テスト中に、この API は推奨するには汎用性が足りないことがわかりました。現在、[`FetchableDevEnvironment`プロポーザル](https://github.com/vitejs/vite/discussions/18191)に関するフィードバックを募集しています。
100
100
101
101
:::
102
102
103
-
`RunnableDevEnvironment`has a `runner.import`function that returns the value of the module. But this function is not available in the raw `DevEnvironment`and requires the code using the Vite's APIs and the user modules to be decoupled.
103
+
`RunnableDevEnvironment`には、モジュールの値を返す `runner.import`関数があります。ただし、この関数は生の `DevEnvironment`では使用できず、Vite の API を使用するコードとユーザーモジュールを分離する必要があります。
104
104
105
-
For example, the following example uses the value of the user module from the code using the Vite's APIs:
105
+
たとえば、次の例では、Vite の API を使用するコードからユーザーモジュールの値を使用しています:
106
106
107
107
```ts
108
-
//code using the Vite's APIs
108
+
//Vite の API を使用するコード
109
109
import { createServer } from'vite'
110
110
111
111
const server =createServer()
@@ -125,26 +125,26 @@ export function createHandler(input) {
125
125
}
126
126
```
127
127
128
-
If your code can run in the same runtime as the user modules (i.e., it does not rely on Node.js-specific APIs), you can use a virtual module. This approach eliminates the need to access the value from the code using Vite's APIs.
128
+
ユーザーモジュールと同じランタイムでコードを実行できる場合(つまり、Node.js 固有の API に依存しない場合)、仮想モジュールを使用できます。このアプローチにより、Vite の API を使用してコードから値にアクセスする必要がなくなります。
129
129
130
130
```ts
131
-
//code using the Vite's APIs
131
+
//Vite の API を使用するコード
132
132
import { createServer } from'vite'
133
133
134
134
const server =createServer({
135
135
plugins: [
136
-
//a plugin that handles `virtual:entrypoint`
136
+
// `virtual:entrypoint` を処理するプラグイン
137
137
{
138
138
name: 'virtual-module',
139
-
/*plugin implementation*/
139
+
/*プラグインの実装*/
140
140
},
141
141
],
142
142
})
143
143
const ssrEnvironment =server.environment.ssr
144
144
const input = {}
145
145
146
-
//use exposed functions by each environment factories that runs the code
147
-
//check for each environment factories what they provide
146
+
//コードを実行する各環境ファクトリーによって公開されている関数を使用します
147
+
//各環境ファクトリーについて、それらが提供するものをチェックします
148
148
if (ssrEnvironmentinstanceofRunnableDevEnvironment) {
@@ -199,26 +199,26 @@ function vitePluginVirtualIndexHtml(): Plugin {
199
199
}
200
200
```
201
201
202
-
If your code requires Node.js APIs, you can use `hot.send`to communicate with the code that uses Vite's APIs from the user modules. However, be aware that this approach may not work the same way after the build process.
202
+
コードに Node.js API が必要な場合は、`hot.send`を使用して、ユーザーモジュールから Vite の API を使用するコードと通信できます。ただし、このアプローチはビルドプロセス後に同じように機能しない可能性があることに注意してください。
203
203
204
204
```ts
205
-
//code using the Vite's APIs
205
+
//Vite の API を使用するコード
206
206
import { createServer } from'vite'
207
207
208
208
const server =createServer({
209
209
plugins: [
210
-
//a plugin that handles `virtual:entrypoint`
210
+
// `virtual:entrypoint` を処理するプラグイン
211
211
{
212
212
name: 'virtual-module',
213
-
/*plugin implementation*/
213
+
/*プラグインの実装*/
214
214
},
215
215
],
216
216
})
217
217
const ssrEnvironment =server.environment.ssr
218
218
const input = {}
219
219
220
-
//use exposed functions by each environment factories that runs the code
221
-
//check for each environment factories what they provide
220
+
//コードを実行する各環境ファクトリーによって公開されている関数を使用します
221
+
//各環境ファクトリーについて、それらが提供するものをチェックします
222
222
if (ssrEnvironmentinstanceofRunnableDevEnvironment) {
@@ -262,11 +262,11 @@ export function createHandler(input) {
262
262
}
263
263
```
264
264
265
-
## Environments duringbuild
265
+
## ビルド中の環境 {#environments-during-build}
266
266
267
-
In the CLI, calling `vite build`and`vite build --ssr`will still build the client only and ssr only environments for backward compatibility.
267
+
CLI において、`vite build`と`vite build --ssr`を呼び出すと、後方互換性のためにクライアントのみの環境と ssr のみの環境がビルドされます。
268
268
269
-
When `builder.entireApp`is`true`(or when calling `vite build --app`), `vite build`will opt-in into building the entire app instead. This would later on become the default in a future major. A `ViteBuilder`instance will be created (build-time equivalent to a `ViteDevServer`) to build all configured environments for production. By default the build of environments is run in series respecting the order of the `environments`record. A framework or user can further configure how the environments are built using:
Most of the time, the current `environment`instance will be available as part of the context of the code being run so the need to access them through `server.environments`should be rare. For example, inside plugin hooks the environment is exposed as part of the `PluginContext`, so it can be accessed using `this.environment`. See [Environment API for Plugins](./api-environment-plugins.md)to learn about how to build environment aware plugins.
0 commit comments