rewriteDefault() — vue Function Reference
Architecture documentation for the rewriteDefault() function in rewriteDefault.ts from the vue codebase.
Entity Profile
Dependency Diagram
graph TD 7e881ac8_aa91_c10a_bd6a_7071900a38fa["rewriteDefault()"] af57cd20_c4ce_0877_c02c_52056ca04d4a["compileScript()"] af57cd20_c4ce_0877_c02c_52056ca04d4a -->|calls| 7e881ac8_aa91_c10a_bd6a_7071900a38fa 3b943cf7_6d62_ecf7_226d_a35892b66468["hasDefaultExport()"] 7e881ac8_aa91_c10a_bd6a_7071900a38fa -->|calls| 3b943cf7_6d62_ecf7_226d_a35892b66468 3be7e92f_b701_5270_8d86_c8014f4ae977["specifierEnd()"] 7e881ac8_aa91_c10a_bd6a_7071900a38fa -->|calls| 3be7e92f_b701_5270_8d86_c8014f4ae977 style 7e881ac8_aa91_c10a_bd6a_7071900a38fa 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
Source
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