diff --git a/.gitignore b/.gitignore index 5e733d7..f961904 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ test/d3.chart.test-build.js +test/cjs-app.test-build.js bower_components/ diff --git a/Gruntfile.js b/Gruntfile.js index 546c0f7..8cb165c 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -22,7 +22,10 @@ module.exports = function(grunt) { grunt.registerTask("test-unit", ["mocha:unit"]); grunt.registerTask( "test-build", - ["concat:test", "mocha:exportsAmd", "mocha:exportsGlobal"] + [ + "concat:test", "mocha:exportsAmd", "mocha:exportsGlobal", + "browserify", "mocha:cjs" + ] ); grunt.registerTask("test", ["jshint", "jscs", "test-unit", "test-build"]); diff --git a/build/tasks/browserify.js b/build/tasks/browserify.js new file mode 100644 index 0000000..d090ba3 --- /dev/null +++ b/build/tasks/browserify.js @@ -0,0 +1,13 @@ +module.exports = function(grunt) { + "use strict"; + + grunt.config.set("browserify", { + foo: { + files: { + "test/cjs-app.test-build.js": ["test/cjs-app.js"] + } + } + }); + + grunt.loadNpmTasks("grunt-browserify"); +}; diff --git a/build/tasks/mocha.js b/build/tasks/mocha.js index 52c2a3a..c65f183 100644 --- a/build/tasks/mocha.js +++ b/build/tasks/mocha.js @@ -46,6 +46,16 @@ module.exports = function(grunt) { } } } + }, + cjs: { + src: ["test/index.html"], + options: { + page: { + settings: { + userAgent: "PhantomJS:testSource(cjs)" + } + } + } } }); diff --git a/package.json b/package.json index 3eb56d2..37428f6 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,12 @@ "test": "grunt test" }, "license": "MIT", + "dependencies": { + "d3": "^3.5.6" + }, "devDependencies": { "grunt": "~0.4.0", + "grunt-browserify": "^3.8.0", "grunt-cli": "~0.1.11", "grunt-contrib-concat": "~0.1.3", "grunt-contrib-jshint": "^0.9.2", diff --git a/src/wrapper/start.frag b/src/wrapper/start.frag index e2032d6..30306e8 100644 --- a/src/wrapper/start.frag +++ b/src/wrapper/start.frag @@ -1,10 +1,12 @@ (function(global, factory) { "use strict"; - if (typeof global.define === "function" && global.define.amd) { + if (typeof define === "function" && define.amd) { define(["d3"], function(d3) { factory(global, d3); }); + } else if (typeof require === "function") { + factory(global, require("d3")); } else { factory(global, global.d3); } diff --git a/test/cjs-app.js b/test/cjs-app.js new file mode 100644 index 0000000..7b0a013 --- /dev/null +++ b/test/cjs-app.js @@ -0,0 +1,6 @@ +var d3 = require("d3"); +require("./d3.chart.test-build"); + +// Explicitly leak the value exported by the `d3` module for use in the unit +// tests. +window.d3 = d3; diff --git a/test/index.html b/test/index.html index 15a319c..4845324 100644 --- a/test/index.html +++ b/test/index.html @@ -21,7 +21,7 @@

Test Sources

-

Direct | AMD* | Global*

+

Direct | AMD* | Global* | CJS

(* requires built file)

@@ -106,7 +106,13 @@

Test Sources

setup: function(done) { done(); } - } + }, + cjs: { + prereqs: ["cjs-app.test-build.js"], + setup: function(done) { + done(); + } + } }; var source = getSource(sources, location.search, navigator.userAgent);