From 702b539d33f10fb6dd73cef6b9fc8484319154c2 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 2 Feb 2018 01:03:36 -0500 Subject: [PATCH] revert: feat: load config w/ cosmiconfig This reverts commit 52881223c31c20870f5f027cca7120a618c5d548. --- .../cli-service/__tests__/Service.spec.js | 17 ++---- packages/@vue/cli-service/lib/Service.js | 54 +++++++++++++------ packages/@vue/cli-service/package.json | 1 - 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/packages/@vue/cli-service/__tests__/Service.spec.js b/packages/@vue/cli-service/__tests__/Service.spec.js index 8e08dce87b..0d39a50223 100644 --- a/packages/@vue/cli-service/__tests__/Service.spec.js +++ b/packages/@vue/cli-service/__tests__/Service.spec.js @@ -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') @@ -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', () => { diff --git a/packages/@vue/cli-service/lib/Service.js b/packages/@vue/cli-service/lib/Service.js index 33a273eae3..f0e923c98c 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 cosmiconfig = require('cosmiconfig') -const { error } = require('@vue/cli-shared-utils') +const { warn, error } = require('@vue/cli-shared-utils') const { defaults, validate } = require('./options') @@ -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') diff --git a/packages/@vue/cli-service/package.json b/packages/@vue/cli-service/package.json index 50dde4a9eb..804e7613d0 100644 --- a/packages/@vue/cli-service/package.json +++ b/packages/@vue/cli-service/package.json @@ -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",