Repository navigation
fix!: worker.plugins is a function #14685
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
Changes from all commits
f6a807f
61fc900
b6cf182
2fc829d
4f1f11f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -259,9 +259,11 @@ export interface UserConfig { | |
| */ | ||
| format?: 'es' | 'iife' | ||
| /** | ||
| * Vite plugins that apply to worker bundle | ||
| * Vite plugins that apply to worker bundle. The plugins retured by this function | ||
| * should be new instances every time it is called, because they are used for each | ||
| * rollup worker bundling process. | ||
| */ | ||
| plugins?: PluginOption[] | ||
| plugins?: () => PluginOption[] | ||
| /** | ||
| * Rollup options to build worker bundle | ||
| */ | ||
|
|
@@ -316,9 +318,9 @@ export interface LegacyOptions { | |
| */ | ||
| } | ||
|
|
||
| export interface ResolvedWorkerOptions extends PluginHookUtils { | ||
| export interface ResolvedWorkerOptions { | ||
| format: 'es' | 'iife' | ||
| plugins: Plugin[] | ||
| plugins: () => Promise<Plugin[]> | ||
| rollupOptions: RollupOptions | ||
| } | ||
|
|
||
|
|
@@ -440,12 +442,6 @@ export async function resolveConfig( | |
| return p.apply === command | ||
| } | ||
| } | ||
| // 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(filterPlugin) | ||
|
|
||
| // resolve plugins | ||
| const rawUserPlugins = ( | ||
|
|
@@ -659,27 +655,74 @@ export async function resolveConfig( | |
|
|
||
| const BASE_URL = resolvedBase | ||
|
|
||
| // resolve worker | ||
| let workerConfig = mergeConfig({}, config) | ||
| const [workerPrePlugins, workerNormalPlugins, workerPostPlugins] = | ||
| sortUserPlugins(rawWorkerUserPlugins) | ||
| let resolved: ResolvedConfig | ||
|
|
||
| let createUserWorkerPlugins = config.worker?.plugins | ||
| if (Array.isArray(createUserWorkerPlugins)) { | ||
| // @ts-expect-error backward compatibility | ||
| createUserWorkerPlugins = () => config.worker?.plugins | ||
|
|
||
| logger.warn( | ||
| colors.yellow( | ||
| `worker.plugins is now a function that returns an array of plugins. ` + | ||
| `Please update your Vite config accordingly.\n`, | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| const createWorkerPlugins = async function () { | ||
| // 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(createUserWorkerPlugins?.() || [])) as Plugin[] | ||
| ).filter(filterPlugin) | ||
|
|
||
| // resolve worker | ||
| let workerConfig = mergeConfig({}, config) | ||
| const [workerPrePlugins, workerNormalPlugins, workerPostPlugins] = | ||
| sortUserPlugins(rawWorkerUserPlugins) | ||
|
|
||
| // run config hooks | ||
| const workerUserPlugins = [ | ||
| ...workerPrePlugins, | ||
| ...workerNormalPlugins, | ||
| ...workerPostPlugins, | ||
| ] | ||
| workerConfig = await runConfigHook( | ||
| workerConfig, | ||
| workerUserPlugins, | ||
| configEnv, | ||
| ) | ||
|
|
||
| const workerResolved: ResolvedConfig = { | ||
| ...workerConfig, | ||
| ...resolved, | ||
| isWorker: true, | ||
| mainConfig: resolved, | ||
| } | ||
| const resolvedWorkerPlugins = await resolvePlugins( | ||
| workerResolved, | ||
| workerPrePlugins, | ||
| workerNormalPlugins, | ||
| workerPostPlugins, | ||
| ) | ||
|
|
||
| // run configResolved hooks | ||
| createPluginHookUtils(resolvedWorkerPlugins) | ||
| .getSortedPluginHooks('configResolved') | ||
| .map((hook) => hook(workerResolved)) | ||
|
|
||
| return resolvedWorkerPlugins | ||
| } | ||
|
|
||
| // run config hooks | ||
| const workerUserPlugins = [ | ||
| ...workerPrePlugins, | ||
| ...workerNormalPlugins, | ||
| ...workerPostPlugins, | ||
| ] | ||
| workerConfig = await runConfigHook(workerConfig, workerUserPlugins, configEnv) | ||
| const resolvedWorkerOptions: ResolvedWorkerOptions = { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can move the workers resolve code as a function like
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about that but it is using internal functions like |
||
| format: workerConfig.worker?.format || 'iife', | ||
| plugins: [], | ||
| rollupOptions: workerConfig.worker?.rollupOptions || {}, | ||
| getSortedPlugins: undefined!, | ||
| getSortedPluginHooks: undefined!, | ||
| format: config.worker?.format || 'iife', | ||
| plugins: createWorkerPlugins, | ||
| rollupOptions: config.worker?.rollupOptions || {}, | ||
| } | ||
|
|
||
| const resolvedConfig: ResolvedConfig = { | ||
| resolved = { | ||
| configFile: configFile ? normalizePath(configFile) : undefined, | ||
| configFileDependencies: configFileDependencies.map((name) => | ||
| normalizePath(path.resolve(name)), | ||
|
|
@@ -741,11 +784,10 @@ export async function resolveConfig( | |
| getSortedPlugins: undefined!, | ||
| getSortedPluginHooks: undefined!, | ||
| } | ||
| const resolved: ResolvedConfig = { | ||
| resolved = { | ||
| ...config, | ||
| ...resolvedConfig, | ||
| ...resolved, | ||
| } | ||
|
|
||
| ;(resolved.plugins as Plugin[]) = await resolvePlugins( | ||
| resolved, | ||
| prePlugins, | ||
|
|
@@ -754,32 +796,12 @@ export async function resolveConfig( | |
| ) | ||
| Object.assign(resolved, createPluginHookUtils(resolved.plugins)) | ||
|
|
||
| const workerResolved: ResolvedConfig = { | ||
| ...workerConfig, | ||
| ...resolvedConfig, | ||
| isWorker: true, | ||
| mainConfig: resolved, | ||
| } | ||
| resolvedConfig.worker.plugins = await resolvePlugins( | ||
| workerResolved, | ||
| workerPrePlugins, | ||
| workerNormalPlugins, | ||
| workerPostPlugins, | ||
| ) | ||
| Object.assign( | ||
| resolvedConfig.worker, | ||
| createPluginHookUtils(resolvedConfig.worker.plugins), | ||
| ) | ||
|
|
||
| // call configResolved hooks | ||
| await Promise.all([ | ||
| ...resolved | ||
| await Promise.all( | ||
| resolved | ||
| .getSortedPluginHooks('configResolved') | ||
| .map((hook) => hook(resolved)), | ||
| ...resolvedConfig.worker | ||
| .getSortedPluginHooks('configResolved') | ||
| .map((hook) => hook(workerResolved)), | ||
| ]) | ||
| ) | ||
|
|
||
| // validate config | ||
|
|
||
|
|
@@ -804,7 +826,7 @@ export async function resolveConfig( | |
| plugins: resolved.plugins.map((p) => p.name), | ||
| worker: { | ||
| ...resolved.worker, | ||
| plugins: resolved.worker.plugins.map((p) => p.name), | ||
| plugins: `() => plugins`, | ||
| }, | ||
| }) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,30 +41,7 @@ function saveEmitWorkerAsset( | |
| workerMap.assets.set(fileName, asset) | ||
| } | ||
|
|
||
| // Ensure that only one rollup build is called at the same time to avoid | ||
| // leaking state in plugins between worker builds. | ||
| // TODO: Review if we can parallelize the bundling of workers. | ||
| const workerConfigSemaphore = new WeakMap< | ||
| ResolvedConfig, | ||
| Promise<OutputChunk> | ||
| >() | ||
| export async function bundleWorkerEntry( | ||
| config: ResolvedConfig, | ||
| id: string, | ||
| query: Record<string, string> | null, | ||
| ): Promise<OutputChunk> { | ||
| const processing = workerConfigSemaphore.get(config) | ||
| if (processing) { | ||
| await processing | ||
| return bundleWorkerEntry(config, id, query) | ||
| } | ||
| const promise = serialBundleWorkerEntry(config, id, query) | ||
| workerConfigSemaphore.set(config, promise) | ||
| promise.then(() => workerConfigSemaphore.delete(config)) | ||
| return promise | ||
| } | ||
|
|
||
| async function serialBundleWorkerEntry( | ||
| async function bundleWorkerEntry( | ||
| config: ResolvedConfig, | ||
| id: string, | ||
| query: Record<string, string> | null, | ||
|
|
@@ -75,7 +52,7 @@ async function serialBundleWorkerEntry( | |
| const bundle = await rollup({ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's useful if there's a debug log here that shows the plugin names like we had here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to see how to do this only once because it will be too noisy if not having the logs everytime for projects that have a lot of workers. I'd say we merge it so it is part of the release and do this in a future PR. |
||
| ...rollupOptions, | ||
| input: cleanUrl(id), | ||
| plugins, | ||
| plugins: await plugins(), | ||
| onwarn(warning, warn) { | ||
| onRollupWarning(warning, warn, config) | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| self.postMessage({ | ||
| type: 'deeplyNestedSecondWorker', | ||
| data: [ | ||
| 'Hello from second level nested worker', | ||
| import.meta.env.BASE_URL, | ||
| self.location.url, | ||
| import.meta.url, | ||
| ].join(' '), | ||
| }) | ||
|
|
||
| const deeplyNestedThirdWorker = new Worker( | ||
| new URL('deeply-nested-third-worker.js', import.meta.url), | ||
| { type: 'module' }, | ||
| ) | ||
| deeplyNestedThirdWorker.addEventListener('message', (ev) => { | ||
| self.postMessage(ev.data) | ||
| }) | ||
|
|
||
| console.log('deeply-nested-second-worker.js') |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| self.postMessage({ | ||
| type: 'deeplyNestedThirdWorker', | ||
| data: [ | ||
| 'Hello from third level nested worker', | ||
| import.meta.env.BASE_URL, | ||
| self.location.url, | ||
| import.meta.url, | ||
| ].join(' '), | ||
| }) | ||
|
|
||
| console.log('deeply-nested-third-worker.js') |
Uh oh!
There was an error while loading. Please reload this page.