@@ -10,6 +10,22 @@ const { chalk, warn, error, isPlugin, resolvePluginId, loadModule, resolvePkg }
1010
1111const { defaults, validate } = require ( './options' )
1212
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+
1329module . exports = class Service {
1430 constructor ( context , { plugins, pkg, inlineOptions, useBuiltIn } = { } ) {
1531 process . VUE_CLI_SERVICE = this
@@ -299,31 +315,43 @@ module.exports = class Service {
299315
300316 loadUserOptions ( ) {
301317 // vue.config.js
318+ // vue.config.cjs
302319 let fileConfig , pkgConfig , resolved , resolvedFrom
320+ 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' )
303323 const configPath = (
304324 process . env . VUE_CLI_SERVICE_CONFIG_PATH ||
305- path . resolve ( this . context , 'vue.config.js' )
325+ jsConfigPath
306326 )
307- try {
308- fileConfig = require ( configPath )
309-
310- if ( typeof fileConfig === 'function' ) {
311- fileConfig = fileConfig ( )
312- }
313327
314- if ( ! fileConfig || typeof fileConfig !== 'object' ) {
315- error (
316- `Error loading ${ chalk . bold ( 'vue.config.js' ) } : should export an object or a function that returns object.`
317- )
318- fileConfig = null
319- }
328+ try {
329+ fileConfig = loadConfig ( configPath )
320330 } catch ( e ) {
321331 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+ }
322335 error ( `Error loading ${ chalk . bold ( 'vue.config.js' ) } :` )
323336 throw e
324337 }
325338 }
326339
340+ // vue.config.js not found, esm enabled, no env set
341+ if ( ! fileConfig && esm && ! process . env . VUE_CLI_SERVICE_CONFIG_PATH ) {
342+ 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
348+ }
349+ }
350+ if ( fileConfig ) {
351+ warn ( `ECMAScript modules is detected, config loaded from ${ chalk . bold ( 'vue.config.cjs' ) } ` )
352+ }
353+ }
354+
327355 // package.vue
328356 pkgConfig = this . pkg . vue
329357 if ( pkgConfig && typeof pkgConfig !== 'object' ) {
0 commit comments