diff --git a/package.json b/package.json index d6f2e7fe7aefc9..24907bca40ea0d 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "test": "run-s test-unit test-serve test-build", "test-serve": "vitest run -c vitest.config.e2e.ts", "test-build": "VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts", - "test-build-without-plugin-commonjs": "VITE_TEST_WITHOUT_PLUGIN_COMMONJS=1 pnpm test-build", + "test-build-optimized-deps": "VITE_TEST_BUILD_OPTIMIZED_DEPS=1 pnpm test-build", "test-unit": "vitest run", "test-docs": "pnpm run docs-build", "debug-serve": "VITE_DEBUG_SERVE=1 vitest run -c vitest.config.e2e.ts", diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index f7285332671b91..bbf6bba5325069 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -427,10 +427,9 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{ post: Plugin[] }> { const options = config.build - const { commonjsOptions } = options + // We don't use optimized dependencies while bundling workers const usePluginCommonjs = - !Array.isArray(commonjsOptions?.include) || - commonjsOptions?.include.length !== 0 + !isDepsOptimizerEnabled(config, !!config.build.ssr) || config.isWorker const rollupOptionsPlugins = options.rollupOptions.plugins return { pre: [ diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 1933a5484b7565..5f4ef480f71b14 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -457,13 +457,11 @@ export async function resolveConfig( const userPlugins = [...prePlugins, ...normalPlugins, ...postPlugins] config = await runConfigHook(config, userPlugins, configEnv) - if (process.env.VITE_TEST_WITHOUT_PLUGIN_COMMONJS) { + if (process.env.VITE_TEST_BUILD_OPTIMIZED_DEPS) { config = mergeConfig(config, { optimizeDeps: { disabled: false }, ssr: { optimizeDeps: { disabled: false } }, }) - config.build ??= {} - config.build.commonjsOptions = { include: [] } } // Define logger diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index a1d88a46e90d8c..68a3e96adc0bea 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -57,7 +57,6 @@ export interface DepsOptimizer { isOptimizedDepUrl: (url: string) => boolean getOptimizedDepId: (depInfo: OptimizedDepInfo) => string delayDepsOptimizerUntil: (id: string, done: () => Promise) => void - registerWorkersSource: (id: string) => void resetRegisteredIds: () => void ensureFirstRun: () => void diff --git a/packages/vite/src/node/optimizer/optimizer.ts b/packages/vite/src/node/optimizer/optimizer.ts index 1ba95b5ffca274..eef07218d83c7b 100644 --- a/packages/vite/src/node/optimizer/optimizer.ts +++ b/packages/vite/src/node/optimizer/optimizer.ts @@ -41,11 +41,8 @@ export function getDepsOptimizer( config: ResolvedConfig, ssr?: boolean, ): DepsOptimizer | undefined { - // Workers compilation shares the DepsOptimizer from the main build const isDevSsr = ssr && config.command !== 'build' - return (isDevSsr ? devSsrDepsOptimizerMap : depsOptimizerMap).get( - config.mainConfig || config, - ) + return (isDevSsr ? devSsrDepsOptimizerMap : depsOptimizerMap).get(config) } export async function initDepsOptimizer( @@ -114,7 +111,6 @@ async function createDepsOptimizer( isOptimizedDepUrl: createIsOptimizedDepUrl(config), getOptimizedDepId: (depInfo: OptimizedDepInfo) => isBuild ? depInfo.file : `${depInfo.file}?v=${depInfo.browserHash}`, - registerWorkersSource, delayDepsOptimizerUntil, resetRegisteredIds, ensureFirstRun, @@ -702,9 +698,6 @@ async function createDepsOptimizer( crawlEndFinder = setupOnCrawlEnd(onCrawlEnd) } - function registerWorkersSource(id: string) { - crawlEndFinder?.registerWorkersSource(id) - } function delayDepsOptimizerUntil(id: string, done: () => Promise) { if (crawlEndFinder && !depsOptimizer.isOptimizedDepFile(id)) { crawlEndFinder.delayDepsOptimizerUntil(id, done) @@ -719,7 +712,6 @@ const callCrawlEndIfIdleAfterMs = 50 interface CrawlEndFinder { ensureFirstRun: () => void - registerWorkersSource: (id: string) => void delayDepsOptimizerUntil: (id: string, done: () => Promise) => void cancel: () => void } @@ -727,7 +719,6 @@ interface CrawlEndFinder { function setupOnCrawlEnd(onCrawlEnd: () => void): CrawlEndFinder { const registeredIds = new Set() const seenIds = new Set() - const workersSources = new Set() let timeoutHandle: NodeJS.Timeout | undefined let cancelled = false @@ -758,25 +749,13 @@ function setupOnCrawlEnd(onCrawlEnd: () => void): CrawlEndFinder { firstRunEnsured = true } - function registerWorkersSource(id: string): void { - workersSources.add(id) - - // Avoid waiting for this id, as it may be blocked by the rollup - // bundling process of the worker that also depends on the optimizer - registeredIds.delete(id) - - checkIfCrawlEndAfterTimeout() - } - function delayDepsOptimizerUntil(id: string, done: () => Promise): void { if (!seenIds.has(id)) { seenIds.add(id) - if (!workersSources.has(id)) { - registeredIds.add(id) - done() - .catch(() => {}) - .finally(() => markIdAsDone(id)) - } + registeredIds.add(id) + done() + .catch(() => {}) + .finally(() => markIdAsDone(id)) } } function markIdAsDone(id: string): void { @@ -800,7 +779,6 @@ function setupOnCrawlEnd(onCrawlEnd: () => void): CrawlEndFinder { return { ensureFirstRun, - registerWorkersSource, delayDepsOptimizerUntil, cancel, } @@ -826,7 +804,6 @@ async function createDevSsrDepsOptimizer( // noop, there is no scanning during dev SSR // the optimizer blocks the server start run: () => {}, - registerWorkersSource: (id: string) => {}, delayDepsOptimizerUntil: (id: string, done: () => Promise) => {}, resetRegisteredIds: () => {}, ensureFirstRun: () => {}, diff --git a/packages/vite/src/node/plugins/index.ts b/packages/vite/src/node/plugins/index.ts index fc2a80d51fbb61..29a39f71df4c0a 100644 --- a/packages/vite/src/node/plugins/index.ts +++ b/packages/vite/src/node/plugins/index.ts @@ -38,16 +38,25 @@ export async function resolvePlugins( ? await (await import('../build')).resolveBuildPlugins(config) : { pre: [], post: [] } const { modulePreload } = config.build - + let depsOptimizerPlugin + if (isBuild) { + if ( + isDepsOptimizerEnabled(config, !!config.build.ssr) && + !config.isWorker + ) { + depsOptimizerPlugin = optimizedDepsBuildPlugin(config) + } + } else { + // dev + if ( + isDepsOptimizerEnabled(config, false) || + isDepsOptimizerEnabled(config, true) + ) { + depsOptimizerPlugin = optimizedDepsPlugin(config) + } + } return [ - ...(isDepsOptimizerEnabled(config, false) || - isDepsOptimizerEnabled(config, true) - ? [ - isBuild - ? optimizedDepsBuildPlugin(config) - : optimizedDepsPlugin(config), - ] - : []), + ...(depsOptimizerPlugin ? [depsOptimizerPlugin] : []), isWatch ? ensureWatchPlugin() : null, isBuild ? metadataPlugin() : null, watchPackageDataPlugin(config.packageCache), diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 92c7f19563e4b3..4a84b9287453da 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -11,7 +11,6 @@ import { onRollupWarning, toOutputFilePathInJS, } from '../build' -import { getDepsOptimizer } from '../optimizer' import { fileToUrl } from './asset' interface WorkerCache { @@ -247,7 +246,6 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { }, async transform(raw, id, options) { - const ssr = options?.ssr === true const query = parseRequest(id) if (query && query[WORKER_FILE_ID] != null) { // if import worker by worker constructor will have query.type @@ -294,7 +292,6 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { const workerOptions = workerType === 'classic' ? '' : ',{type: "module"}' if (isBuild) { - getDepsOptimizer(config, ssr)?.registerWorkersSource(id) if (query.inline != null) { const chunk = await bundleWorkerEntry(config, id, query) const encodedJs = `const encodedJs = "${Buffer.from( diff --git a/packages/vite/src/node/plugins/workerImportMetaUrl.ts b/packages/vite/src/node/plugins/workerImportMetaUrl.ts index 5e186af64f8648..360664c323b78d 100644 --- a/packages/vite/src/node/plugins/workerImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/workerImportMetaUrl.ts @@ -12,7 +12,6 @@ import { slash, transformStableResult, } from '../utils' -import { getDepsOptimizer } from '../optimizer' import type { ResolveFn } from '..' import type { WorkerType } from './worker' import { WORKER_FILE_ID, workerFileToUrl } from './worker' @@ -115,7 +114,6 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin { name: 'vite:worker-import-meta-url', async transform(code, id, options) { - const ssr = options?.ssr === true if ( !options?.ssr && (code.includes('new Worker') || code.includes('new SharedWorker')) && @@ -171,7 +169,6 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin { let builtUrl: string if (isBuild) { - getDepsOptimizer(config, ssr)?.registerWorkersSource(id) builtUrl = await workerFileToUrl(config, file, query) } else { builtUrl = await fileToUrl(cleanUrl(file), config, this)