From d202588fdfe29df95007bb0012d9b6c9aa5bde75 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Fri, 7 Apr 2023 03:28:36 +0800 Subject: [PATCH] refactor: use simpler resolve for nested optimized deps (#12770) --- packages/vite/package.json | 1 - packages/vite/rollup.config.ts | 7 ++++ packages/vite/src/node/plugins/preAlias.ts | 2 + packages/vite/src/node/plugins/resolve.ts | 45 +++++++++++++--------- packages/vite/src/node/utils.ts | 19 --------- pnpm-lock.yaml | 2 - 6 files changed, 35 insertions(+), 41 deletions(-) diff --git a/packages/vite/package.json b/packages/vite/package.json index 291051b920736f..825d1ea81aff3e 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -68,7 +68,6 @@ "dependencies": { "esbuild": "^0.17.5", "postcss": "^8.4.21", - "resolve": "^1.22.1", "rollup": "^3.20.2" }, "optionalDependencies": { diff --git a/packages/vite/rollup.config.ts b/packages/vite/rollup.config.ts index e241a77182c4c7..b50359581147e0 100644 --- a/packages/vite/rollup.config.ts +++ b/packages/vite/rollup.config.ts @@ -120,6 +120,13 @@ function createNodePlugins( pattern: /^var json = typeof JSON.+require\('jsonify'\);$/gm, replacement: 'var json = JSON', }, + // postcss-import uses the `resolve` dep if the `resolve` option is not passed. + // However, we always pass the `resolve` option. Remove this import to avoid + // bundling the `resolve` dep. + 'postcss-import/index.js': { + src: 'const resolveId = require("./lib/resolve-id")', + replacement: 'const resolveId = undefined', + }, }), commonjs({ diff --git a/packages/vite/src/node/plugins/preAlias.ts b/packages/vite/src/node/plugins/preAlias.ts index f3d31e14e0a733..3a4e6c2ac0fe44 100644 --- a/packages/vite/src/node/plugins/preAlias.ts +++ b/packages/vite/src/node/plugins/preAlias.ts @@ -43,6 +43,8 @@ export function preAliasPlugin(config: ResolvedConfig): Plugin { depsOptimizer, id, importer, + config.resolve.preserveSymlinks, + config.packageCache, ) if (optimizedId) { return optimizedId // aliased dep already optimized diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index a1c5ab353110b4..8d5b29947799c5 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -32,7 +32,6 @@ import { isTsRequest, isWindows, normalizePath, - resolveFrom, safeRealpathSync, slash, tryStatSync, @@ -318,7 +317,13 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { asSrc && depsOptimizer && !options.scan && - (res = await tryOptimizedResolve(depsOptimizer, id, importer)) + (res = await tryOptimizedResolve( + depsOptimizer, + id, + importer, + options.preserveSymlinks, + options.packageCache, + )) ) { return res } @@ -854,6 +859,8 @@ export async function tryOptimizedResolve( depsOptimizer: DepsOptimizer, id: string, importer?: string, + preserveSymlinks?: boolean, + packageCache?: PackageCache, ): Promise { // TODO: we need to wait until scanning is done here as this function // is used in the preAliasPlugin to decide if an aliased dep is optimized, @@ -871,31 +878,31 @@ export async function tryOptimizedResolve( if (!importer) return // further check if id is imported by nested dependency - let resolvedSrc: string | undefined + let idPkgDir: string | undefined + const nestedIdMatch = `> ${id}` for (const optimizedData of metadata.depInfoList) { if (!optimizedData.src) continue // Ignore chunks - const pkgPath = optimizedData.id - // check for scenarios, e.g. - // pkgPath => "my-lib > foo" - // id => "foo" - // this narrows the need to do a full resolve - if (!pkgPath.endsWith(id)) continue + // check where "foo" is nested in "my-lib > foo" + if (!optimizedData.id.endsWith(nestedIdMatch)) continue - // lazily initialize resolvedSrc - if (resolvedSrc == null) { - try { - // this may throw errors if unable to resolve, e.g. aliased id - resolvedSrc = normalizePath(resolveFrom(id, path.dirname(importer))) - } catch { - // this is best-effort only so swallow errors - break - } + // lazily initialize idPkgDir + if (idPkgDir == null) { + idPkgDir = resolvePackageData( + id, + importer, + preserveSymlinks, + packageCache, + )?.dir + // if still null, it likely means that this id isn't a dep for importer. + // break to bail early + if (idPkgDir == null) break + idPkgDir = normalizePath(idPkgDir) } // match by src to correctly identify if id belongs to nested dependency - if (optimizedData.src === resolvedSrc) { + if (optimizedData.src.startsWith(idPkgDir)) { return depsOptimizer.getOptimizedDepId(optimizedData) } } diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 2f73dfef78e32b..14a016f94311ec 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -8,7 +8,6 @@ import { builtinModules, createRequire } from 'node:module' import { promises as dns } from 'node:dns' import { performance } from 'node:perf_hooks' import type { AddressInfo, Server } from 'node:net' -import resolve from 'resolve' import type { FSWatcher } from 'chokidar' import remapping from '@ampproject/remapping' import type { DecodedSourceMap, RawSourceMap } from '@ampproject/remapping' @@ -22,7 +21,6 @@ import { createFilter as _createFilter } from '@rollup/pluginutils' import { CLIENT_ENTRY, CLIENT_PUBLIC_PATH, - DEFAULT_EXTENSIONS, ENV_PUBLIC_PATH, FS_PREFIX, NULL_BYTE_PLACEHOLDER, @@ -145,23 +143,6 @@ export const deepImportRE = /^([^@][^/]*)\/|^(@[^/]+\/[^/]+)\// // TODO: use import() const _require = createRequire(import.meta.url) -const ssrExtensions = ['.js', '.cjs', '.json', '.node'] - -export function resolveFrom( - id: string, - basedir: string, - preserveSymlinks = false, - ssr = false, -): string { - return resolve.sync(id, { - basedir, - paths: [], - extensions: ssr ? ssrExtensions : DEFAULT_EXTENSIONS, - // necessary to work with pnpm - preserveSymlinks: preserveSymlinks || !!process.versions.pnp || false, - }) -} - // set in bin/vite.js const filter = process.env.VITE_DEBUG_FILTER diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9459b1c3d56402..b9c47f2d0e4e91 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -211,7 +211,6 @@ importers: postcss-import: ^15.1.0 postcss-load-config: ^4.0.1 postcss-modules: ^6.0.0 - resolve: ^1.22.1 resolve.exports: ^2.0.1 rollup: ^3.20.2 rollup-plugin-license: ^3.0.1 @@ -228,7 +227,6 @@ importers: dependencies: esbuild: 0.17.5 postcss: 8.4.21 - resolve: 1.22.1 rollup: 3.20.2 optionalDependencies: fsevents: 2.3.2