Skip to content

Commit ce58549

Browse files
committed
fix: infer rootOptions for late invoked generators
close #1820
1 parent a748c26 commit ce58549

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const debug = require('debug')
33
const GeneratorAPI = require('./GeneratorAPI')
44
const sortObject = require('./util/sortObject')
55
const writeFileTree = require('./util/writeFileTree')
6+
const inferRootOptions = require('./util/inferRootOptions')
67
const normalizeFilePaths = require('./util/normalizeFilePaths')
78
const injectImportsAndOptions = require('./util/injectImportsAndOptions')
89
const { toShortPluginId, matchesPluginId } = require('@vue/cli-shared-utils')
@@ -86,10 +87,12 @@ module.exports = class Generator {
8687
this.exitLogs = []
8788

8889
const cliService = plugins.find(p => p.id === '@vue/cli-service')
89-
const rootOptions = cliService && cliService.options
90+
const rootOptions = cliService
91+
? cliService.options
92+
: inferRootOptions(pkg)
9093
// apply generators from plugins
9194
plugins.forEach(({ id, apply, options }) => {
92-
const api = new GeneratorAPI(id, this, options, rootOptions || {})
95+
const api = new GeneratorAPI(id, this, options, rootOptions)
9396
apply(api, options, rootOptions)
9497
})
9598
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Infer rootOptions for individual generators being invoked
2+
// in an existing project.
3+
4+
module.exports = function inferRootOptions (pkg) {
5+
const rootOptions = {}
6+
const deps = Object.assign({}, pkg.dependencies, pkg.devDependencies)
7+
8+
// projectName
9+
rootOptions.projectName = pkg.name
10+
11+
// router
12+
if ('vue-router' in deps) {
13+
rootOptions.router = true
14+
}
15+
16+
// vuex
17+
if ('vuex' in deps) {
18+
rootOptions.vuex = true
19+
}
20+
21+
// cssPreprocessors
22+
if ('sass-loader' in deps) {
23+
rootOptions.cssPreprocessor = 'sass'
24+
} else if ('less-loader' in deps) {
25+
rootOptions.cssPreprocessor = 'less'
26+
} else if ('stylus-loader' in deps) {
27+
rootOptions.cssPreprocessor = 'stylus'
28+
}
29+
30+
return rootOptions
31+
}

0 commit comments

Comments
 (0)