Skip to content

Allow vue.config.js to export a function #3213

Closed
@jkzing

Description

@jkzing

What problem does this feature solve?

Currently vue.config.js only allowed to export an object containing options.

When user want to have more complicated configuration, they have to do:

const baseConfig = {
  // basic configurations
}

if (something) {
  module.exports = {
    ...baseConfig,
    // specific configurations
    baseUrl: '/foo/'
  }
} else {
  module.exports = {
    ...baseConfig,
    // specific configurations
    baseUrl: '/bar/'
  }
}

Moreover, we have received several issues asking about how to build with different configurations. (Like this one: #2861) Although this can be done with current api, I thinks it's not intutive enough so that people with limited nodejs knowledge couldn't figure it out.

What does the proposed API look like?

Like what babel.config.js did, let user use function to generate configuration:

module.exports = function () {
  const baseConfig = {
    // basic configurations
  }
  if (something) {
    return {
      ...baseConfig,
      // specific configurations
      baseUrl: '/foo/'
    }
  } else {
    return {
      ...baseConfig,
      // specific configurations
      baseUrl: '/foo/'
    }
  }
}

Further more, we can pass some useful information/api as arguments into the configuration function, for example:

module.exports = function (options, api) {
  const {
    args, // command line arguments parsed with cli
    env // project default ENV
    // any other ones ?
  } = options

  // like chainWebpack and configureWebpack inside PluginAPI
  api.chainWebpack(fn)
  api.configureWebpack(config)

  // this could solve the problem met in:
  // https://github.com/vuejs/vue-cli/issues/2506
  // https://github.com/vuejs/vue-cli/pull/3135
  api.loadEnvDir(pathToDir)
  api.loadEnvFile(pathToFile)
  // then user can use ENV variables below
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestintend to implementThe team has the intention to implement this feature at some point. Contribution is also welcome.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions