Home / Function/ importGlobPlugin() — vite Function Reference

importGlobPlugin() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  c5cada27_b602_8e85_58cc_b778b0c71441["importGlobPlugin()"]
  b68d9c02_4026_8cfa_8eb1_35ec4a8f23cb["importMetaGlob.ts"]
  c5cada27_b602_8e85_58cc_b778b0c71441 -->|defined in| b68d9c02_4026_8cfa_8eb1_35ec4a8f23cb
  b1b40b5b_3e43_2197_dea0_d36389d312a1["resolvePlugins()"]
  b1b40b5b_3e43_2197_dea0_d36389d312a1 -->|calls| c5cada27_b602_8e85_58cc_b778b0c71441
  a6007673_0baa_9293_6af0_ff74ed056981["transformGlobImport()"]
  c5cada27_b602_8e85_58cc_b778b0c71441 -->|calls| a6007673_0baa_9293_6af0_ff74ed056981
  6841c80c_8127_d8ce_ac5d_bb48de9eee42["transformStableResult()"]
  c5cada27_b602_8e85_58cc_b778b0c71441 -->|calls| 6841c80c_8127_d8ce_ac5d_bb48de9eee42
  1c3423db_563a_92c5_87fa_42d035132b6c["getModuleById()"]
  c5cada27_b602_8e85_58cc_b778b0c71441 -->|calls| 1c3423db_563a_92c5_87fa_42d035132b6c
  style c5cada27_b602_8e85_58cc_b778b0c71441 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/importMetaGlob.ts lines 44–124

export function importGlobPlugin(config: ResolvedConfig): Plugin {
  if (config.isBundled && config.nativePluginEnabledLevel >= 1) {
    return nativeImportGlobPlugin({
      root: config.root,
      sourcemap: !!config.build.sourcemap,
      restoreQueryExtension: config.experimental.importGlobRestoreExtension,
    })
  }

  const importGlobMaps = new Map<
    Environment,
    Map<string, Array<(file: string) => boolean>>
  >()

  return {
    name: 'vite:import-glob',
    buildStart() {
      importGlobMaps.clear()
    },
    transform: {
      filter: { code: 'import.meta.glob' },
      async handler(code, id) {
        const result = await transformGlobImport(
          code,
          id,
          config.root,
          (im, _, options) =>
            this.resolve(im, id, options).then((i) => i?.id || im),
          config.experimental.importGlobRestoreExtension,
          config.logger,
        )
        if (result) {
          const allGlobs = result.matches.map((i) => i.globsResolved)
          if (!importGlobMaps.has(this.environment)) {
            importGlobMaps.set(this.environment, new Map())
          }

          const globMatchers = allGlobs.map((globs) => {
            const affirmed: string[] = []
            const negated: string[] = []
            for (const glob of globs) {
              if (glob[0] === '!') {
                negated.push(glob.slice(1))
              } else {
                affirmed.push(glob)
              }
            }
            const affirmedMatcher = picomatch(affirmed)
            const negatedMatcher = picomatch(negated)

            return (file: string) => {
              // (glob1 || glob2) && !(glob3 || glob4)...
              return (
                (affirmed.length === 0 || affirmedMatcher(file)) &&
                !(negated.length > 0 && negatedMatcher(file))
              )
            }
          })
          importGlobMaps.get(this.environment)!.set(id, globMatchers)

          return transformStableResult(result.s, id, config)
        }
      },
    },
    hotUpdate({ type, file, modules: oldModules }) {
      if (type === 'update') return

      const importGlobMap = importGlobMaps.get(this.environment)
      if (!importGlobMap) return

      const modules: EnvironmentModuleNode[] = []
      for (const [id, globMatchers] of importGlobMap) {
        if (globMatchers.some((matcher) => matcher(file))) {
          const mod = this.environment.moduleGraph.getModuleById(id)
          if (mod) modules.push(mod)
        }
      }
      return modules.length > 0 ? [...oldModules, ...modules] : undefined
    },
  }
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does importGlobPlugin() do?
importGlobPlugin() is a function in the vite codebase, defined in packages/vite/src/node/plugins/importMetaGlob.ts.
Where is importGlobPlugin() defined?
importGlobPlugin() is defined in packages/vite/src/node/plugins/importMetaGlob.ts at line 44.
What does importGlobPlugin() call?
importGlobPlugin() calls 3 function(s): getModuleById, transformGlobImport, transformStableResult.
What calls importGlobPlugin()?
importGlobPlugin() is called by 1 function(s): resolvePlugins.

Analyze Your Own Codebase

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

Try Supermodel Free