Skip to content

Commit 42e0d6a

Browse files
bluwysapphi-red
andauthoredMar 14, 2023
refactor(resolve): remove deep import syntax handling (#12381)
Co-authored-by: 翠 / green <green@sapphi.red>
1 parent 47a0f4a commit 42e0d6a

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed
 

‎packages/vite/src/node/optimizer/index.ts

+32-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
getHash,
1919
isOptimizable,
2020
lookupFile,
21+
nestedResolveFrom,
2122
normalizeId,
2223
normalizePath,
2324
removeDir,
@@ -812,18 +813,13 @@ export async function addManuallyIncludedOptimizeDeps(
812813
)
813814
}
814815
}
815-
const resolve = config.createResolver({
816-
asSrc: false,
817-
scan: true,
818-
ssrOptimizeCheck: ssr,
819-
ssrConfig: config.ssr,
820-
})
816+
const resolve = createOptimizeDepsIncludeResolver(config, ssr)
821817
for (const id of [...optimizeDepsInclude, ...extra]) {
822818
// normalize 'foo >bar` as 'foo > bar' to prevent same id being added
823819
// and for pretty printing
824820
const normalizedId = normalizeId(id)
825821
if (!deps[normalizedId] && filter?.(normalizedId) !== false) {
826-
const entry = await resolve(id, undefined, undefined, ssr)
822+
const entry = await resolve(id)
827823
if (entry) {
828824
if (isOptimizable(entry, optimizeDeps)) {
829825
if (!entry.endsWith('?__vite_skip_optimization')) {
@@ -840,6 +836,35 @@ export async function addManuallyIncludedOptimizeDeps(
840836
}
841837
}
842838

839+
function createOptimizeDepsIncludeResolver(
840+
config: ResolvedConfig,
841+
ssr: boolean,
842+
) {
843+
const resolve = config.createResolver({
844+
asSrc: false,
845+
scan: true,
846+
ssrOptimizeCheck: ssr,
847+
ssrConfig: config.ssr,
848+
})
849+
return async (id: string) => {
850+
const lastArrowIndex = id.lastIndexOf('>')
851+
if (lastArrowIndex === -1) {
852+
return await resolve(id, undefined, undefined, ssr)
853+
}
854+
// split nested selected id by last '>', for example:
855+
// 'foo > bar > baz' => 'foo > bar' & 'baz'
856+
const nestedRoot = id.substring(0, lastArrowIndex).trim()
857+
const nestedPath = id.substring(lastArrowIndex + 1).trim()
858+
const basedir = nestedResolveFrom(
859+
nestedRoot,
860+
config.root,
861+
config.resolve.preserveSymlinks,
862+
ssr,
863+
)
864+
return await resolve(nestedPath, basedir, undefined, ssr)
865+
}
866+
}
867+
843868
export function newDepOptimizationProcessing(): DepOptimizationProcessing {
844869
let resolve: () => void
845870
const promise = new Promise((_resolve) => {

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

+15-35
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import {
3333
isTsRequest,
3434
isWindows,
3535
lookupFile,
36-
nestedResolveFrom,
3736
normalizePath,
3837
resolveFrom,
3938
slash,
@@ -645,32 +644,20 @@ export function tryNodeResolve(
645644
options: InternalResolveOptionsWithOverrideConditions,
646645
targetWeb: boolean,
647646
depsOptimizer?: DepsOptimizer,
648-
ssr?: boolean,
647+
ssr: boolean = false,
649648
externalize?: boolean,
650649
allowLinkedExternal: boolean = true,
651650
): PartialResolvedId | undefined {
652651
const { root, dedupe, isBuild, preserveSymlinks, packageCache } = options
653652

654-
ssr ??= false
655-
656-
// split id by last '>' for nested selected packages, for example:
657-
// 'foo > bar > baz' => 'foo > bar' & 'baz'
658-
// 'foo' => '' & 'foo'
659-
const lastArrowIndex = id.lastIndexOf('>')
660-
const nestedRoot = id.substring(0, lastArrowIndex).trim()
661-
const nestedPath = id.substring(lastArrowIndex + 1).trim()
662-
663653
const possiblePkgIds: string[] = []
664654
for (let prevSlashIndex = -1; ; ) {
665-
let slashIndex = nestedPath.indexOf('/', prevSlashIndex + 1)
655+
let slashIndex = id.indexOf('/', prevSlashIndex + 1)
666656
if (slashIndex < 0) {
667-
slashIndex = nestedPath.length
657+
slashIndex = id.length
668658
}
669659

670-
const part = nestedPath.slice(
671-
prevSlashIndex + 1,
672-
(prevSlashIndex = slashIndex),
673-
)
660+
const part = id.slice(prevSlashIndex + 1, (prevSlashIndex = slashIndex))
674661
if (!part) {
675662
break
676663
}
@@ -683,7 +670,7 @@ export function tryNodeResolve(
683670
continue
684671
}
685672

686-
const possiblePkgId = nestedPath.slice(0, slashIndex)
673+
const possiblePkgId = id.slice(0, slashIndex)
687674
possiblePkgIds.push(possiblePkgId)
688675
}
689676

@@ -700,11 +687,6 @@ export function tryNodeResolve(
700687
basedir = root
701688
}
702689

703-
// nested node module, step-by-step resolve to the basedir of the nestedPath
704-
if (nestedRoot) {
705-
basedir = nestedResolveFrom(nestedRoot, basedir, preserveSymlinks)
706-
}
707-
708690
let pkg: PackageData | undefined
709691
let pkgId: string | undefined
710692
// nearest package.json
@@ -742,9 +724,9 @@ export function tryNodeResolve(
742724
// if so, we can resolve to a special id that errors only when imported.
743725
if (
744726
basedir !== root && // root has no peer dep
745-
!isBuiltin(nestedPath) &&
746-
!nestedPath.includes('\0') &&
747-
bareImportRE.test(nestedPath)
727+
!isBuiltin(id) &&
728+
!id.includes('\0') &&
729+
bareImportRE.test(id)
748730
) {
749731
// find package.json with `name` as main
750732
const mainPackageJson = lookupFile(basedir, ['package.json'], {
@@ -753,11 +735,11 @@ export function tryNodeResolve(
753735
if (mainPackageJson) {
754736
const mainPkg = JSON.parse(mainPackageJson)
755737
if (
756-
mainPkg.peerDependencies?.[nestedPath] &&
757-
mainPkg.peerDependenciesMeta?.[nestedPath]?.optional
738+
mainPkg.peerDependencies?.[id] &&
739+
mainPkg.peerDependenciesMeta?.[id]?.optional
758740
) {
759741
return {
760-
id: `${optionalPeerDepId}:${nestedPath}:${mainPkg.name}`,
742+
id: `${optionalPeerDepId}:${id}:${mainPkg.name}`,
761743
}
762744
}
763745
}
@@ -767,10 +749,10 @@ export function tryNodeResolve(
767749

768750
let resolveId = resolvePackageEntry
769751
let unresolvedId = pkgId
770-
const isDeepImport = unresolvedId !== nestedPath
752+
const isDeepImport = unresolvedId !== id
771753
if (isDeepImport) {
772754
resolveId = resolveDeepImport
773-
unresolvedId = '.' + nestedPath.slice(pkgId.length)
755+
unresolvedId = '.' + id.slice(pkgId.length)
774756
}
775757

776758
let resolved: string | undefined
@@ -865,16 +847,14 @@ export function tryNodeResolve(
865847
!isJsType ||
866848
importer?.includes('node_modules') ||
867849
exclude?.includes(pkgId) ||
868-
exclude?.includes(nestedPath) ||
850+
exclude?.includes(id) ||
869851
SPECIAL_QUERY_RE.test(resolved) ||
870852
// During dev SSR, we don't have a way to reload the module graph if
871853
// a non-optimized dep is found. So we need to skip optimization here.
872854
// The only optimized deps are the ones explicitly listed in the config.
873855
(!options.ssrOptimizeCheck && !isBuild && ssr) ||
874856
// Only optimize non-external CJS deps during SSR by default
875-
(ssr &&
876-
!isCJS &&
877-
!(include?.includes(pkgId) || include?.includes(nestedPath)))
857+
(ssr && !isCJS && !(include?.includes(pkgId) || include?.includes(id)))
878858

879859
if (options.ssrOptimizeCheck) {
880860
return {

‎packages/vite/src/node/utils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,12 @@ export function nestedResolveFrom(
160160
id: string,
161161
basedir: string,
162162
preserveSymlinks = false,
163+
ssr = false,
163164
): string {
164165
const pkgs = id.split('>').map((pkg) => pkg.trim())
165166
try {
166167
for (const pkg of pkgs) {
167-
basedir = resolveFrom(pkg, basedir, preserveSymlinks)
168+
basedir = resolveFrom(pkg, basedir, preserveSymlinks, ssr)
168169
}
169170
} catch {}
170171
return basedir

0 commit comments

Comments
 (0)
Please sign in to comment.