Skip to content

Commit

Permalink
feat: load config w/ cosmiconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 27, 2018
1 parent 63c8f65 commit 5288122
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 42 deletions.
25 changes: 20 additions & 5 deletions packages/@vue/cli-service/__tests__/Service.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
jest.mock('fs')
jest.mock('mock-config', () => ({ lintOnSave: false }), { virtual: true })
jest.mock('vue-cli-plugin-foo', () => () => {}, { virtual: true })

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

test('load project options from vue.config.js', () => {
process.env.VUE_CLI_SERVICE_CONFIG_PATH = 'mock-config'
fs.writeFileSync('/vue.config.js', `module.exports=${JSON.stringify({ lintOnSave: false })}`)
const service = createMockService()
// vue.config.js has higher priority
expect(service.projectOptions.lintOnSave).toBe(false)
fs.unlinkSync('/vue.config.js')
})

test('load project options from .vuerc', () => {
fs.writeFileSync('/.vuerc', JSON.stringify({ lintOnSave: false }))
const service = createMockService()
// vue.config.js has higher priority
expect(service.projectOptions.lintOnSave).toBe(false)
fs.unlinkSync('/.vuerc')
})

test('package.json option should take priority', () => {
fs.writeFileSync('/vue.config.js', `module.exports=${JSON.stringify({ lintOnSave: false })}`)
mockPkg({
vue: {
lintOnSave: true
}
})
const service = createMockService()
// vue.config.js has higher priority
expect(service.projectOptions.lintOnSave).toBe(false)
delete process.env.VUE_CLI_SERVICE_CONFIG_PATH
// package.json has higher priority
expect(service.projectOptions.lintOnSave).toBe(true)
fs.unlinkSync('/vue.config.js')
})

test('api: setMode', () => {
Expand Down
53 changes: 16 additions & 37 deletions packages/@vue/cli-service/lib/Service.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const fs = require('fs')
const path = require('path')
const debug = require('debug')
const chalk = require('chalk')
const readPkg = require('read-pkg')
const merge = require('webpack-merge')
const deepMerge = require('deepmerge')
const Config = require('webpack-chain')
const PluginAPI = require('./PluginAPI')
const loadEnv = require('./util/loadEnv')
const { warn, error } = require('@vue/cli-shared-utils')
const cosmiconfig = require('cosmiconfig')
const { error } = require('@vue/cli-shared-utils')

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

Expand Down Expand Up @@ -150,45 +150,24 @@ module.exports = class Service {
}

loadProjectOptions (inlineOptions) {
// vue.config.js
let fileConfig, pkgConfig, resolved
const configPath = (
process.env.VUE_CLI_SERVICE_CONFIG_PATH ||
path.resolve(this.context, 'vue.config.js')
)
try {
fileConfig = require(configPath)
if (!fileConfig || typeof fileConfig !== 'object') {
let resolved
if (this.pkg.vue) {
resolved = this.pkg.vue
} else {
const explorer = cosmiconfig('vue', {
sync: true,
stopDir: this.context
})
try {
const res = explorer.load(this.context)
if (res) resolved = res.config
} catch (e) {
error(
`Error loading ${chalk.bold('vue.config.js')}: should export an object.`
)
fileConfig = null
}
} catch (e) {}

// package.vue
pkgConfig = this.pkg.vue
if (pkgConfig && typeof pkgConfig !== 'object') {
error(
`Error loading vue-cli config in ${chalk.bold(`package.json`)}: ` +
`the "vue" field should be an object.`
)
pkgConfig = null
}

if (fileConfig) {
if (pkgConfig) {
warn(
`"vue" field in ${chalk.bold(`package.json`)} ignored ` +
`due to presence of ${chalk.bold('vue.config.js')}.`
`Error loading vue-cli config: ${e.message}`
)
}
resolved = fileConfig
} else if (pkgConfig) {
resolved = pkgConfig
} else {
resolved = inlineOptions || {}
}
resolved = resolved || inlineOptions || {}

// normlaize some options
ensureSlash(resolved, 'base')
Expand Down
1 change: 1 addition & 0 deletions packages/@vue/cli-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"case-sensitive-paths-webpack-plugin": "^2.1.1",
"chalk": "^2.3.0",
"copy-webpack-plugin": "^4.3.1",
"cosmiconfig": "^4.0.0",
"css-loader": "^0.28.9",
"deepmerge": "^2.0.1",
"escape-string-regexp": "^1.0.5",
Expand Down

0 comments on commit 5288122

Please sign in to comment.