Skip to content

Commit

Permalink
revert: feat: load config w/ cosmiconfig
Browse files Browse the repository at this point in the history
This reverts commit 5288122.
  • Loading branch information
yyx990803 committed Feb 2, 2018
1 parent 9a07eeb commit 702b539
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
17 changes: 5 additions & 12 deletions packages/@vue/cli-service/__tests__/Service.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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 @@ -52,24 +53,16 @@ test('load project options from package.json', () => {
})

test('load project options from vue.config.js', () => {
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('package.json option should take priority', () => {
fs.writeFileSync('/vue.config.js', `module.exports=${JSON.stringify({ lintOnSave: false })}`)
process.env.VUE_CLI_SERVICE_CONFIG_PATH = 'mock-config'
mockPkg({
vue: {
lintOnSave: true
}
})
const service = createMockService()
// package.json has higher priority
expect(service.projectOptions.lintOnSave).toBe(true)
fs.unlinkSync('/vue.config.js')
// vue.config.js has higher priority
expect(service.projectOptions.lintOnSave).toBe(false)
delete process.env.VUE_CLI_SERVICE_CONFIG_PATH
})

test('api: setMode', () => {
Expand Down
54 changes: 37 additions & 17 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 cosmiconfig = require('cosmiconfig')
const { error } = require('@vue/cli-shared-utils')
const { warn, error } = require('@vue/cli-shared-utils')

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

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

loadProjectOptions (inlineOptions) {
let resolved
if (this.pkg.vue) {
resolved = this.pkg.vue
} else {
const explorer = cosmiconfig('vue', {
rc: false,
sync: true,
stopDir: this.context
})
try {
const res = explorer.load(this.context)
if (res) resolved = res.config
} catch (e) {
// 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') {
error(
`Error loading vue-cli config: ${e.message}`
`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')}.`
)
}
resolved = fileConfig
} else if (pkgConfig) {
resolved = pkgConfig
} else {
resolved = inlineOptions || {}
}
resolved = resolved || inlineOptions || {}

// normlaize some options
ensureSlash(resolved, 'base')
Expand Down
1 change: 0 additions & 1 deletion packages/@vue/cli-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"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 702b539

Please sign in to comment.