Shared Plugins during Build
::: tip Feedback Give us feedback at Environment API feedback discussion :::
Affected scope: Vite Plugin Authors
::: warning Future Default Change
builder.sharedConfigBuild was first introduced in v6.0. You can set it true to check how your plugins work with a shared config. We're looking for feedback about changing the default in a future major once the plugin ecosystem is ready.
:::
Motivation
Align dev and build plugin pipelines.
Migration Guide
function CountTransformedModulesPlugin() {
let transformedModules
return {
name: 'count-transformed-modules',
buildStart() {
transformedModules = 0
},
transform(id) {
transformedModules++
},
buildEnd() {
console.log(transformedModules)
},
}
}
If we instead want to count the number of transformed modules for each environment, we need to keep a map:
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)
},
}
}
To simplify this pattern, Vite exports a perEnvironmentState helper:
function PerEnvironmentCountTransformedModulesPlugin() {
const state = perEnvironmentState<{ count: number }>(() => ({ count: 0 }))
return {
name: 'count-transformed-modules',
perEnvironmentStartEndDuringDev: true,
buildStart() {
state(this).count = 0
},
transform(id) {
state(this).count++
},
buildEnd() {
console.log(this.environment.name, state(this).count)
},
}
}
这篇文档对你有帮助吗?
谢谢你的反馈!