Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions packages/@vue/cli-plugin-typescript/generator/convert.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
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 content = files[api.entryFile]
const tsFile = api.entryFile.replace(jsRE, '.ts')
files[tsFile] = content
delete files[api.entryFile]
}
})
}
6 changes: 4 additions & 2 deletions packages/@vue/cli-plugin-typescript/generator/index.js
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ const prompts = module.exports = [
value: 'commit'
}
]
},
{
name: `convertJsToTs`,
type: `confirm`,
message: `Convert all .js files to .ts?`,
default: false
},
{
name: `allowJs`,
type: `confirm`,
message: `Allow .js files to be compiled?`,
default: false
}
]

Expand Down