700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > vue中webpack5版本报错If you want to include a polyfill you need If you don‘t want to include a polyfill

vue中webpack5版本报错If you want to include a polyfill you need If you don‘t want to include a polyfill

时间:2021-01-12 09:35:49

相关推荐

vue中webpack5版本报错If you want to include a polyfill you need   If you don‘t want to include a polyfill

错误提示

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: { "crypto": require.resolve("crypto-browserify") }'- install 'crypto-browserify'If you don't want to include a polyfill, you can use an empty module like this:resolve.fallback: {"crypto": false }

原因

使用webpack5的原因

v5 与 v4

v4以前附带了许多node.js核心模块的polyfill,在构建时给 bundle附加了庞大的polyfills,在大部分情况下,polyfills并不是必须。

现在v5将要停止这一切,在模块的应用中不再自动引入Polyfills,明显的减小了打包体积。

v4

在v4中,crypto模块会主动添加 polyfill,也就是crypto-browserify,我们运行的代码是不需要的,反而会使最后的包变大,影响编译速度

v5

在v5编译中,会出现polyfill添加提示,如果不需要node polyfille,按照提示 alias 设置为 false 即可

解决方法

1、安装

npm install crypto-browserify

2、配置package.json,新增以下配置项(其中browserdependencies等配置项同级别)

"browser": {"crypto": false},

配置后

{// ..."browser": {"crypto": false},"dependencies": {"crypto-browserify": "^3.12.0",// ...}}``

3、在webpack.config.js中增加配置项

// webpack.config.jsresolve: {// 1.不需要node polyfilssalias: {crypto: false},},

参考文章

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。