From 2250ffac62e55c89232d745d2f99ece539be9195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Sat, 30 Nov 2024 05:57:21 +0900 Subject: [PATCH] fix(resolve): don't set builtinModules to `external` by default (#18821) --- packages/vite/src/node/config.ts | 9 +++------ packages/vite/src/node/plugins/resolve.ts | 12 ++++++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 47b495e1b994d7..de7c16b559c427 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -4,7 +4,7 @@ import path from 'node:path' import { pathToFileURL } from 'node:url' import { promisify } from 'node:util' import { performance } from 'node:perf_hooks' -import { builtinModules, createRequire } from 'node:module' +import { createRequire } from 'node:module' import colors from 'picocolors' import type { Alias, AliasOptions } from 'dep-types/alias' import { build } from 'esbuild' @@ -621,7 +621,7 @@ export const configDefaults = Object.freeze({ dedupe: [], /** @experimental */ noExternal: [], - // external + external: [], preserveSymlinks: false, alias: [], }, @@ -882,10 +882,7 @@ function resolveEnvironmentResolveOptions( consumer === 'client' || isSsrTargetWebworkerEnvironment ? DEFAULT_CLIENT_CONDITIONS : DEFAULT_SERVER_CONDITIONS.filter((c) => c !== 'browser'), - external: - consumer === 'server' && !isSsrTargetWebworkerEnvironment - ? builtinModules - : [], + enableBuiltinNoExternalCheck: !!isSsrTargetWebworkerEnvironment, }, resolve ?? {}, ) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index c7a113a2b31727..f75798b84abe8e 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -96,6 +96,10 @@ export interface EnvironmentResolveOptions { * @experimental */ external?: string[] | true + /** + * @internal + */ + enableBuiltinNoExternalCheck?: boolean } export interface ResolveOptions extends EnvironmentResolveOptions { @@ -169,8 +173,11 @@ interface ResolvePluginOptions { } export interface InternalResolveOptions - extends Required, - ResolvePluginOptions {} + extends Required>, + ResolvePluginOptions { + /** @internal this is always optional for backward compat */ + enableBuiltinNoExternalCheck?: boolean +} // Defined ResolveOptions are used to overwrite the values for all environments // It is used when creating custom resolvers (for CSS, scanning, etc) @@ -420,6 +427,7 @@ export function resolvePlugin( if (isBuiltin(id)) { if (currentEnvironmentOptions.consumer === 'server') { if ( + options.enableBuiltinNoExternalCheck && 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.