Skip to content

Commit 7683aa7

Browse files
authored
refactor: remove @babel/generator and simplify codegen for script setup (#6)
1 parent 48f8b30 commit 7683aa7

File tree

5 files changed

+44
-146
lines changed

5 files changed

+44
-146
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,11 @@
4747
"vue": "^3.5.13"
4848
},
4949
"dependencies": {
50-
"@babel/generator": "^7.27.0",
5150
"@babel/parser": "^7.27.0"
5251
},
5352
"devDependencies": {
5453
"@antfu/eslint-config": "4.11.0",
5554
"@babel/types": "^7.27.0",
56-
"@types/babel__generator": "^7.27.0",
5755
"@types/node": "^22.14.0",
5856
"@vitest/coverage-v8": "3.1.1",
5957
"@vue/compiler-dom": "^3.5.13",

pnpm-lock.yaml

Lines changed: 1 addition & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/script-setup.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { ArrayExpression, CallExpression, Expression, ExpressionStatement, Identifier, Node, ObjectExpression, ObjectProperty, StringLiteral } from '@babel/types'
1+
import type { ArrayExpression, CallExpression, Expression, Identifier, Node, ObjectExpression, ObjectProperty, StringLiteral } from '@babel/types'
22
import type { SFCDescriptor, SFCScriptBlock, SimpleTypeResolveContext } from 'vue/compiler-sfc'
33

4-
import { generate } from '@babel/generator'
54
import { parse } from '@babel/parser'
65

76
interface Context {
@@ -97,19 +96,8 @@ function processDefineProps(node: Expression, context: Context): string | undefi
9796

9897
context.ctx.propsTypeDecl = propsTypeDecl
9998
const propsStr = context.utils.extractRuntimeProps(context.ctx)
100-
const propsAst = parse(`${DEFINE_PROPS}(${propsStr})`, {
101-
sourceType: 'module',
102-
plugins: ['typescript'],
103-
})
104-
105-
node.typeArguments = undefined
106-
node.typeParameters = undefined
107-
node.arguments = (
108-
(propsAst.program.body[0] as ExpressionStatement)
109-
.expression as CallExpression
110-
).arguments
11199

112-
return generate(node).code
100+
return `${DEFINE_PROPS}(${propsStr})`
113101
}
114102
function processDefineEmits(node: Expression, context: Context): string | undefined {
115103
if (!isCallOf(node, DEFINE_EMITS)) {
@@ -136,17 +124,7 @@ function processDefineEmits(node: Expression, context: Context): string | undefi
136124
context.ctx.emitsTypeDecl = emitsTypeDecl
137125
const emits = context.utils.extractRuntimeEmits(context.ctx)
138126

139-
node.typeArguments = undefined
140-
node.typeParameters = undefined
141-
node.arguments[0] = {
142-
type: 'ArrayExpression',
143-
elements: [...emits].map(emit => ({
144-
type: 'StringLiteral',
145-
value: emit,
146-
})),
147-
}
148-
149-
return generate(node).code
127+
return `defineEmits([${[...emits].map(emit => `"${emit}"`).join(', ')}])`
150128
}
151129
function processWithDefaults(node: Expression, context: Context): string | undefined {
152130
if (!isCallOf(node, WITH_DEFAULTS)) {
@@ -234,7 +212,16 @@ function processDefineModel(node: Expression, context: Context): string | undefi
234212
? [modelNameDecl, modelCodegenDecl]
235213
: [modelCodegenDecl]
236214

237-
return generate(node).code
215+
const codegenArgs: string[] = []
216+
if (modelNameDecl) {
217+
codegenArgs.push(`"${modelNameDecl.value}"`)
218+
}
219+
220+
const codegenType = model.length === 1 ? model[0] : `[${model.join(',')}]`
221+
const codegenExtra = modelRuntimeDecl ? `...${context.ctx.getString(modelRuntimeDecl)}` : ''
222+
codegenArgs.push(`{ ${[`type: ${codegenType}`, codegenExtra].filter(s => !!s).join(', ')} }`)
223+
224+
return `${DEFINE_MODEL}(${codegenArgs.join(', ')})`
238225
}
239226

240227
function getDefineModelRuntimeDecl(node: CallExpression, context: Context): [StringLiteral | undefined, ObjectExpression | undefined] {

test/mkdist.test.ts

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ describe('transform typescript script setup', () => {
1818
).toMatchInlineSnapshot(`
1919
"<script setup>
2020
defineProps({
21-
msg: {
22-
type: String,
23-
required: true
24-
}
21+
msg: { type: String, required: true }
2522
});
2623
</script>
2724
"
@@ -33,10 +30,7 @@ describe('transform typescript script setup', () => {
3330
).toMatchInlineSnapshot(`
3431
"<script setup>
3532
const props = defineProps({
36-
msg: {
37-
type: String,
38-
required: true
39-
}
33+
msg: { type: String, required: true }
4034
});
4135
</script>
4236
"
@@ -48,10 +42,7 @@ describe('transform typescript script setup', () => {
4842
).toMatchInlineSnapshot(`
4943
"<script setup>
5044
const { msg } = defineProps({
51-
msg: {
52-
type: String,
53-
required: true
54-
}
45+
msg: { type: String, required: true }
5546
});
5647
</script>
5748
"
@@ -63,10 +54,7 @@ describe('transform typescript script setup', () => {
6354
).toMatchInlineSnapshot(`
6455
"<script setup>
6556
const { msg = "hello" } = defineProps({
66-
msg: {
67-
type: String,
68-
required: false
69-
}
57+
msg: { type: String, required: false }
7058
});
7159
</script>
7260
"
@@ -81,11 +69,7 @@ describe('transform typescript script setup', () => {
8169
).toMatchInlineSnapshot(`
8270
"<script setup>
8371
const props = defineProps({
84-
msg: {
85-
type: String,
86-
required: false,
87-
default: "hi"
88-
}
72+
msg: { type: String, required: false, default: "hi" }
8973
});
9074
</script>
9175
"
@@ -97,11 +81,7 @@ describe('transform typescript script setup', () => {
9781
).toMatchInlineSnapshot(`
9882
"<script setup>
9983
defineProps({
100-
msg: {
101-
type: String,
102-
required: false,
103-
default: "hi"
104-
}
84+
msg: { type: String, required: false, default: "hi" }
10585
});
10686
</script>
10787
"
@@ -168,19 +148,15 @@ describe('transform typescript script setup', () => {
168148
),
169149
).toMatchInlineSnapshot(`
170150
"<script setup>
171-
const model = defineModel({
172-
"type": String
173-
});
151+
const model = defineModel({ type: String });
174152
</script>
175153
"
176154
`)
177155
expect(
178156
await fixture(`<script setup lang="ts">defineModel<string>()</script>`),
179157
).toMatchInlineSnapshot(`
180158
"<script setup>
181-
defineModel({
182-
"type": String
183-
});
159+
defineModel({ type: String });
184160
</script>
185161
"
186162
`)
@@ -190,9 +166,7 @@ describe('transform typescript script setup', () => {
190166
),
191167
).toMatchInlineSnapshot(`
192168
"<script setup>
193-
defineModel("msg", {
194-
"type": String
195-
});
169+
defineModel("msg", { type: String });
196170
</script>
197171
"
198172
`)
@@ -202,12 +176,7 @@ describe('transform typescript script setup', () => {
202176
),
203177
).toMatchInlineSnapshot(`
204178
"<script setup>
205-
defineModel({
206-
"type": String,
207-
...{
208-
required: true
209-
}
210-
});
179+
defineModel({ type: String, ...{ required: true } });
211180
</script>
212181
"
213182
`)
@@ -217,12 +186,7 @@ describe('transform typescript script setup', () => {
217186
),
218187
).toMatchInlineSnapshot(`
219188
"<script setup>
220-
defineModel("msg", {
221-
"type": String,
222-
...{
223-
required: true
224-
}
225-
});
189+
defineModel("msg", { type: String, ...{ required: true } });
226190
</script>
227191
"
228192
`)

0 commit comments

Comments
 (0)