bitwiseshiftleft / sjcl

Stanford Javascript Crypto Library
http://bitwiseshiftleft.github.com/sjcl/
Other
7.18k stars 987 forks source link

Webpack bundle size and require('crypto') #345

Closed dominic-p closed 6 years ago

dominic-p commented 7 years ago

When I import sjcl I notice that webpack adds a browser compatible version of the node crypto module to my bundle (significantly increasing the size).

From what I've read, this should be avoidable, but the code where the require is made already seems to be doing everything right to avoid this.

I'm not sure if this is a bug with the way the crypto module is required (maybe the conditional just needs to be switched so the check for window comes before the check for module?), or a bug with webpack, or a problem with my webpack configuration. But, I wanted bring it up here to see what the devs think.

Thanks, btw, for the fantastic library.

ryanand26 commented 6 years ago

I've just ran into this myself and had to work around it by setting externals: { crypto: 'null'} in my webpack config. That drops it into the right condition for the browser. IgnorePlugin would probably be better but i couldn't get it to match.

As for why this is happening my best guess is that because this code is contained in an immediately executed function it's getting picked up by webpack?

Nilos commented 6 years ago

I fixed this in one of my projects by telling webpack to not parse sjcl.js at all:

noParse: [
  /sjcl\.js$/,
]
dominic-p commented 6 years ago

@ryanand26's solution works perfectly for me. Should I close this?

jelhan commented 5 years ago

Please consider to reopen this one. It's annoying to fix that issue in all applications that uses sjcl together with webpack. In a related issue @ef4 suggested this fix:

The right place to fix this is still upstream in sjcl, probably by having separate entrypoint for browesr vs node, and using the main and browser fields in package.json to distinguish them.