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

Code before:
    },

    load: {
      handler(id) {
        if (id.startsWith(browserExternalId)) {
          if (isProduction) {
            return `export default {}`
          } else {
            id = id.slice(browserExternalId.length + 1)
            return `\
  export default new Proxy({}, {
    get(_, key) {
      throw new Error(\`Module "${id}" has been externalized for browser compatibility. Cannot access "${id}.\${key}" in client code.  See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.\`)
    }
  })`
          }
        }
        if (id.startsWith(optionalPeerDepId)) {
          if (isProduction) {
            return `export default {}`
          } else {
            const [, peerDep, parentDep] = id.split(':')
            return `throw new Error(\`Could not resolve "${peerDep}" imported by "${parentDep}". Is it installed?\`)`
          }
        }
      },
    },
  }
}

function resolveSubpathImports(
  id: string,
  importer: string | undefined,
  options: InternalResolveOptions,
) {
  if (!importer || !id.startsWith(subpathImportsPrefix)) return
  const basedir = path.dirname(importer)
  const pkgData = findNearestPackageData(basedir, options.packageCache)
  if (!pkgData) return

  let { file: idWithoutPostfix, postfix } = splitFileAndPostfix(id.slice(1))
  idWithoutPostfix = '#' + idWithoutPostfix

  let importsPath = resolveExportsOrImports(
    pkgData.data,
    idWithoutPostfix,
    options,
    'imports',
  )

  if (importsPath?.[0] === '.') {

Review comment (refactor):
We can remove this part and fix the `require` case when we migrate to rolldown (https://github.com/vitejs/rolldown-vite/pull/167).


Code after:
    },

    load: {
      handler(id) {
        if (id.startsWith(browserExternalId)) {
          if (isProduction) {
            return `export default {}`
          } else {
            id = id.slice(browserExternalId.length + 1)
            return `\
  export default new Proxy({}, {
    get(_, key) {
      throw new Error(\`Module "${id}" has been externalized for browser compatibility. Cannot access "${id}.\${key}" in client code.  See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.\`)
    }
  })`
          }
        }
        if (id.startsWith(optionalPeerDepId)) {
          const [, peerDep, parentDep, isRequire] = id.split(':')
          // rollup + @rollup/plugin-commonjs hoists dynamic `require`s by default
          // If we add a `throw` statement, it will be injected to the top-level and break the whole bundle
          // Instead, we mock the module for now
          // This can be fixed when we migrate to rolldown
          if (isRequire === 'true' && isProduction) {
            return 'export default {}'
          }
          return (
            'export default {};' +
            `throw new Error(\`Could not resolve "${peerDep}" imported by "${parentDep}".${isProduction ? '' : ' Is it installed?'}\`)`
          )
        }
      },
    },
  }
}

function resolveSubpathImports(
  id: string,
  importer: string | undefined,
  options: InternalResolveOptions,
) {
  if (!importer || !id.startsWith(subpathImportsPrefix)) return
  const basedir = path.dirname(importer)
  const pkgData = findNearestPackageData(basedir, options.packageCache)
  if (!pkgData) return

  let { file: idWithoutPostfix, postfix } = splitFileAndPostfix(id.slice(1))
  idWithoutPostfix = '#' + idWithoutPostfix

  let importsPath = resolveExportsOrImports(
    pkgData.data,