Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Commit

Permalink
fix: vue template tsx support
Browse files Browse the repository at this point in the history
  • Loading branch information
zoy-l committed Apr 6, 2021
1 parent 62fc94e commit 8590a3d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
4 changes: 1 addition & 3 deletions examples/normal-react/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ function App() {
<img src={logo} className="App-logo" alt="logo" />
<p>Hello zmi + React!</p>
<p>
<button onClick={() => setCount((count) => count + 1)}>
count is: {count}
</button>
<button onClick={() => setCount((count) => count + 1)}>count is: {count}</button>
</p>
<p>
Edit <code>App.jsx</code> and save to test HMR updates.
Expand Down
11 changes: 4 additions & 7 deletions examples/normal-vue/src/home.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<template>
<div @click="hello" class="div">
{{ msg }}
</div>
</template>

<script lang="ts">
<script lang="tsx">
import { defineComponent } from 'vue'
export default defineComponent({
Expand All @@ -18,6 +12,9 @@ export default defineComponent({
hello(num: number) {
console.log(num)
}
},
render() {
return <div onClick={() => this.hello(123)}>123</div>
}
})
</script>
Expand Down
37 changes: 31 additions & 6 deletions packages/zmi-webpack/src/applyLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,39 @@ async function applyLoader(options: IPenetrateOptions) {
.options({ hotReload: hot, prettify: false })

if (isTypescript) {
WConifg.module
.rule('vue-ts')
.test(/\.(ts|tsx)$/)
.use('ts-loader')
.loader(require.resolve('ts-loader'))
.options({
const tsRule = WConifg.module.rule('ts').test(/\.ts$/)
const tsxRule = WConifg.module.rule('tsx').test(/\.tsx$/)

const addLoader = ({
name,
loader,
options
}: {
name: string
loader: string
options: Record<string, any>
}) => {
tsRule.use(name).loader(loader).options(options)
tsxRule.use(name).loader(loader).options(options)
}

addLoader({
name: 'ts-loader',
loader: require.resolve('ts-loader'),
options: {
transpileOnly: true,
appendTsSuffixTo: ['\\.vue$']
}
})
// make sure to append TSX suffix
tsxRule
.use('ts-loader')
.loader(require.resolve('ts-loader'))
.tap((options) => {
options = { ...options }
delete options.appendTsSuffixTo
options.appendTsxSuffixTo = ['\\.vue$']
return options
})
}
})
Expand Down

0 comments on commit 8590a3d

Please sign in to comment.