Home / Function/ expandGlobIds() — vite Function Reference

expandGlobIds() — vite Function Reference

Architecture documentation for the expandGlobIds() function in resolve.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  0d9f6ef1_e83d_dc1d_d305_6a3df47149f2["expandGlobIds()"]
  f5dd930e_f496_d00c_dda8_8cf9daf4b473["resolve.ts"]
  0d9f6ef1_e83d_dc1d_d305_6a3df47149f2 -->|defined in| f5dd930e_f496_d00c_dda8_8cf9daf4b473
  1c26b345_9001_5539_856a_e1c755036eaa["addManuallyIncludedOptimizeDeps()"]
  1c26b345_9001_5539_856a_e1c755036eaa -->|calls| 0d9f6ef1_e83d_dc1d_d305_6a3df47149f2
  cb293321_1174_554d_b5ce_f153b06a2fb4["getNpmPackageName()"]
  0d9f6ef1_e83d_dc1d_d305_6a3df47149f2 -->|calls| cb293321_1174_554d_b5ce_f153b06a2fb4
  170ceb82_4bf1_2290_1f2e_bbdec11e5184["resolvePackageData()"]
  0d9f6ef1_e83d_dc1d_d305_6a3df47149f2 -->|calls| 170ceb82_4bf1_2290_1f2e_bbdec11e5184
  b1d4cd8c_5465_149c_c18d_8523476a1063["getFirstExportStringValue()"]
  0d9f6ef1_e83d_dc1d_d305_6a3df47149f2 -->|calls| b1d4cd8c_5465_149c_c18d_8523476a1063
  f094d39d_cd97_2548_86c3_38902c2f3301["slash()"]
  0d9f6ef1_e83d_dc1d_d305_6a3df47149f2 -->|calls| f094d39d_cd97_2548_86c3_38902c2f3301
  style 0d9f6ef1_e83d_dc1d_d305_6a3df47149f2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/optimizer/resolve.ts lines 46–155

export function expandGlobIds(id: string, config: ResolvedConfig): string[] {
  const pkgName = getNpmPackageName(id)
  if (!pkgName) return []

  const pkgData = resolvePackageData(
    pkgName,
    config.root,
    config.resolve.preserveSymlinks,
    config.packageCache,
  )
  if (!pkgData) return []

  const pattern = '.' + id.slice(pkgName.length)
  const exports = pkgData.data.exports

  // if package has exports field, get all possible export paths and apply
  // glob on them with picomatch
  if (exports) {
    if (typeof exports === 'string' || Array.isArray(exports)) {
      return [pkgName]
    }

    const possibleExportPaths: string[] = []
    for (const key in exports) {
      if (key[0] === '.') {
        if (key.includes('*')) {
          // "./glob/*": {
          //   "browser": "./dist/glob/*-browser/*.js", <-- get this one
          //   "default": "./dist/glob/*/*.js"
          // }
          // NOTE: theoretically the "default" condition could map to a different
          // set of files, but that complicates the resolve logic, so we assume
          // all conditions map to the same set of files, and get the first one.
          const exportsValue = getFirstExportStringValue(exports[key])
          if (!exportsValue) continue

          // "./dist/glob/*-browser/*.js" => "./dist/glob/**/*-browser/**/*.js"
          // NOTE: in some cases, this could expand to consecutive /**/*/**/* etc
          // but it's fine since `tinyglobby` handles it the same.
          const exportValuePattern = exportsValue.replace(/\*/g, '**/*')
          // "./dist/glob/*-browser/*.js" => /dist\/glob\/(.*)-browser\/(.*)\.js/
          const exportsValueGlobRe = new RegExp(
            exportsValue.split('*').map(escapeRegex).join('(.*)'),
          )

          possibleExportPaths.push(
            ...globSync(exportValuePattern, {
              cwd: pkgData.dir,
              expandDirectories: false,
              ignore: ['node_modules'],
            })
              .map((filePath) => {
                // `tinyglobby` returns paths as they are formatted by the underlying `fdir`.
                // Both `globSync("./some-dir/**/*")` and `globSync("./**/*")` result in
                // `"some-dir/somefile"` being returned, so we ensure the correct prefix manually.
                if (exportsValue.startsWith('./')) {
                  filePath = './' + filePath
                }

                // "./glob/*": "./dist/glob/*-browser/*.js"
                // `filePath`: "./dist/glob/foo-browser/foo.js"
                // we need to revert the file path back to the export key by
                // matching value regex and replacing the capture groups to the key
                const matched = exportsValueGlobRe.exec(slash(filePath))
                // `matched`: [..., 'foo', 'foo']
                if (matched) {
                  let allGlobSame = matched.length === 2
                  // exports key can only have one *, so for >=2 matched groups,
                  // make sure they have the same value
                  if (!allGlobSame) {
                    // assume true, if one group is different, set false and break
                    allGlobSame = true
                    for (let i = 2; i < matched.length; i++) {
                      if (matched[i] !== matched[i - 1]) {
                        allGlobSame = false
                        break
                      }
                    }
                  }
                  if (allGlobSame) {
                    return key.replace('*', matched[1]).slice(2)

Subdomains

Frequently Asked Questions

What does expandGlobIds() do?
expandGlobIds() is a function in the vite codebase, defined in packages/vite/src/node/optimizer/resolve.ts.
Where is expandGlobIds() defined?
expandGlobIds() is defined in packages/vite/src/node/optimizer/resolve.ts at line 46.
What does expandGlobIds() call?
expandGlobIds() calls 4 function(s): getFirstExportStringValue, getNpmPackageName, resolvePackageData, slash.
What calls expandGlobIds()?
expandGlobIds() is called by 1 function(s): addManuallyIncludedOptimizeDeps.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free