From bbf2e88b751f19046b3e68ea1a3cd01f0f3e90c6 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 28 Nov 2024 18:56:15 +0900 Subject: [PATCH 1/3] fix(resolve): don't set builtinModules to `external` by default --- packages/vite/src/node/config.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 6c65a2aaaa9012..f8e125bc4a32fa 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,6 @@ function resolveEnvironmentResolveOptions( consumer === 'client' || isSsrTargetWebworkerEnvironment ? DEFAULT_CLIENT_CONDITIONS : DEFAULT_SERVER_CONDITIONS.filter((c) => c !== 'browser'), - external: - consumer === 'server' && !isSsrTargetWebworkerEnvironment - ? builtinModules - : [], }, resolve ?? {}, ) From 4200f37a829afad2b7c5a45c2ec6f0d757f4ccdb Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 28 Nov 2024 20:03:02 +0900 Subject: [PATCH 2/3] fix: add `enableBuiltinNoExternalCheck` --- packages/vite/src/node/config.ts | 1 + packages/vite/src/node/plugins/resolve.ts | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index f8e125bc4a32fa..fe019430605b75 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -882,6 +882,7 @@ function resolveEnvironmentResolveOptions( consumer === 'client' || isSsrTargetWebworkerEnvironment ? DEFAULT_CLIENT_CONDITIONS : DEFAULT_SERVER_CONDITIONS.filter((c) => c !== 'browser'), + enableBuiltinNoExternalCheck: !!isSsrTargetWebworkerEnvironment, }, resolve ?? {}, ) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index c7a113a2b31727..139799744e50e4 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 { @@ -420,6 +424,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. From 2b4f369160704d98be990bc986fb5bc5813fe5df Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 28 Nov 2024 20:09:44 +0900 Subject: [PATCH 3/3] chore: fix types --- packages/vite/src/node/plugins/resolve.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 139799744e50e4..f75798b84abe8e 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -173,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)