Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How use Bundler Build Feature Flags in Vue 3.0 with Webpack?

Vue 3.0 (now stable) has an undocumented feature Bundler Build Feature Flags:

Starting with 3.0.0-rc.3, esm-bundler builds now exposes global feature flags that can be overwritten at compile time:

VUE_OPTIONS_API (enable/disable Options API support, default: true)

VUE_PROD_DEVTOOLS (enable/disable devtools support in production, default: false)

The build will work without configuring these flags, however it is strongly recommended to properly configure them in order to get proper tree-shaking in the final bundle. To configure these flags:

webpack: use DefinePlugin

Rollup: use @rollup/plugin-replace

Vite: configured by default, but can be overwritten using the define option

Note: the replacement value must be boolean literals and cannot be strings, otherwise the bundler/minifier will not be able to properly evaluate the conditions.

How to actually configure this build flag using vue.config.js and Webpack?

Tried this way but it doesn't seem to affect the vendors bundle size, or is it supposed to work with production builds only (can't try currently as there is a vue-loader bug breaking my prod builds)?

const webpack = require('webpack');

module.exports = {
  configureWebpack: {
    plugins: [
      // Define Bundler Build Feature Flags
      new webpack.DefinePlugin({
        // Drop Options API from bundle
        __VUE_OPTIONS_API__: false,
      }),
    },
  },
};
like image 672
ux.engineer Avatar asked Sep 26 '20 18:09

ux.engineer


People also ask

Is it recommended to configure your bundler in Vue?

It is recommended to configure your bundler - Stack Overflow You are running the esm-bundler build of Vue. It is recommended to configure your bundler Bookmark this question. Show activity on this post.

What are the __Vue_options_API__ and __Vue_prod_DevTools__ Flags?

Feature flags __VUE_OPTIONS_API__, __VUE_PROD_DEVTOOLS__ are not explicitly defined. You are running the esm-bundler build of Vue, which expects these compile-time feature flags to be globally injected via the bundler config in order to get better tree-shaking in the production bundle.

Why am I getting this error on my Vue 3 project?

It is recommended to configure your bundler Bookmark this question. Show activity on this post. I am getting this error on my Vue 3 project: You are running the esm-bundler build of Vue. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle.

How do I specify Globals in Webpack?

Finally we also have feature flags, a way to specify globals from your webpack.config.js. We do this with a plugin called DefinePlugin. It's called define plugin because it lets you define free variables. It is built into to webpack so all we really have to do is load webpack.


2 Answers

You can get info in this file: node_modules/@vue/cli-service/lib/config/base.js

  // feature flags <http://link.vuejs.org/feature-flags>
  webpackConfig
    .plugin('feature-flags')
      .use(require('webpack').DefinePlugin, [{
        __VUE_OPTIONS_API__: 'true',
        __VUE_PROD_DEVTOOLS__: 'false'
      }])

So config vue.config.js like this:

module.exports = {
 chainWebpack: config =>
   config.plugin('feature-flags').tap(args => {
     args[0].__VUE_OPTIONS_API__ = JSON.stringify(false)
     return args
   })
}

Or:

chainWebpack: config =>
  config
    .plugin('feature-flags')
    .use(require('webpack').DefinePlugin, [{
      __VUE_OPTIONS_API__: 'true',
      __VUE_PROD_DEVTOOLS__: 'false'
    }])
like image 178
Dreamoon Avatar answered Sep 18 '22 02:09

Dreamoon


What you wrote is almost correct. Just delete the configureWebpack key and define it like any other plugin.

const webpack = require('webpack');

module.exports = {
  plugins: [
    // Define Bundler Build Feature Flags
    new webpack.DefinePlugin({
      // Drop Options API from bundle
      __VUE_OPTIONS_API__: false,
    }),
  ],
};
like image 30
kyrylo Avatar answered Sep 19 '22 02:09

kyrylo