Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(typescript): add an option to not rename all .js files to .ts and to set allowJs to true #4212

Merged
merged 5 commits into from Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 23 additions & 12 deletions packages/@vue/cli-plugin-typescript/generator/convert.js
@@ -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')
NataliaTepluhina marked this conversation as resolved.
Show resolved Hide resolved
let content = files[api.entryFile]
if (tsLint) {
content = convertLintFlags(content)
}
files[tsFile] = content
delete files[api.entryFile]
}
})
}
6 changes: 4 additions & 2 deletions packages/@vue/cli-plugin-typescript/generator/index.js
@@ -1,7 +1,9 @@
module.exports = (api, {
classComponent,
tsLint,
lintOn = []
lintOn = [],
convertJsToTs,
allowJs
}, _, invoking) => {
if (typeof lintOn === 'string') {
lintOn = lintOn.split(',')
Expand Down Expand Up @@ -82,5 +84,5 @@ module.exports = (api, {
hasJest: api.hasPlugin('unit-jest')
})

require('./convert')(api, { tsLint })
require('./convert')(api, { tsLint, convertJsToTs })
}
Expand Up @@ -9,6 +9,9 @@
<%_ if (options.classComponent) { _%>
"experimentalDecorators": true,
<%_ } _%>
<%_ if (options.allowJs) { _%>
"allowJs": true,
<%_ } _%>
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
Expand Down
12 changes: 12 additions & 0 deletions packages/@vue/cli-plugin-typescript/prompts.js
Expand Up @@ -36,6 +36,18 @@ const prompts = module.exports = [
value: 'commit'
}
]
},
{
name: `convertJsToTs`,
type: `confirm`,
message: `Convert all .js files to .ts?`,
default: true
},
{
name: `allowJs`,
type: `confirm`,
message: `Allow .js files to be compiled?`,
default: false
}
]

Expand Down