Skip to content

Commit c2c0af0

Browse files
authored
feat: support decorators and import-attributes syntax
1 parent 75a7d0c commit c2c0af0

File tree

9 files changed

+406
-233
lines changed

9 files changed

+406
-233
lines changed

packages/core/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@
4545
"vite": "^3.0.0-0 || ^4.0.0-0"
4646
},
4747
"dependencies": {
48-
"@babel/core": "^7.21.3",
48+
"@babel/core": "^7.22.17",
49+
"@babel/plugin-proposal-decorators": "^7.22.15",
50+
"@babel/plugin-syntax-import-attributes": "^7.22.5",
4951
"@babel/plugin-syntax-import-meta": "^7.10.4",
50-
"@babel/plugin-transform-typescript": "^7.21.3",
52+
"@babel/plugin-transform-typescript": "^7.22.15",
5153
"@vue/babel-plugin-jsx": "^1.1.1",
5254
"@vue/compiler-dom": "^3.2.47",
5355
"esno": "^0.16.3",

packages/core/src/compiler/template.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import path from 'path'
1+
import path from 'node:path'
22
import MagicString from 'magic-string'
33
import { parse as vueParse, transform as vueTransform } from '@vue/compiler-dom'
44
import { parse as babelParse, traverse as babelTraverse } from '@babel/core'
55
import vueJsxPlugin from '@vue/babel-plugin-jsx'
66
import typescriptPlugin from '@babel/plugin-transform-typescript'
77
import importMeta from '@babel/plugin-syntax-import-meta'
8-
import { parseJSXIdentifier } from '../utils'
8+
import decoratorsPlugin from '@babel/plugin-proposal-decorators'
9+
import importAttributesPlugin from '@babel/plugin-syntax-import-attributes'
910
import { normalizePath } from 'vite'
1011

1112
const EXCLUDE_TAG = ['template', 'script', 'style']
@@ -62,6 +63,14 @@ export async function compileSFCTemplate(
6263
typescriptPlugin,
6364
{ isTSX: true, allowExtensions: true },
6465
],
66+
[
67+
decoratorsPlugin,
68+
{ legacy: true },
69+
],
70+
[
71+
importAttributesPlugin,
72+
{ deprecatedAssertSyntax: true },
73+
],
6574
],
6675
})
6776

packages/playground/vue2/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"dependencies": {
99
"@vue/composition-api": "^1.7.1",
1010
"vue": "2.7.14",
11+
"vue-property-decorator": "^9.1.2",
1112
"vue-template-compiler": "2.7.14"
1213
},
1314
"devDependencies": {

packages/playground/vue2/src/App.vue

+3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<script lang="ts">
22
import Hi from './Hi.vue'
33
import Welcome from './Welcome'
4+
import Count from './Count.vue'
45
export default {
56
name: 'App',
67
components: {
78
Hi,
89
Welcome,
10+
Count,
911
},
1012
}
1113
</script>
@@ -15,6 +17,7 @@ export default {
1517
<div>
1618
<Hi />
1719
<Welcome />
20+
<Count />
1821
<!-- -->
1922
<!-- -->
2023
<!-- -->
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script lang="ts">
2+
import { Component, Prop, Vue } from 'vue-property-decorator'
3+
4+
@Component
5+
export default class MyComponent extends Vue {
6+
@Prop() readonly message!: string
7+
@Prop({ default: 0 }) count!: number
8+
9+
increment() {
10+
this.count++
11+
}
12+
}
13+
</script>
14+
15+
<template>
16+
<div>
17+
<p>Message: {{ message }}</p>
18+
<p>Count: {{ count }}</p>
19+
<button @click="increment">
20+
Increment
21+
</button>
22+
</div>
23+
</template>
+33-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1-
import { defineComponent } from "@vue/composition-api"
2-
3-
export default defineComponent({
4-
name: "Welcome",
5-
setup() {
6-
return () => <p style="color: #fcb80f;cursor: pointer;"> Welcome to here 🚀 .</p>
7-
},
8-
})
1+
// import { defineComponent } from "@vue/composition-api"
2+
3+
// export default defineComponent({
4+
// name: "Welcome",
5+
// setup() {
6+
// return () => <p style="color: #fcb80f;cursor: pointer;"> Welcome to here 🚀 .</p>
7+
// },
8+
// })
9+
10+
import Vue from 'vue'
11+
import { Component } from 'vue-property-decorator'
12+
13+
@Component
14+
export default class Welcome extends Vue {
15+
private count = 0
16+
17+
private get message(): string {
18+
return `Count: ${this.count}`
19+
}
20+
21+
private increment(): void {
22+
this.count++
23+
}
24+
25+
render() {
26+
return (
27+
<div>
28+
<h1>{this.message}</h1>
29+
<button onClick={this.increment}>Increment</button>
30+
</div>
31+
)
32+
}
33+
}

packages/playground/vue2/tsconfig.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"strict": true,
77
"module": "ESNext",
88
"moduleResolution": "Node",
9-
"jsx": "preserve"
9+
"jsx": "preserve",
10+
"experimentalDecorators": true
1011
},
11-
"include": ["src"],
12-
}
12+
"include": ["src"]
13+
}

packages/playground/vue2/vite.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default defineConfig({
1414
vue: 2,
1515
toggleButtonVisibility: 'always',
1616
enabled: true,
17+
disableInspectorOnEditorOpen: true,
1718
}),
1819
],
1920
})

0 commit comments

Comments
 (0)