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.
I think, information about current build type could be accessed via process.env variable.
For example: process.env.IS_MODERN_BUILD
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.
Most helpful comment
@Djaler