Skip to content

Commit 3467128

Browse files
committed
fix: use babel to compile TS
1 parent 48d41d8 commit 3467128

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

lib/compilers/typescript-compiler.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const ensureRequire = require('../ensure-require')
21
const tsconfig = require('tsconfig')
2+
const ensureRequire = require('../ensure-require')
3+
const compileBabel = require('./babel-compiler')
34
const cache = require('../cache')
45
const logger = require('../logger')
56

@@ -10,7 +11,7 @@ const defaultTypescriptConfig = {
1011
'dom',
1112
'es6'
1213
],
13-
'module': 'commonjs',
14+
'module': 'es2015',
1415
'moduleResolution': 'node',
1516
'types': ['vue-typescript-import-dts', 'jest', 'node'],
1617
'isolatedModules': false,
@@ -28,20 +29,20 @@ const defaultTypescriptConfig = {
2829
}
2930

3031
function getTypescriptConfig () {
31-
// const cachedConfig = cache.get('typescript-config')
32-
// if (cachedConfig) {
33-
// return cachedConfig
34-
// } else {
35-
const { config } = tsconfig.loadSync(process.cwd())
32+
const cachedConfig = cache.get('typescript-config')
33+
if (cachedConfig) {
34+
return cachedConfig
35+
} else {
36+
const { config } = tsconfig.loadSync(process.cwd())
3637

37-
if (!config) {
38-
logger.info('no tsconfig.json found, defaulting to default typescript options')
39-
}
38+
if (!config) {
39+
logger.info('no tsconfig.json found, defaulting to default typescript options')
40+
}
4041

41-
const typescriptConfig = config || defaultTypescriptConfig
42-
cache.set('typescript-config', typescriptConfig)
43-
return typescriptConfig
44-
// }
42+
const typescriptConfig = config || defaultTypescriptConfig
43+
cache.set('typescript-config', typescriptConfig)
44+
return typescriptConfig
45+
}
4546
}
4647

4748
module.exports = function compileTypescript (scriptContent) {
@@ -51,5 +52,5 @@ module.exports = function compileTypescript (scriptContent) {
5152

5253
const res = typescript.transpileModule(scriptContent, tsConfig)
5354

54-
return { code: res.outputText, sourceMap: res.sourceMapText }
55+
return compileBabel(res.outputText, JSON.parse(res.sourceMapText))
5556
}

test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ cp package.json temp-dir/package.json
88
cp .babelrc temp-dir/.babelrc
99
cp tsconfig.json temp-dir/tsconfig.json
1010
cd temp-dir
11-
yarn unit:run
11+
yarn unit:run

test/resources/TypeScript.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
</template>
44

55
<script lang="ts">
6+
import TypeScriptChild from './TypeScriptChild.vue'
7+
68
export default {
79
computed: {
810
exclamationMarks(): string {
911
return 'string'
1012
}
13+
},
14+
components: {
15+
TypeScriptChild
1116
}
1217
};
1318
</script>

test/resources/TypeScriptChild.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>

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"dom",
66
"es6"
77
],
8-
"module": "commonjs",
8+
"module": "es2015",
99
"moduleResolution": "node",
1010
"types": ["vue-typescript-import-dts", "jest", "node"],
1111
"isolatedModules": false,

0 commit comments

Comments
 (0)