From a44810a7d08e59ff29db4d57fb7985bf600c67c1 Mon Sep 17 00:00:00 2001 From: patak-dev Date: Fri, 26 Apr 2024 22:36:49 +0200 Subject: [PATCH] fix: use environment.plugins during build, support create hook --- packages/vite/src/node/build.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index d77f4b16b16d95..32d13a1b490a26 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -49,6 +49,7 @@ import { partialEncodeURIPath, requireResolveFromRootWithFallback, } from './utils' +import { resolveBoundedPlugins } from './plugin' import { manifestPlugin } from './plugins/manifest' import type { Logger } from './logger' import { dataURIPlugin } from './plugins/dataUri' @@ -605,11 +606,10 @@ export async function buildEnvironment( const outDir = resolve(options.outDir) // inject environment and ssr arg to plugin load/transform hooks - const plugins = ( - environment || ssr - ? config.plugins.map((p) => injectEnvironmentToHooks(p, environment)) - : config.plugins - ) as Plugin[] + // TODO: rework lib mode + const plugins = (libOptions ? config : environment).plugins.map((p) => + injectEnvironmentToHooks(p, environment), + ) const rollupOptions: RollupOptions = { preserveEntrySignatures: ssr @@ -1396,6 +1396,14 @@ export class BuildEnvironment extends Environment { } super(name, config, options) } + + async init(): Promise { + if (this._inited) { + return + } + this._inited = true + this._plugins = await resolveBoundedPlugins(this) + } } export interface ViteBuilder { @@ -1504,6 +1512,9 @@ export async function createBuilder( } const environment = await createEnvironment(name, environmentConfig) + + await environment.init() + environments[name] = environment }