Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions guide/api-environment-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,29 @@ interface HotUpdateOptions {
}
```

## 插件中的基于环境的状态 {#per-environment-state-in-plugins}

鉴于相同的插件实例会被用于不同的环境,插件的状态需要以 `this.environment` 作为键来存储。这与生态系统中已使用的模式相同,即使用 `ssr` 布尔值作为键来避免混合客户端和 SSR 模块状态的方式。可以使用 `Map<Environment, State>` 来分别为每个环境保存其对应的状态。注意:为了保持向后兼容性,在未设置 `perEnvironmentStartEndDuringDev: true` 标志时,`buildStart` 和 `buildEnd` 仅会针对客户端环境被调用。

```js
function PerEnvironmentCountTransformedModulesPlugin() {
const state = new Map<Environment, { count: number }>()
return {
name: 'count-transformed-modules',
perEnvironmentStartEndDuringDev: true,
buildStart() {
state.set(this.environment, { count: 0 })
},
transform(id) {
state.get(this.environment).count++
},
buildEnd() {
console.log(this.environment.name, state.get(this.environment).count)
}
}
}
```

## 基于环境的插件 {#per-environment-plugins}

插件可以使用 `applyToEnvironment` 函数来定义它适用的环境。
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"feed": "^5.1.0",
"vitepress": "^1.6.3",
"vitepress-plugin-group-icons": "^1.6.0",
"vue": "^3.5.16",
"vue": "^3.5.17",
"@types/node": "^20.9.2",
"@type-challenges/utils": "^0.1.1",
"chalk": "^4.1.2",
Expand Down
Loading