Skip to content

Commit d202588

Browse files
authored
refactor: use simpler resolve for nested optimized deps (#12770)
1 parent a78588f commit d202588

File tree

6 files changed

+35
-41
lines changed

6 files changed

+35
-41
lines changed

packages/vite/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
"dependencies": {
6969
"esbuild": "^0.17.5",
7070
"postcss": "^8.4.21",
71-
"resolve": "^1.22.1",
7271
"rollup": "^3.20.2"
7372
},
7473
"optionalDependencies": {

packages/vite/rollup.config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ function createNodePlugins(
120120
pattern: /^var json = typeof JSON.+require\('jsonify'\);$/gm,
121121
replacement: 'var json = JSON',
122122
},
123+
// postcss-import uses the `resolve` dep if the `resolve` option is not passed.
124+
// However, we always pass the `resolve` option. Remove this import to avoid
125+
// bundling the `resolve` dep.
126+
'postcss-import/index.js': {
127+
src: 'const resolveId = require("./lib/resolve-id")',
128+
replacement: 'const resolveId = undefined',
129+
},
123130
}),
124131

125132
commonjs({

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

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export function preAliasPlugin(config: ResolvedConfig): Plugin {
4343
depsOptimizer,
4444
id,
4545
importer,
46+
config.resolve.preserveSymlinks,
47+
config.packageCache,
4648
)
4749
if (optimizedId) {
4850
return optimizedId // aliased dep already optimized

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

+26-19
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
isTsRequest,
3333
isWindows,
3434
normalizePath,
35-
resolveFrom,
3635
safeRealpathSync,
3736
slash,
3837
tryStatSync,
@@ -318,7 +317,13 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
318317
asSrc &&
319318
depsOptimizer &&
320319
!options.scan &&
321-
(res = await tryOptimizedResolve(depsOptimizer, id, importer))
320+
(res = await tryOptimizedResolve(
321+
depsOptimizer,
322+
id,
323+
importer,
324+
options.preserveSymlinks,
325+
options.packageCache,
326+
))
322327
) {
323328
return res
324329
}
@@ -854,6 +859,8 @@ export async function tryOptimizedResolve(
854859
depsOptimizer: DepsOptimizer,
855860
id: string,
856861
importer?: string,
862+
preserveSymlinks?: boolean,
863+
packageCache?: PackageCache,
857864
): Promise<string | undefined> {
858865
// TODO: we need to wait until scanning is done here as this function
859866
// is used in the preAliasPlugin to decide if an aliased dep is optimized,
@@ -871,31 +878,31 @@ export async function tryOptimizedResolve(
871878
if (!importer) return
872879

873880
// further check if id is imported by nested dependency
874-
let resolvedSrc: string | undefined
881+
let idPkgDir: string | undefined
882+
const nestedIdMatch = `> ${id}`
875883

876884
for (const optimizedData of metadata.depInfoList) {
877885
if (!optimizedData.src) continue // Ignore chunks
878886

879-
const pkgPath = optimizedData.id
880-
// check for scenarios, e.g.
881-
// pkgPath => "my-lib > foo"
882-
// id => "foo"
883-
// this narrows the need to do a full resolve
884-
if (!pkgPath.endsWith(id)) continue
887+
// check where "foo" is nested in "my-lib > foo"
888+
if (!optimizedData.id.endsWith(nestedIdMatch)) continue
885889

886-
// lazily initialize resolvedSrc
887-
if (resolvedSrc == null) {
888-
try {
889-
// this may throw errors if unable to resolve, e.g. aliased id
890-
resolvedSrc = normalizePath(resolveFrom(id, path.dirname(importer)))
891-
} catch {
892-
// this is best-effort only so swallow errors
893-
break
894-
}
890+
// lazily initialize idPkgDir
891+
if (idPkgDir == null) {
892+
idPkgDir = resolvePackageData(
893+
id,
894+
importer,
895+
preserveSymlinks,
896+
packageCache,
897+
)?.dir
898+
// if still null, it likely means that this id isn't a dep for importer.
899+
// break to bail early
900+
if (idPkgDir == null) break
901+
idPkgDir = normalizePath(idPkgDir)
895902
}
896903

897904
// match by src to correctly identify if id belongs to nested dependency
898-
if (optimizedData.src === resolvedSrc) {
905+
if (optimizedData.src.startsWith(idPkgDir)) {
899906
return depsOptimizer.getOptimizedDepId(optimizedData)
900907
}
901908
}

packages/vite/src/node/utils.ts

-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { builtinModules, createRequire } from 'node:module'
88
import { promises as dns } from 'node:dns'
99
import { performance } from 'node:perf_hooks'
1010
import type { AddressInfo, Server } from 'node:net'
11-
import resolve from 'resolve'
1211
import type { FSWatcher } from 'chokidar'
1312
import remapping from '@ampproject/remapping'
1413
import type { DecodedSourceMap, RawSourceMap } from '@ampproject/remapping'
@@ -22,7 +21,6 @@ import { createFilter as _createFilter } from '@rollup/pluginutils'
2221
import {
2322
CLIENT_ENTRY,
2423
CLIENT_PUBLIC_PATH,
25-
DEFAULT_EXTENSIONS,
2624
ENV_PUBLIC_PATH,
2725
FS_PREFIX,
2826
NULL_BYTE_PLACEHOLDER,
@@ -145,23 +143,6 @@ export const deepImportRE = /^([^@][^/]*)\/|^(@[^/]+\/[^/]+)\//
145143
// TODO: use import()
146144
const _require = createRequire(import.meta.url)
147145

148-
const ssrExtensions = ['.js', '.cjs', '.json', '.node']
149-
150-
export function resolveFrom(
151-
id: string,
152-
basedir: string,
153-
preserveSymlinks = false,
154-
ssr = false,
155-
): string {
156-
return resolve.sync(id, {
157-
basedir,
158-
paths: [],
159-
extensions: ssr ? ssrExtensions : DEFAULT_EXTENSIONS,
160-
// necessary to work with pnpm
161-
preserveSymlinks: preserveSymlinks || !!process.versions.pnp || false,
162-
})
163-
}
164-
165146
// set in bin/vite.js
166147
const filter = process.env.VITE_DEBUG_FILTER
167148

pnpm-lock.yaml

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)