Skip to content

Commit 3e510ab

Browse files
authored
feat: worker config call config hook (#9212)
1 parent 88baa53 commit 3e510ab

File tree

4 files changed

+88
-36
lines changed

4 files changed

+88
-36
lines changed

packages/vite/src/node/config.ts

+65-30
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,42 @@ export async function resolveConfig(
395395
mode = inlineConfig.mode || config.mode || mode
396396
configEnv.mode = mode
397397

398+
// Some plugins that aren't intended to work in the bundling of workers (doing post-processing at build time for example).
399+
// And Plugins may also have cached that could be corrupted by being used in these extra rollup calls.
400+
// So we need to separate the worker plugin from the plugin that vite needs to run.
401+
const rawWorkerUserPlugins = (
402+
(await asyncFlatten(config.worker?.plugins || [])) as Plugin[]
403+
).filter((p) => {
404+
if (!p) {
405+
return false
406+
} else if (!p.apply) {
407+
return true
408+
} else if (typeof p.apply === 'function') {
409+
return p.apply({ ...config, mode }, configEnv)
410+
} else {
411+
return p.apply === command
412+
}
413+
})
414+
415+
let workerConfig = mergeConfig({}, config)
416+
const [workerPrePlugins, workerNormalPlugins, workerPostPlugins] =
417+
sortUserPlugins(rawWorkerUserPlugins)
418+
419+
// run config hooks
420+
const workerUserPlugins = [
421+
...workerPrePlugins,
422+
...workerNormalPlugins,
423+
...workerPostPlugins
424+
]
425+
for (const p of workerUserPlugins) {
426+
if (p.config) {
427+
const res = await p.config(workerConfig, configEnv)
428+
if (res) {
429+
workerConfig = mergeConfig(workerConfig, res)
430+
}
431+
}
432+
}
433+
398434
// resolve plugins
399435
const rawUserPlugins = (
400436
(await asyncFlatten(config.plugins || [])) as Plugin[]
@@ -412,13 +448,6 @@ export async function resolveConfig(
412448
const [prePlugins, normalPlugins, postPlugins] =
413449
sortUserPlugins(rawUserPlugins)
414450

415-
// resolve worker
416-
const resolvedWorkerOptions: ResolveWorkerOptions = {
417-
format: config.worker?.format || 'iife',
418-
plugins: [],
419-
rollupOptions: config.worker?.rollupOptions || {}
420-
}
421-
422451
// run config hooks
423452
const userPlugins = [...prePlugins, ...normalPlugins, ...postPlugins]
424453
for (const p of userPlugins) {
@@ -577,8 +606,14 @@ export async function resolveConfig(
577606

578607
const BASE_URL = resolvedBase
579608

580-
const resolved: ResolvedConfig = {
581-
...config,
609+
// resolve worker
610+
const resolvedWorkerOptions: ResolveWorkerOptions = {
611+
format: workerConfig.worker?.format || 'iife',
612+
plugins: [],
613+
rollupOptions: workerConfig.worker?.rollupOptions || {}
614+
}
615+
616+
const resolvedConfig: ResolvedConfig = {
582617
configFile: configFile ? normalizePath(configFile) : undefined,
583618
configFileDependencies: configFileDependencies.map((name) =>
584619
normalizePath(path.resolve(name))
@@ -628,6 +663,7 @@ export async function resolveConfig(
628663
...config.experimental
629664
}
630665
}
666+
const resolved: ResolvedConfig = Object.assign(config, resolvedConfig)
631667

632668
if (middlewareMode === 'ssr') {
633669
logger.warn(
@@ -659,35 +695,34 @@ export async function resolveConfig(
659695
)
660696
}
661697

662-
// Some plugins that aren't intended to work in the bundling of workers (doing post-processing at build time for example).
663-
// And Plugins may also have cached that could be corrupted by being used in these extra rollup calls.
664-
// So we need to separate the worker plugin from the plugin that vite needs to run.
665-
const [workerPrePlugins, workerNormalPlugins, workerPostPlugins] =
666-
sortUserPlugins(config.worker?.plugins as Plugin[])
667-
const workerResolved: ResolvedConfig = {
668-
...resolved,
669-
isWorker: true,
670-
mainConfig: resolved
671-
}
672-
resolved.worker.plugins = await resolvePlugins(
673-
workerResolved,
674-
workerPrePlugins,
675-
workerNormalPlugins,
676-
workerPostPlugins
677-
)
678-
// call configResolved worker plugins hooks
679-
await Promise.all(
680-
resolved.worker.plugins.map((p) => p.configResolved?.(workerResolved))
681-
)
682698
;(resolved.plugins as Plugin[]) = await resolvePlugins(
683699
resolved,
684700
prePlugins,
685701
normalPlugins,
686702
postPlugins
687703
)
688704

705+
const workerResolved: ResolvedConfig = Object.assign(
706+
workerConfig,
707+
resolvedConfig,
708+
{
709+
isWorker: true,
710+
mainConfig: resolved
711+
}
712+
)
713+
resolved.worker.plugins = await resolvePlugins(
714+
workerResolved,
715+
workerPrePlugins,
716+
workerNormalPlugins,
717+
workerPostPlugins
718+
)
719+
689720
// call configResolved hooks
690-
await Promise.all(userPlugins.map((p) => p.configResolved?.(resolved)))
721+
await Promise.all(
722+
userPlugins
723+
.map((p) => p.configResolved?.(resolved))
724+
.concat(workerUserPlugins.map((p) => p.configResolved?.(workerResolved)))
725+
)
691726

692727
if (process.env.DEBUG) {
693728
debug(`using resolved config: %O`, {
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('../../vite.config')
1+
module.exports = require('../../vite.config-iife')

playground/worker/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"private": true,
44
"version": "0.0.0",
55
"scripts": {
6-
"dev": "vite",
7-
"build": "vite build",
8-
"preview": "vite preview",
6+
"dev": "vite --config ./vite.config-iife.js",
7+
"build": "vite --config ./vite.config-iife.js build",
8+
"preview": "vite --config ./vite.config-iife.js preview",
99
"dev:es": "vite --config ./vite.config-es.js dev",
1010
"build:es": "vite --config ./vite.config-es.js build",
1111
"preview:es": "vite --config ./vite.config-es.js preview",

playground/worker/vite.config.js playground/worker/vite.config-iife.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,29 @@ module.exports = vite.defineConfig({
55
base: '/iife/',
66
worker: {
77
format: 'iife',
8-
plugins: [vueJsx()],
8+
plugins: [
9+
vueJsx(),
10+
{
11+
name: 'config-test',
12+
config() {
13+
return {
14+
worker: {
15+
rollupOptions: {
16+
output: {
17+
entryFileNames: 'assets/worker_entry.[name].js'
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
],
925
rollupOptions: {
1026
output: {
1127
assetFileNames: 'assets/worker_asset.[name].[ext]',
1228
chunkFileNames: 'assets/worker_chunk.[name].js',
13-
entryFileNames: 'assets/worker_entry.[name].js'
29+
// should fix by config-test plugin
30+
entryFileNames: 'assets/worker_.[name].js'
1431
}
1532
}
1633
},

0 commit comments

Comments
 (0)