-
Notifications
You must be signed in to change notification settings - Fork 475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Crypto dependency issue when bundling with browserify or webpack #5
Comments
Not yet fully tested, but the following seems to work when bundled via webpack: test for amd first. if ( typeof define == 'function' && define.amd ) {
// AMD.
crypto = global['crypto'];
define(function () {
return DecimalConstructor
});
}
else if ( typeof module != 'undefined' && module && module.exports ) {
// Node and other CommonJS-like environments that support module.exports.
module.exports = DecimalConstructor;
if ( typeof require == 'function' ) {
crypto = require('crypto');
}
} else {
// Browser.
crypto = global['crypto'];
noConflict = global['Decimal'];
DecimalConstructor['noConflict'] = function () {
global['Decimal'] = noConflict;
return DecimalConstructor;
};
global['Decimal'] = DecimalConstructor;
} |
Looks good, Jos. Fixed in v2.1.0. I am also pushing v3.0.0 in a minute or two. Only one small API change - a simplification of |
Thanks a lot for this fast fix Michael! It works great now with webpack. With browserify I have still some trouble. When I bundle decimal.js like this (imagine as part of a larger application):
And load it in a browser like this:
I still get an error that crypto is missing. I think this can be solved by adding a check whether // ...
else if ( typeof module != 'undefined' && module && module.exports ) {
// Node and other CommonJS-like environments that support module.exports.
module.exports = DecimalConstructor;
if (typeof window !== 'undefined' ) {
// browserified app
crypto = global['crypto'];
}
else if ( typeof _dereq_ == 'function' ) {
crypto = _dereq_('crypto');
}
} else {
// ... |
Fixed in v3.0.1. I've taken a Let me know if you have any issues with it. |
Awesome, your try/catch solution is indeed a better solution. I can now bundle and load decimal.js in any way I can imagine and in it loads without errors. Thanks! p.s. I really hope this kind of dependency issues will become easier once ES6 modules is adopted by all browsers... |
I hope nobody minds me leaving this note here - it took me a bit of time to realize this library is what was injecting the entire The way I got around it being injected for Browserify was by just marking it as external. It might be worth documenting this somewhere? |
Yes, crypto is just used for random number generation. I think a check to see if |
After looking through the Browserify docs, I see that the The solution seems to be to add the following to the package.json here
That should avoid the need to exclude the |
@MikeMcl Hi. I just want yo say that using the Thanks a bunch for leaving that note, @ryanmcgrath. |
Okay, I'll have a look at this again. I want to get it right as I am pushing a major update in a couple of weeks. |
Decimal.js has a dependency on the
crypto
library. When bundling decimal.js as part of a larger application using browserify or webpack, a browser compatible version of crypto is bundled with the app by default. This works, but crypto is quite a large dependency.Looking at the code of decimal.js, the intention is to use native crypto functionality when in the browser, and use the crypto library when in node.js.
I can exclude crypto when bundling my application. That works fine in node.js, but not in the browser via AMD/require.js: decimal.js thinks that we are in a node.js environment because
module
andmodule.exports
are defined, and tries to load crypto viarequire
rather than using the browsers native crypto implementation (see L3598).It would be great if this loading of crypto could be rewritten such that the browser environment is correctly detected. Maybe by just checking the ducktype way: first check whether
window
andwindow.crypto
exist, and if not, fallback to loading viarequire('crypto')
, and if that fails, silently disable the crypto functionality.The text was updated successfully, but these errors were encountered: