Skip to content

Commit 10aa992

Browse files
authored
feat: add legacy inconsistent cjs interop (#464)
1 parent e373d76 commit 10aa992

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

packages/vite/src/node/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,17 @@ export interface LegacyOptions {
566566
* that security weakness.**
567567
*/
568568
skipWebSocketTokenCheck?: boolean
569+
/**
570+
* Opt-in to the pre-Vite 8 CJS interop behavior, which was inconsistent.
571+
*
572+
* In pre-Vite 8 versions, Vite had inconsistent CJS interop behavior. This was due to
573+
* the different behavior of esbuild and the Rollup commonjs plugin.
574+
* Vite 8+ uses Rolldown for both the dependency optimization in dev and the production build,
575+
* which aligns the behavior to esbuild.
576+
*
577+
* See the Vite 8 migration guide for more details.
578+
*/
579+
inconsistentCjsInterop?: boolean
569580
}
570581

571582
export interface ResolvedWorkerOptions {

packages/vite/src/node/plugins/importAnalysis.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
441441
imports.length,
442442
)
443443

444-
let _isNodeModeResult: boolean | undefined
444+
let _isNodeModeResult = config.legacy?.inconsistentCjsInterop
445+
? false
446+
: undefined
445447
const isNodeMode = () => {
446448
_isNodeModeResult ??= isFilePathESM(importer, config.packageCache)
447449
return _isNodeModeResult

packages/vite/src/node/plugins/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export async function resolvePlugins(
8585
asSrc: true,
8686
optimizeDeps: true,
8787
externalize: true,
88+
legacyInconsistentCjsInterop: config.legacy?.inconsistentCjsInterop,
8889
},
8990
isWorker
9091
? { ...config, consumer: 'client', optimizeDepsPluginNames: [] }

packages/vite/src/node/plugins/resolve.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ interface ResolvePluginOptions {
167167
* @internal
168168
*/
169169
idOnly?: boolean
170+
171+
/**
172+
* Enable when `legacy.inconsistentCjsInterop` is true. See that option for more details.
173+
*/
174+
legacyInconsistentCjsInterop?: boolean
170175
}
171176

172177
export interface InternalResolveOptions
@@ -283,6 +288,7 @@ export function oxcResolvePlugin(
283288
external: options.external,
284289
noExternal: noExternal,
285290
dedupe: options.dedupe,
291+
legacyInconsistentCjsInterop: options.legacyInconsistentCjsInterop,
286292
finalizeBareSpecifier: !depsOptimizerEnabled
287293
? undefined
288294
: (resolvedId, rawId, importer) => {
@@ -553,6 +559,7 @@ export function resolvePlugin(
553559
id: ensureVersionQuery(res, id, options, depsOptimizer),
554560
packageJsonPath: findNearestPackagePath(
555561
res,
562+
options.legacyInconsistentCjsInterop,
556563
options.packageCache,
557564
isBuild,
558565
),
@@ -573,6 +580,7 @@ export function resolvePlugin(
573580
id: ensureVersionQuery(res, id, options, depsOptimizer),
574581
packageJsonPath: findNearestPackagePath(
575582
res,
583+
options.legacyInconsistentCjsInterop,
576584
options.packageCache,
577585
isBuild,
578586
),
@@ -629,7 +637,9 @@ export function resolvePlugin(
629637
return {
630638
id: res,
631639
moduleSideEffects: resPkg.hasSideEffects(res),
632-
packageJsonPath: path.join(resPkg.dir, 'package.json'),
640+
packageJsonPath: options.legacyInconsistentCjsInterop
641+
? undefined
642+
: path.join(resPkg.dir, 'package.json'),
633643
}
634644
}
635645
}
@@ -653,6 +663,7 @@ export function resolvePlugin(
653663
id: ensureVersionQuery(res, id, options, depsOptimizer),
654664
packageJsonPath: findNearestPackagePath(
655665
res,
666+
options.legacyInconsistentCjsInterop,
656667
options.packageCache,
657668
isBuild,
658669
),
@@ -670,6 +681,7 @@ export function resolvePlugin(
670681
id: ensureVersionQuery(res, id, options, depsOptimizer),
671682
packageJsonPath: findNearestPackagePath(
672683
res,
684+
options.legacyInconsistentCjsInterop,
673685
options.packageCache,
674686
isBuild,
675687
),
@@ -1169,6 +1181,7 @@ export function tryNodeResolve(
11691181
moduleSideEffects: pkg.hasSideEffects(resolved),
11701182
packageJsonPath: findNearestPackagePath(
11711183
resolved,
1184+
options.legacyInconsistentCjsInterop,
11721185
options.packageCache,
11731186
isBuild,
11741187
),
@@ -1518,7 +1531,9 @@ function tryResolveBrowserMapping(
15181531
result = {
15191532
id: res,
15201533
moduleSideEffects: resPkg.hasSideEffects(res),
1521-
packageJsonPath: path.join(resPkg.dir, 'package.json'),
1534+
packageJsonPath: options.legacyInconsistentCjsInterop
1535+
? undefined
1536+
: path.join(resPkg.dir, 'package.json'),
15221537
}
15231538
}
15241539
}
@@ -1654,10 +1669,11 @@ function isDirectory(path: string): boolean {
16541669

16551670
function findNearestPackagePath(
16561671
file: string,
1672+
legacyInconsistentCjsInterop: boolean | undefined,
16571673
packageCache: PackageCache | undefined,
16581674
isBuild: boolean,
16591675
) {
1660-
if (!isBuild) return
1676+
if (!isBuild || legacyInconsistentCjsInterop) return
16611677
const pkgData = findNearestPackageData(file, packageCache)
16621678
return pkgData ? path.join(pkgData.dir, 'package.json') : null
16631679
}

0 commit comments

Comments
 (0)