为什么 webpack 在我的包中包含椭圆 bn.js 模块 [英] Why webpack includes elliptic bn.js modules in my bundle

查看:24
本文介绍了为什么 webpack 在我的包中包含椭圆 bn.js 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

webpack-bundle-analyzer 显示我的 vendor.js 中包含的椭圆和 bn.js但是这些模块没有在代码中使用,也没有包含在 package.json 中.

webpack-bundle-analyzer shows elliptic and bn.js included in my vendor.js But these modules are not used in code or included in package.json.

npm ls bn.js 给出:

npm ls bn.js gives:

├─┬ eslint-import-resolver-webpack@0.8.1
│ └─┬ node-libs-browser@1.1.1
│   └─┬ crypto-browserify@3.11.0
│     └─┬ browserify-sign@4.0.0
│       └── bn.js@4.11.6

推荐答案

Webpack 包含 ellipticbn.js(以及其他更小的模块)导入 crypto 作为代码中某处的依赖项.

Webpack includes elliptic and bn.js (and other, smaller modules) in your bundle if you import crypto as a dependency somewhere in your code.

为了避免这些巨大的依赖,你可以寻找一个特定的 npm 模块,它只提供你需要的功能.

To avoid these huge dependencies, you can look for a specific npm module which provides just the function(ality) you need.

比如我是导入crypto来做的;

const crypto = require('crypto');
const hmac = crypto.createHmac('sha1', buf);

... 相反(在这种情况下...),您可以安装 create-hmac 模块,然后做;

... instead (in this situation...), you can install the create-hmac module, and do;

const createHmac = require('create-hmac');
const hmac = createHmac('sha1', buf);

如果你需要一些动力;删除 crypto 作为依赖项,我们的 gzip 压缩包大小减少了 150Kb(但 YMMV 取决于您使用的加密方法).

In case you need some motivation; removing crypto as a dependancy trimmed 150Kb off our gzipped bundle size (but YMMV depending on which crypto methods you're using).

这篇关于为什么 webpack 在我的包中包含椭圆 bn.js 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆