Skip to content

Commit 4f2f6f0

Browse files
committed
fix: move plugin data extraction into GeneratorAPI
1 parent 0f9a44a commit 4f2f6f0

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

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

+1-24
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
1-
// get link for a 3rd party plugin.
2-
function getLink (id) {
3-
const pkg = require(`${id}/package.json`)
4-
return (
5-
pkg.homepage ||
6-
(pkg.repository && pkg.repository.url) ||
7-
`https://www.npmjs.com/package/${id.replace(`/`, `%2F`)}`
8-
)
9-
}
10-
111
module.exports = (api, options) => {
12-
api.render('./template', {
13-
plugins: api.generator.plugins
14-
.filter(({ id }) => id !== `@vue/cli-service`)
15-
.map(({ id }) => {
16-
const name = id.replace(/^(@vue|vue-)\/cli-plugin-/, '')
17-
const isOfficial = /^@vue/.test(id)
18-
return {
19-
name: name,
20-
link: isOfficial
21-
? `https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-${name}`
22-
: getLink(id)
23-
}
24-
})
25-
})
2+
api.render('./template')
263

274
api.extendPackage({
285
scripts: {

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

+36-8
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,46 @@ const isString = val => typeof val === 'string'
99
const isFunction = val => typeof val === 'function'
1010
const isObject = val => val && typeof val === 'object'
1111

12+
// get link for a 3rd party plugin.
13+
function getLink (id) {
14+
let pkg = {}
15+
try {
16+
pkg = require(`${id}/package.json`)
17+
} catch (e) {}
18+
return (
19+
pkg.homepage ||
20+
(pkg.repository && pkg.repository.url) ||
21+
`https://www.npmjs.com/package/${id.replace(`/`, `%2F`)}`
22+
)
23+
}
24+
1225
module.exports = class GeneratorAPI {
1326
constructor (id, generator, options, rootOptions) {
1427
this.id = id
1528
this.generator = generator
1629
this.options = options
1730
this.rootOptions = rootOptions
31+
32+
this.pluginsData = generator.plugins
33+
.filter(({ id }) => id !== `@vue/cli-service`)
34+
.map(({ id }) => {
35+
const name = id.replace(/^(@vue|vue-)\/cli-plugin-/, '')
36+
const isOfficial = /^@vue/.test(id)
37+
return {
38+
name: name,
39+
link: isOfficial
40+
? `https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-${name}`
41+
: getLink(id)
42+
}
43+
})
44+
}
45+
46+
_resolveData (additionalData) {
47+
return Object.assign({
48+
options: this.options,
49+
rootOptions: this.rootOptions,
50+
plugins: this.pluginsData
51+
}, additionalData)
1852
}
1953

2054
injectFileMiddleware (middleware) {
@@ -52,10 +86,7 @@ module.exports = class GeneratorAPI {
5286
if (isString(fileDir)) {
5387
fileDir = path.resolve(baseDir, fileDir)
5488
this.injectFileMiddleware(async (files) => {
55-
const data = Object.assign({
56-
options: this.options,
57-
rootOptions: this.rootOptions
58-
}, additionalData)
89+
const data = this._resolveData(additionalData)
5990
const _files = await globby(['**/*'], { cwd: fileDir })
6091
for (const rawPath of _files) {
6192
let filename = path.basename(rawPath)
@@ -75,10 +106,7 @@ module.exports = class GeneratorAPI {
75106
})
76107
} else if (isObject(fileDir)) {
77108
this.injectFileMiddleware(files => {
78-
const data = Object.assign({
79-
options: this.options,
80-
rootOptions: this.rootOptions
81-
}, additionalData)
109+
const data = this._resolveData(additionalData)
82110
for (const targetPath in fileDir) {
83111
const sourcePath = path.resolve(baseDir, fileDir[targetPath])
84112
const content = renderFile(sourcePath, data, ejsOptions)

0 commit comments

Comments
 (0)