Skip to content

Commit b74d363

Browse files
authored
refactor(resolve): remove allowLinkedExternal parameter from tryNodeResolve (#18670)
1 parent 3f5fab0 commit b74d363

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed

packages/vite/src/node/external.ts

+27-16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
createFilter,
88
getNpmPackageName,
99
isBuiltin,
10+
isInNodeModules,
1011
} from './utils'
1112
import type { Environment } from './environment'
1213
import type { PartialEnvironment } from './baseEnvironment'
@@ -15,14 +16,14 @@ const debug = createDebugger('vite:external')
1516

1617
const isExternalCache = new WeakMap<
1718
Environment,
18-
(id: string, importer?: string) => boolean | undefined
19+
(id: string, importer?: string) => boolean
1920
>()
2021

2122
export function shouldExternalize(
2223
environment: Environment,
2324
id: string,
2425
importer: string | undefined,
25-
): boolean | undefined {
26+
): boolean {
2627
let isExternal = isExternalCache.get(environment)
2728
if (!isExternal) {
2829
isExternal = createIsExternal(environment)
@@ -72,27 +73,31 @@ export function createIsConfiguredAsExternal(
7273

7374
const isExternalizable = (
7475
id: string,
75-
importer?: string,
76-
configuredAsExternal?: boolean,
76+
importer: string | undefined,
77+
configuredAsExternal: boolean,
7778
): boolean => {
7879
if (!bareImportRE.test(id) || id.includes('\0')) {
7980
return false
8081
}
8182
try {
82-
return !!tryNodeResolve(
83+
const resolved = tryNodeResolve(
8384
id,
8485
// Skip passing importer in build to avoid externalizing non-hoisted dependencies
8586
// unresolvable from root (which would be unresolvable from output bundles also)
8687
config.command === 'build' ? undefined : importer,
8788
resolveOptions,
8889
undefined,
89-
// try to externalize, will return undefined or an object without
90-
// a external flag if it isn't externalizable
91-
true,
92-
// Allow linked packages to be externalized if they are explicitly
93-
// configured as external
94-
!!configuredAsExternal,
95-
)?.external
90+
false,
91+
)
92+
if (!resolved) {
93+
return false
94+
}
95+
// Only allow linked packages to be externalized
96+
// if they are explicitly configured as external
97+
if (!configuredAsExternal && !isInNodeModules(resolved.id)) {
98+
return false
99+
}
100+
return canExternalizeFile(resolved.id)
96101
} catch {
97102
debug?.(
98103
`Failed to node resolve "${id}". Skipping externalizing it by default.`,
@@ -115,7 +120,7 @@ export function createIsConfiguredAsExternal(
115120
}
116121
const pkgName = getNpmPackageName(id)
117122
if (!pkgName) {
118-
return isExternalizable(id, importer)
123+
return isExternalizable(id, importer, false)
119124
}
120125
if (
121126
// A package name in ssr.external externalizes every
@@ -139,14 +144,14 @@ export function createIsConfiguredAsExternal(
139144

140145
function createIsExternal(
141146
environment: Environment,
142-
): (id: string, importer?: string) => boolean | undefined {
143-
const processedIds = new Map<string, boolean | undefined>()
147+
): (id: string, importer?: string) => boolean {
148+
const processedIds = new Map<string, boolean>()
144149

145150
const isConfiguredAsExternal = createIsConfiguredAsExternal(environment)
146151

147152
return (id: string, importer?: string) => {
148153
if (processedIds.has(id)) {
149-
return processedIds.get(id)
154+
return processedIds.get(id)!
150155
}
151156
let isExternal = false
152157
if (id[0] !== '.' && !path.isAbsolute(id)) {
@@ -156,3 +161,9 @@ function createIsExternal(
156161
return isExternal
157162
}
158163
}
164+
165+
export function canExternalizeFile(filePath: string): boolean {
166+
const ext = path.extname(filePath)
167+
// only external js imports
168+
return !ext || ext === '.js' || ext === '.mjs' || ext === '.cjs'
169+
}

packages/vite/src/node/plugins/resolve.ts

+8-23
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { optimizedDepInfoFromFile, optimizedDepInfoFromId } from '../optimizer'
3737
import type { DepsOptimizer } from '../optimizer'
3838
import type { SSROptions } from '..'
3939
import type { PackageCache, PackageData } from '../packages'
40-
import { shouldExternalize } from '../external'
40+
import { canExternalizeFile, shouldExternalize } from '../external'
4141
import {
4242
findNearestMainPackageData,
4343
findNearestPackageData,
@@ -410,14 +410,7 @@ export function resolvePlugin(
410410
}
411411

412412
if (
413-
(res = tryNodeResolve(
414-
id,
415-
importer,
416-
options,
417-
depsOptimizer,
418-
external,
419-
undefined,
420-
))
413+
(res = tryNodeResolve(id, importer, options, depsOptimizer, external))
421414
) {
422415
return res
423416
}
@@ -696,7 +689,6 @@ export function tryNodeResolve(
696689
options: InternalResolveOptions,
697690
depsOptimizer?: DepsOptimizer,
698691
externalize?: boolean,
699-
allowLinkedExternal: boolean = true,
700692
): PartialResolvedId | undefined {
701693
const { root, dedupe, isBuild, preserveSymlinks, packageCache } = options
702694

@@ -771,22 +763,16 @@ export function tryNodeResolve(
771763
if (!externalize) {
772764
return resolved
773765
}
774-
// don't external symlink packages
775-
if (!allowLinkedExternal && !isInNodeModules(resolved.id)) {
766+
if (!canExternalizeFile(resolved.id)) {
776767
return resolved
777768
}
778-
const resolvedExt = path.extname(resolved.id)
779-
// don't external non-js imports
769+
770+
let resolvedId = id
780771
if (
781-
resolvedExt &&
782-
resolvedExt !== '.js' &&
783-
resolvedExt !== '.mjs' &&
784-
resolvedExt !== '.cjs'
772+
deepMatch &&
773+
!pkg?.data.exports &&
774+
path.extname(id) !== path.extname(resolved.id)
785775
) {
786-
return resolved
787-
}
788-
let resolvedId = id
789-
if (deepMatch && !pkg?.data.exports && path.extname(id) !== resolvedExt) {
790776
// id date-fns/locale
791777
// resolve.id ...date-fns/esm/locale/index.js
792778
const index = resolved.id.indexOf(id)
@@ -1106,7 +1092,6 @@ function tryResolveBrowserMapping(
11061092
options,
11071093
undefined,
11081094
undefined,
1109-
undefined,
11101095
)?.id
11111096
: tryFsResolve(path.join(pkg.dir, browserMappedPath), options))
11121097
) {

0 commit comments

Comments
 (0)