Skip to content

Commit 5288122

Browse files
committed
feat: load config w/ cosmiconfig
1 parent 63c8f65 commit 5288122

File tree

3 files changed

+37
-42
lines changed

3 files changed

+37
-42
lines changed

packages/@vue/cli-service/__tests__/Service.spec.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
jest.mock('fs')
2-
jest.mock('mock-config', () => ({ lintOnSave: false }), { virtual: true })
32
jest.mock('vue-cli-plugin-foo', () => () => {}, { virtual: true })
43

54
const fs = require('fs')
@@ -53,16 +52,32 @@ test('load project options from package.json', () => {
5352
})
5453

5554
test('load project options from vue.config.js', () => {
56-
process.env.VUE_CLI_SERVICE_CONFIG_PATH = 'mock-config'
55+
fs.writeFileSync('/vue.config.js', `module.exports=${JSON.stringify({ lintOnSave: false })}`)
56+
const service = createMockService()
57+
// vue.config.js has higher priority
58+
expect(service.projectOptions.lintOnSave).toBe(false)
59+
fs.unlinkSync('/vue.config.js')
60+
})
61+
62+
test('load project options from .vuerc', () => {
63+
fs.writeFileSync('/.vuerc', JSON.stringify({ lintOnSave: false }))
64+
const service = createMockService()
65+
// vue.config.js has higher priority
66+
expect(service.projectOptions.lintOnSave).toBe(false)
67+
fs.unlinkSync('/.vuerc')
68+
})
69+
70+
test('package.json option should take priority', () => {
71+
fs.writeFileSync('/vue.config.js', `module.exports=${JSON.stringify({ lintOnSave: false })}`)
5772
mockPkg({
5873
vue: {
5974
lintOnSave: true
6075
}
6176
})
6277
const service = createMockService()
63-
// vue.config.js has higher priority
64-
expect(service.projectOptions.lintOnSave).toBe(false)
65-
delete process.env.VUE_CLI_SERVICE_CONFIG_PATH
78+
// package.json has higher priority
79+
expect(service.projectOptions.lintOnSave).toBe(true)
80+
fs.unlinkSync('/vue.config.js')
6681
})
6782

6883
test('api: setMode', () => {

packages/@vue/cli-service/lib/Service.js

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const fs = require('fs')
22
const path = require('path')
33
const debug = require('debug')
4-
const chalk = require('chalk')
54
const readPkg = require('read-pkg')
65
const merge = require('webpack-merge')
76
const deepMerge = require('deepmerge')
87
const Config = require('webpack-chain')
98
const PluginAPI = require('./PluginAPI')
109
const loadEnv = require('./util/loadEnv')
11-
const { warn, error } = require('@vue/cli-shared-utils')
10+
const cosmiconfig = require('cosmiconfig')
11+
const { error } = require('@vue/cli-shared-utils')
1212

1313
const { defaults, validate } = require('./options')
1414

@@ -150,45 +150,24 @@ module.exports = class Service {
150150
}
151151

152152
loadProjectOptions (inlineOptions) {
153-
// vue.config.js
154-
let fileConfig, pkgConfig, resolved
155-
const configPath = (
156-
process.env.VUE_CLI_SERVICE_CONFIG_PATH ||
157-
path.resolve(this.context, 'vue.config.js')
158-
)
159-
try {
160-
fileConfig = require(configPath)
161-
if (!fileConfig || typeof fileConfig !== 'object') {
153+
let resolved
154+
if (this.pkg.vue) {
155+
resolved = this.pkg.vue
156+
} else {
157+
const explorer = cosmiconfig('vue', {
158+
sync: true,
159+
stopDir: this.context
160+
})
161+
try {
162+
const res = explorer.load(this.context)
163+
if (res) resolved = res.config
164+
} catch (e) {
162165
error(
163-
`Error loading ${chalk.bold('vue.config.js')}: should export an object.`
164-
)
165-
fileConfig = null
166-
}
167-
} catch (e) {}
168-
169-
// package.vue
170-
pkgConfig = this.pkg.vue
171-
if (pkgConfig && typeof pkgConfig !== 'object') {
172-
error(
173-
`Error loading vue-cli config in ${chalk.bold(`package.json`)}: ` +
174-
`the "vue" field should be an object.`
175-
)
176-
pkgConfig = null
177-
}
178-
179-
if (fileConfig) {
180-
if (pkgConfig) {
181-
warn(
182-
`"vue" field in ${chalk.bold(`package.json`)} ignored ` +
183-
`due to presence of ${chalk.bold('vue.config.js')}.`
166+
`Error loading vue-cli config: ${e.message}`
184167
)
185168
}
186-
resolved = fileConfig
187-
} else if (pkgConfig) {
188-
resolved = pkgConfig
189-
} else {
190-
resolved = inlineOptions || {}
191169
}
170+
resolved = resolved || inlineOptions || {}
192171

193172
// normlaize some options
194173
ensureSlash(resolved, 'base')

packages/@vue/cli-service/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"case-sensitive-paths-webpack-plugin": "^2.1.1",
3131
"chalk": "^2.3.0",
3232
"copy-webpack-plugin": "^4.3.1",
33+
"cosmiconfig": "^4.0.0",
3334
"css-loader": "^0.28.9",
3435
"deepmerge": "^2.0.1",
3536
"escape-string-regexp": "^1.0.5",

0 commit comments

Comments
 (0)