Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jkzing opened this issue Dec 29, 2018 · 2 comments · Fixed by #3499
Closed

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

jkzing opened this issue Dec 29, 2018 · 2 comments · Fixed by #3499
Labels
feature request intend to implement The team has the intention to implement this feature at some point. Contribution is also welcome.

Comments

@jkzing
Copy link
Member

jkzing commented Dec 29, 2018

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
}
@haoqunjiang haoqunjiang added the intend to implement The team has the intention to implement this feature at some point. Contribution is also welcome. label Feb 18, 2019
@haoqunjiang
Copy link
Member

While I appreciate this idea very much, I think the API should be minimal as compared to the proposed ones in the OP.

We already have a project local plugin mechanism, so it seems redundant to replicate this logic in the config file.
Also, vue.config.js is a higher abstraction layer over webpack, we should distinguish it from plain webpack config IMHO.
As for the loadEnv* API, it's against out guideline as stated by Evan: #2506 (comment)

So I think we can divide this proposal into 2 PRs: one for support of functional export, one for the minimal API interface (yet to be designed and discussed).

@jkzing
Copy link
Member Author

jkzing commented Feb 18, 2019

@sodatea , nice to see progress on this FR, support for functional export would be a good start!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request intend to implement The team has the intention to implement this feature at some point. Contribution is also welcome.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants