Skip to content

Commit 4354570

Browse files
committed
feat(typescript): add ts support
1 parent 221b1e0 commit 4354570

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

jest-vue.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,39 @@ const babel = require('babel-core')
33
const compileTemplate = require('./lib/template-compiler')
44
const generateSourceMap = require('./lib/generate-source-map')
55
const addTemplateMapping = require('./lib/add-template-mapping')
6+
const convertSourceMap = require('convert-source-map')
7+
const typescript = require('typescript')
68

7-
const convert = require('convert-source-map')
89
const splitRE = /\r?\n/g
910

11+
function compileBabel (scriptContent) {
12+
return babel.transform(scriptContent, {
13+
sourceMaps: true,
14+
presets: ['es2015'],
15+
plugins: ['transform-runtime']
16+
})
17+
}
18+
19+
function compileTypescript (scriptContent) {
20+
return {
21+
code: typescript.transpile(scriptContent)
22+
}
23+
}
24+
25+
function processScript (scriptPart) {
26+
if (scriptPart.lang === 'typescript' || scriptPart.lang === 'ts') {
27+
return compileTypescript(scriptPart.content)
28+
}
29+
30+
return compileBabel(scriptPart.content)
31+
}
32+
1033
module.exports = {
1134
process (src, path) {
1235
var parts = vueCompiler.parseComponent(src, { pad: true })
1336
const renderFunctions = compileTemplate(parts.template.content)
1437

15-
const result = babel.transform(parts.script.content, {
16-
sourceMaps: true,
17-
presets: ['es2015'],
18-
plugins: ['transform-runtime']
19-
})
38+
const result = processScript(parts.script)
2039

2140
const script = result.code
2241

@@ -38,7 +57,7 @@ module.exports = {
3857
}
3958

4059
if (map) {
41-
output += '\n' + convert.fromJSON(map.toString()).toComment()
60+
output += '\n' + convertSourceMap.fromJSON(map.toString()).toComment()
4261
}
4362

4463
return output

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"chalk": "^2.1.0",
5050
"convert-source-map": "^1.5.0",
5151
"object-assign": "^4.1.1",
52-
"source-map": "^0.5.6"
52+
"source-map": "^0.5.6",
53+
"typescript": "^2.5.2"
5354
}
5455
}

test/TypeScript.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Vue from 'vue'
2+
import TypeScript from './resources/TypeScript.vue'
3+
import jestVue from '../jest-vue'
4+
import { resolve } from 'path'
5+
import { readFileSync } from 'fs'
6+
7+
test('processes .vue files', () => {
8+
const vm = new Vue(TypeScript).$mount()
9+
expect(typeof vm.$el).toBe('object')
10+
})
11+
12+
test.skip('generates inline sourcemap', () => {
13+
const expectedMap = '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIkJhc2ljLnZ1ZSJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7QUFBQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7OztBQUdBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBSEE7QUFLQTtBQVBBO0FBU0E7QUFDQTtBQUNBO0FBQ0E7QUFGQTtBQUlBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFIQTtBQWpCQTs7OztBQVBBO0FBQUEiLCJzb3VyY2VzQ29udGVudCI6WyI8dGVtcGxhdGU+XG4gICAgPGRpdiBjbGFzcz1cImhlbGxvXCI+XG4gICAgICAgIDxoMSA6Y2xhc3M9XCJoZWFkaW5nQ2xhc3Nlc1wiPnt7IG1zZyB9fTwvaDE+XG4gICAgPC9kaXY+XG48L3RlbXBsYXRlPlxuXG48c2NyaXB0PlxuICAgIGV4cG9ydCBkZWZhdWx0IHtcbiAgICAgICAgbmFtZTogJ2Jhc2ljJyxcbiAgICAgICAgY29tcHV0ZWQ6IHtcbiAgICAgICAgICAgIGhlYWRpbmdDbGFzc2VzOiBmdW5jdGlvbiBoZWFkaW5nQ2xhc3NlcygpIHtcbiAgICAgICAgICAgICAgICByZXR1cm4ge1xuICAgICAgICAgICAgICAgICAgICByZWQ6IHRoaXMuaXNDcmF6eSxcbiAgICAgICAgICAgICAgICAgICAgYmx1ZTogIXRoaXMuaXNDcmF6eSxcbiAgICAgICAgICAgICAgICAgICAgc2hhZG93OiB0aGlzLmlzQ3JhenksXG4gICAgICAgICAgICAgICAgfTtcbiAgICAgICAgICAgIH0sXG4gICAgICAgIH0sXG4gICAgICAgIGRhdGE6IGZ1bmN0aW9uIGRhdGEoKSB7XG4gICAgICAgICAgICByZXR1cm4ge1xuICAgICAgICAgICAgICAgIG1zZzogJ1dlbGNvbWUgdG8gWW91ciBWdWUuanMgQXBwJyxcbiAgICAgICAgICAgICAgICBpc0NyYXp5OiBmYWxzZSxcbiAgICAgICAgICAgIH07XG4gICAgICAgIH0sXG4gICAgICAgIG1ldGhvZHM6IHtcbiAgICAgICAgICAgIHRvZ2dsZUNsYXNzOiBmdW5jdGlvbiB0b2dnbGVDbGFzcygpIHtcbiAgICAgICAgICAgICAgICB0aGlzLmlzQ3JhenkgPSAhdGhpcy5pc0NyYXp5O1xuICAgICAgICAgICAgfSxcbiAgICAgICAgfSxcbiAgICB9O1xuPC9zY3JpcHQ+XG4iXX0='
14+
const filePath = resolve(__dirname, './resources/TypeScript.vue')
15+
const fileString = readFileSync(filePath, { encoding: 'utf8' })
16+
const output = jestVue.process(fileString, filePath)
17+
expect(output).toContain(expectedMap)
18+
})

test/resources/TypeScript.vue

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<div></div>
3+
</template>
4+
5+
<script lang="ts">
6+
export default {
7+
computed: {
8+
exclamationMarks(): string {
9+
return 'string'
10+
}
11+
}
12+
};
13+
</script>

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,6 +2885,10 @@ typedarray@^0.0.6:
28852885
version "0.0.6"
28862886
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
28872887

2888+
typescript@^2.5.2:
2889+
version "2.5.2"
2890+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.5.2.tgz#038a95f7d9bbb420b1bf35ba31d4c5c1dd3ffe34"
2891+
28882892
uglify-js@^2.6:
28892893
version "2.8.29"
28902894
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.8.29.tgz#29c5733148057bb4e1f75df35b7a9cb72e6a59dd"

0 commit comments

Comments
 (0)