Permalink
Browse files

Add support for non-CommonJS dependencies

  • Loading branch information...
1 parent 078c491 commit b045f5d91c345e25e970349072d99c1bd67a367e @sstephenson committed Mar 23, 2011
View
24 lib/stitch.js
@@ -34,15 +34,35 @@
}
exports.Package = Package = (function() {
function Package(config) {
- var _ref, _ref2, _ref3;
+ this.compileSources = __bind(this.compileSources, this);;
+ this.compileDependencies = __bind(this.compileDependencies, this);; var _ref, _ref2, _ref3, _ref4;
this.identifier = (_ref = config.identifier) != null ? _ref : 'require';
this.paths = (_ref2 = config.paths) != null ? _ref2 : ['lib'];
+ this.dependencies = (_ref3 = config.dependencies) != null ? _ref3 : [];
this.compilers = _.extend({}, compilers, config.compilers);
- this.cache = (_ref3 = config.cache) != null ? _ref3 : true;
+ this.cache = (_ref4 = config.cache) != null ? _ref4 : true;
this.mtimeCache = {};
this.compileCache = {};
}
Package.prototype.compile = function(callback) {
+ return async.parallel([this.compileDependencies, this.compileSources], function(err, parts) {
+ if (err) {
+ return callback(err);
+ } else {
+ return callback(null, parts.join("\n"));
+ }
+ });
+ };
+ Package.prototype.compileDependencies = function(callback) {
+ return async.map(this.dependencies, fs.readFile, __bind(function(err, dependencySources) {
+ if (err) {
+ return callback(err);
+ } else {
+ return callback(null, dependencySources.join("\n"));
+ }
+ }, this));
+ };
+ Package.prototype.compileSources = function(callback) {
return async.reduce(this.paths, {}, _.bind(this.gatherSourcesFromPath, this), __bind(function(err, sources) {
var filename, index, name, result, source, _ref;
if (err) {
View
20 src/stitch.coffee
@@ -26,15 +26,29 @@ catch err
exports.Package = class Package
constructor: (config) ->
- @identifier = config.identifier ? 'require'
- @paths = config.paths ? ['lib']
- @compilers = _.extend {}, compilers, config.compilers
+ @identifier = config.identifier ? 'require'
+ @paths = config.paths ? ['lib']
+ @dependencies = config.dependencies ? []
+ @compilers = _.extend {}, compilers, config.compilers
@cache = config.cache ? true
@mtimeCache = {}
@compileCache = {}
compile: (callback) ->
+ async.parallel [
+ @compileDependencies
+ @compileSources
+ ], (err, parts) ->
+ if err then callback err
+ else callback null, parts.join("\n")
+
+ compileDependencies: (callback) =>
+ async.map @dependencies, fs.readFile, (err, dependencySources) =>
+ if err then callback err
+ else callback null, dependencySources.join("\n")
+
+ compileSources: (callback) =>
async.reduce @paths, {}, _.bind(@gatherSourcesFromPath, @), (err, sources) =>
return callback err if err
View
1 test/fixtures/dependencies/backbone.js
@@ -0,0 +1 @@
+// Backbone
View
1 test/fixtures/dependencies/underscore.js
@@ -0,0 +1 @@
+// Underscore
View
1 test/fixtures/dependencies/zepto.js
@@ -0,0 +1 @@
+// Zepto
View
26 test/test_stitch.coffee
@@ -28,6 +28,17 @@ ecoOptions =
paths: [ecoFixtures]
ecoPackage = stitch.createPackage ecoOptions
+dependencyOptions =
+ identifier: "testRequire"
+ paths: [fixtures]
+ dependencies: [
+ fixtureRoot + "/dependencies/zepto.js"
+ fixtureRoot + "/dependencies/underscore.js"
+ fixtureRoot + "/dependencies/backbone.js"
+ ]
+dependencyPackage = stitch.createPackage dependencyOptions
+
+
load = (source, callback) ->
(-> eval source).call module = {}
callback? (source) -> (-> eval source).call module
@@ -41,6 +52,7 @@ rescue = (callback) ->
rescued = true
rescued
+
module.exports =
"walk tree": (test) ->
test.expect fixtureCount
@@ -261,6 +273,20 @@ module.exports =
test.ok rescue -> testRequire("circular/error")
test.done()
+ "dependencies option concatenates files in order": (test) ->
+ test.expect 5
+ dependencyPackage.compile (err, sources) ->
+ test.ok !err
+ lines = sources.split("\n").slice(0, 5)
+
+ test.same "// Zepto", lines[0]
+ test.same "// Underscore", lines[2]
+ test.same "// Backbone", lines[4]
+
+ testRequire = load sources
+ test.ok testRequire("foo/bar/baz")
+ test.done()
+
if stitch.compilers.eco
module.exports["eco compiler"] = (test) ->
test.expect 2

0 comments on commit b045f5d

Please sign in to comment.