|
1 | 1 | import * as T from '@babel/types' |
2 | | -import template from '@babel/template' |
3 | | -import generator, { GeneratorResult, GeneratorOptions } from '@babel/generator' |
| 2 | +import * as template from '@babel/template' |
| 3 | +import { |
| 4 | + GeneratorResult, |
| 5 | + GeneratorOptions, |
| 6 | + CodeGenerator, |
| 7 | +} from '@babel/generator' |
4 | 8 | import { parse, ParserOptions } from '@babel/parser' |
5 | 9 | import MagicString from 'magic-string' |
6 | 10 |
|
@@ -69,10 +73,10 @@ export function toCode( |
69 | 73 | ), |
70 | 74 | ) |
71 | 75 |
|
72 | | - const result = generator(statement as any, { |
| 76 | + const result = new CodeGenerator(statement as any, { |
73 | 77 | comments: true, |
74 | 78 | ...options, |
75 | | - }) |
| 79 | + }).generate() |
76 | 80 |
|
77 | 81 | if (sourceText == null) return result |
78 | 82 |
|
@@ -218,7 +222,7 @@ export const createExportDeclarationForComponent = memoizeByFirstArg( |
218 | 222 | return T.exportNamedDeclaration(statement) |
219 | 223 |
|
220 | 224 | function createDeclarationForScriptSetup(): any { |
221 | | - const createExpr = template( |
| 225 | + const createExpr = template.statement( |
222 | 226 | `const ${config.exportName} = ${config.defineComponent}( |
223 | 227 | %%props%%, |
224 | 228 | %%emits%%, |
@@ -249,7 +253,7 @@ export const createExportDeclarationForComponent = memoizeByFirstArg( |
249 | 253 | return createExpr({ props, emits, bindings, extra }) |
250 | 254 | } |
251 | 255 | function createDeclarationForScript(): any { |
252 | | - const createExpr = template( |
| 256 | + const createExpr = template.statement( |
253 | 257 | `const ${config.exportName} = ${config.defineComponent}(%%options%%)`, |
254 | 258 | ) |
255 | 259 |
|
@@ -485,7 +489,9 @@ function createExportDeclarationFor( |
485 | 489 | ...options, |
486 | 490 | } |
487 | 491 |
|
488 | | - const createExpr = template(`const ${config.exportName} = %%value%%;`) |
| 492 | + const createExpr = template.statement( |
| 493 | + `const ${config.exportName} = %%value%%;`, |
| 494 | + ) |
489 | 495 | const declaration = createExpr({ |
490 | 496 | value: getExpressionOrReference(findTargetExpression(), config), |
491 | 497 | }) |
|
0 commit comments