diff --git a/docs/config/index.md b/docs/config/index.md index eb37e4228e87..5a8e131ffdc3 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -68,6 +68,22 @@ Externalize means that Vite will bypass the package to native Node. Externalized Vite will process inlined modules. This could be helpful to handle packages that ship `.js` in ESM format (that Node can't handle). +#### deps.fallbackCJS + +- **Type** `boolean` +- **Default:** `false` + +When a dependency is a valid ESM package, try to guess the cjs version based on the path. + +This might potentially cause some misalignment if a package has different logic in ESM and CJS mode. + +#### deps.interopDefault + +- **Type:** `boolean` +- **Default:** `true` + +Interpret CJS module's default as named exports. + ### globals - **Type:** `boolean` @@ -223,10 +239,6 @@ Maximum number of threads Minimum number of threads -### interopDefault - -- **Type:** `boolean` - ### testTimeout - **Type:** `number` diff --git a/packages/vite-node/src/externalize.ts b/packages/vite-node/src/externalize.ts index eb39b678658e..e4cf233e07dc 100644 --- a/packages/vite-node/src/externalize.ts +++ b/packages/vite-node/src/externalize.ts @@ -72,7 +72,8 @@ async function _shouldExternalize( return id const isNodeModule = id.includes('/node_modules/') - id = isNodeModule ? guessCJSversion(id) || id : id + const guessCJS = isNodeModule && options?.fallbackCJS + id = guessCJS ? guessCJSversion(id) || id : id if (matchExternalizePattern(id, defaultInline)) return false diff --git a/packages/vite-node/src/types.ts b/packages/vite-node/src/types.ts index c7421a33e4bd..9bfc1e47c83e 100644 --- a/packages/vite-node/src/types.ts +++ b/packages/vite-node/src/types.ts @@ -3,7 +3,7 @@ export interface DepsHandlingOptions { inline?: (string | RegExp)[] /** * Try to guess the CJS version of a package when it's invalid ESM - * @default true + * @default false */ fallbackCJS?: boolean } diff --git a/packages/vitest/src/types/config.ts b/packages/vitest/src/types/config.ts index 3fac40aa6062..7a8aed92b205 100644 --- a/packages/vitest/src/types/config.ts +++ b/packages/vitest/src/types/config.ts @@ -73,7 +73,7 @@ export interface InlineConfig { * This will significantly improve the performance in huge repo, but might potentially * cause some misalignment if a package have different logic in ESM and CJS mode. * - * @default true + * @default false */ fallbackCJS?: boolean }