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
  3f2874de_dacf_5309_7716_ce1aa1b354a6["rewriteDefault()"]
  95a0be6b_4d0f_5c1a_879f_f3e1d3321465["rewriteDefault.ts"]
  3f2874de_dacf_5309_7716_ce1aa1b354a6 -->|defined in| 95a0be6b_4d0f_5c1a_879f_f3e1d3321465
  4dff3da5_c3f4_4303_7989_1d16ed3f091d["compileScript()"]
  4dff3da5_c3f4_4303_7989_1d16ed3f091d -->|calls| 3f2874de_dacf_5309_7716_ce1aa1b354a6
  25e4973d_93b9_9dbe_1aa4_f503bf9fa1a2["hasDefaultExport()"]
  3f2874de_dacf_5309_7716_ce1aa1b354a6 -->|calls| 25e4973d_93b9_9dbe_1aa4_f503bf9fa1a2
  239c8102_72a4_cf58_12d6_c7d3c6163a31["specifierEnd()"]
  3f2874de_dacf_5309_7716_ce1aa1b354a6 -->|calls| 239c8102_72a4_cf58_12d6_c7d3c6163a31
  style 3f2874de_dacf_5309_7716_ce1aa1b354a6 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}`)
        }
      }
    }
  })

Domain

Subdomains

Called By

Frequently Asked Questions

What does rewriteDefault() do?
rewriteDefault() is a function in the vue codebase, defined in packages/compiler-sfc/src/rewriteDefault.ts.
Where is rewriteDefault() defined?
rewriteDefault() is defined in packages/compiler-sfc/src/rewriteDefault.ts at line 13.
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