diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index ef1fdbe522c133..f0c46ed9f94b19 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -395,6 +395,42 @@ export async function resolveConfig( mode = inlineConfig.mode || config.mode || mode configEnv.mode = mode + // Some plugins that aren't intended to work in the bundling of workers (doing post-processing at build time for example). + // And Plugins may also have cached that could be corrupted by being used in these extra rollup calls. + // So we need to separate the worker plugin from the plugin that vite needs to run. + const rawWorkerUserPlugins = ( + (await asyncFlatten(config.worker?.plugins || [])) as Plugin[] + ).filter((p) => { + if (!p) { + return false + } else if (!p.apply) { + return true + } else if (typeof p.apply === 'function') { + return p.apply({ ...config, mode }, configEnv) + } else { + return p.apply === command + } + }) + + let workerConfig = mergeConfig({}, config) + const [workerPrePlugins, workerNormalPlugins, workerPostPlugins] = + sortUserPlugins(rawWorkerUserPlugins) + + // run config hooks + const workerUserPlugins = [ + ...workerPrePlugins, + ...workerNormalPlugins, + ...workerPostPlugins + ] + for (const p of workerUserPlugins) { + if (p.config) { + const res = await p.config(workerConfig, configEnv) + if (res) { + workerConfig = mergeConfig(workerConfig, res) + } + } + } + // resolve plugins const rawUserPlugins = ( (await asyncFlatten(config.plugins || [])) as Plugin[] @@ -412,13 +448,6 @@ export async function resolveConfig( const [prePlugins, normalPlugins, postPlugins] = sortUserPlugins(rawUserPlugins) - // resolve worker - const resolvedWorkerOptions: ResolveWorkerOptions = { - format: config.worker?.format || 'iife', - plugins: [], - rollupOptions: config.worker?.rollupOptions || {} - } - // run config hooks const userPlugins = [...prePlugins, ...normalPlugins, ...postPlugins] for (const p of userPlugins) { @@ -577,8 +606,14 @@ export async function resolveConfig( const BASE_URL = resolvedBase - const resolved: ResolvedConfig = { - ...config, + // resolve worker + const resolvedWorkerOptions: ResolveWorkerOptions = { + format: workerConfig.worker?.format || 'iife', + plugins: [], + rollupOptions: workerConfig.worker?.rollupOptions || {} + } + + const resolvedConfig: ResolvedConfig = { configFile: configFile ? normalizePath(configFile) : undefined, configFileDependencies: configFileDependencies.map((name) => normalizePath(path.resolve(name)) @@ -628,6 +663,7 @@ export async function resolveConfig( ...config.experimental } } + const resolved: ResolvedConfig = Object.assign(config, resolvedConfig) if (middlewareMode === 'ssr') { logger.warn( @@ -659,26 +695,6 @@ export async function resolveConfig( ) } - // Some plugins that aren't intended to work in the bundling of workers (doing post-processing at build time for example). - // And Plugins may also have cached that could be corrupted by being used in these extra rollup calls. - // So we need to separate the worker plugin from the plugin that vite needs to run. - const [workerPrePlugins, workerNormalPlugins, workerPostPlugins] = - sortUserPlugins(config.worker?.plugins as Plugin[]) - const workerResolved: ResolvedConfig = { - ...resolved, - isWorker: true, - mainConfig: resolved - } - resolved.worker.plugins = await resolvePlugins( - workerResolved, - workerPrePlugins, - workerNormalPlugins, - workerPostPlugins - ) - // call configResolved worker plugins hooks - await Promise.all( - resolved.worker.plugins.map((p) => p.configResolved?.(workerResolved)) - ) ;(resolved.plugins as Plugin[]) = await resolvePlugins( resolved, prePlugins, @@ -686,8 +702,27 @@ export async function resolveConfig( postPlugins ) + const workerResolved: ResolvedConfig = Object.assign( + workerConfig, + resolvedConfig, + { + isWorker: true, + mainConfig: resolved + } + ) + resolved.worker.plugins = await resolvePlugins( + workerResolved, + workerPrePlugins, + workerNormalPlugins, + workerPostPlugins + ) + // call configResolved hooks - await Promise.all(userPlugins.map((p) => p.configResolved?.(resolved))) + await Promise.all( + userPlugins + .map((p) => p.configResolved?.(resolved)) + .concat(workerUserPlugins.map((p) => p.configResolved?.(workerResolved))) + ) if (process.env.DEBUG) { debug(`using resolved config: %O`, { diff --git a/playground/worker/__tests__/iife/vite.config.js b/playground/worker/__tests__/iife/vite.config.js index 4204f532b7ac1c..47821e59505e02 100644 --- a/playground/worker/__tests__/iife/vite.config.js +++ b/playground/worker/__tests__/iife/vite.config.js @@ -1 +1 @@ -module.exports = require('../../vite.config') +module.exports = require('../../vite.config-iife') diff --git a/playground/worker/package.json b/playground/worker/package.json index a4ee96e1620538..701fca25628e76 100644 --- a/playground/worker/package.json +++ b/playground/worker/package.json @@ -3,9 +3,9 @@ "private": true, "version": "0.0.0", "scripts": { - "dev": "vite", - "build": "vite build", - "preview": "vite preview", + "dev": "vite --config ./vite.config-iife.js", + "build": "vite --config ./vite.config-iife.js build", + "preview": "vite --config ./vite.config-iife.js preview", "dev:es": "vite --config ./vite.config-es.js dev", "build:es": "vite --config ./vite.config-es.js build", "preview:es": "vite --config ./vite.config-es.js preview", diff --git a/playground/worker/vite.config.js b/playground/worker/vite.config-iife.js similarity index 56% rename from playground/worker/vite.config.js rename to playground/worker/vite.config-iife.js index d62d6c4f6d6d36..3a0578c329f8ea 100644 --- a/playground/worker/vite.config.js +++ b/playground/worker/vite.config-iife.js @@ -5,12 +5,29 @@ module.exports = vite.defineConfig({ base: '/iife/', worker: { format: 'iife', - plugins: [vueJsx()], + plugins: [ + vueJsx(), + { + name: 'config-test', + config() { + return { + worker: { + rollupOptions: { + output: { + entryFileNames: 'assets/worker_entry.[name].js' + } + } + } + } + } + } + ], rollupOptions: { output: { assetFileNames: 'assets/worker_asset.[name].[ext]', chunkFileNames: 'assets/worker_chunk.[name].js', - entryFileNames: 'assets/worker_entry.[name].js' + // should fix by config-test plugin + entryFileNames: 'assets/worker_.[name].js' } } },