From d8c81665c59fa2f397dc0a63765eed4714599856 Mon Sep 17 00:00:00 2001 From: Jacob Beard Date: Thu, 31 May 2012 15:28:18 -0400 Subject: [PATCH 1/5] Added new feature: ability to pass in array of files to exclude from build, via 'excludes' property on configuration object. --- lib/stitch.js | 14 +++++++++----- src/stitch.coffee | 3 ++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/stitch.js b/lib/stitch.js index 967fb2b..0cbc6c5 100644 --- a/lib/stitch.js +++ b/lib/stitch.js @@ -1,6 +1,7 @@ (function() { - var CoffeeScript, Package, async, compilers, eco, extname, fs, join, normalize, _, _ref; - var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + var CoffeeScript, Package, async, compilers, eco, extname, fs, join, normalize, _, _ref, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; _ = require('underscore'); @@ -53,12 +54,13 @@ function Package(config) { this.compileSources = __bind(this.compileSources, this); this.compileDependencies = __bind(this.compileDependencies, this); - var _ref2, _ref3, _ref4, _ref5; + var _ref2, _ref3, _ref4, _ref5, _ref6; this.identifier = (_ref2 = config.identifier) != null ? _ref2 : 'require'; this.paths = (_ref3 = config.paths) != null ? _ref3 : ['lib']; this.dependencies = (_ref4 = config.dependencies) != null ? _ref4 : []; this.compilers = _.extend({}, compilers, config.compilers); - this.cache = (_ref5 = config.cache) != null ? _ref5 : true; + this.excludes = (_ref5 = config.excludes) != null ? _ref5 : []; + this.cache = (_ref6 = config.cache) != null ? _ref6 : true; this.mtimeCache = {}; this.compileCache = {}; } @@ -223,8 +225,10 @@ if (err) return callback(err); return async.forEach(files, function(file, next) { var filename; - if (file.match(/^\./)) return next(); filename = join(directory, file); + if ((__indexOf.call(_this.excludes, filename) >= 0) || file.match(/^\./)) { + return next(); + } return fs.stat(filename, function(err, stats) { var _ref2; _this.mtimeCache[filename] = stats != null ? (_ref2 = stats.mtime) != null ? _ref2.toString() : void 0 : void 0; diff --git a/src/stitch.coffee b/src/stitch.coffee index cecca07..e439fea 100644 --- a/src/stitch.coffee +++ b/src/stitch.coffee @@ -35,6 +35,7 @@ exports.Package = class Package @paths = config.paths ? ['lib'] @dependencies = config.dependencies ? [] @compilers = _.extend {}, compilers, config.compilers + @excludes = config.excludes ? [] @cache = config.cache ? true @mtimeCache = {} @@ -208,8 +209,8 @@ exports.Package = class Package return callback err if err async.forEach files, (file, next) => - return next() if file.match /^\./ filename = join directory, file + return next() if (filename in @excludes) or file.match /^\./ fs.stat filename, (err, stats) => @mtimeCache[filename] = stats?.mtime?.toString() From 266542ab8a9e7b4f1db43f7a7e1ebecfbd0a1e76 Mon Sep 17 00:00:00 2001 From: Jacob Beard Date: Mon, 11 Jun 2012 09:08:52 -0400 Subject: [PATCH 2/5] Fixed bug that was breaking build on win32. --- lib/stitch.js | 2 +- src/stitch.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/stitch.js b/lib/stitch.js index 0cbc6c5..32802bb 100644 --- a/lib/stitch.js +++ b/lib/stitch.js @@ -175,7 +175,7 @@ if (err) return callback(err); for (_i = 0, _len = expandedPaths.length; _i < _len; _i++) { expandedPath = expandedPaths[_i]; - base = expandedPath + "/"; + base = expandedPath + (process.platform === "win32" ? "\\" : "/"); if (sourcePath.indexOf(base) === 0) { return callback(null, sourcePath.slice(base.length)); } diff --git a/src/stitch.coffee b/src/stitch.coffee index e439fea..8ffaf08 100644 --- a/src/stitch.coffee +++ b/src/stitch.coffee @@ -172,7 +172,7 @@ exports.Package = class Package return callback err if err for expandedPath in expandedPaths - base = expandedPath + "/" + base = expandedPath + (if process.platform is "win32" then "\\" else "/") if sourcePath.indexOf(base) is 0 return callback null, sourcePath.slice base.length callback new Error "#{path} isn't in the require path" From 09abf44852c5298a09e69db8fee73d1a447538c6 Mon Sep 17 00:00:00 2001 From: Jacob Beard Date: Mon, 11 Jun 2012 11:01:53 -0400 Subject: [PATCH 3/5] Fixed another Windows build bug. Seems to work now. --- lib/stitch.js | 12 ++++++++---- src/stitch.coffee | 7 +++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/stitch.js b/lib/stitch.js index 32802bb..332f8a9 100644 --- a/lib/stitch.js +++ b/lib/stitch.js @@ -1,5 +1,5 @@ (function() { - var CoffeeScript, Package, async, compilers, eco, extname, fs, join, normalize, _, _ref, + var CoffeeScript, Package, async, compilers, eco, extname, fs, isWindows, join, normalize, _, _ref, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; @@ -49,6 +49,8 @@ } + isWindows = process.platform === "win32"; + exports.Package = Package = (function() { function Package(config) { @@ -171,13 +173,15 @@ return fs.realpath(path, function(err, sourcePath) { if (err) return callback(err); return async.map(_this.paths, fs.realpath, function(err, expandedPaths) { - var base, expandedPath, _i, _len; + var base, expandedPath, sp, _i, _len; if (err) return callback(err); for (_i = 0, _len = expandedPaths.length; _i < _len; _i++) { expandedPath = expandedPaths[_i]; - base = expandedPath + (process.platform === "win32" ? "\\" : "/"); + base = expandedPath + (isWindows ? "\\" : "/"); if (sourcePath.indexOf(base) === 0) { - return callback(null, sourcePath.slice(base.length)); + sp = sourcePath.slice(base.length); + if (isWindows) sp = sp.replace(/\\/g, "/"); + return callback(null, sp); } } return callback(new Error("" + path + " isn't in the require path")); diff --git a/src/stitch.coffee b/src/stitch.coffee index 8ffaf08..48a84ae 100644 --- a/src/stitch.coffee +++ b/src/stitch.coffee @@ -28,6 +28,7 @@ try module._compile content, filename catch err +isWindows = process.platform is "win32" exports.Package = class Package constructor: (config) -> @@ -172,9 +173,11 @@ exports.Package = class Package return callback err if err for expandedPath in expandedPaths - base = expandedPath + (if process.platform is "win32" then "\\" else "/") + base = expandedPath + (if isWindows then "\\" else "/") if sourcePath.indexOf(base) is 0 - return callback null, sourcePath.slice base.length + sp = sourcePath.slice base.length + if isWindows then sp = sp.replace /\\/g, "/" #replace backslashes with forward slashes + return callback null,sp callback new Error "#{path} isn't in the require path" compileFile: (path, callback) -> From 8c22c5877c54b6a0a392ed0db34d6f3f46ec8a1c Mon Sep 17 00:00:00 2001 From: Jacob Beard Date: Thu, 28 Mar 2013 15:54:19 -0400 Subject: [PATCH 4/5] Added support to stitch for generation of UMD modules. --- lib/stitch.js | 60 ++++++++++++++++++---------- src/stitch.coffee | 100 +++++++++++++++++++++++++++------------------- 2 files changed, 98 insertions(+), 62 deletions(-) diff --git a/lib/stitch.js b/lib/stitch.js index 332f8a9..81e0815 100644 --- a/lib/stitch.js +++ b/lib/stitch.js @@ -1,7 +1,8 @@ +// Generated by CoffeeScript 1.4.0 (function() { var CoffeeScript, Package, async, compilers, eco, extname, fs, isWindows, join, normalize, _, _ref, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, - __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; + __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; _ = require('underscore'); @@ -55,14 +56,17 @@ function Package(config) { this.compileSources = __bind(this.compileSources, this); + this.compileDependencies = __bind(this.compileDependencies, this); - var _ref2, _ref3, _ref4, _ref5, _ref6; - this.identifier = (_ref2 = config.identifier) != null ? _ref2 : 'require'; - this.paths = (_ref3 = config.paths) != null ? _ref3 : ['lib']; - this.dependencies = (_ref4 = config.dependencies) != null ? _ref4 : []; + + var _ref1, _ref2, _ref3, _ref4, _ref5; + this.rootModuleName = config.rootModuleName; + this.identifier = (_ref1 = config.identifier) != null ? _ref1 : 'require'; + this.paths = (_ref2 = config.paths) != null ? _ref2 : ['lib']; + this.dependencies = (_ref3 = config.dependencies) != null ? _ref3 : []; this.compilers = _.extend({}, compilers, config.compilers); - this.excludes = (_ref5 = config.excludes) != null ? _ref5 : []; - this.cache = (_ref6 = config.cache) != null ? _ref6 : true; + this.excludes = (_ref4 = config.excludes) != null ? _ref4 : []; + this.cache = (_ref5 = config.cache) != null ? _ref5 : true; this.mtimeCache = {}; this.compileCache = {}; } @@ -91,12 +95,14 @@ Package.prototype.compileSources = function(callback) { var _this = this; return async.reduce(this.paths, {}, _.bind(this.gatherSourcesFromPath, this), function(err, sources) { - var filename, index, name, result, source, _ref2; - if (err) return callback(err); - result = "(function(/*! Stitch !*/) {\n if (!this." + _this.identifier + ") {\n var modules = {}, cache = {}, require = function(name, root) {\n var path = expand(root, name), module = cache[path], fn;\n if (module) {\n return module.exports;\n } else if (fn = modules[path] || modules[path = expand(path, './index')]) {\n module = {id: path, exports: {}};\n try {\n cache[path] = module;\n fn(module.exports, function(name) {\n return require(name, dirname(path));\n }, module);\n return module.exports;\n } catch (err) {\n delete cache[path];\n throw err;\n }\n } else {\n throw 'module \\'' + name + '\\' not found';\n }\n }, expand = function(root, name) {\n var results = [], parts, part;\n if (/^\\.\\.?(\\/|$)/.test(name)) {\n parts = [root, name].join('/').split('/');\n } else {\n parts = name.split('/');\n }\n for (var i = 0, length = parts.length; i < length; i++) {\n part = parts[i];\n if (part == '..') {\n results.pop();\n } else if (part != '.' && part != '') {\n results.push(part);\n }\n }\n return results.join('/');\n }, dirname = function(path) {\n return path.split('/').slice(0, -1).join('/');\n };\n this." + _this.identifier + " = function(name) {\n return require(name, '');\n }\n this." + _this.identifier + ".define = function(bundle) {\n for (var key in bundle)\n modules[key] = bundle[key];\n };\n }\n return this." + _this.identifier + ".define;\n}).call(this)({"; + var filename, index, name, result, source, _ref1; + if (err) { + return callback(err); + } + result = "(function(/*! Stitch !*/) {\n\n var modules = {}, cache = {}, req = function(name, root) {\n var path = expand(root, name), module = cache[path], fn;\n if (module) {\n return module.exports;\n } else if (fn = modules[path] || modules[path = expand(path, './index')]) {\n module = {id: path, exports: {}};\n try {\n cache[path] = module;\n fn(module.exports, function(name) {\n return req(name, dirname(path));\n }, module);\n return module.exports;\n } catch (err) {\n delete cache[path];\n throw err;\n }\n } else {\n throw 'module \\'' + name + '\\' not found';\n }\n }, expand = function(root, name) {\n var results = [], parts, part;\n if (/^\\.\\.?(\\/|$)/.test(name)) {\n parts = [root, name].join('/').split('/');\n } else {\n parts = name.split('/');\n }\n for (var i = 0, length = parts.length; i < length; i++) {\n part = parts[i];\n if (part == '..') {\n results.pop();\n } else if (part != '.' && part != '') {\n results.push(part);\n }\n }\n return results.join('/');\n }, dirname = function(path) {\n return path.split('/').slice(0, -1).join('/');\n };\n\n return function(bundle) {\n for (var key in bundle){\n modules[key] = bundle[key];\n }\n\n //UMD\n if (typeof define === 'function' && define.amd) {\n // AMD. Register as a named module\n define('" + _this.rootModuleName + "',[],function(){\n return req('" + _this.rootModuleName + "','');\n });\n } else {\n // Browser globals\n this." + _this.rootModuleName + " = req('" + _this.rootModuleName + "','');\n\n //define global require\n if (!this." + _this.identifier + ") {\n this." + _this.identifier + " = function(name) {\n return req(name, '');\n }\n }\n }\n };\n}).call(this)({"; index = 0; for (name in sources) { - _ref2 = sources[name], filename = _ref2.filename, source = _ref2.source; + _ref1 = sources[name], filename = _ref1.filename, source = _ref1.source; result += index++ === 0 ? "" : ", "; result += JSON.stringify(name); result += ": function(exports, require, module) {" + source + "}"; @@ -131,10 +137,14 @@ Package.prototype.gatherSourcesFromPath = function(sources, sourcePath, callback) { var _this = this; return fs.stat(sourcePath, function(err, stat) { - if (err) return callback(err); + if (err) { + return callback(err); + } if (stat.isDirectory()) { return _this.getFilesInTree(sourcePath, function(err, paths) { - if (err) return callback(err); + if (err) { + return callback(err); + } return async.reduce(paths, sources, _.bind(_this.gatherCompilableSource, _this), callback); }); } else { @@ -147,7 +157,9 @@ var _this = this; if (this.compilers[extname(path).slice(1)]) { return this.getRelativePath(path, function(err, relativePath) { - if (err) return callback(err); + if (err) { + return callback(err); + } return _this.compileFile(path, function(err, source) { var extension, key; if (err) { @@ -171,16 +183,22 @@ Package.prototype.getRelativePath = function(path, callback) { var _this = this; return fs.realpath(path, function(err, sourcePath) { - if (err) return callback(err); + if (err) { + return callback(err); + } return async.map(_this.paths, fs.realpath, function(err, expandedPaths) { var base, expandedPath, sp, _i, _len; - if (err) return callback(err); + if (err) { + return callback(err); + } for (_i = 0, _len = expandedPaths.length; _i < _len; _i++) { expandedPath = expandedPaths[_i]; base = expandedPath + (isWindows ? "\\" : "/"); if (sourcePath.indexOf(base) === 0) { sp = sourcePath.slice(base.length); - if (isWindows) sp = sp.replace(/\\/g, "/"); + if (isWindows) { + sp = sp.replace(/\\/g, "/"); + } return callback(null, sp); } } @@ -226,7 +244,9 @@ Package.prototype.walkTree = function(directory, callback) { var _this = this; return fs.readdir(directory, function(err, files) { - if (err) return callback(err); + if (err) { + return callback(err); + } return async.forEach(files, function(file, next) { var filename; filename = join(directory, file); @@ -234,8 +254,8 @@ return next(); } return fs.stat(filename, function(err, stats) { - var _ref2; - _this.mtimeCache[filename] = stats != null ? (_ref2 = stats.mtime) != null ? _ref2.toString() : void 0 : void 0; + var _ref1; + _this.mtimeCache[filename] = stats != null ? (_ref1 = stats.mtime) != null ? _ref1.toString() : void 0 : void 0; if (!err && stats.isDirectory()) { return _this.walkTree(filename, function(err, filename) { if (filename) { diff --git a/src/stitch.coffee b/src/stitch.coffee index 48a84ae..1a49623 100644 --- a/src/stitch.coffee +++ b/src/stitch.coffee @@ -32,6 +32,7 @@ isWindows = process.platform is "win32" exports.Package = class Package constructor: (config) -> + @rootModuleName = config.rootModuleName @identifier = config.identifier ? 'require' @paths = config.paths ? ['lib'] @dependencies = config.dependencies ? [] @@ -61,54 +62,69 @@ exports.Package = class Package result = """ (function(/*! Stitch !*/) { - if (!this.#{@identifier}) { - var modules = {}, cache = {}, require = function(name, root) { - var path = expand(root, name), module = cache[path], fn; - if (module) { + + var modules = {}, cache = {}, req = function(name, root) { + var path = expand(root, name), module = cache[path], fn; + if (module) { + return module.exports; + } else if (fn = modules[path] || modules[path = expand(path, './index')]) { + module = {id: path, exports: {}}; + try { + cache[path] = module; + fn(module.exports, function(name) { + return req(name, dirname(path)); + }, module); return module.exports; - } else if (fn = modules[path] || modules[path = expand(path, './index')]) { - module = {id: path, exports: {}}; - try { - cache[path] = module; - fn(module.exports, function(name) { - return require(name, dirname(path)); - }, module); - return module.exports; - } catch (err) { - delete cache[path]; - throw err; - } - } else { - throw 'module \\'' + name + '\\' not found'; + } catch (err) { + delete cache[path]; + throw err; } - }, expand = function(root, name) { - var results = [], parts, part; - if (/^\\.\\.?(\\/|$)/.test(name)) { - parts = [root, name].join('/').split('/'); - } else { - parts = name.split('/'); + } else { + throw 'module \\'' + name + '\\' not found'; + } + }, expand = function(root, name) { + var results = [], parts, part; + if (/^\\.\\.?(\\/|$)/.test(name)) { + parts = [root, name].join('/').split('/'); + } else { + parts = name.split('/'); + } + for (var i = 0, length = parts.length; i < length; i++) { + part = parts[i]; + if (part == '..') { + results.pop(); + } else if (part != '.' && part != '') { + results.push(part); } - for (var i = 0, length = parts.length; i < length; i++) { - part = parts[i]; - if (part == '..') { - results.pop(); - } else if (part != '.' && part != '') { - results.push(part); + } + return results.join('/'); + }, dirname = function(path) { + return path.split('/').slice(0, -1).join('/'); + }; + + return function(bundle) { + for (var key in bundle){ + modules[key] = bundle[key]; + } + + //UMD + if (typeof define === 'function' && define.amd) { + // AMD. Register as a named module + define('#{@rootModuleName}',[],function(){ + return req('#{@rootModuleName}',''); + }); + } else { + // Browser globals + this.#{@rootModuleName} = req('#{@rootModuleName}',''); + + //define global require + if (!this.#{@identifier}) { + this.#{@identifier} = function(name) { + return req(name, ''); } } - return results.join('/'); - }, dirname = function(path) { - return path.split('/').slice(0, -1).join('/'); - }; - this.#{@identifier} = function(name) { - return require(name, ''); } - this.#{@identifier}.define = function(bundle) { - for (var key in bundle) - modules[key] = bundle[key]; - }; - } - return this.#{@identifier}.define; + }; }).call(this)({ """ From de7d3a70088d973c67846a473715413a569d35bd Mon Sep 17 00:00:00 2001 From: Jacob Beard Date: Mon, 1 Apr 2013 10:29:36 -0400 Subject: [PATCH 5/5] Generated anonymous AMD module, rather than named. --- lib/stitch.js | 2 +- src/stitch.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/stitch.js b/lib/stitch.js index 81e0815..91a38e4 100644 --- a/lib/stitch.js +++ b/lib/stitch.js @@ -99,7 +99,7 @@ if (err) { return callback(err); } - result = "(function(/*! Stitch !*/) {\n\n var modules = {}, cache = {}, req = function(name, root) {\n var path = expand(root, name), module = cache[path], fn;\n if (module) {\n return module.exports;\n } else if (fn = modules[path] || modules[path = expand(path, './index')]) {\n module = {id: path, exports: {}};\n try {\n cache[path] = module;\n fn(module.exports, function(name) {\n return req(name, dirname(path));\n }, module);\n return module.exports;\n } catch (err) {\n delete cache[path];\n throw err;\n }\n } else {\n throw 'module \\'' + name + '\\' not found';\n }\n }, expand = function(root, name) {\n var results = [], parts, part;\n if (/^\\.\\.?(\\/|$)/.test(name)) {\n parts = [root, name].join('/').split('/');\n } else {\n parts = name.split('/');\n }\n for (var i = 0, length = parts.length; i < length; i++) {\n part = parts[i];\n if (part == '..') {\n results.pop();\n } else if (part != '.' && part != '') {\n results.push(part);\n }\n }\n return results.join('/');\n }, dirname = function(path) {\n return path.split('/').slice(0, -1).join('/');\n };\n\n return function(bundle) {\n for (var key in bundle){\n modules[key] = bundle[key];\n }\n\n //UMD\n if (typeof define === 'function' && define.amd) {\n // AMD. Register as a named module\n define('" + _this.rootModuleName + "',[],function(){\n return req('" + _this.rootModuleName + "','');\n });\n } else {\n // Browser globals\n this." + _this.rootModuleName + " = req('" + _this.rootModuleName + "','');\n\n //define global require\n if (!this." + _this.identifier + ") {\n this." + _this.identifier + " = function(name) {\n return req(name, '');\n }\n }\n }\n };\n}).call(this)({"; + result = "(function(/*! Stitch !*/) {\n\n var modules = {}, cache = {}, req = function(name, root) {\n var path = expand(root, name), module = cache[path], fn;\n if (module) {\n return module.exports;\n } else if (fn = modules[path] || modules[path = expand(path, './index')]) {\n module = {id: path, exports: {}};\n try {\n cache[path] = module;\n fn(module.exports, function(name) {\n return req(name, dirname(path));\n }, module);\n return module.exports;\n } catch (err) {\n delete cache[path];\n throw err;\n }\n } else {\n throw 'module \\'' + name + '\\' not found';\n }\n }, expand = function(root, name) {\n var results = [], parts, part;\n if (/^\\.\\.?(\\/|$)/.test(name)) {\n parts = [root, name].join('/').split('/');\n } else {\n parts = name.split('/');\n }\n for (var i = 0, length = parts.length; i < length; i++) {\n part = parts[i];\n if (part == '..') {\n results.pop();\n } else if (part != '.' && part != '') {\n results.push(part);\n }\n }\n return results.join('/');\n }, dirname = function(path) {\n return path.split('/').slice(0, -1).join('/');\n };\n\n return function(bundle) {\n for (var key in bundle){\n modules[key] = bundle[key];\n }\n\n //UMD\n if (typeof define === 'function' && define.amd) {\n // AMD. Register as a named module\n define([],function(){\n return req('" + _this.rootModuleName + "','');\n });\n } else {\n // Browser globals\n this." + _this.rootModuleName + " = req('" + _this.rootModuleName + "','');\n\n //define global require\n if (!this." + _this.identifier + ") {\n this." + _this.identifier + " = function(name) {\n return req(name, '');\n }\n }\n }\n };\n}).call(this)({"; index = 0; for (name in sources) { _ref1 = sources[name], filename = _ref1.filename, source = _ref1.source; diff --git a/src/stitch.coffee b/src/stitch.coffee index 1a49623..4d99e3f 100644 --- a/src/stitch.coffee +++ b/src/stitch.coffee @@ -110,7 +110,7 @@ exports.Package = class Package //UMD if (typeof define === 'function' && define.amd) { // AMD. Register as a named module - define('#{@rootModuleName}',[],function(){ + define([],function(){ return req('#{@rootModuleName}',''); }); } else {