最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue 3 - How to add Polyfills to ChainWebpack - Stack Overflow

programmeradmin0浏览0评论

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 }

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 }
Share Improve this question asked Mar 16, 2022 at 10:44 Ryan PrentissRyan Prentiss 1,6423 gold badges29 silver badges49 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 11

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",
      },
    },
  },
});

With @Zack's help, using chainWebpack:

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

module.exports = {
    chainWebpack: config => {
        config.plugin('polyfills').use(NodePolyfillPlugin)
    },
}
发布评论

评论列表(0)

  1. 暂无评论