Repository: vitejs/vite
File: packages/vite/src/node/plugins/resolve.ts (TypeScript)

Code before:
      const ssr = resolveOpts?.ssr === true

      // We need to delay depsOptimizer until here instead of passing it as an option
      // the resolvePlugin because the optimizer is created on server listen during dev
      const depsOptimizer = resolveOptions.getDepsOptimizer?.(ssr)

      if (id.startsWith(browserExternalId)) {
        return id
      }

      const targetWeb = !ssr || ssrTarget === 'webworker'

      // this is passed by @rollup/plugin-commonjs
      const isRequire: boolean =
        resolveOpts?.custom?.['node-resolve']?.isRequire ?? false

      const options: InternalResolveOptions = {
        isRequire,
        ...resolveOptions,
        scan: resolveOpts?.scan ?? resolveOptions.scan,
      }

      const resolveSubpathImports = (id: string, importer?: string) => {
        if (!importer || !id.startsWith(subpathImportsPrefix)) return
        const basedir = path.dirname(importer)
        const importerPkgJson = lookupFile(basedir, ['package.json'], {
          predicate: (content) => !!JSON.parse(content).name,
        })
        if (!importerPkgJson) return

        return resolveExportsOrImports(
          JSON.parse(importerPkgJson),
          id,
          options,
          targetWeb,
          'imports',
        )
      }

      const resolvedImports = resolveSubpathImports(id, importer)
      if (resolvedImports) {
        id = resolvedImports
      }

      if (importer) {
        const _importer = isWorkerRequest(importer)
          ? splitFileAndPostfix(importer).file
          : importer
        if (
          isTsRequest(_importer) ||
          resolveOpts.custom?.depScan?.loader?.startsWith('ts')

Review comment (performance):
Should this use `loadPackageData` instead? It looks like `loadPackageData` has an internal cache, so may be faster

Code after:
      const ssr = resolveOpts?.ssr === true

      // We need to delay depsOptimizer until here instead of passing it as an option
      // the resolvePlugin because the optimizer is created on server listen during dev
      const depsOptimizer = resolveOptions.getDepsOptimizer?.(ssr)

      if (id.startsWith(browserExternalId)) {
        return id
      }

      const targetWeb = !ssr || ssrTarget === 'webworker'

      // this is passed by @rollup/plugin-commonjs
      const isRequire: boolean =
        resolveOpts?.custom?.['node-resolve']?.isRequire ?? false

      const options: InternalResolveOptions = {
        isRequire,
        ...resolveOptions,
        scan: resolveOpts?.scan ?? resolveOptions.scan,
      }

      const resolveSubpathImports = (id: string, importer?: string) => {
        if (!importer || !id.startsWith(subpathImportsPrefix)) return
        const basedir = path.dirname(importer)
        const pkgJsonPath = lookupFile(basedir, ['package.json'], {
          pathOnly: true,
        })
        if (!pkgJsonPath) return

        const pkgData = loadPackageData(pkgJsonPath, options.preserveSymlinks)
        return resolveExportsOrImports(
          pkgData.data,
          id,
          options,
          targetWeb,
          'imports',
        )
      }

      const resolvedImports = resolveSubpathImports(id, importer)
      if (resolvedImports) {
        id = resolvedImports
      }

      if (importer) {
        const _importer = isWorkerRequest(importer)
          ? splitFileAndPostfix(importer).file
          : importer
        if (
          isTsRequest(_importer) ||