From 16bafb6999a74bc22a2ae7c3aef1412eb8dac16f Mon Sep 17 00:00:00 2001 From: ngn Date: Sun, 24 Mar 2013 03:54:54 +0200 Subject: [PATCH] passing the exports object as "this", issue #13 --- lib/stitch.js | 40 +++++++++++++++++++++++++++++----------- src/stitch.coffee | 4 ++-- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/stitch.js b/lib/stitch.js index 2688d3d..2cff12d 100644 --- a/lib/stitch.js +++ b/lib/stitch.js @@ -1,6 +1,6 @@ -// Generated by CoffeeScript 1.3.3 +// Generated by CoffeeScript 1.6.2 (function() { - var CoffeeScript, Package, async, compilers, eco, extname, fs, join, normalize, _, _ref, + var CoffeeScript, Package, async, compilers, eco, err, extname, fs, join, normalize, _, _ref, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; _ = require('underscore'); @@ -14,6 +14,7 @@ exports.compilers = compilers = { js: function(module, filename) { var content; + content = fs.readFileSync(filename, 'utf8'); return module._compile(content, filename); } @@ -23,11 +24,12 @@ CoffeeScript = require('coffee-script'); compilers.coffee = function(module, filename) { var content; + content = CoffeeScript.compile(fs.readFileSync(filename, 'utf8')); return module._compile(content, filename); }; - } catch (err) { - + } catch (_error) { + err = _error; } try { @@ -35,28 +37,28 @@ if (eco.precompile) { compilers.eco = function(module, filename) { var content; + content = eco.precompile(fs.readFileSync(filename, 'utf8')); return module._compile("module.exports = " + content, filename); }; } else { compilers.eco = function(module, filename) { var content; + content = eco.compile(fs.readFileSync(filename, 'utf8')); return module._compile(content, filename); }; } - } catch (err) { - + } catch (_error) { + err = _error; } exports.Package = Package = (function() { - function Package(config) { this.compileSources = __bind(this.compileSources, this); - this.compileDependencies = __bind(this.compileDependencies, this); - var _ref1, _ref2, _ref3, _ref4; + this.identifier = (_ref1 = config.identifier) != null ? _ref1 : 'require'; this.paths = (_ref2 = config.paths) != null ? _ref2 : ['lib']; this.dependencies = (_ref3 = config.dependencies) != null ? _ref3 : []; @@ -78,6 +80,7 @@ Package.prototype.compileDependencies = function(callback) { var _this = this; + return async.map(this.dependencies, fs.readFile, function(err, dependencySources) { if (err) { return callback(err); @@ -89,12 +92,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, _ref1; + 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)({"; + 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.apply(module.exports, [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)({"; index = 0; for (name in sources) { _ref1 = sources[name], filename = _ref1.filename, source = _ref1.source; @@ -109,9 +114,11 @@ Package.prototype.createServer = function() { var _this = this; + return function(req, res, next) { return _this.compile(function(err, source) { var message; + if (err) { console.error("" + err.stack); message = "" + err.stack; @@ -131,6 +138,7 @@ Package.prototype.gatherSourcesFromPath = function(sources, sourcePath, callback) { var _this = this; + return fs.stat(sourcePath, function(err, stat) { if (err) { return callback(err); @@ -150,6 +158,7 @@ Package.prototype.gatherCompilableSource = function(sources, path, callback) { var _this = this; + if (this.compilers[extname(path).slice(1)]) { return this.getRelativePath(path, function(err, relativePath) { if (err) { @@ -157,6 +166,7 @@ } return _this.compileFile(path, function(err, source) { var extension, key; + if (err) { return callback(err); } else { @@ -177,12 +187,14 @@ Package.prototype.getRelativePath = function(path, callback) { var _this = this; + 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; + if (err) { return callback(err); } @@ -200,6 +212,7 @@ Package.prototype.compileFile = function(path, callback) { var compile, extension, mod, mtime, source; + extension = extname(path).slice(1); if (this.cache && this.compileCache[path] && this.mtimeCache[path] === this.compileCache[path].mtime) { return callback(null, this.compileCache[path].source); @@ -219,7 +232,8 @@ }; } return callback(null, source); - } catch (err) { + } catch (_error) { + err = _error; if (err instanceof Error) { err.message = "can't compile " + path + "\n" + err.message; } else { @@ -234,18 +248,21 @@ Package.prototype.walkTree = function(directory, callback) { var _this = this; + return fs.readdir(directory, function(err, files) { if (err) { return callback(err); } return async.forEach(files, function(file, next) { var filename; + if (file.match(/^\./)) { return next(); } filename = join(directory, file); return fs.stat(filename, function(err, stats) { 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) { @@ -266,6 +283,7 @@ Package.prototype.getFilesInTree = function(directory, callback) { var files; + files = []; return this.walkTree(directory, function(err, filename) { if (err) { diff --git a/src/stitch.coffee b/src/stitch.coffee index cecca07..b8967ed 100644 --- a/src/stitch.coffee +++ b/src/stitch.coffee @@ -68,9 +68,9 @@ exports.Package = class Package module = {id: path, exports: {}}; try { cache[path] = module; - fn(module.exports, function(name) { + fn.apply(module.exports, [module.exports, function(name) { return require(name, dirname(path)); - }, module); + }, module]); return module.exports; } catch (err) { delete cache[path];