Skip to content

Commit a80cf18

Browse files
committed
fix: typescript caching problems
1 parent fbc25a0 commit a80cf18

File tree

2 files changed

+57
-23
lines changed

2 files changed

+57
-23
lines changed

packages/@vue/cli-plugin-typescript/generator/template/tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"target": "<%- options.useTsWithBabel ? 'es2015' : 'es5' %>",
44
"module": "es2015",
55
"strict": true,
6+
"jsx": "preserve",
67
"moduleResolution": "node",
78
<%_ if (options.classComponent) { _%>
89
"experimentalDecorators": true,
@@ -13,6 +14,7 @@
1314
"baseUrl": ".",
1415
<%_if (isTest) { _%>
1516
"types": [
17+
"node",
1618
<%_ if (hasMocha) { _%>
1719
"mocha",
1820
"chai"

packages/@vue/cli-plugin-typescript/index.js

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,75 @@ module.exports = (api, options) => {
1717
.add(api.resolve('src'))
1818
.add(api.resolve('test'))
1919
.end()
20-
21-
if (options.experimentalCompileTsWithBabel) {
22-
// Experimental: compile TS with babel so that it can leverage
23-
// preset-env for auto-detected polyfills based on browserslists config.
24-
// this is pending on the readiness of @babel/preset-typescript.
25-
tsRule
2620
.use('cache-loader')
2721
.loader('cache-loader')
2822
.options({ cacheDirectory })
2923
.end()
30-
.use('babel-loader')
31-
.loader('babel-loader')
3224

33-
config.module
34-
.rule('vue')
35-
.use('vue-loader')
36-
.tap(options => {
37-
options.loaders.ts = 'babel-loader'
38-
return options
39-
})
40-
} else {
25+
const vueLoader = config.module
26+
.rule('vue')
27+
.use('vue-loader')
28+
.tap(options => {
29+
options.loaders.ts = [
30+
{
31+
loader: 'cache-loader',
32+
options: { cacheDirectory }
33+
}
34+
]
35+
return options
36+
})
37+
38+
if (!options.experimentalCompileTsWithBabel) {
39+
const tsLoaderOptions = {
40+
transpileOnly: true,
41+
appendTsSuffixTo: [/\.vue$/]
42+
}
43+
44+
// if has babel plugin, inject babel-loader before ts-loader
4145
if (api.hasPlugin('babel')) {
4246
tsRule
43-
.use('cache-loader')
44-
.loader('cache-loader')
45-
.options({ cacheDirectory })
46-
.end()
4747
.use('babel-loader')
4848
.loader('babel-loader')
49+
vueLoader
50+
.tap(options => {
51+
if (api.hasPlugin('babel')) {
52+
options.loaders.ts.push({
53+
loader: 'babel-loader'
54+
})
55+
}
56+
return options
57+
})
4958
}
59+
60+
// apply ts-loader
5061
tsRule
5162
.use('ts-loader')
5263
.loader('ts-loader')
53-
.options({
54-
transpileOnly: true,
55-
appendTsSuffixTo: [/\.vue$/]
64+
.options(tsLoaderOptions)
65+
vueLoader
66+
.tap(options => {
67+
options.loaders.ts.push({
68+
loader: 'ts-loader',
69+
options: tsLoaderOptions
5670
})
71+
return options
72+
})
73+
} else {
74+
// Experimental: compile TS with babel so that it can leverage
75+
// preset-env for auto-detected polyfills based on browserslists config.
76+
// this is pending on the readiness of @babel/preset-typescript.
77+
tsRule
78+
.use('babel-loader')
79+
.loader('babel-loader')
80+
vueLoader
81+
.tap(options => {
82+
options.loaders.ts.push(
83+
{
84+
loader: 'babel-loader'
85+
}
86+
)
87+
return options
88+
})
5789
}
5890

5991
config

0 commit comments

Comments
 (0)