Skip to content

Commit

Permalink
Merge pull request #333 from MAXLZ1/fix/vue3-lang-tsx-error
Browse files Browse the repository at this point in the history
fix: 修复vue3中使用lang=tsx时的报错问题
  • Loading branch information
ckken authored Dec 25, 2022
2 parents a73e714 + 08a5b66 commit 93e68e9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/plugin-vue-3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ const PluginBabelVue3 = ({wpChain}: ConfigPluginOptions): void => {
.rule('scripts')
.use('babel')
.tap(o => {
// console.log(o)
const typescriptIndex = o.presets.findIndex((plugin: string) => {
return plugin === require.resolve('@babel/preset-typescript')
})
o.presets[typescriptIndex] = [
require.resolve('@babel/preset-typescript'),
{
isTSX: true, // allExtensions依赖isTSX https://babeljs.io/docs/en/babel-preset-typescript#allextensions
allExtensions: true, // 支持所有文件扩展名
},
]

o.plugins.push([require.resolve('@vue/babel-plugin-jsx'), {optimize: true}])
return o
})
Expand Down
1 change: 1 addition & 0 deletions projects/vue-3-base/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

declare module 'vue' {
export interface GlobalComponents {
AButton: typeof import('ant-design-vue/es')['Button']
ATable: typeof import('ant-design-vue/es')['Table']
ButtonComponent: typeof import('./src/components/ButtonComponent.vue')['default']
Count: typeof import('./src/components/Count.vue')['default']
Expand Down
2 changes: 2 additions & 0 deletions projects/vue-3-base/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ButtonComponent />
<JSXComponent />
<Count />
<TsxScript />
</div>
</template>

Expand All @@ -32,6 +33,7 @@ import TableComponent from './components/TableComponent.vue'
import ButtonComponent from './components/ButtonComponent.vue'
import JSXComponent from './components/JSXComponent'
import Count from './components/Count.vue'
import TsxScript from './components/TsxScript.vue'
</script>

<style lang="less">
Expand Down
28 changes: 28 additions & 0 deletions projects/vue-3-base/src/components/TsxScript.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script lang="tsx">
import {defineComponent, ref} from 'vue'
export default defineComponent({
name: 'TsxScript',
setup() {
const count = ref(0)
function add() {
count.value++
}
return () => (
<>
<p>============ lang=tsx component start =============</p>
<AButton type="primary" onClick={add}>add</AButton>
<span class="value">count: {count.value}</span>
<p>============ lang=tsx component end =============</p>
</>
)
}
})
</script>

<style lang="less" scoped>
.value {
margin-left: 10px;
}
</style>

0 comments on commit 93e68e9

Please sign in to comment.