From 2a0b524ff825c1c28612207676300d1a8d3db151 Mon Sep 17 00:00:00 2001 From: patak-dev Date: Fri, 5 Apr 2024 12:38:46 +0200 Subject: [PATCH] feat: ssr.external/noExternal -> resolve.external/noExternal --- .../vite/src/node/__tests__/external.spec.ts | 32 +++++++ .../__tests__/fixtures/cjs-ssr-dep/index.js | 0 .../fixtures/cjs-ssr-dep/package.json | 0 .../src/node/{ssr => }/__tests__/package.json | 0 packages/vite/src/node/config.ts | 26 +++--- .../node/{ssr/ssrExternal.ts => external.ts} | 91 +++++++++++-------- packages/vite/src/node/idResolver.ts | 1 - .../vite/src/node/plugins/importAnalysis.ts | 4 +- packages/vite/src/node/plugins/index.ts | 6 +- packages/vite/src/node/plugins/preAlias.ts | 15 +-- packages/vite/src/node/plugins/resolve.ts | 43 ++++++--- .../vite/src/node/server/pluginContainer.ts | 2 +- .../node/ssr/__tests__/ssrExternal.spec.ts | 29 ------ packages/vite/src/node/ssr/fetchModule.ts | 2 + packages/vite/src/node/ssr/index.ts | 6 ++ packages/vite/src/node/ssr/ssrModuleLoader.ts | 2 + packages/vite/src/node/utils.ts | 2 +- pnpm-lock.yaml | 16 ++-- 18 files changed, 162 insertions(+), 115 deletions(-) create mode 100644 packages/vite/src/node/__tests__/external.spec.ts rename packages/vite/src/node/{ssr => }/__tests__/fixtures/cjs-ssr-dep/index.js (100%) rename packages/vite/src/node/{ssr => }/__tests__/fixtures/cjs-ssr-dep/package.json (100%) rename packages/vite/src/node/{ssr => }/__tests__/package.json (100%) rename packages/vite/src/node/{ssr/ssrExternal.ts => external.ts} (57%) delete mode 100644 packages/vite/src/node/ssr/__tests__/ssrExternal.spec.ts diff --git a/packages/vite/src/node/__tests__/external.spec.ts b/packages/vite/src/node/__tests__/external.spec.ts new file mode 100644 index 00000000000000..dd3a6253e117c7 --- /dev/null +++ b/packages/vite/src/node/__tests__/external.spec.ts @@ -0,0 +1,32 @@ +import { fileURLToPath } from 'node:url' +import { describe, expect, test } from 'vitest' +import type { SSROptions } from '../ssr' +import { resolveConfig } from '../config' +import { createIsConfiguredAsExternal } from '../external' +import { Environment } from '../environment' + +describe('createIsConfiguredAsExternal', () => { + test('default', async () => { + const isExternal = await createIsExternal() + expect(isExternal('@vitejs/cjs-ssr-dep')).toBe(false) + }) + + test('force external', async () => { + const isExternal = await createIsExternal(true) + expect(isExternal('@vitejs/cjs-ssr-dep')).toBe(true) + }) +}) + +async function createIsExternal(external?: true) { + const resolvedConfig = await resolveConfig( + { + configFile: false, + root: fileURLToPath(new URL('./', import.meta.url)), + resolve: { external }, + }, + 'serve', + ) + const environment = new Environment('ssr', resolvedConfig) + console.log(environment.options) + return createIsConfiguredAsExternal(environment) +} diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/cjs-ssr-dep/index.js b/packages/vite/src/node/__tests__/fixtures/cjs-ssr-dep/index.js similarity index 100% rename from packages/vite/src/node/ssr/__tests__/fixtures/cjs-ssr-dep/index.js rename to packages/vite/src/node/__tests__/fixtures/cjs-ssr-dep/index.js diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/cjs-ssr-dep/package.json b/packages/vite/src/node/__tests__/fixtures/cjs-ssr-dep/package.json similarity index 100% rename from packages/vite/src/node/ssr/__tests__/fixtures/cjs-ssr-dep/package.json rename to packages/vite/src/node/__tests__/fixtures/cjs-ssr-dep/package.json diff --git a/packages/vite/src/node/ssr/__tests__/package.json b/packages/vite/src/node/__tests__/package.json similarity index 100% rename from packages/vite/src/node/ssr/__tests__/package.json rename to packages/vite/src/node/__tests__/package.json diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index ad8588868d6bab..5e87466d9081ef 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -645,6 +645,8 @@ function resolveEnvironmentResolveOptions( mainFields: resolve?.mainFields ?? DEFAULT_MAIN_FIELDS, conditions: resolve?.conditions ?? [], externalConditions: resolve?.externalConditions ?? [], + external: resolve?.external ?? [], + noExternal: resolve?.noExternal ?? [], extensions: resolve?.extensions ?? DEFAULT_EXTENSIONS, dedupe: resolve?.dedupe ?? [], preserveSymlinks: resolve?.preserveSymlinks ?? false, @@ -804,8 +806,6 @@ export async function resolveConfig( ) // Backward compatibility: merge ssr into environments.ssr.config as defaults - // Done: ssr.optimizeDeps, ssr.resolve.conditions, ssr.resolve.externalConditions, - // TODO: ssr.external, ssr.noExternal const deprecatedSsrOptimizeDepsConfig = config.ssr?.optimizeDeps ?? {} const configEnvironmentsSsr = config.environments!.ssr if (configEnvironmentsSsr) { @@ -814,13 +814,13 @@ export async function resolveConfig( configEnvironmentsSsr.dev.optimizeDeps ?? {}, deprecatedSsrOptimizeDepsConfig, ) + // TODO: should we merge here? configEnvironmentsSsr.resolve ??= {} - const deprecatedSsrResolveConditions = config.ssr?.resolve?.conditions - configEnvironmentsSsr.resolve.conditions ??= deprecatedSsrResolveConditions // TODO: should we merge? - const deprecatedSsrResolveExternalConditions = - config.ssr?.resolve?.externalConditions + configEnvironmentsSsr.resolve.conditions ??= config.ssr?.resolve?.conditions configEnvironmentsSsr.resolve.externalConditions ??= - deprecatedSsrResolveExternalConditions // TODO: should we merge? + config.ssr?.resolve?.externalConditions + configEnvironmentsSsr.resolve.external ??= config.ssr?.external + configEnvironmentsSsr.resolve.noExternal ??= config.ssr?.noExternal if (config.ssr?.target === 'webworker') { configEnvironmentsSsr.webCompatible = true @@ -891,17 +891,20 @@ export async function resolveConfig( undefined, // default environment ) - // Backward compatibility: merge environments.ssr.dev.optimizeDeps back into ssr.optimizeDeps + // Backward compatibility: merge config.environments.ssr back into config.ssr + // so ecosystem SSR plugins continue to work if only environments.ssr is configured const patchedConfigSsr = { ...config.ssr, + external: resolvedEnvironments.ssr?.resolve.external, + noExternal: resolvedEnvironments.ssr?.resolve.noExternal, optimizeDeps: mergeConfig( - resolvedEnvironments.ssr?.dev?.optimizeDeps ?? {}, config.ssr?.optimizeDeps ?? {}, + resolvedEnvironments.ssr?.dev?.optimizeDeps ?? {}, ), resolve: { + ...config.ssr?.resolve, conditions: resolvedEnvironments.ssr?.resolve.conditions, externalConditions: resolvedEnvironments.ssr?.resolve.externalConditions, - ...config.ssr?.resolve, }, } const ssr = resolveSSROptions( @@ -1150,7 +1153,6 @@ export async function resolveConfig( root: resolvedRoot, isProduction, isBuild: command === 'build', - ssrConfig: resolved.ssr, asSrc: true, preferRelative: false, tryIndex: true, @@ -1439,6 +1441,8 @@ async function bundleConfigFile( mainFields: [], conditions: [], externalConditions: [], + external: [], + noExternal: [], overrideConditions: ['node'], dedupe: [], extensions: DEFAULT_EXTENSIONS, diff --git a/packages/vite/src/node/ssr/ssrExternal.ts b/packages/vite/src/node/external.ts similarity index 57% rename from packages/vite/src/node/ssr/ssrExternal.ts rename to packages/vite/src/node/external.ts index e0d0dc6956c643..3d95773d0eef55 100644 --- a/packages/vite/src/node/ssr/ssrExternal.ts +++ b/packages/vite/src/node/external.ts @@ -1,54 +1,73 @@ import path from 'node:path' -import type { InternalResolveOptions } from '../plugins/resolve' -import { tryNodeResolve } from '../plugins/resolve' +import type { InternalResolveOptions } from './plugins/resolve' +import { tryNodeResolve } from './plugins/resolve' import { bareImportRE, createDebugger, createFilter, getNpmPackageName, isBuiltin, -} from '../utils' -import type { ResolvedConfig } from '..' +} from './utils' +import type { Environment } from './environment' -const debug = createDebugger('vite:ssr-external') +const debug = createDebugger('vite:external') -const isSsrExternalCache = new WeakMap< - ResolvedConfig, +const isExternalCache = new WeakMap< + Environment, (id: string, importer?: string) => boolean | undefined >() -export function shouldExternalizeForSSR( +export function shouldExternalize( + environment: Environment, id: string, importer: string | undefined, - config: ResolvedConfig, ): boolean | undefined { - let isSsrExternal = isSsrExternalCache.get(config) - if (!isSsrExternal) { - isSsrExternal = createIsSsrExternal(config) - isSsrExternalCache.set(config, isSsrExternal) + let isExternal = isExternalCache.get(environment) + if (!isExternal) { + isExternal = createIsExternal(environment) + isExternalCache.set(environment, isExternal) } - return isSsrExternal(id, importer) + return isExternal(id, importer) } -export function createIsConfiguredAsSsrExternal( - config: ResolvedConfig, +const isConfiguredAsExternalCache = new WeakMap< + Environment, + (id: string, importer?: string) => boolean +>() + +export function isConfiguredAsExternal( + environment: Environment, + id: string, + importer?: string, +): boolean { + let isExternal = isConfiguredAsExternalCache.get(environment) + if (!isExternal) { + isExternal = createIsConfiguredAsExternal(environment) + isConfiguredAsExternalCache.set(environment, isExternal) + } + return isExternal(id, importer) +} + +export function createIsConfiguredAsExternal( + environment: Environment, ): (id: string, importer?: string) => boolean { - const { ssr, root } = config - const noExternal = ssr?.noExternal + const { config, options } = environment + const { root } = config + const { external, noExternal } = options.resolve const noExternalFilter = - noExternal !== 'undefined' && typeof noExternal !== 'boolean' && + !(Array.isArray(noExternal) && noExternal.length === 0) && createFilter(undefined, noExternal, { resolve: false }) - const targetConditions = config.ssr.resolve?.externalConditions || [] + const targetConditions = options.resolve?.externalConditions || [] const resolveOptions: InternalResolveOptions = { - ...config.resolve, + ...options.resolve, root, isProduction: false, isBuild: true, conditions: targetConditions, - webCompatible: ssr.target === 'webworker', // TODO: back compat + webCompatible: config.ssr.target === 'webworker', // TODO: back compat nodeCompatible: true, } @@ -90,9 +109,9 @@ export function createIsConfiguredAsSsrExternal( return (id: string, importer?: string) => { if ( // If this id is defined as external, force it as external - // Note that individual package entries are allowed in ssr.external - ssr.external !== true && - ssr.external?.includes(id) + // Note that individual package entries are allowed in `external` + external !== true && + external.includes(id) ) { return true } @@ -103,8 +122,8 @@ export function createIsConfiguredAsSsrExternal( if ( // A package name in ssr.external externalizes every // externalizable package entry - ssr.external !== true && - ssr.external?.includes(pkgName) + external !== true && + external.includes(pkgName) ) { return isExternalizable(id, importer, true) } @@ -114,28 +133,28 @@ export function createIsConfiguredAsSsrExternal( if (noExternalFilter && !noExternalFilter(pkgName)) { return false } - // If `ssr.external: true`, all will be externalized by default, regardless if + // If external is true, all will be externalized by default, regardless if // it's a linked package - return isExternalizable(id, importer, ssr.external === true) + return isExternalizable(id, importer, external === true) } } -function createIsSsrExternal( - config: ResolvedConfig, +function createIsExternal( + environment: Environment, ): (id: string, importer?: string) => boolean | undefined { const processedIds = new Map() - const isConfiguredAsExternal = createIsConfiguredAsSsrExternal(config) + const isConfiguredAsExternal = createIsConfiguredAsExternal(environment) return (id: string, importer?: string) => { if (processedIds.has(id)) { return processedIds.get(id) } - let external = false + let isExternal = false if (id[0] !== '.' && !path.isAbsolute(id)) { - external = isBuiltin(id) || isConfiguredAsExternal(id, importer) + isExternal = isBuiltin(id) || isConfiguredAsExternal(id, importer) } - processedIds.set(id, external) - return external + processedIds.set(id, isExternal) + return isExternal } } diff --git a/packages/vite/src/node/idResolver.ts b/packages/vite/src/node/idResolver.ts index 1efb4310a4c09b..e78ea0a17fa45c 100644 --- a/packages/vite/src/node/idResolver.ts +++ b/packages/vite/src/node/idResolver.ts @@ -41,7 +41,6 @@ export function createIdResolver( root: config.root, isProduction: config.isProduction, isBuild: config.command === 'build', - ssrConfig: config.ssr, asSrc: true, preferRelative: false, tryIndex: true, diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 271a31c55a5861..3a72dddc2f8505 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -55,7 +55,7 @@ import { getDepOptimizationConfig } from '../config' import type { ResolvedConfig } from '../config' import type { Plugin } from '../plugin' import type { DevEnvironment } from '../server/environment' -import { shouldExternalizeForSSR } from '../ssr/ssrExternal' +import { shouldExternalize } from '../external' import { optimizedDepNeedsInterop } from '../optimizer' import { cleanUrl, @@ -493,7 +493,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { } // skip ssr external if (ssr && !matchAlias(specifier)) { - if (shouldExternalizeForSSR(specifier, importer, config)) { + if (shouldExternalize(environment, specifier, importer)) { return } if (isBuiltin(specifier)) { diff --git a/packages/vite/src/node/plugins/index.ts b/packages/vite/src/node/plugins/index.ts index 588a79ae29ddc9..10d6cd8d8d50cb 100644 --- a/packages/vite/src/node/plugins/index.ts +++ b/packages/vite/src/node/plugins/index.ts @@ -3,7 +3,6 @@ import type { ObjectHook } from 'rollup' import type { PluginHookUtils, ResolvedConfig } from '../config' import { isDepsOptimizerEnabled } from '../config' import type { HookHandler, Plugin, PluginWithRequiredHook } from '../plugin' -import { shouldExternalizeForSSR } from '../ssr/ssrExternal' import { watchPackageDataPlugin } from '../packages' import { getFsUtils } from '../fsUtils' import { jsonPlugin } from './json' @@ -65,10 +64,7 @@ export async function resolvePlugins( asSrc: true, fsUtils: getFsUtils(config), optimizeDeps: true, - shouldExternalize: - isBuild && config.build.ssr - ? (id, importer) => shouldExternalizeForSSR(id, importer, config) - : undefined, + externalize: isBuild && !!config.build.ssr, // TODO: should we do this for all environments? }, config.environments, ), diff --git a/packages/vite/src/node/plugins/preAlias.ts b/packages/vite/src/node/plugins/preAlias.ts index 81453320f8a27b..8006ca33ea467f 100644 --- a/packages/vite/src/node/plugins/preAlias.ts +++ b/packages/vite/src/node/plugins/preAlias.ts @@ -6,7 +6,7 @@ import type { ResolvedConfig, } from '..' import type { Plugin } from '../plugin' -import { createIsConfiguredAsSsrExternal } from '../ssr/ssrExternal' +import { isConfiguredAsExternal } from '../external' import { bareImportRE, isInNodeModules, @@ -22,18 +22,17 @@ import { tryOptimizedResolve } from './resolve' */ export function preAliasPlugin(config: ResolvedConfig): Plugin { const findPatterns = getAliasPatterns(config.resolve.alias) - const isConfiguredAsExternal = createIsConfiguredAsSsrExternal(config) const isBuild = config.command === 'build' const fsUtils = getFsUtils(config) return { name: 'vite:pre-alias', async resolveId(id, importer, options) { + const { environment } = this const ssr = options?.ssr === true const depsOptimizer = - this.environment?.mode === 'dev' - ? this.environment.depsOptimizer - : undefined + environment?.mode === 'dev' ? environment.depsOptimizer : undefined if ( + environment && importer && depsOptimizer && bareImportRE.test(id) && @@ -71,7 +70,11 @@ export function preAliasPlugin(config: ResolvedConfig): Plugin { (isInNodeModules(resolvedId) || optimizeDeps.include?.includes(id)) && isOptimizable(resolvedId, optimizeDeps) && - !(isBuild && ssr && isConfiguredAsExternal(id, importer)) && + !( + isBuild && + ssr && + isConfiguredAsExternal(environment, id, importer) + ) && (!ssr || optimizeAliasReplacementForSSR(resolvedId, optimizeDeps)) ) { // aliased dep has not yet been optimized diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 3555e33ef90aec..aa486f0aee425d 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -42,6 +42,7 @@ import type { DepOptimizationConfig, SSROptions } from '..' import type { PackageCache, PackageData } from '../packages' import type { FsUtils } from '../fsUtils' import { commonFsUtils } from '../fsUtils' +import { shouldExternalize } from '../external' import { findNearestMainPackageData, findNearestPackageData, @@ -90,13 +91,19 @@ export interface ResolveOptions { * @default false */ preserveSymlinks?: boolean + /** + * external/noExternal logic, this only works for certain environments + * Previously this was ssr.external/ssr.noExternal + * TODO: better abstraction that works for the client environment too? + */ + noExternal?: string | RegExp | (string | RegExp)[] | true + external?: string[] | true } interface ResolvePluginOptions { root: string isBuild: boolean isProduction: boolean - ssrConfig?: SSROptions packageCache?: PackageCache fsUtils?: FsUtils /** @@ -123,11 +130,17 @@ interface ResolvePluginOptions { ssrOptimizeCheck?: boolean /** - * Optimize deps during dev + * Optimize deps during dev, defaults to false // TODO: Review default * @internal */ optimizeDeps?: boolean + /** + * externalize using external/noExternal, defaults to false // TODO: Review default + * @internal + */ + externalize?: boolean + /** * Previous deps optimizer logic * @internal @@ -138,6 +151,7 @@ interface ResolvePluginOptions { /** * Externalize logic for SSR builds * @internal + * @deprecated */ shouldExternalize?: (id: string, importer?: string) => boolean | undefined @@ -147,6 +161,11 @@ interface ResolvePluginOptions { * @internal */ idOnly?: boolean + + /** + * @deprecated environment.options are used instead + */ + ssrConfig?: SSROptions } export interface InternalResolveOptions @@ -171,15 +190,7 @@ export function resolvePlugin( */ environmentsOptions: Record, ): Plugin { - const { - root, - isProduction, - asSrc, - ssrConfig, - preferRelative = false, - } = resolveOptions - - const { noExternal: ssrNoExternal, external: ssrExternal } = ssrConfig ?? {} + const { root, isProduction, asSrc, preferRelative = false } = resolveOptions // In unix systems, absolute paths inside root first needs to be checked as an // absolute URL (/root/root/path-to-file) resulting in failed checks before falling @@ -395,7 +406,9 @@ export function resolvePlugin( // bare package imports, perform node resolve if (bareImportRE.test(id)) { - const external = options.shouldExternalize?.(id, importer) + const external = + options.externalize && + shouldExternalize(this.environment!, id, importer) // TODO if ( !external && asSrc && @@ -448,10 +461,10 @@ export function resolvePlugin( if (options.nodeCompatible) { if ( options.webCompatible && - ssrNoExternal === true && + options.noExternal === true && // if both noExternal and external are true, noExternal will take the higher priority and bundle it. // only if the id is explicitly listed in external, we will externalize it and skip this error. - (ssrExternal === true || !ssrExternal?.includes(id)) + (options.external === true || !options.external.includes(id)) ) { let message = `Cannot bundle Node.js built-in "${id}"` if (importer) { @@ -460,7 +473,7 @@ export function resolvePlugin( importer, )}"` } - message += `. Consider disabling environments.${this.environment?.name}.noExternal or remove the built-in dependency.` + message += `. Consider disabling environments.${environmentName}.noExternal or remove the built-in dependency.` this.error(message) } diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index 7d6590e318d0c0..36be718aeba110 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -194,7 +194,7 @@ export async function createPluginContainer( }) { const environment = options?.environment ?? environments?.[options?.ssr ? 'ssr' : 'client'] - const ssr = options?.ssr ?? (environment?.name === 'ssr' ? true : false) + const ssr = environment?.name === 'ssr' ? true : !!options?.ssr return { environment, ssr } } diff --git a/packages/vite/src/node/ssr/__tests__/ssrExternal.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrExternal.spec.ts deleted file mode 100644 index 68e753af703ce2..00000000000000 --- a/packages/vite/src/node/ssr/__tests__/ssrExternal.spec.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { fileURLToPath } from 'node:url' -import { describe, expect, test } from 'vitest' -import type { SSROptions } from '..' -import { resolveConfig } from '../../config' -import { createIsConfiguredAsSsrExternal } from '../ssrExternal' - -describe('createIsConfiguredAsSsrExternal', () => { - test('default', async () => { - const isExternal = await createIsExternal() - expect(isExternal('@vitejs/cjs-ssr-dep')).toBe(false) - }) - - test('force external', async () => { - const isExternal = await createIsExternal({ external: true }) - expect(isExternal('@vitejs/cjs-ssr-dep')).toBe(true) - }) -}) - -async function createIsExternal(ssrConfig?: SSROptions) { - const resolvedConfig = await resolveConfig( - { - configFile: false, - root: fileURLToPath(new URL('./', import.meta.url)), - ssr: ssrConfig, - }, - 'serve', - ) - return createIsConfiguredAsSsrExternal(resolvedConfig) -} diff --git a/packages/vite/src/node/ssr/fetchModule.ts b/packages/vite/src/node/ssr/fetchModule.ts index 4128ece8ccdee4..566b293c66e726 100644 --- a/packages/vite/src/node/ssr/fetchModule.ts +++ b/packages/vite/src/node/ssr/fetchModule.ts @@ -49,6 +49,8 @@ export async function fetchModule( mainFields: ['main'], conditions: [], externalConditions, + external: [], // TODO, should it be ssr.resolve.external? + noExternal: [], overrideConditions: [...externalConditions, 'production', 'development'], extensions: ['.js', '.cjs', '.json'], dedupe, diff --git a/packages/vite/src/node/ssr/index.ts b/packages/vite/src/node/ssr/index.ts index eb737b7641801e..4b0626c58d76b9 100644 --- a/packages/vite/src/node/ssr/index.ts +++ b/packages/vite/src/node/ssr/index.ts @@ -8,7 +8,13 @@ export type SsrDepOptimizationOptions = DepOptimizationConfig * @deprecated use environments.ssr */ export interface SSROptions { + /** + * @deprecated use environment.resolve.noExternal + */ noExternal?: string | RegExp | (string | RegExp)[] | true + /** + * @deprecated use environment.resolve.external + */ external?: string[] | true /** diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index 66da7fb47b0469..64d45feee7b75d 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -137,6 +137,8 @@ async function instantiateModule( mainFields: ['main'], conditions: [], externalConditions, + external: [], // TODO, should it be ssr.resolve.external? + noExternal: [], overrideConditions: [...externalConditions, 'production', 'development'], extensions: ['.js', '.cjs', '.json'], dedupe, diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 90fa8daa038b74..9f0892c30374f5 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1094,7 +1094,7 @@ function mergeConfigRecursively( merged[key] = [].concat(existing, value) continue } else if ( - key === 'noExternal' && + key === 'noExternal' && // TODO: environments rootPath === 'ssr' && (existing === true || value === true) ) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 370f46023f964f..4ffb1f1dda1448 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -433,6 +433,14 @@ importers: specifier: ^8.16.0 version: 8.16.0 + packages/vite/src/node/__tests__: + dependencies: + '@vitejs/cjs-ssr-dep': + specifier: link:./fixtures/cjs-ssr-dep + version: link:fixtures/cjs-ssr-dep + + packages/vite/src/node/__tests__/fixtures/cjs-ssr-dep: {} + packages/vite/src/node/__tests__/packages/module: {} packages/vite/src/node/__tests__/packages/name: {} @@ -451,14 +459,6 @@ importers: packages/vite/src/node/server/__tests__/fixtures/yarn/nested: {} - packages/vite/src/node/ssr/__tests__: - dependencies: - '@vitejs/cjs-ssr-dep': - specifier: link:./fixtures/cjs-ssr-dep - version: link:fixtures/cjs-ssr-dep - - packages/vite/src/node/ssr/__tests__/fixtures/cjs-ssr-dep: {} - packages/vite/src/node/ssr/runtime/__tests__: dependencies: '@vitejs/cjs-external':