Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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)
- Loading branch information
1 parent
1f0d66f
commit 51e63cf
Showing
4 changed files
with
42 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,33 @@ | ||
module.exports = (api, { tsLint = false } = {}) => { | ||
// delete all js files that have a ts file of the same name | ||
// and simply rename other js files to ts | ||
module.exports = (api, { tsLint = false, convertAllFiles = true } = {}) => { | ||
const jsRE = /\.js$/ | ||
const excludeRE = /^tests\/e2e\/|(\.config|rc)\.js$/ | ||
const convertLintFlags = require('../lib/convertLintFlags') | ||
api.postProcessFiles(files => { | ||
for (const file in files) { | ||
if (jsRE.test(file) && !excludeRE.test(file)) { | ||
const tsFile = file.replace(jsRE, '.ts') | ||
if (!files[tsFile]) { | ||
let content = files[file] | ||
if (tsLint) { | ||
content = convertLintFlags(content) | ||
if (convertAllFiles) { | ||
// delete all js files that have a ts file of the same name | ||
// and simply rename other js files to ts | ||
for (const file in files) { | ||
if (jsRE.test(file) && !excludeRE.test(file)) { | ||
const tsFile = file.replace(jsRE, '.ts') | ||
if (!files[tsFile]) { | ||
let content = files[file] | ||
if (tsLint) { | ||
content = convertLintFlags(content) | ||
} | ||
files[tsFile] = content | ||
} | ||
files[tsFile] = content | ||
delete files[file] | ||
} | ||
delete files[file] | ||
} | ||
} else { | ||
// rename only main file to main.ts | ||
const tsFile = api.entryFile.replace(jsRE, '.ts') | ||
let content = files[api.entryFile] | ||
if (tsLint) { | ||
content = convertLintFlags(content) | ||
} | ||
files[tsFile] = content | ||
delete files[api.entryFile] | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters