MCPcopy
hub / github.com/vuejs/core / rewriteDefaultAST

Function rewriteDefaultAST

packages/compiler-sfc/src/rewriteDefault.ts:27–90  ·  view source on GitHub ↗
(
  ast: Statement[],
  s: MagicString,
  as: string,
)

Source from the content-addressed store, hash-verified

25 * declaration so that we can inject things into it
26 */
27export function rewriteDefaultAST(
28 ast: Statement[],
29 s: MagicString,
30 as: string,
31): void {
32 if (!hasDefaultExport(ast)) {
33 s.append(`\nconst ${as} = {}`)
34 return
35 }
36
37 // if the script somehow still contains `default export`, it probably has
38 // multi-line comments or template strings. fallback to a full parse.
39 ast.forEach(node => {
40 if (node.type === 'ExportDefaultDeclaration') {
41 if (node.declaration.type === 'ClassDeclaration' && node.declaration.id) {
42 const start: number =
43 node.declaration.decorators && node.declaration.decorators.length > 0
44 ? node.declaration.decorators[
45 node.declaration.decorators.length - 1
46 ].end!
47 : node.start!
48 s.overwrite(start, node.declaration.id.start!, ` class `)
49 s.append(`\nconst ${as} = ${node.declaration.id.name}`)
50 } else {
51 s.overwrite(node.start!, node.declaration.start!, `const ${as} = `)
52 }
53 } else if (node.type === 'ExportNamedDeclaration') {
54 for (const specifier of node.specifiers) {
55 if (
56 specifier.type === 'ExportSpecifier' &&
57 specifier.exported.type === 'Identifier' &&
58 specifier.exported.name === 'default'
59 ) {
60 if (node.source) {
61 if (specifier.local.name === 'default') {
62 s.prepend(
63 `import { default as __VUE_DEFAULT__ } from '${node.source.value}'\n`,
64 )
65 const end = specifierEnd(s, specifier.local.end!, node.end!)
66 s.remove(specifier.start!, end)
67 s.append(`\nconst ${as} = __VUE_DEFAULT__`)
68 continue
69 } else {
70 s.prepend(
71 `import { ${s.slice(
72 specifier.local.start!,
73 specifier.local.end!,
74 )} as __VUE_DEFAULT__ } from '${node.source.value}'\n`,
75 )
76 const end = specifierEnd(s, specifier.exported.end!, node.end!)
77 s.remove(specifier.start!, end)
78 s.append(`\nconst ${as} = __VUE_DEFAULT__`)
79 continue
80 }
81 }
82
83 const end = specifierEnd(s, specifier.end!, node.end!)
84 s.remove(specifier.start!, end)

Callers 2

processNormalScriptFunction · 0.90
rewriteDefaultFunction · 0.85

Calls 4

hasDefaultExportFunction · 0.85
specifierEndFunction · 0.85
forEachMethod · 0.80
removeMethod · 0.80

Tested by

no test coverage detected