Home / File/ rewriteDefault.ts — vue Source File

rewriteDefault.ts — vue Source File

Architecture documentation for rewriteDefault.ts, a typescript file in the vue codebase. 2 imports, 1 dependents.

File typescript SfcCompiler ScriptCompiler 2 imports 1 dependents 3 functions

Entity Profile

Dependency Diagram

graph LR
  95a0be6b_4d0f_5c1a_879f_f3e1d3321465["rewriteDefault.ts"]
  349d6930_cc0f_5a60_fcc7_13e5b0d624c0["parser"]
  95a0be6b_4d0f_5c1a_879f_f3e1d3321465 --> 349d6930_cc0f_5a60_fcc7_13e5b0d624c0
  39525954_b18d_7146_ce6a_0e8b51196765["magic-string"]
  95a0be6b_4d0f_5c1a_879f_f3e1d3321465 --> 39525954_b18d_7146_ce6a_0e8b51196765
  c9346cac_54e3_f6ca_68a7_03c6e82c9609["compileScript.ts"]
  c9346cac_54e3_f6ca_68a7_03c6e82c9609 --> 95a0be6b_4d0f_5c1a_879f_f3e1d3321465
  style 95a0be6b_4d0f_5c1a_879f_f3e1d3321465 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { parse, ParserPlugin } from '@babel/parser'
import MagicString from 'magic-string'

const defaultExportRE = /((?:^|\n|;)\s*)export(\s*)default/
const namedDefaultExportRE = /((?:^|\n|;)\s*)export(.+)(?:as)?(\s*)default/s
const exportDefaultClassRE =
  /((?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/

/**
 * Utility for rewriting `export default` in a script block into a variable
 * declaration so that we can inject things into it
 */
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 (
// ... (62 more lines)

Domain

Subdomains

Dependencies

  • magic-string
  • parser

Frequently Asked Questions

What does rewriteDefault.ts do?
rewriteDefault.ts is a source file in the vue codebase, written in typescript. It belongs to the SfcCompiler domain, ScriptCompiler subdomain.
What functions are defined in rewriteDefault.ts?
rewriteDefault.ts defines 3 function(s): hasDefaultExport, rewriteDefault, specifierEnd.
What does rewriteDefault.ts depend on?
rewriteDefault.ts imports 2 module(s): magic-string, parser.
What files import rewriteDefault.ts?
rewriteDefault.ts is imported by 1 file(s): compileScript.ts.
Where is rewriteDefault.ts in the architecture?
rewriteDefault.ts is located at packages/compiler-sfc/src/rewriteDefault.ts (domain: SfcCompiler, subdomain: ScriptCompiler, directory: packages/compiler-sfc/src).

Analyze Your Own Codebase

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

Try Supermodel Free