From 9d7d4f05e74d0caa672217d82dc4f4a07acc5588 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Sat, 5 Oct 2024 02:29:23 +0900 Subject: [PATCH] feat: convert `optimizeDeps.esbuildOptions` to `optimizeDeps.rollupOptions` --- packages/vite/src/node/config.ts | 116 ++++++++++++++++++++++ packages/vite/src/node/optimizer/index.ts | 23 ++++- packages/vite/src/node/optimizer/scan.ts | 10 +- 3 files changed, 136 insertions(+), 13 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index bb85d898fa4383..4824929f675ca0 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -599,6 +599,7 @@ export function resolveDevEnvironmentOptions( preserverSymlinks: boolean, environmentName: string | undefined, consumer: 'client' | 'server' | undefined, + logger: Logger, // Backward compatibility skipSsrTransform?: boolean, ): ResolvedDevEnvironmentOptions { @@ -614,6 +615,7 @@ export function resolveDevEnvironmentOptions( dev?.optimizeDeps, preserverSymlinks, consumer, + logger, ), createEnvironment: dev?.createEnvironment ?? @@ -659,6 +661,7 @@ function resolveEnvironmentOptions( resolve.preserveSymlinks, environmentName, consumer, + logger, skipSsrTransform, ), build: resolveBuildEnvironmentOptions( @@ -807,8 +810,120 @@ function resolveDepOptimizationOptions( optimizeDeps: DepOptimizationOptions | undefined, preserveSymlinks: boolean, consumer: 'client' | 'server' | undefined, + logger: Logger, ): DepOptimizationOptions { optimizeDeps ??= {} + + if (optimizeDeps.esbuildOptions) { + logger.warn( + colors.yellow( + `You have set \`optimizeDeps.esbuildOptions\` but this options is now deprecated. ` + + `Vite now uses Rolldown to optimize the dependencies. ` + + `Please use \`optimizeDeps.rollupOptions\` instead.`, + ), + ) + + optimizeDeps.rollupOptions ??= {} + optimizeDeps.rollupOptions.resolve ??= {} + optimizeDeps.rollupOptions.output ??= {} + + const setResolveOptions = < + T extends keyof Exclude, + >( + key: T, + value: Exclude[T], + ) => { + if ( + value !== undefined && + optimizeDeps.rollupOptions!.resolve![key] === undefined + ) { + optimizeDeps.rollupOptions!.resolve![key] = value + } + } + + if ( + optimizeDeps.esbuildOptions.minify !== undefined && + optimizeDeps.rollupOptions.output.minify === undefined + ) { + optimizeDeps.rollupOptions.output.minify = + optimizeDeps.esbuildOptions.minify + } + if ( + optimizeDeps.esbuildOptions.treeShaking !== undefined && + optimizeDeps.rollupOptions.treeshake === undefined + ) { + optimizeDeps.rollupOptions.treeshake = + optimizeDeps.esbuildOptions.treeShaking + } + if ( + optimizeDeps.esbuildOptions.define !== undefined && + optimizeDeps.rollupOptions.define === undefined + ) { + optimizeDeps.rollupOptions.define = optimizeDeps.esbuildOptions.define + } + if (optimizeDeps.esbuildOptions.loader !== undefined) { + const loader = optimizeDeps.esbuildOptions.loader + optimizeDeps.rollupOptions.moduleTypes ??= {} + for (const [key, value] of Object.entries(loader)) { + if ( + optimizeDeps.rollupOptions.moduleTypes[key] === undefined && + value !== 'copy' && + value !== 'css' && + value !== 'default' && + value !== 'file' && + value !== 'local-css' + ) { + optimizeDeps.rollupOptions.moduleTypes[key] = value + } + } + } + setResolveOptions('symlinks', optimizeDeps.esbuildOptions.preserveSymlinks) + setResolveOptions( + 'extensions', + optimizeDeps.esbuildOptions.resolveExtensions, + ) + setResolveOptions('mainFields', optimizeDeps.esbuildOptions.mainFields) + setResolveOptions('conditionNames', optimizeDeps.esbuildOptions.conditions) + + // NOTE: the following options cannot be converted + // - legalComments + // - target, supported (Vite used to transpile down to `ESBUILD_MODULES_TARGET`) + // - ignoreAnnotations + // - jsx, jsxFactory, jsxFragment, jsxImportSource, jsxDev, jsxSideEffects + // - tsconfigRaw, tsconfig + + // NOTE: the following options can be converted but probably not worth it + // - sourceRoot + // - sourcesContent (`output.sourcemapExcludeSources` is not supported by rolldown) + // - drop + // - dropLabels + // - mangleProps, reserveProps, mangleQuoted, mangleCache + // - minifyWhitespace, minifyIdentifiers, minifySyntax + // - lineLimit + // - charset + // - pure (`treeshake.manualPureFunctions` is not supported by rolldown) + // - alias (it probably does not work the same with `resolve.alias`) + // - inject + // - banner, footer + // - plugins (not sure if it's possible and need to check if it's worth it before) + // - nodePaths + + // NOTE: the following options does not make sense to set / convert it + // - globalName (we only use ESM format) + // - keepNames (probably rolldown does not need it? not sure) + // - color + // - logLimit + // - logOverride + // - splitting + // - outbase + // - packages (this should not be set) + // - allowOverwrite + // - publicPath (`file` loader is not supported by rolldown) + // - entryNames, chunkNames, assetNames (Vite does not support changing these options) + // - stdin + // - absWorkingDir + } + return { include: optimizeDeps.include ?? [], exclude: optimizeDeps.exclude ?? [], @@ -1032,6 +1147,7 @@ export async function resolveConfig( // default environment options undefined, undefined, + logger, ) const resolvedBuildOptions = resolveBuildEnvironmentOptions( diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 99dad1a07e668a..8489a946f63395 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -7,7 +7,12 @@ import colors from 'picocolors' import type { BuildOptions as EsbuildBuildOptions } from 'esbuild' import { init, parse } from 'es-module-lexer' import glob from 'fast-glob' -import { type RollupOptions, type RollupOutput, rolldown } from 'rolldown' +import { + type RollupOptions, + type RollupOutput, + type OutputOptions as RollupOutputOptions, + rolldown, +} from 'rolldown' import type { ResolvedConfig } from '../config' import { asyncFlatten, @@ -103,7 +108,15 @@ export interface DepOptimizationConfig { | 'outExtension' | 'metafile' > - rollupOptions?: RollupOptions + rollupOptions?: Omit< + RollupOptions, + 'input' | 'logLevel' | 'platform' | 'output' + > & { + output?: Omit< + RollupOutputOptions, + 'format' | 'sourcemap' | 'dir' | 'banner' + > + } /** * List of file extensions that can be optimized. A corresponding esbuild * plugin must exist to handle the specific extension. @@ -770,9 +783,11 @@ async function prepareRolldownOptimizerRun( let canceled = false async function build() { const bundle = await rolldown({ + ...rollupOptions, input: flatIdDeps, logLevel: 'warn', plugins, + platform, resolve: { // TODO: set aliasFields, conditionNames depending on `platform` mainFields: ['module', 'main'], @@ -780,7 +795,6 @@ async function prepareRolldownOptimizerRun( extensions: ['.js', '.css'], conditionNames: ['browser'], }, - ...rollupOptions, // TODO: remove this and enable rolldown's CSS support later moduleTypes: { '.css': 'js', @@ -792,6 +806,7 @@ async function prepareRolldownOptimizerRun( throw new Error('The build was canceled') } const result = await bundle.write({ + ...rollupOptions.output, format: 'esm', sourcemap: true, dir: processingCacheDir, @@ -799,7 +814,6 @@ async function prepareRolldownOptimizerRun( platform === 'node' ? `import { createRequire } from 'module';const require = createRequire(import.meta.url);` : undefined, - ...rollupOptions.output, }) await bundle.close() return result @@ -1076,6 +1090,7 @@ export async function extractExportsData( const result = await build.generate({ ...rollupOptions.output, format: 'esm', + sourcemap: false, }) const [, exports, , hasModuleSyntax] = parse(result.output[0].code) return { diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index 2ad292daf5b1b4..606f14b31cba23 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -253,14 +253,6 @@ async function prepareRolldownScanner( ): Promise<{ build: () => Promise } | undefined> { if (scanContext?.cancelled) return - if (environment.config.dev.optimizeDeps.esbuildOptions) { - environment.logger.error( - `You've set "optimizeDeps.esbuildOptions" in your config. ` + - `This is deprecated and vite already use rollup to optimize packages. ` + - `Please use "optimizeDeps.rollupOptions" instead.`, - ) - } - const { plugins: pluginsFromConfig = [], ...rollupOptions } = environment.config.dev.optimizeDeps.rollupOptions ?? {} @@ -270,10 +262,10 @@ async function prepareRolldownScanner( async function build() { await scan({ + ...rollupOptions, input: entries, logLevel: 'silent', plugins, - ...rollupOptions, }) }