diff --git a/packages/@vue/cli-service/__tests__/Service.spec.js b/packages/@vue/cli-service/__tests__/Service.spec.js index eb37fe5f26..da6bf71b3a 100644 --- a/packages/@vue/cli-service/__tests__/Service.spec.js +++ b/packages/@vue/cli-service/__tests__/Service.spec.js @@ -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') @@ -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', () => { diff --git a/packages/@vue/cli-service/lib/Service.js b/packages/@vue/cli-service/lib/Service.js index 69c2c8c815..14677dfc19 100644 --- a/packages/@vue/cli-service/lib/Service.js +++ b/packages/@vue/cli-service/lib/Service.js @@ -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') @@ -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') diff --git a/packages/@vue/cli-service/package.json b/packages/@vue/cli-service/package.json index cbd25652ad..6e8aed7897 100644 --- a/packages/@vue/cli-service/package.json +++ b/packages/@vue/cli-service/package.json @@ -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",