Commit 99ff885b authored by Mike Bostock's avatar Mike Bostock
Browse files

Support AMD/RequireJS.

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.
parent 3234f47a
......@@ -18,7 +18,6 @@ console.log(JSON.stringify({
"Makefile",
"bin",
"component.json",
"index-browserify.js",
"index.js",
"lib",
"node_modules",
......
......@@ -9,10 +9,9 @@ console.log(JSON.stringify({
"description": package.description,
"keywords": package.keywords,
"version": package.version,
"main": "index-browserify.js",
"main": "d3.js",
"scripts": [
"d3.js",
"index-browserify.js"
"d3.js"
],
"dependencies": {},
"development": {},
......
#!/usr/bin/env node
console.log("d3 = (function(){\n var d3 = {version: " + JSON.stringify(require("../package.json").version) + "}; // semver");
console.log("!function(){\n var d3 = {version: " + JSON.stringify(require("../package.json").version) + "}; // semver");
......@@ -14,7 +14,6 @@
"Makefile",
"bin",
"component.json",
"index-browserify.js",
"index.js",
"lib",
"node_modules",
......
......@@ -11,10 +11,9 @@
"canvas"
],
"version": "3.3.13",
"main": "index-browserify.js",
"main": "d3.js",
"scripts": [
"d3.js",
"index-browserify.js"
"d3.js"
],
"dependencies": {},
"development": {},
......
d3 = function() {
!function() {
var d3 = {
version: "3.3.13"
};
......@@ -9289,5 +9289,11 @@ d3 = function() {
d3.xml = d3_xhrType(function(request) {
return request.responseXML;
});
return d3;
if (typeof define === "function" && define.amd) {
define(d3);
} else if (typeof module === "object" && module.exports) {
module.exports = d3;
} else {
this.d3 = d3;
}
}();
\ No newline at end of file
This diff is collapsed.
require("./d3");
module.exports = d3;
(function () { delete this.d3; })(); // unset global
var fs = require("fs"),
path = require("path"),
document = require("jsdom").jsdom("<html><head></head><body></body></html>"),
window = document.createWindow();
var document = require("jsdom").jsdom("<html><head></head><body></body></html>"),
window = document.createWindow(),
globals = {};
// stash globals
if ("window" in global) globals.window = global.window;
if ("document" in global) globals.document = global.document;
global.window = window;
global.document = document;
// https://github.com/chad3814/CSSStyleDeclaration/issues/3
var CSSStyleDeclaration_prototype = window.CSSStyleDeclaration.prototype,
......@@ -10,6 +15,8 @@ CSSStyleDeclaration_prototype.setProperty = function(name, value, priority) {
return CSSStyleDeclaration_setProperty.call(this, name + "", value == null ? null : value + "", priority == null ? null : priority + "");
};
module.exports = (new Function("window", "document",
"return " + fs.readFileSync(path.join(__dirname, "d3.js"), "utf-8"))
)(window, document);
module.exports = require("./d3");
// restore globals
if ("window" in globals) global.window = globals.window;
if ("document" in globals) global.document = globals.document;
return d3;
})();
if (typeof define === "function" && define.amd) {
define(d3);
} else if (typeof module === "object" && module.exports) {
module.exports = d3;
} else {
this.d3 = d3;
}
}();
d3 = (function(){
!function(){
var d3 = {version: "3.3.13"}; // semver
var vows = require("vows"),
_ = require("../../"),
load = require("../load"),
assert = require("../assert");
......@@ -93,7 +94,7 @@ suite.addBatch({
},
"the returned cells’ have positive area": function(cells) {
cells.forEach(function(cell) {
assert.ok(d3.geom.polygon(cell).area() > 0);
assert.ok(_.geom.polygon(cell).area() > 0);
});
}
},
......@@ -206,7 +207,7 @@ suite.addBatch({
]);
},
"the returned cell has positive area": function(cells) {
assert.ok(d3.geom.polygon(cells[0]).area() > 0);
assert.ok(_.geom.polygon(cells[0]).area() > 0);
}
},
"with two points": {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment