geeeger / blog

https://loofp.com
1 stars 0 forks source link

crypto-js v4.0.0 依赖使用指北 #18

Open geeeger opened 4 years ago

geeeger commented 4 years ago

近期,将crypto-js bump to 4.0,发现webpack打包出来大了600k.

遂查看crypto-js源码。发现这一行

根源在于

            // Native crypto import via require (NodeJS)
        if (!crypto && typeof require === 'function') {
            try {
                crypto = require('crypto');
            } catch (err) {}
        }

webpack打包umd把crypto给解析了,使用polyfill模块替代了。

ok,

webpack添加配置

externals: {
    crypto: 'crypto'
}

将crypto作为可选依赖,不进入打包。