Home / Function/ transformCjsImport() — vite Function Reference

transformCjsImport() — vite Function Reference

Architecture documentation for the transformCjsImport() function in importAnalysis.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  df4b153e_b429_d51a_4d38_896cbcb5f010["transformCjsImport()"]
  5a7b98e4_4eb1_dfca_508b_2d43e2a077e6["importAnalysis.ts"]
  df4b153e_b429_d51a_4d38_896cbcb5f010 -->|defined in| 5a7b98e4_4eb1_dfca_508b_2d43e2a077e6
  56b889e3_7358_a651_5888_fcaa50d129e3["interopNamedImports()"]
  56b889e3_7358_a651_5888_fcaa50d129e3 -->|calls| df4b153e_b429_d51a_4d38_896cbcb5f010
  f937582f_6dad_49b6_41b9_e1ec9c62637b["getIdentifierNameOrLiteralValue()"]
  df4b153e_b429_d51a_4d38_896cbcb5f010 -->|calls| f937582f_6dad_49b6_41b9_e1ec9c62637b
  9d025481_71dc_8fbb_c07e_b6e74a08a45a["getHash()"]
  df4b153e_b429_d51a_4d38_896cbcb5f010 -->|calls| 9d025481_71dc_8fbb_c07e_b6e74a08a45a
  3cf1d94a_16a2_96d6_7d1d_9757e22a2557["warn()"]
  df4b153e_b429_d51a_4d38_896cbcb5f010 -->|calls| 3cf1d94a_16a2_96d6_7d1d_9757e22a2557
  style df4b153e_b429_d51a_4d38_896cbcb5f010 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/importAnalysis.ts lines 1004–1112

export function transformCjsImport(
  importExp: string,
  url: string,
  rawUrl: string,
  importIndex: number,
  importer: string,
  isNodeMode: boolean,
  config: ResolvedConfig,
): string | undefined {
  const node = (parseAst(importExp) as Program).body[0]

  // `export * from '...'` may cause unexpected problem, so give it a warning
  if (
    config.command === 'serve' &&
    node.type === 'ExportAllDeclaration' &&
    !node.exported
  ) {
    config.logger.warn(
      colors.yellow(
        `\nUnable to interop \`${importExp}\` in ${importer}, this may lose module exports. Please export "${rawUrl}" as ESM or use named exports instead, e.g. \`export { A, B } from "${rawUrl}"\``,
      ),
    )
  } else if (
    node.type === 'ImportDeclaration' ||
    node.type === 'ExportNamedDeclaration'
  ) {
    if (!node.specifiers.length) {
      return `import "${url}"`
    }

    const importNames: ImportNameSpecifier[] = []
    const exportNames: string[] = []
    let defaultExports: string = ''
    for (const spec of node.specifiers) {
      if (spec.type === 'ImportSpecifier') {
        const importedName = getIdentifierNameOrLiteralValue(
          spec.imported,
        ) as string
        const localName = spec.local.name
        importNames.push({ importedName, localName })
      } else if (spec.type === 'ImportDefaultSpecifier') {
        importNames.push({
          importedName: 'default',
          localName: spec.local.name,
        })
      } else if (spec.type === 'ImportNamespaceSpecifier') {
        importNames.push({ importedName: '*', localName: spec.local.name })
      } else if (spec.type === 'ExportSpecifier') {
        // for ExportSpecifier, local name is same as imported name
        // prefix the variable name to avoid clashing with other local variables
        const importedName = getIdentifierNameOrLiteralValue(
          spec.local,
        ) as string
        // we want to specify exported name as variable and re-export it
        const exportedName = getIdentifierNameOrLiteralValue(
          spec.exported,
        ) as string
        if (exportedName === 'default') {
          defaultExports = makeLegalIdentifier(
            `__vite__cjsExportDefault_${importIndex}`,
          )
          importNames.push({ importedName, localName: defaultExports })
        } else {
          const localName = `__vite__cjsExport${
            spec.exported.type === 'Literal'
              ? `L_${getHash(spec.exported.value as string)}`
              : 'I_' + spec.exported.name
          }`
          importNames.push({ importedName, localName })
          exportNames.push(
            `${localName} as ${spec.exported.type === 'Literal' ? JSON.stringify(exportedName) : exportedName}`,
          )
        }
      }
    }

    // If there is multiple import for same id in one file,
    // importIndex will prevent the cjsModuleName to be duplicate
    const cjsModuleName = makeLegalIdentifier(
      `__vite__cjsImport${importIndex}_${rawUrl}`,
    )

Domain

Subdomains

Frequently Asked Questions

What does transformCjsImport() do?
transformCjsImport() is a function in the vite codebase, defined in packages/vite/src/node/plugins/importAnalysis.ts.
Where is transformCjsImport() defined?
transformCjsImport() is defined in packages/vite/src/node/plugins/importAnalysis.ts at line 1004.
What does transformCjsImport() call?
transformCjsImport() calls 3 function(s): getHash, getIdentifierNameOrLiteralValue, warn.
What calls transformCjsImport()?
transformCjsImport() is called by 1 function(s): interopNamedImports.

Analyze Your Own Codebase

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

Try Supermodel Free