diff --git a/docs/config/index.md b/docs/config/index.md index 8338c8812fab..656122a0b799 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -82,11 +82,13 @@ Externalize means that Vite will bypass the package to native Node. Externalized #### deps.inline -- **Type:** `(string | RegExp)[]` +- **Type:** `(string | RegExp)[] | true` - **Default:** `[]` Vite will process inlined modules. This could be helpful to handle packages that ship `.js` in ESM format (that Node can't handle). +If `true`, every dependency will be inlined. All dependencies, specified in [`ssr.noExternal`](https://vitejs.dev/guide/ssr.html#ssr-externals) will be inlined by default. + #### deps.fallbackCJS - **Type** `boolean` diff --git a/packages/vite-node/src/cli.ts b/packages/vite-node/src/cli.ts index 47c47bddf7ff..ad3daeb585f8 100644 --- a/packages/vite-node/src/cli.ts +++ b/packages/vite-node/src/cli.ts @@ -90,15 +90,19 @@ async function run(files: string[], options: CliOptions = {}) { } function parseServerOptions(serverOptions: ViteNodeServerOptionsCLI): ViteNodeServerOptions { + const inlineOptions = serverOptions.deps?.inline === true ? true : toArray(serverOptions.deps?.inline) + return { ...serverOptions, deps: { ...serverOptions.deps, - inline: toArray(serverOptions.deps?.inline).map((dep) => { - return dep.startsWith('/') && dep.endsWith('/') - ? new RegExp(dep) - : dep - }), + inline: inlineOptions !== true + ? inlineOptions.map((dep) => { + return dep.startsWith('/') && dep.endsWith('/') + ? new RegExp(dep) + : dep + }) + : true, external: toArray(serverOptions.deps?.external).map((dep) => { return dep.startsWith('/') && dep.endsWith('/') ? new RegExp(dep) @@ -121,9 +125,11 @@ type ComputeViteNodeServerOptionsCLI> = { ? string | string[] : T[K] extends Optional<(string | RegExp)[]> ? string | string[] - : T[K] extends Optional> - ? ComputeViteNodeServerOptionsCLI - : T[K] + : T[K] extends Optional<(string | RegExp)[] | true> + ? string | string[] | true + : T[K] extends Optional> + ? ComputeViteNodeServerOptionsCLI + : T[K] } export type ViteNodeServerOptionsCLI = ComputeViteNodeServerOptionsCLI diff --git a/packages/vite-node/src/externalize.ts b/packages/vite-node/src/externalize.ts index cf3b7482a6a8..bd65bd31024e 100644 --- a/packages/vite-node/src/externalize.ts +++ b/packages/vite-node/src/externalize.ts @@ -85,9 +85,11 @@ async function _shouldExternalize( return false } -function matchExternalizePattern(id: string, patterns?: (string | RegExp)[]) { - if (!patterns) +function matchExternalizePattern(id: string, patterns?: (string | RegExp)[] | true) { + if (patterns == null) return false + if (patterns === true) + return true for (const ex of patterns) { if (typeof ex === 'string') { if (id.includes(`/node_modules/${ex}/`)) diff --git a/packages/vite-node/src/server.ts b/packages/vite-node/src/server.ts index 5058f6c41a3c..de9a25dc754e 100644 --- a/packages/vite-node/src/server.ts +++ b/packages/vite-node/src/server.ts @@ -3,7 +3,7 @@ import type { TransformResult, ViteDevServer } from 'vite' import createDebug from 'debug' import type { FetchResult, RawSourceMap, ViteNodeResolveId, ViteNodeServerOptions } from './types' import { shouldExternalize } from './externalize' -import { toFilePath, withInlineSourcemap } from './utils' +import { toArray, toFilePath, withInlineSourcemap } from './utils' export * from './externalize' @@ -24,7 +24,27 @@ export class ViteNodeServer { constructor( public server: ViteDevServer, public options: ViteNodeServerOptions = {}, - ) {} + ) { + // @ts-expect-error ssr is not typed + const ssrOptions = server.config.ssr + if (ssrOptions) { + options.deps ??= {} + + // we don't externalize ssr, because it has different semantics in Vite + // if (ssrOptions.external) { + // options.deps.external ??= [] + // options.deps.external.push(...ssrOptions.external) + // } + + if (ssrOptions.noExternal === true) { + options.deps.inline ??= true + } + else if (options.deps.inline !== true) { + options.deps.inline ??= [] + options.deps.inline.push(...toArray(ssrOptions.noExternal)) + } + } + } shouldExternalize(id: string) { return shouldExternalize(id, this.options.deps) diff --git a/packages/vite-node/src/types.ts b/packages/vite-node/src/types.ts index 0a4defce4fe5..ab84483655cb 100644 --- a/packages/vite-node/src/types.ts +++ b/packages/vite-node/src/types.ts @@ -5,7 +5,7 @@ export type Arrayable = T | Array export interface DepsHandlingOptions { external?: (string | RegExp)[] - inline?: (string | RegExp)[] + inline?: (string | RegExp)[] | true /** * Try to guess the CJS version of a package when it's invalid ESM * @default false diff --git a/packages/vitest/src/node/config.ts b/packages/vitest/src/node/config.ts index 4b27305a2461..30ef0b861cce 100644 --- a/packages/vitest/src/node/config.ts +++ b/packages/vitest/src/node/config.ts @@ -93,8 +93,18 @@ export function resolveConfig( resolved.deps = resolved.deps || {} // vitenode will try to import such file with native node, // but then our mocker will not work properly - resolved.deps.inline ??= [] - resolved.deps.inline.push(...extraInlineDeps) + if (resolved.deps.inline !== true) { + // @ts-expect-error ssr is not typed + const ssrOptions = viteConfig.ssr || {} + + if (ssrOptions.noExternal === true && resolved.deps.inline == null) { + resolved.deps.inline = true + } + else { + resolved.deps.inline ??= [] + resolved.deps.inline.push(...extraInlineDeps) + } + } resolved.testNamePattern = resolved.testNamePattern ? resolved.testNamePattern instanceof RegExp diff --git a/packages/vitest/src/types/config.ts b/packages/vitest/src/types/config.ts index de282baff7d9..474c72582b24 100644 --- a/packages/vitest/src/types/config.ts +++ b/packages/vitest/src/types/config.ts @@ -59,8 +59,10 @@ export interface InlineConfig { * Vite will process inlined modules. * * This could be helpful to handle packages that ship `.js` in ESM format (that Node can't handle). + * + * If `true`, every dependency will be inlined */ - inline?: (string | RegExp)[] + inline?: (string | RegExp)[] | true /** * Interpret CJS module's default as named exports