Skip to content

Commit 5b19826

Browse files
committed
feat: use Babel w/ TS for polyfills
1 parent e4dcc2f commit 5b19826

File tree

4 files changed

+71
-39
lines changed

4 files changed

+71
-39
lines changed

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

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = (api, {
22
classComponent,
33
lint,
44
lintOn = [],
5+
useTsWithBabel,
56
experimentalCompileTsWithBabel
67
}) => {
78
if (classComponent) {
@@ -13,6 +14,52 @@ module.exports = (api, {
1314
})
1415
}
1516

17+
if (useTsWithBabel) {
18+
api.extendPackage({
19+
devDependencies: {
20+
'babel-loader': '8 || ^8.0.0-beta || ^8.0.0-rc',
21+
'@babel/core': '7 || ^7.0.0-beta || ^7.0.0-rc',
22+
'@vue/babel-preset-app': '^3.0.0-alpha.1'
23+
},
24+
vue: {
25+
useTsWithBabel: true
26+
},
27+
babel: {
28+
presets: ['@vue/app']
29+
}
30+
})
31+
} else if (experimentalCompileTsWithBabel) {
32+
api.extendPackage({
33+
devDependencies: {
34+
'babel-loader': '8 || ^8.0.0-beta || ^8.0.0-rc',
35+
'@babel/core': '7 || ^7.0.0-beta || ^7.0.0-rc',
36+
'@babel/preset-typescript': '7 || ^7.0.0-beta || ^7.0.0-rc',
37+
'@vue/babel-preset-app': '^3.0.0-alpha.1'
38+
},
39+
vue: {
40+
experimentalCompileTsWithBabel: true
41+
},
42+
babel: {
43+
presets: ['@babel/typescript', '@vue/app']
44+
}
45+
})
46+
47+
if (classComponent) {
48+
api.extendPackage({
49+
devDependencies: {
50+
'@babel/plugin-proposal-decorators': '7 || ^7.0.0-beta || ^7.0.0-rc',
51+
'@babel/plugin-proposal-class-properties': '7 || ^7.0.0-beta || ^7.0.0-rc'
52+
},
53+
babel: {
54+
plugins: [
55+
'@babel/proposal-decorators',
56+
['@babel/proposal-class-properties', { 'loose': true }]
57+
]
58+
}
59+
})
60+
}
61+
}
62+
1663
if (lint) {
1764
api.extendPackage({
1865
scripts: {
@@ -68,8 +115,9 @@ module.exports = (api, {
68115
// TODO cater to e2e test plugins
69116

70117
api.render('./template', {
71-
hasJest,
72-
hasMocha
118+
isTest: process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG,
119+
hasMocha,
120+
hasJest
73121
})
74122

75123
// delete all js files that have a ts file of the same name
@@ -87,36 +135,4 @@ module.exports = (api, {
87135
}
88136
}
89137
})
90-
91-
if (experimentalCompileTsWithBabel) {
92-
api.extendPackage({
93-
devDependencies: {
94-
'babel-loader': '8 || ^8.0.0-beta || ^8.0.0-rc',
95-
'@babel/core': '7 || ^7.0.0-beta || ^7.0.0-rc',
96-
'@babel/preset-typescript': '7 || ^7.0.0-beta || ^7.0.0-rc',
97-
'@vue/babel-preset-app': '^3.0.0-alpha.1'
98-
},
99-
vue: {
100-
experimentalCompileTsWithBabel: true
101-
},
102-
babel: {
103-
presets: ['@babel/typescript', '@vue/app']
104-
}
105-
})
106-
107-
if (classComponent) {
108-
api.extendPackage({
109-
devDependencies: {
110-
'@babel/plugin-proposal-decorators': '7 || ^7.0.0-beta || ^7.0.0-rc',
111-
'@babel/plugin-proposal-class-properties': '7 || ^7.0.0-beta || ^7.0.0-rc'
112-
},
113-
babel: {
114-
plugins: [
115-
'@babel/proposal-decorators',
116-
['@babel/proposal-class-properties', { 'loose': true }]
117-
]
118-
}
119-
})
120-
}
121-
}
122138
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es5",
3+
"target": "<%- options.useTsWithBabel ? 'es2015' : 'es5' %>",
44
"module": "es2015",
55
"strict": true,
66
"moduleResolution": "node",
@@ -15,14 +15,16 @@
1515
"src/*"
1616
]
1717
},
18+
<%_if (isTest) { _%>
1819
"types": [
19-
<%_ if (hasMocha) { _%>
20+
<%_ if (hasMocha) { _%>
2021
"mocha",
2122
"chai"
22-
<%_ } else if (hasJest) { _%>
23+
<%_ } else if (hasJest) { _%>
2324
"jest"
24-
<%_ } _%>
25+
<%_ } _%>
2526
]
27+
<%_ } _%>
2628
},
2729
"include": [
2830
"src/**/*.ts",

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ module.exports = (api, options) => {
3232
return options
3333
})
3434
} else {
35+
if (options.useTsWithBabel) {
36+
tsRule
37+
.use('babel-loader')
38+
.loader('babel-loader')
39+
}
3540
tsRule
3641
.use('ts-loader')
3742
.loader('ts-loader')

packages/@vue/cli/lib/promptModules/typescript.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ module.exports = cli => {
2121
type: 'confirm',
2222
message: `Compile TS with babel? ${chalk.yellow(`(experimental)`)}`
2323
})
24+
} else {
25+
cli.injectPrompt({
26+
name: 'useTsWithBabel',
27+
when: answers => answers.features.includes('ts'),
28+
type: 'confirm',
29+
message: 'Use TypeScript together with Babel for auto-detected polyfills?'
30+
})
2431
}
2532

2633
cli.onPromptComplete((answers, options) => {
@@ -32,7 +39,9 @@ module.exports = cli => {
3239
tsOptions.lint = true
3340
tsOptions.lintOn = answers.lintOn
3441
}
35-
if (answers.compileTsWithBabel) {
42+
if (answers.useTsWithBabel) {
43+
tsOptions.useTsWithBabel = true
44+
} else if (answers.compileTsWithBabel) {
3645
tsOptions.experimentalCompileTsWithBabel = true
3746
}
3847
options.plugins['@vue/cli-plugin-typescript'] = tsOptions

0 commit comments

Comments
 (0)