Skip to content

Commit 23711ec

Browse files
authored
fix(optimizer): sync optimizeDeps.rollupOptions and optimizeDeps.rolldownOptions (#482)
1 parent 1c7d85a commit 23711ec

File tree

2 files changed

+70
-66
lines changed

2 files changed

+70
-66
lines changed

packages/vite/src/node/__tests__/config.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,19 @@ describe('resolveConfig', () => {
754754

755755
await resolveConfig({ root: './inc?ud#s*', customLogger: logger }, 'build')
756756
})
757+
758+
test('syncs `build.rollupOptions` and `build.rolldownOptions`', async () => {
759+
const resolved = await resolveConfig({}, 'build')
760+
expect(resolved.build!.rollupOptions).toStrictEqual(
761+
resolved.build!.rolldownOptions,
762+
)
763+
expect(resolved.worker!.rollupOptions).toStrictEqual(
764+
resolved.worker!.rolldownOptions,
765+
)
766+
expect(resolved.optimizeDeps!.rollupOptions).toStrictEqual(
767+
resolved.optimizeDeps!.rolldownOptions,
768+
)
769+
})
757770
})
758771

759772
test('config compat 1', async () => {

packages/vite/src/node/config.ts

Lines changed: 57 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,10 +1094,23 @@ function resolveDepOptimizationOptions(
10941094
consumer: 'client' | 'server' | undefined,
10951095
logger: Logger,
10961096
): DepOptimizationOptions {
1097-
if (
1098-
optimizeDeps?.esbuildOptions &&
1099-
Object.keys(optimizeDeps.esbuildOptions).length > 0
1100-
) {
1097+
const merged = mergeWithDefaults(
1098+
{
1099+
...configDefaults.optimizeDeps,
1100+
disabled: undefined, // do not set here to avoid deprecation warning
1101+
noDiscovery: consumer !== 'client',
1102+
force: forceOptimizeDeps ?? configDefaults.optimizeDeps.force,
1103+
},
1104+
optimizeDeps ?? {},
1105+
)
1106+
setupRollupOptionCompat(merged)
1107+
1108+
const rollupOptions = merged.rollupOptions as Exclude<
1109+
DepOptimizationOptions['rollupOptions'],
1110+
undefined
1111+
>
1112+
1113+
if (merged.esbuildOptions && Object.keys(merged.esbuildOptions).length > 0) {
11011114
logger.warn(
11021115
colors.yellow(
11031116
`You or a plugin you are using have set \`optimizeDeps.esbuildOptions\` ` +
@@ -1107,87 +1120,76 @@ function resolveDepOptimizationOptions(
11071120
),
11081121
)
11091122

1110-
optimizeDeps.rollupOptions ??= {}
1111-
optimizeDeps.rollupOptions.resolve ??= {}
1112-
optimizeDeps.rollupOptions.output ??= {}
1123+
rollupOptions.resolve ??= {}
1124+
rollupOptions.output ??= {}
11131125

11141126
const setResolveOptions = <
11151127
T extends keyof Exclude<RolldownOptions['resolve'], undefined>,
11161128
>(
11171129
key: T,
11181130
value: Exclude<RolldownOptions['resolve'], undefined>[T],
11191131
) => {
1120-
if (
1121-
value !== undefined &&
1122-
optimizeDeps.rollupOptions!.resolve![key] === undefined
1123-
) {
1124-
optimizeDeps.rollupOptions!.resolve![key] = value
1132+
if (value !== undefined && rollupOptions.resolve![key] === undefined) {
1133+
rollupOptions.resolve![key] = value
11251134
}
11261135
}
11271136

11281137
if (
1129-
optimizeDeps.esbuildOptions.minify !== undefined &&
1130-
optimizeDeps.rollupOptions.output.minify === undefined
1138+
merged.esbuildOptions.minify !== undefined &&
1139+
rollupOptions.output.minify === undefined
11311140
) {
1132-
optimizeDeps.rollupOptions.output.minify =
1133-
optimizeDeps.esbuildOptions.minify
1141+
rollupOptions.output.minify = merged.esbuildOptions.minify
11341142
}
11351143
if (
1136-
optimizeDeps.esbuildOptions.treeShaking !== undefined &&
1137-
optimizeDeps.rollupOptions.treeshake === undefined
1144+
merged.esbuildOptions.treeShaking !== undefined &&
1145+
rollupOptions.treeshake === undefined
11381146
) {
1139-
optimizeDeps.rollupOptions.treeshake =
1140-
optimizeDeps.esbuildOptions.treeShaking
1147+
rollupOptions.treeshake = merged.esbuildOptions.treeShaking
11411148
}
11421149
if (
1143-
optimizeDeps.esbuildOptions.define !== undefined &&
1144-
optimizeDeps.rollupOptions.define === undefined
1150+
merged.esbuildOptions.define !== undefined &&
1151+
rollupOptions.define === undefined
11451152
) {
1146-
optimizeDeps.rollupOptions.define = optimizeDeps.esbuildOptions.define
1153+
rollupOptions.define = merged.esbuildOptions.define
11471154
}
1148-
if (optimizeDeps.esbuildOptions.loader !== undefined) {
1149-
const loader = optimizeDeps.esbuildOptions.loader
1150-
optimizeDeps.rollupOptions.moduleTypes ??= {}
1155+
if (merged.esbuildOptions.loader !== undefined) {
1156+
const loader = merged.esbuildOptions.loader
1157+
rollupOptions.moduleTypes ??= {}
11511158
for (const [key, value] of Object.entries(loader)) {
11521159
if (
1153-
optimizeDeps.rollupOptions.moduleTypes[key] === undefined &&
1160+
rollupOptions.moduleTypes[key] === undefined &&
11541161
value !== 'copy' &&
11551162
value !== 'css' &&
11561163
value !== 'default' &&
11571164
value !== 'file' &&
11581165
value !== 'local-css'
11591166
) {
1160-
optimizeDeps.rollupOptions.moduleTypes[key] = value
1167+
rollupOptions.moduleTypes[key] = value
11611168
}
11621169
}
11631170
}
11641171
if (
1165-
optimizeDeps.esbuildOptions.preserveSymlinks !== undefined &&
1166-
optimizeDeps.rollupOptions.resolve.symlinks === undefined
1172+
merged.esbuildOptions.preserveSymlinks !== undefined &&
1173+
rollupOptions.resolve.symlinks === undefined
11671174
) {
1168-
optimizeDeps.rollupOptions.resolve.symlinks =
1169-
!optimizeDeps.esbuildOptions.preserveSymlinks
1175+
rollupOptions.resolve.symlinks = !merged.esbuildOptions.preserveSymlinks
11701176
}
1171-
setResolveOptions(
1172-
'extensions',
1173-
optimizeDeps.esbuildOptions.resolveExtensions,
1174-
)
1175-
setResolveOptions('mainFields', optimizeDeps.esbuildOptions.mainFields)
1176-
setResolveOptions('conditionNames', optimizeDeps.esbuildOptions.conditions)
1177+
setResolveOptions('extensions', merged.esbuildOptions.resolveExtensions)
1178+
setResolveOptions('mainFields', merged.esbuildOptions.mainFields)
1179+
setResolveOptions('conditionNames', merged.esbuildOptions.conditions)
11771180
if (
1178-
optimizeDeps.esbuildOptions.keepNames !== undefined &&
1179-
optimizeDeps.rollupOptions.keepNames === undefined &&
1180-
optimizeDeps.rollupOptions.output.keepNames === undefined
1181+
merged.esbuildOptions.keepNames !== undefined &&
1182+
rollupOptions.keepNames === undefined &&
1183+
rollupOptions.output.keepNames === undefined
11811184
) {
1182-
optimizeDeps.rollupOptions.output.keepNames =
1183-
optimizeDeps.esbuildOptions.keepNames
1185+
rollupOptions.output.keepNames = merged.esbuildOptions.keepNames
11841186
}
11851187

11861188
if (
1187-
optimizeDeps.esbuildOptions.platform !== undefined &&
1188-
optimizeDeps.rollupOptions.platform === undefined
1189+
merged.esbuildOptions.platform !== undefined &&
1190+
rollupOptions.platform === undefined
11891191
) {
1190-
optimizeDeps.rollupOptions.platform = optimizeDeps.esbuildOptions.platform
1192+
rollupOptions.platform = merged.esbuildOptions.platform
11911193
}
11921194

11931195
// NOTE: the following options cannot be converted
@@ -1227,26 +1229,15 @@ function resolveDepOptimizationOptions(
12271229
// - absWorkingDir
12281230
}
12291231

1230-
return mergeWithDefaults(
1231-
{
1232-
...configDefaults.optimizeDeps,
1233-
disabled: undefined, // do not set here to avoid deprecation warning
1234-
noDiscovery: consumer !== 'client',
1235-
esbuildOptions: {
1236-
preserveSymlinks,
1237-
},
1238-
rollupOptions: {
1239-
resolve: {
1240-
symlinks: !preserveSymlinks,
1241-
},
1242-
output: {
1243-
topLevelVar: true,
1244-
},
1245-
},
1246-
force: forceOptimizeDeps ?? configDefaults.optimizeDeps.force,
1247-
},
1248-
optimizeDeps ?? {},
1249-
)
1232+
merged.esbuildOptions ??= {}
1233+
merged.esbuildOptions.preserveSymlinks ??= preserveSymlinks
1234+
1235+
rollupOptions.resolve ??= {}
1236+
rollupOptions.resolve.symlinks ??= !preserveSymlinks
1237+
rollupOptions.output ??= {}
1238+
rollupOptions.output.topLevelVar ??= true
1239+
1240+
return merged
12501241
}
12511242

12521243
async function setOptimizeDepsPluginNames(resolvedConfig: ResolvedConfig) {

0 commit comments

Comments
 (0)