Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,17 @@ export interface LegacyOptions {
* that security weakness.**
*/
skipWebSocketTokenCheck?: boolean
/**
* Opt-in to the pre-Vite 8 CJS interop behavior, which was inconsistent.
*
* In pre-Vite 8 versions, Vite had inconsistent CJS interop behavior. This was due to
* the different behavior of esbuild and the Rollup commonjs plugin.
* Vite 8+ uses Rolldown for both the dependency optimization in dev and the production build,
* which aligns the behavior to esbuild.
*
* See the Vite 8 migration guide for more details.
*/
inconsistentCjsInterop?: boolean
}

export interface ResolvedWorkerOptions {
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
imports.length,
)

let _isNodeModeResult: boolean | undefined
let _isNodeModeResult = config.legacy?.inconsistentCjsInterop
? false
: undefined
const isNodeMode = () => {
_isNodeModeResult ??= isFilePathESM(importer, config.packageCache)
return _isNodeModeResult
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export async function resolvePlugins(
asSrc: true,
optimizeDeps: true,
externalize: true,
legacyInconsistentCjsInterop: config.legacy?.inconsistentCjsInterop,
},
isWorker
? { ...config, consumer: 'client', optimizeDepsPluginNames: [] }
Expand Down
22 changes: 19 additions & 3 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ interface ResolvePluginOptions {
* @internal
*/
idOnly?: boolean

/**
* Enable when `legacy.inconsistentCjsInterop` is true. See that option for more details.
*/
legacyInconsistentCjsInterop?: boolean
}

export interface InternalResolveOptions
Expand Down Expand Up @@ -283,6 +288,7 @@ export function oxcResolvePlugin(
external: options.external,
noExternal: noExternal,
dedupe: options.dedupe,
legacyInconsistentCjsInterop: options.legacyInconsistentCjsInterop,
finalizeBareSpecifier: !depsOptimizerEnabled
? undefined
: (resolvedId, rawId, importer) => {
Expand Down Expand Up @@ -553,6 +559,7 @@ export function resolvePlugin(
id: ensureVersionQuery(res, id, options, depsOptimizer),
packageJsonPath: findNearestPackagePath(
res,
options.legacyInconsistentCjsInterop,
options.packageCache,
isBuild,
),
Expand All @@ -573,6 +580,7 @@ export function resolvePlugin(
id: ensureVersionQuery(res, id, options, depsOptimizer),
packageJsonPath: findNearestPackagePath(
res,
options.legacyInconsistentCjsInterop,
options.packageCache,
isBuild,
),
Expand Down Expand Up @@ -629,7 +637,9 @@ export function resolvePlugin(
return {
id: res,
moduleSideEffects: resPkg.hasSideEffects(res),
packageJsonPath: path.join(resPkg.dir, 'package.json'),
packageJsonPath: options.legacyInconsistentCjsInterop
? undefined
: path.join(resPkg.dir, 'package.json'),
}
}
}
Expand All @@ -653,6 +663,7 @@ export function resolvePlugin(
id: ensureVersionQuery(res, id, options, depsOptimizer),
packageJsonPath: findNearestPackagePath(
res,
options.legacyInconsistentCjsInterop,
options.packageCache,
isBuild,
),
Expand All @@ -670,6 +681,7 @@ export function resolvePlugin(
id: ensureVersionQuery(res, id, options, depsOptimizer),
packageJsonPath: findNearestPackagePath(
res,
options.legacyInconsistentCjsInterop,
options.packageCache,
isBuild,
),
Expand Down Expand Up @@ -1169,6 +1181,7 @@ export function tryNodeResolve(
moduleSideEffects: pkg.hasSideEffects(resolved),
packageJsonPath: findNearestPackagePath(
resolved,
options.legacyInconsistentCjsInterop,
options.packageCache,
isBuild,
),
Expand Down Expand Up @@ -1518,7 +1531,9 @@ function tryResolveBrowserMapping(
result = {
id: res,
moduleSideEffects: resPkg.hasSideEffects(res),
packageJsonPath: path.join(resPkg.dir, 'package.json'),
packageJsonPath: options.legacyInconsistentCjsInterop
? undefined
: path.join(resPkg.dir, 'package.json'),
}
}
}
Expand Down Expand Up @@ -1654,10 +1669,11 @@ function isDirectory(path: string): boolean {

function findNearestPackagePath(
file: string,
legacyInconsistentCjsInterop: boolean | undefined,
packageCache: PackageCache | undefined,
isBuild: boolean,
) {
if (!isBuild) return
if (!isBuild || legacyInconsistentCjsInterop) return
const pkgData = findNearestPackageData(file, packageCache)
return pkgData ? path.join(pkgData.dir, 'package.json') : null
}
Loading