Skip to content

Commit 76dfcad

Browse files
fix: support for class component mixins (#334)
Fix for using class component with mixins and also for importing Vue from vue-property-decorator instead of vue-class-component. Co-authored-by: Evert van der Weit <evert@mett.nl>
1 parent af80ab0 commit 76dfcad

File tree

6 files changed

+67
-4
lines changed

6 files changed

+67
-4
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
<div class="hello">
3+
<h1 data-computed>{{ computedMsg }}</h1>
4+
<h2 data-props>{{ msg }}</h2>
5+
</div>
6+
</template>
7+
8+
<script lang="ts">
9+
import { Vue, Prop } from 'vue-property-decorator'
10+
11+
export default class ClassComponent extends Vue {
12+
dataText: string = 'Hello'
13+
14+
@Prop() msg!: string
15+
16+
get computedMsg(): string {
17+
return `Message: ${this.dataText}`
18+
}
19+
}
20+
</script>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<template>
2+
<div class="hello">
3+
<h1 data-mixin>{{ message }}</h1>
4+
</div>
5+
</template>
6+
7+
<script lang="ts">
8+
import { mixins } from 'vue-class-component'
9+
import ClassMixin from './ClassMixin'
10+
11+
export default class ClassComponent extends mixins(ClassMixin) {}
12+
</script>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Vue } from 'vue-class-component'
2+
3+
export default class ClassMixin extends Vue {
4+
message = 'Hello world!'
5+
}

e2e/__projects__/basic/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@
2020
"jest": "^26.0.0",
2121
"ts-jest": "^26.4.4",
2222
"typescript": "^4.1.2",
23-
"vue-class-component": "^8.0.0-beta.4"
23+
"vue-class-component": "^8.0.0-beta.4",
24+
"vue-property-decorator": "^10.0.0-rc.3"
2425
},
2526
"jest": {
2627
"moduleFileExtensions": [
2728
"js",
2829
"json",
29-
"vue"
30+
"vue",
31+
"ts"
3032
],
3133
"transform": {
34+
"^.+\\.ts$": "ts-jest",
3235
"^.+\\.js$": "babel-jest",
3336
"^.+\\.vue$": "../../../lib/index.js"
3437
},

e2e/__projects__/basic/test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import Pug from './components/Pug.vue'
88
import Coffee from './components/Coffee.vue'
99
import Basic from './components/Basic.vue'
1010
import ClassComponent from './components/ClassComponent.vue'
11+
import ClassComponentWithMixin from './components/ClassComponentWithMixin.vue'
12+
import ClassComponentProperty from './components/ClassComponentProperty.vue'
1113
import TypeScript from './components/TypeScript.vue'
1214
import jestVue from '../../../'
1315
import RenderFunction from './components/RenderFunction.vue'
@@ -141,6 +143,25 @@ test('supports class component .vue files', () => {
141143
})
142144
})
143145

146+
test('supports class component .vue files with mixins', () => {
147+
expect.assertions(1)
148+
mount(ClassComponentWithMixin)
149+
expect(document.querySelector('[data-mixin]').textContent).toBe(
150+
'Hello world!'
151+
)
152+
})
153+
154+
test('supports class component .vue files using vue-property-decorator', () => {
155+
expect.assertions(2)
156+
mount(ClassComponentProperty, { msg: 'Props Message' })
157+
expect(document.querySelector('[data-computed]').textContent).toBe(
158+
'Message: Hello'
159+
)
160+
expect(document.querySelector('[data-props]').textContent).toBe(
161+
'Props Message'
162+
)
163+
})
164+
144165
// TODO: How do functional components work in Vue 3?
145166
xtest('processes functional components', () => {
146167
// const clickSpy = jest.fn()

lib/generate-code.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ module.exports = function generateCode(
2929
var tempOutput = node.toString()
3030

3131
if (
32-
tempOutput.match(/\}\(.*.?Vue\);/) &&
33-
tempOutput.includes('vue-class-component')
32+
// vue-property-decorator also exports Vue, which can be used to create a class component.
33+
// In that case vue-class-component is not present in the tempOutput.
34+
tempOutput.includes('vue-class-component') ||
35+
tempOutput.includes('vue-property-decorator')
3436
) {
3537
node.add(`
3638
;exports.default = {

0 commit comments

Comments
 (0)