1+ const fs = require ( 'fs' )
12const path = require ( 'path' )
23const debug = require ( 'debug' )
34const merge = require ( 'webpack-merge' )
@@ -10,22 +11,6 @@ const { chalk, warn, error, isPlugin, resolvePluginId, loadModule, resolvePkg }
1011
1112const { defaults, validate } = require ( './options' )
1213
13- const loadConfig = configPath => {
14- let fileConfig = require ( configPath )
15-
16- if ( typeof fileConfig === 'function' ) {
17- fileConfig = fileConfig ( )
18- }
19-
20- if ( ! fileConfig || typeof fileConfig !== 'object' ) {
21- error (
22- `Error loading ${ chalk . bold ( 'vue.config.js' ) } : should export an object or a function that returns object.`
23- )
24- fileConfig = null
25- }
26- return fileConfig
27- }
28-
2914module . exports = class Service {
3015 constructor ( context , { plugins, pkg, inlineOptions, useBuiltIn } = { } ) {
3116 process . VUE_CLI_SERVICE = this
@@ -314,41 +299,46 @@ module.exports = class Service {
314299 }
315300
316301 loadUserOptions ( ) {
317- // vue.config.js
318- // vue.config.cjs
302+ // vue.config.c?js
319303 let fileConfig , pkgConfig , resolved , resolvedFrom
320304 const esm = this . pkg . type && this . pkg . type === 'module'
321- const jsConfigPath = path . resolve ( this . context , 'vue.config.js' )
322- const cjsConfigPath = path . resolve ( this . context , 'vue.config.cjs' )
323- const configPath = (
324- process . env . VUE_CLI_SERVICE_CONFIG_PATH ||
325- jsConfigPath
326- )
327-
328- try {
329- fileConfig = loadConfig ( configPath )
330- } catch ( e ) {
331- if ( e . code !== 'MODULE_NOT_FOUND' ) {
332- if ( e . code === 'ERR_REQUIRE_ESM' ) {
333- warn ( `Rename ${ chalk . bold ( 'vue.config.js' ) } to ${ chalk . bold ( 'vue.config.cjs' ) } when ECMAScript modules is enabled` )
334- }
335- error ( `Error loading ${ chalk . bold ( 'vue.config.js' ) } :` )
336- throw e
305+
306+ const possibleConfigPaths = [
307+ process . env . VUE_CLI_SERVICE_CONFIG_PATH ,
308+ './vue.config.js' ,
309+ './vue.config.cjs'
310+ ]
311+
312+ let fileConfigPath
313+ for ( const p of possibleConfigPaths ) {
314+ const resolvedPath = p && path . resolve ( this . context , p )
315+ if ( resolvedPath && fs . existsSync ( resolvedPath ) ) {
316+ fileConfigPath = resolvedPath
337317 }
338318 }
339319
340- // vue.config.js not found, esm enabled, no env set
341- if ( ! fileConfig && esm && ! process . env . VUE_CLI_SERVICE_CONFIG_PATH ) {
320+ if ( fileConfigPath ) {
321+ if ( esm && fileConfigPath === './vue.config.js' ) {
322+ throw new Error ( `Please rename ${ chalk . bold ( 'vue.config.js' ) } to ${ chalk . bold ( 'vue.config.cjs' ) } when ECMAScript modules is enabled` )
323+ }
324+
342325 try {
343- fileConfig = loadConfig ( cjsConfigPath )
344- } catch ( e ) {
345- if ( e . code !== 'MODULE_NOT_FOUND' ) {
346- error ( `Error loading ${ chalk . bold ( 'vue.config.cjs' ) } :` )
347- throw e
326+ fileConfig = loadModule ( fileConfigPath , this . context )
327+
328+ if ( typeof fileConfig === 'function' ) {
329+ fileConfig = fileConfig ( )
348330 }
349- }
350- if ( fileConfig ) {
351- warn ( `ECMAScript modules is detected, config loaded from ${ chalk . bold ( 'vue.config.cjs' ) } ` )
331+
332+ if ( ! fileConfig || typeof fileConfig !== 'object' ) {
333+ // TODO: show throw an Error here, to be fixed in v5
334+ error (
335+ `Error loading ${ chalk . bold ( fileConfigPath ) } : should export an object or a function that returns object.`
336+ )
337+ fileConfig = null
338+ }
339+ } catch ( e ) {
340+ error ( `Error loading ${ chalk . bold ( fileConfigPath ) } :` )
341+ throw e
352342 }
353343 }
354344
0 commit comments