Home / Function/ parseImportGlob() — vite Function Reference

parseImportGlob() — vite Function Reference

Architecture documentation for the parseImportGlob() function in importMetaGlob.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  e4ded9fb_6262_73a6_ae73_068c08e2b994["parseImportGlob()"]
  b68d9c02_4026_8cfa_8eb1_35ec4a8f23cb["importMetaGlob.ts"]
  e4ded9fb_6262_73a6_ae73_068c08e2b994 -->|defined in| b68d9c02_4026_8cfa_8eb1_35ec4a8f23cb
  6f63bd9e_a1e9_8210_f7b7_9dbb3133505a["run()"]
  6f63bd9e_a1e9_8210_f7b7_9dbb3133505a -->|calls| e4ded9fb_6262_73a6_ae73_068c08e2b994
  a6007673_0baa_9293_6af0_ff74ed056981["transformGlobImport()"]
  a6007673_0baa_9293_6af0_ff74ed056981 -->|calls| e4ded9fb_6262_73a6_ae73_068c08e2b994
  c9d40398_718e_f740_f335_71974e4b9ccd["findCorrespondingCloseParenthesisPosition()"]
  e4ded9fb_6262_73a6_ae73_068c08e2b994 -->|calls| c9d40398_718e_f740_f335_71974e4b9ccd
  97ec4c6d_f8dd_eac0_9f3c_6fbc9c14481f["err()"]
  e4ded9fb_6262_73a6_ae73_068c08e2b994 -->|calls| 97ec4c6d_f8dd_eac0_9f3c_6fbc9c14481f
  82795e66_8dfd_fa82_3c8a_e25c455f5e42["parseGlobOptions()"]
  e4ded9fb_6262_73a6_ae73_068c08e2b994 -->|calls| 82795e66_8dfd_fa82_3c8a_e25c455f5e42
  2c2bd501_c65a_dbfd_7656_698e0b5e10f2["toAbsoluteGlob()"]
  e4ded9fb_6262_73a6_ae73_068c08e2b994 -->|calls| 2c2bd501_c65a_dbfd_7656_698e0b5e10f2
  style e4ded9fb_6262_73a6_ae73_068c08e2b994 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/importMetaGlob.ts lines 245–369

export async function parseImportGlob(
  code: string,
  importer: string | undefined,
  root: string,
  resolveId: IdResolver,
  logger?: Logger,
): Promise<ParsedImportGlob[]> {
  let cleanCode: string
  try {
    cleanCode = stripLiteral(code)
  } catch {
    // skip invalid js code
    return []
  }
  const matches = Array.from(cleanCode.matchAll(importGlobRE))

  const tasks = matches.map(async (match, index) => {
    const start = match.index!

    const err = (msg: string) => {
      const e = new Error(`Invalid glob import syntax: ${msg}`)
      ;(e as any).pos = start
      return e
    }

    const end =
      findCorrespondingCloseParenthesisPosition(
        cleanCode,
        start + match[0].length,
      ) + 1
    if (end <= 0) {
      throw err('Close parenthesis not found')
    }

    const statementCode = code.slice(start, end)

    const rootAst = (await parseAstAsync(statementCode)).body[0]
    if (rootAst.type !== 'ExpressionStatement') {
      throw err(`Expect CallExpression, got ${rootAst.type}`)
    }
    const ast = rootAst.expression
    if (ast.type !== 'CallExpression') {
      throw err(`Expect CallExpression, got ${ast.type}`)
    }
    if (ast.arguments.length < 1 || ast.arguments.length > 2)
      throw err(`Expected 1-2 arguments, but got ${ast.arguments.length}`)

    const arg1 = ast.arguments[0] as ArrayExpression | Literal | TemplateLiteral
    const arg2 = ast.arguments[1] as
      | (Node & { start: number; end: number })
      | undefined

    const globs: string[] = []

    const validateLiteral = (element: Expression | SpreadElement | null) => {
      if (!element) return
      if (element.type === 'Literal') {
        if (typeof element.value !== 'string')
          throw err(
            `Expected glob to be a string, but got "${typeof element.value}"`,
          )
        globs.push(element.value)
      } else if (element.type === 'TemplateLiteral') {
        if (element.expressions.length !== 0) {
          throw err(
            `Expected glob to be a string, but got dynamic template literal`,
          )
        }
        globs.push(element.quasis[0].value.raw)
      } else {
        throw err('Could only use literals')
      }
    }

    if (arg1.type === 'ArrayExpression') {
      for (const element of arg1.elements) {
        validateLiteral(element)
      }
    } else {
      validateLiteral(arg1)
    }

Domain

Subdomains

Frequently Asked Questions

What does parseImportGlob() do?
parseImportGlob() is a function in the vite codebase, defined in packages/vite/src/node/plugins/importMetaGlob.ts.
Where is parseImportGlob() defined?
parseImportGlob() is defined in packages/vite/src/node/plugins/importMetaGlob.ts at line 245.
What does parseImportGlob() call?
parseImportGlob() calls 4 function(s): err, findCorrespondingCloseParenthesisPosition, parseGlobOptions, toAbsoluteGlob.
What calls parseImportGlob()?
parseImportGlob() is called by 2 function(s): run, transformGlobImport.

Analyze Your Own Codebase

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

Try Supermodel Free