Skip to content

Commit 767f072

Browse files
authored
Fix an error during collecting data in case of using Babel (#32)
* should inherit component class and vue to pass babel's instance type check * add test cases for babel
2 parents 0a11308 + 02124f8 commit 767f072

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

Diff for: package.json

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
"devDependencies": {
3636
"@types/chai": "^3.4.34",
3737
"@types/mocha": "^2.2.32",
38+
"babel-core": "^6.18.2",
39+
"babel-loader": "^6.2.7",
40+
"babel-plugin-transform-class-properties": "^6.18.0",
41+
"babel-plugin-transform-decorators-legacy": "^1.3.4",
42+
"babel-preset-es2015": "^6.18.0",
3843
"chai": "^3.5.0",
3944
"mocha": "^3.1.2",
4045
"node-libs-browser": "^1.0.0",

Diff for: src/component.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ export function componentFactory (
6565

6666
// find super
6767
const superProto = Object.getPrototypeOf(Component.prototype)
68-
const Super: VueClass = superProto instanceof Vue
69-
? superProto.constructor as VueClass
70-
: Vue
71-
return Super.extend(options)
68+
if (!(superProto instanceof Vue)) {
69+
Component.prototype = Object.create(Vue.prototype)
70+
Component.prototype.constructor = Component
71+
Object.keys(Vue).forEach(key => {
72+
Component[key] = Vue[key]
73+
})
74+
}
75+
return Component.extend(options)
7276
}

Diff for: test/.babelrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"presets": [
3+
["es2015", {"modules": false}]
4+
],
5+
"plugins": [
6+
"transform-decorators-legacy",
7+
"transform-class-properties"
8+
]
9+
}

Diff for: test/test-babel.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Component from '../lib/index'
2+
import { expect } from 'chai'
3+
import Vue from 'vue'
4+
5+
describe('vue-class-component with Babel', () => {
6+
it('should be instantiated without any errors', () => {
7+
@Component
8+
class MyComp {}
9+
expect(() => new MyComp()).to.not.throw(Error)
10+
})
11+
12+
it('should collect class properties as data', () => {
13+
@Component
14+
class MyComp {
15+
foo = 'hello'
16+
}
17+
const c = new MyComp()
18+
expect(c.foo).to.equal('hello')
19+
})
20+
})

Diff for: test/webpack.config.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module.exports = {
2-
entry: './test/test.ts',
2+
entry: [
3+
'./test/test.ts',
4+
'./test/test-babel.js'
5+
],
36
output: {
47
path: './test',
58
filename: 'test.build.js'
@@ -10,6 +13,11 @@ module.exports = {
1013
test: /\.ts$/,
1114
exclude: /node_modules|vue\/src/,
1215
loader: 'ts'
16+
},
17+
{
18+
test: /\.js$/,
19+
exclude: /node_modules|vue\/src/,
20+
loader: 'babel'
1321
}
1422
]
1523
}

0 commit comments

Comments
 (0)