-
Notifications
You must be signed in to change notification settings - Fork 22.9k
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
Support AMD/RequireJS. #1689
Support AMD/RequireJS. #1689
Conversation
When d3.js is loaded, it now prefers the AMD define function or module.exports if available; the global `d3` is only set if neither of these are available. A side benefit of this change is that browserify, bower and component can now load the D3 library directly rather than needing a custom definition.
Cool, thanks! I was just about to throw out another option based on this example. Pros: works with the (function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(factory);
/* EDIT: instead of this:
} else if (typeof exports === 'object') {
do this: */
} else if (typeof module === "object" && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.d3 = factory();
}
}(this, function () {
var d3 = {};
// make d3
return d3;
})); I'm fine leaving it as you have it now. |
The only effective difference is using |
Yes, I just noticed that after copying the example. I agree that |
It'd be less cludgy if everyone would just use AMD :) Ok I'll stop |
Folded into #1690. |
With regards to d3#1689 adding AMD compatibility, it seemed odd to me that D3 opt'd out of exposing itself globally for third-party plugins. Within a discussion about this very problem, found here: mpld3/mpld3#33 (comment) the argument is made that exposing the global defeats the point of module systems. I agree that module systems help avoid global pollution, but that is not something I'd consider the point of module systems, especially since they are registered in a global namespace themselves. This patch will fix third-party modules that are shimmed.
With regards to d3#1689 adding AMD compatibility, it seemed odd to me that D3 opt'd out of exposing itself globally for third-party plugins. Within a discussion about this very problem, found here: mpld3/mpld3#33 (comment) the argument is made that exposing the global defeats the point of module systems. I agree that module systems help avoid global pollution, but that is not something I'd consider the point of module systems, especially since they are registered in a global namespace themselves. This patch will fix third-party modules that are shimmed.
With regards to d3#1689 adding AMD compatibility, it seemed odd to me that D3 opt'd out of exposing itself globally for third-party plugins. Within a discussion about this very problem, found here: mpld3/mpld3#33 (comment) the argument is made that exposing the global defeats the point of module systems. I agree that module systems help avoid global pollution, but that is not something I'd consider the point of module systems, especially since they are registered in a global namespace themselves. This patch will fix third-party modules that are shimmed.
When d3.js is loaded, it now prefers the AMD define function or module.exports if available; the global
d3
is only set if neither of these are available. A side benefit of this change is that browserify, bower and component can now load the D3 library directly rather than needing a custom definition.