Skip to content

Commit 51e63cf

Browse files
NataliaTepluhinahaoqunjiang
authored andcommitted
feat(typescript): add convertJsToTs and allowJs options (#4212)
Close #2676 At the moment when we run `vue add`, Vue CLI renames all `*.js` files to `*.ts`. This PR introduces two new prompts on `@vue/cli-plugin-typescript` late-invokation: 1. `convertJsToTs`: if set to `true`, renames all `.js` files to `.ts`. Otherwise renames only `main.js` -> `main.ts`; 2. `allowJs`: if set to `true`, adds `allowJs: true` to TSConfig compiler options. (cherry picked from commit 38debb4)
1 parent 1f0d66f commit 51e63cf

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
1-
module.exports = (api, { tsLint = false } = {}) => {
2-
// delete all js files that have a ts file of the same name
3-
// and simply rename other js files to ts
1+
module.exports = (api, { tsLint = false, convertAllFiles = true } = {}) => {
42
const jsRE = /\.js$/
53
const excludeRE = /^tests\/e2e\/|(\.config|rc)\.js$/
64
const convertLintFlags = require('../lib/convertLintFlags')
75
api.postProcessFiles(files => {
8-
for (const file in files) {
9-
if (jsRE.test(file) && !excludeRE.test(file)) {
10-
const tsFile = file.replace(jsRE, '.ts')
11-
if (!files[tsFile]) {
12-
let content = files[file]
13-
if (tsLint) {
14-
content = convertLintFlags(content)
6+
if (convertAllFiles) {
7+
// delete all js files that have a ts file of the same name
8+
// and simply rename other js files to ts
9+
for (const file in files) {
10+
if (jsRE.test(file) && !excludeRE.test(file)) {
11+
const tsFile = file.replace(jsRE, '.ts')
12+
if (!files[tsFile]) {
13+
let content = files[file]
14+
if (tsLint) {
15+
content = convertLintFlags(content)
16+
}
17+
files[tsFile] = content
1518
}
16-
files[tsFile] = content
19+
delete files[file]
1720
}
18-
delete files[file]
1921
}
22+
} else {
23+
// rename only main file to main.ts
24+
const tsFile = api.entryFile.replace(jsRE, '.ts')
25+
let content = files[api.entryFile]
26+
if (tsLint) {
27+
content = convertLintFlags(content)
28+
}
29+
files[tsFile] = content
30+
delete files[api.entryFile]
2031
}
2132
})
2233
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
module.exports = (api, {
22
classComponent,
33
tsLint,
4-
lintOn = []
4+
lintOn = [],
5+
convertJsToTs,
6+
allowJs
57
}, _, invoking) => {
68
if (typeof lintOn === 'string') {
79
lintOn = lintOn.split(',')
@@ -82,5 +84,5 @@ module.exports = (api, {
8284
hasJest: api.hasPlugin('unit-jest')
8385
})
8486

85-
require('./convert')(api, { tsLint })
87+
require('./convert')(api, { tsLint, convertJsToTs })
8688
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
<%_ if (options.classComponent) { _%>
1010
"experimentalDecorators": true,
1111
<%_ } _%>
12+
<%_ if (options.allowJs) { _%>
13+
"allowJs": true,
14+
<%_ } _%>
1215
"esModuleInterop": true,
1316
"allowSyntheticDefaultImports": true,
1417
"sourceMap": true,

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

+12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ const prompts = module.exports = [
3636
value: 'commit'
3737
}
3838
]
39+
},
40+
{
41+
name: `convertJsToTs`,
42+
type: `confirm`,
43+
message: `Convert all .js files to .ts?`,
44+
default: true
45+
},
46+
{
47+
name: `allowJs`,
48+
type: `confirm`,
49+
message: `Allow .js files to be compiled?`,
50+
default: false
3951
}
4052
]
4153

0 commit comments

Comments
 (0)