Vue-cli: A way to detect modern/legacy build

Created on 8 Nov 2018  路  9Comments  路  Source: vuejs/vue-cli

What problem does this feature solve?

I use css variables in my application. But they are not supported in old browsers. Because of this I need to use css-vars-ponyfill. But call to this library is necessary only in old browsers. And because of this I need a way to detect modern/legacy build to include this code only in legacy build.

What does the proposed API look like?

I think, information about current build type could be accessed via process.env variable.
For example: process.env.IS_MODERN_BUILD

documentation has PR

Most helpful comment

@Djaler

// vue.config.js
module.exports = {
  chainWebpack: config =>
    config
      .plugin('define')
        .tap(options => {
          options[0]['process.env']['IS_MODERN_BUILD'] = process.env.VUE_CLI_MODERN_BUILD
          return options
        })
}

All 9 comments

You can generate multiple arguments based bundles.

const isModern = !!require('yargs').argv.modern;
// ...
webpackConfig.plugins.push(
  new webpack.ProvidePlugin({ 'process.env.IS_MODERN_BUILD': isModern}),
);

@cainrus this way I can only detect modern mode, but not modern build. Because in modern mode 2 builds are created: legacy and modern.

We use this line to detect modern/legacy build internally:
https://github.com/vuejs/vue-cli/blob/5d49d5796648cc92277cef79616cc66aa6148f73/packages/%40vue/cli-service/lib/config/app.js#L23

Though it's not a good practice to rely on internal flags, you can use the two environment variables as a workaround for now.

@sodatea I tried, but they are not available in client code

@Djaler

// vue.config.js
module.exports = {
  chainWebpack: config =>
    config
      .plugin('define')
        .tap(options => {
          options[0]['process.env']['IS_MODERN_BUILD'] = process.env.VUE_CLI_MODERN_BUILD
          return options
        })
}

@sodatea thank you! This works as expected.
Can you also help me yet another time? Why this doesn't work?

// vue.config.js
module.exports = {
  configureWebpack: {
    plugins: [
      new webpack.DefinePlugin({
        'process.env': {
          'IS_MODERN_BUILD': process.env.VUE_CLI_MODERN_BUILD,
        },
      }),
    ]
  }
}

@Djaler Maybe it's because there's already a DefinePlugin instance in the config and webpack-merge can't merge the two correctly?

@sodatea Whould there be any concern with exposing this flag as a public API by mentioning it in the docs?

I don't see a clever way of making this available without an env variable.

To keep internals separate, we could introduce a different env variable for the public that (for now) just is set alongside VUE_CLI_MODERN_BUILD.

@LinusBorg Yeah I just reconsidered this issue and found no better way to achieve this.

It should be documented somewhere, along with the recommended way to use it (vue.config.js will be required once and once only, so reference to VUE_CLI_MODERN_MODE has to be inside a function, e.g. chainWebpack).

The naming seems fine as it reflects the fact that it is expected to be used in a Vue CLI build config file.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jgribonvald picture jgribonvald  路  3Comments

OmgImAlexis picture OmgImAlexis  路  3Comments

BusyHe picture BusyHe  路  3Comments

Gonzalo2683 picture Gonzalo2683  路  3Comments

eladcandroid picture eladcandroid  路  3Comments