Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: worker config call config hook #9212

Merged
merged 6 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 65 additions & 30 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
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 || {}
}
Comment on lines +610 to +614
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: the raw userPlugin can not modify the config.worker.

user config config and worker resolved config use the same resolvedWorkerOptions what config by vite.config.js and modify by worker plugin.


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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('../../vite.config')
module.exports = require('../../vite.config-iife')
6 changes: 3 additions & 3 deletions playground/worker/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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