Skip to content

Commit

Permalink
feat: worker config call config hook (#9212)
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed Jul 19, 2022
1 parent 88baa53 commit 3e510ab
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 36 deletions.
95 changes: 65 additions & 30 deletions packages/vite/src/node/config.ts
Expand Up @@ -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[]
Expand All @@ -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) {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -628,6 +663,7 @@ export async function resolveConfig(
...config.experimental
}
}
const resolved: ResolvedConfig = Object.assign(config, resolvedConfig)

if (middlewareMode === 'ssr') {
logger.warn(
Expand Down Expand Up @@ -659,35 +695,34 @@ 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,
normalPlugins,
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`, {
Expand Down
2 changes: 1 addition & 1 deletion playground/worker/__tests__/iife/vite.config.js
@@ -1 +1 @@
module.exports = require('../../vite.config')
module.exports = require('../../vite.config-iife')
6 changes: 3 additions & 3 deletions playground/worker/package.json
Expand Up @@ -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",
Expand Down
Expand Up @@ -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'
}
}
},
Expand Down

0 comments on commit 3e510ab

Please sign in to comment.