Home > Mobile >  Vue 3 - How to add Polyfills to ChainWebpack
Vue 3 - How to add Polyfills to ChainWebpack

Time:03-22

Using Vue 3, how do I add path-browserify to vue.config.js?

module.exports = {
    chainWebpack: config => {}
}

I am receiving the following error when compiling:

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
        - install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "path": false }

CodePudding user response:

Webpack 5 removed some things that Webpack 4 included in the bundle.

To get it all back in a vue3 app you can use the polyfill plugin. From a vanilla create-vue-app with babel:

> npm i node-polyfill-webpack-plugin

babel.config.js

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset'
  ]
}

vue.config.js

const { defineConfig } = require("@vue/cli-service");
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
module.exports = defineConfig({
  transpileDependencies: true,
  configureWebpack: {
    plugins: [new NodePolyfillPlugin()],
    optimization: {
      splitChunks: {
        chunks: "all",
      },
    },
  },
});

CodePudding user response:

With @Zack's help:

const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')

module.exports = {
    chainWebpack: config => {
        config.plugin('polyfills').use(NodePolyfillPlugin)
    },
}
  • Related