Skip to content

Commit ada4022

Browse files
Akryumyyx990803
authored andcommitted
refactor: improve invocation of builtin plugins in TypeScript projects (#1342)
1 parent f50b0db commit ada4022

File tree

9 files changed

+72
-32
lines changed

9 files changed

+72
-32
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ module.exports = api => {
44
'register-service-worker': '^1.0.0'
55
}
66
})
7-
api.injectImports(`src/main.js`, `import './registerServiceWorker'`)
7+
api.injectImports(api.entryFile, `import './registerServiceWorker'`)
88
api.render('./template')
9+
10+
if (api.invoking && api.hasPlugin('typescript')) {
11+
const convertFiles = require('@vue/cli-plugin-typescript/generator/convert')
12+
convertFiles(api)
13+
}
914
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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
4+
const jsRE = /\.js$/
5+
const excludeRE = /^tests\/e2e\/|(\.config|rc)\.js$/
6+
const convertLintFlags = require('../lib/convertLintFlags')
7+
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)
15+
}
16+
files[tsFile] = content
17+
}
18+
delete files[file]
19+
}
20+
}
21+
})
22+
}

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

+1-20
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,5 @@ module.exports = (api, {
7979
hasJest
8080
})
8181

82-
// delete all js files that have a ts file of the same name
83-
// and simply rename other js files to ts
84-
const jsRE = /\.js$/
85-
const excludeRE = /^tests\/e2e\/|(\.config|rc)\.js$/
86-
const convertLintFlags = require('../lib/convertLintFlags')
87-
api.postProcessFiles(files => {
88-
for (const file in files) {
89-
if (jsRE.test(file) && !excludeRE.test(file)) {
90-
const tsFile = file.replace(jsRE, '.ts')
91-
if (!files[tsFile]) {
92-
let content = files[file]
93-
if (tsLint) {
94-
content = convertLintFlags(content)
95-
}
96-
files[tsFile] = content
97-
}
98-
delete files[file]
99-
}
100-
}
101-
})
82+
require('./convert')(api, { tsLint })
10283
}

packages/@vue/cli-service/generator/router/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
module.exports = (api, options) => {
2-
api.injectImports(`src/main.js`, `import router from './router'`)
3-
api.injectRootOptions(`src/main.js`, `router`)
2+
api.injectImports(api.entryFile, `import router from './router'`)
3+
api.injectRootOptions(api.entryFile, `router`)
44
api.extendPackage({
55
dependencies: {
66
'vue-router': '^3.0.1'
77
}
88
})
99
api.render('./template')
1010

11-
if (options.invoking) {
11+
if (api.invoking) {
1212
api.postProcessFiles(files => {
1313
const appFile = files[`src/App.vue`]
1414
if (appFile) {
@@ -26,5 +26,10 @@ module.exports = (api, options) => {
2626
console.log(files[`src/App.vue`])
2727
}
2828
})
29+
30+
if (api.hasPlugin('typescript')) {
31+
const convertFiles = require('@vue/cli-plugin-typescript/generator/convert')
32+
convertFiles(api)
33+
}
2934
}
3035
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
module.exports = (api, options) => {
2-
api.injectImports(`src/main.js`, `import store from './store'`)
3-
api.injectRootOptions(`src/main.js`, `store`)
2+
api.injectImports(api.entryFile, `import store from './store'`)
3+
api.injectRootOptions(api.entryFile, `store`)
44
api.extendPackage({
55
dependencies: {
66
vuex: '^3.0.1'
77
}
88
})
99
api.render('./template')
10+
11+
if (api.invoking && api.hasPlugin('typescript')) {
12+
const convertFiles = require('@vue/cli-plugin-typescript/generator/convert')
13+
convertFiles(api)
14+
}
1015
}

packages/@vue/cli/lib/Generator.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ module.exports = class Generator {
2222
pkg = {},
2323
plugins = [],
2424
completeCbs = [],
25-
files = {}
25+
files = {},
26+
invoking = false
2627
} = {}) {
2728
this.context = context
2829
this.plugins = plugins
@@ -31,6 +32,7 @@ module.exports = class Generator {
3132
this.imports = {}
3233
this.rootOptions = {}
3334
this.completeCbs = completeCbs
35+
this.invoking = invoking
3436

3537
// for conflict resolution
3638
this.depSources = {}

packages/@vue/cli/lib/GeneratorAPI.js

+21
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class GeneratorAPI {
3333
name: toShortPluginId(id),
3434
link: getPluginLink(id)
3535
}))
36+
37+
this._entryFile = undefined
3638
}
3739

3840
/**
@@ -225,6 +227,25 @@ class GeneratorAPI {
225227
_options.add(opt)
226228
})
227229
}
230+
231+
/**
232+
* Get the entry file taking into account typescript.
233+
*
234+
* @readonly
235+
*/
236+
get entryFile () {
237+
if (this._entryFile) return this._entryFile
238+
return (this._entryFile = fs.existsSync(this.resolve('src/main.ts')) ? 'src/main.ts' : 'src/main.js')
239+
}
240+
241+
/**
242+
* Is the plugin being invoked?
243+
*
244+
* @readonly
245+
*/
246+
get invoking () {
247+
return this.generator.invoking
248+
}
228249
}
229250

230251
function extractCallDir () {

packages/@vue/cli/lib/add.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,14 @@ async function add (pluginName, options = {}, context = process.cwd()) {
4646
async function addRouter (context) {
4747
invoke.runGenerator(context, {
4848
id: 'core:router',
49-
apply: require('@vue/cli-service/generator/router'),
50-
options: { invoking: true }
49+
apply: require('@vue/cli-service/generator/router')
5150
})
5251
}
5352

5453
async function addVuex (context) {
5554
invoke.runGenerator(context, {
5655
id: 'core:vuex',
57-
apply: require('@vue/cli-service/generator/vuex'),
58-
options: { invoking: true }
56+
apply: require('@vue/cli-service/generator/vuex')
5957
})
6058
}
6159

packages/@vue/cli/lib/invoke.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ async function runGenerator (context, plugin, pkg = getPkg(context)) {
100100
pkg,
101101
plugins: [plugin],
102102
files: await readFiles(context),
103-
completeCbs: createCompleteCbs
103+
completeCbs: createCompleteCbs,
104+
invoking: true
104105
})
105106

106107
log()

0 commit comments

Comments
 (0)