Home / Function/ rewriteDefault() — vue Function Reference

rewriteDefault() — vue Function Reference

Architecture documentation for the rewriteDefault() function in rewriteDefault.ts from the vue codebase.

Entity Profile

Dependency Diagram

graph TD
  81fc81a6_5762_0cf2_fa0f_6c5be69a5d84["rewriteDefault()"]
  16b5f9ed_1e09_eff8_5797_7cb90fc86d37["compileScript()"]
  16b5f9ed_1e09_eff8_5797_7cb90fc86d37 -->|calls| 81fc81a6_5762_0cf2_fa0f_6c5be69a5d84
  451a37d0_02c5_be91_6f91_8b287688387d["hasDefaultExport()"]
  81fc81a6_5762_0cf2_fa0f_6c5be69a5d84 -->|calls| 451a37d0_02c5_be91_6f91_8b287688387d
  8c89f29c_f2d2_77b1_38ab_4a8401e62be4["specifierEnd()"]
  81fc81a6_5762_0cf2_fa0f_6c5be69a5d84 -->|calls| 8c89f29c_f2d2_77b1_38ab_4a8401e62be4
  style 81fc81a6_5762_0cf2_fa0f_6c5be69a5d84 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/compiler-sfc/src/rewriteDefault.ts lines 13–95

export function rewriteDefault(
  input: string,
  as: string,
  parserPlugins?: ParserPlugin[]
): string {
  if (!hasDefaultExport(input)) {
    return input + `\nconst ${as} = {}`
  }

  let replaced: string | undefined

  const classMatch = input.match(exportDefaultClassRE)
  if (classMatch) {
    replaced =
      input.replace(exportDefaultClassRE, '$1class $2') +
      `\nconst ${as} = ${classMatch[2]}`
  } else {
    replaced = input.replace(defaultExportRE, `$1const ${as} =`)
  }
  if (!hasDefaultExport(replaced)) {
    return replaced
  }

  // if the script somehow still contains `default export`, it probably has
  // multi-line comments or template strings. fallback to a full parse.
  const s = new MagicString(input)
  const ast = parse(input, {
    sourceType: 'module',
    plugins: parserPlugins
  }).program.body
  ast.forEach(node => {
    if (node.type === 'ExportDefaultDeclaration') {
      if (node.declaration.type === 'ClassDeclaration' && node.declaration.id) {
        let start: number =
          node.declaration.decorators && node.declaration.decorators.length > 0
            ? node.declaration.decorators[
                node.declaration.decorators.length - 1
              ].end!
            : node.start!
        s.overwrite(start, node.declaration.id.start!, ` class `)
        s.append(`\nconst ${as} = ${node.declaration.id.name}`)
      } else {
        s.overwrite(node.start!, node.declaration.start!, `const ${as} = `)
      }
    }
    if (node.type === 'ExportNamedDeclaration') {
      for (const specifier of node.specifiers) {
        if (
          specifier.type === 'ExportSpecifier' &&
          specifier.exported.type === 'Identifier' &&
          specifier.exported.name === 'default'
        ) {
          if (node.source) {
            if (specifier.local.name === 'default') {
              const end = specifierEnd(input, specifier.local.end!, node.end)
              s.prepend(
                `import { default as __VUE_DEFAULT__ } from '${node.source.value}'\n`
              )
              s.overwrite(specifier.start!, end, ``)
              s.append(`\nconst ${as} = __VUE_DEFAULT__`)
              continue
            } else {
              const end = specifierEnd(input, specifier.exported.end!, node.end)
              s.prepend(
                `import { ${input.slice(
                  specifier.local.start!,
                  specifier.local.end!
                )} } from '${node.source.value}'\n`
              )
              s.overwrite(specifier.start!, end, ``)
              s.append(`\nconst ${as} = ${specifier.local.name}`)
              continue
            }
          }
          const end = specifierEnd(input, specifier.end!, node.end)
          s.overwrite(specifier.start!, end, ``)
          s.append(`\nconst ${as} = ${specifier.local.name}`)
        }
      }
    }
  })
  return s.toString()
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does rewriteDefault() do?
rewriteDefault() is a function in the vue codebase.
What does rewriteDefault() call?
rewriteDefault() calls 2 function(s): hasDefaultExport, specifierEnd.
What calls rewriteDefault()?
rewriteDefault() is called by 1 function(s): compileScript.

Analyze Your Own Codebase

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

Try Supermodel Free