From fdc61929a2f1c0837562a30688616bfd3de3aa6e Mon Sep 17 00:00:00 2001 From: yoho Date: Tue, 19 Jul 2022 15:39:49 +0800 Subject: [PATCH 1/6] feat: worker config call config hook --- packages/vite/src/node/config.ts | 79 ++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 23 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 6b9195f17927ed..8f8884d0de07ca 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,6 +606,13 @@ export async function resolveConfig( const BASE_URL = resolvedBase + // resolve worker + const resolvedWorkerOptions: ResolveWorkerOptions = { + format: config.worker?.format || 'iife', + plugins: [], + rollupOptions: config.worker?.rollupOptions || {} + } + const resolved: ResolvedConfig = { ...config, configFile: configFile ? normalizePath(configFile) : undefined, @@ -659,13 +695,16 @@ 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[]) + ;(resolved.plugins as Plugin[]) = await resolvePlugins( + resolved, + prePlugins, + normalPlugins, + postPlugins + ) + const workerResolved: ResolvedConfig = { ...resolved, + ...workerConfig, isWorker: true, mainConfig: resolved } @@ -675,19 +714,13 @@ export async function resolveConfig( 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, - normalPlugins, - postPlugins - ) // 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`, { From 324463319d33cb9a8dad08af5226c8828772252a Mon Sep 17 00:00:00 2001 From: yoho Date: Tue, 19 Jul 2022 15:56:15 +0800 Subject: [PATCH 2/6] fix: config and feat test --- packages/vite/src/node/config.ts | 18 ++++++++---------- playground/worker/vite.config.js | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 8f8884d0de07ca..23a6f5e35f2f76 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -608,13 +608,12 @@ export async function resolveConfig( // resolve worker const resolvedWorkerOptions: ResolveWorkerOptions = { - format: config.worker?.format || 'iife', + format: workerConfig.worker?.format || 'iife', plugins: [], - rollupOptions: config.worker?.rollupOptions || {} + rollupOptions: workerConfig.worker?.rollupOptions || {} } - const resolved: ResolvedConfig = { - ...config, + const resolvedConfig: ResolvedConfig = { configFile: configFile ? normalizePath(configFile) : undefined, configFileDependencies: configFileDependencies.map((name) => normalizePath(path.resolve(name)) @@ -664,6 +663,7 @@ export async function resolveConfig( ...config.experimental } } + const resolved: ResolvedConfig = Object.assign(config, resolvedConfig) if (middlewareMode === 'ssr') { logger.warn( @@ -702,12 +702,10 @@ export async function resolveConfig( postPlugins ) - const workerResolved: ResolvedConfig = { - ...resolved, - ...workerConfig, - isWorker: true, - mainConfig: resolved - } + const workerResolved: ResolvedConfig = Object.assign( + workerConfig, + resolvedConfig + ) resolved.worker.plugins = await resolvePlugins( workerResolved, workerPrePlugins, diff --git a/playground/worker/vite.config.js b/playground/worker/vite.config.js index d62d6c4f6d6d36..7f37360d611b2a 100644 --- a/playground/worker/vite.config.js +++ b/playground/worker/vite.config.js @@ -5,7 +5,23 @@ module.exports = vite.defineConfig({ base: '/iife/', worker: { format: 'iife', - plugins: [vueJsx()], + plugins: [ + vueJsx(), + { + name: 'config-test', + config() { + return { + worker: { + rollupOptions: { + output: { + entryFileNames: 'assets/worker_.[name].js' + } + } + } + } + } + } + ], rollupOptions: { output: { assetFileNames: 'assets/worker_asset.[name].[ext]', From 106de0d24123f1897a83b3fa0c61504e8adc2a77 Mon Sep 17 00:00:00 2001 From: yoho Date: Tue, 19 Jul 2022 15:57:27 +0800 Subject: [PATCH 3/6] test: fix test --- playground/worker/vite.config.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/playground/worker/vite.config.js b/playground/worker/vite.config.js index 7f37360d611b2a..3a0578c329f8ea 100644 --- a/playground/worker/vite.config.js +++ b/playground/worker/vite.config.js @@ -14,7 +14,7 @@ module.exports = vite.defineConfig({ worker: { rollupOptions: { output: { - entryFileNames: 'assets/worker_.[name].js' + entryFileNames: 'assets/worker_entry.[name].js' } } } @@ -26,7 +26,8 @@ module.exports = vite.defineConfig({ 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' } } }, From 1b8271e82e7ef508bdbd864a5e03ed00ad391e4f Mon Sep 17 00:00:00 2001 From: yoho Date: Tue, 19 Jul 2022 16:03:15 +0800 Subject: [PATCH 4/6] fix: worker resolved config --- packages/vite/src/node/config.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 23a6f5e35f2f76..28a3cf5252be90 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -704,7 +704,11 @@ export async function resolveConfig( const workerResolved: ResolvedConfig = Object.assign( workerConfig, - resolvedConfig + resolvedConfig, + { + isWorker: true, + mainConfig: resolved + } ) resolved.worker.plugins = await resolvePlugins( workerResolved, From c4f4a82879c912df975e04fc258426b18e970b3a Mon Sep 17 00:00:00 2001 From: yoho Date: Tue, 19 Jul 2022 16:24:43 +0800 Subject: [PATCH 5/6] fix: vite.config.js effect other unit-test --- playground/worker/__tests__/iife/vite.config.js | 2 +- playground/worker/{vite.config.js => vite.config-iife.js} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename playground/worker/{vite.config.js => vite.config-iife.js} (100%) 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/vite.config.js b/playground/worker/vite.config-iife.js similarity index 100% rename from playground/worker/vite.config.js rename to playground/worker/vite.config-iife.js From 58305326653d79b0c252d8631413177df59c8851 Mon Sep 17 00:00:00 2001 From: yoho Date: Tue, 19 Jul 2022 16:25:23 +0800 Subject: [PATCH 6/6] chore: test command --- playground/worker/package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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",