Skip to content

Commit

Permalink
feat: convert optimizeDeps.esbuildOptions to `optimizeDeps.rollupOp…
Browse files Browse the repository at this point in the history
…tions`
  • Loading branch information
sapphi-red committed Oct 4, 2024
1 parent e4ec6bc commit 9d7d4f0
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 13 deletions.
116 changes: 116 additions & 0 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ export function resolveDevEnvironmentOptions(
preserverSymlinks: boolean,
environmentName: string | undefined,
consumer: 'client' | 'server' | undefined,
logger: Logger,
// Backward compatibility
skipSsrTransform?: boolean,
): ResolvedDevEnvironmentOptions {
Expand All @@ -614,6 +615,7 @@ export function resolveDevEnvironmentOptions(
dev?.optimizeDeps,
preserverSymlinks,
consumer,
logger,
),
createEnvironment:
dev?.createEnvironment ??
Expand Down Expand Up @@ -659,6 +661,7 @@ function resolveEnvironmentOptions(
resolve.preserveSymlinks,
environmentName,
consumer,
logger,
skipSsrTransform,
),
build: resolveBuildEnvironmentOptions(
Expand Down Expand Up @@ -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<RollupOptions['resolve'], undefined>,
>(
key: T,
value: Exclude<RollupOptions['resolve'], undefined>[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 ?? [],
Expand Down Expand Up @@ -1032,6 +1147,7 @@ export async function resolveConfig(
// default environment options
undefined,
undefined,
logger,
)

const resolvedBuildOptions = resolveBuildEnvironmentOptions(
Expand Down
23 changes: 19 additions & 4 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -770,17 +783,18 @@ 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'],
aliasFields: [['browser']],
extensions: ['.js', '.css'],
conditionNames: ['browser'],
},
...rollupOptions,
// TODO: remove this and enable rolldown's CSS support later
moduleTypes: {
'.css': 'js',
Expand All @@ -792,14 +806,14 @@ async function prepareRolldownOptimizerRun(
throw new Error('The build was canceled')
}
const result = await bundle.write({
...rollupOptions.output,
format: 'esm',
sourcemap: true,
dir: processingCacheDir,
banner:
platform === 'node'
? `import { createRequire } from 'module';const require = createRequire(import.meta.url);`
: undefined,
...rollupOptions.output,
})
await bundle.close()
return result
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 1 addition & 9 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,6 @@ async function prepareRolldownScanner(
): Promise<{ build: () => Promise<void> } | 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 ?? {}

Expand All @@ -270,10 +262,10 @@ async function prepareRolldownScanner(

async function build() {
await scan({
...rollupOptions,
input: entries,
logLevel: 'silent',
plugins,
...rollupOptions,
})
}

Expand Down

0 comments on commit 9d7d4f0

Please sign in to comment.