From 56c529e502e7814b7b0420078d88b3e6404645de Mon Sep 17 00:00:00 2001 From: isaacs Date: Fri, 19 Mar 2010 15:06:02 -0700 Subject: [PATCH] Fix some odd bugs around compiling to JS and compilers that return an object rather than a JS string --- lib/module.js | 63 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/lib/module.js b/lib/module.js index 414e6cd1fec..a86a4f26642 100644 --- a/lib/module.js +++ b/lib/module.js @@ -90,10 +90,20 @@ function loadModuleSync (module) { var data = module.load(); if ("string" === typeof data) { data = module.compile(data); + } else if (data !== undefined) { + module.exports = data; } if ("string" === typeof data && module.ext !== ".js") { // if it's not already JS, it can compile to a JS string. - module.compile(data, ".js"); + module.compileJS(data); + } else if (data !== undefined) { + // this is a safe usage. However, since it's just been + // put in the cache, and has been the subject of a require(), + // the lock may already be set. + var locked = module.locked; + module.locked = false; + module.exports = data; + module.locked = locked; } return module.exports; } @@ -102,17 +112,26 @@ function loadModule (module, cb) { if (er) return cb(er); if ("string" === typeof data && module.ext !== ".js") { // if it's not already JS, it can compile to a JS string. - return module.compile(data, ".js", cb); + return module.compileJS(data, cb); + } else if (data !== undefined) { + // this is a safe usage. However, since it's just been + // put in the cache, and has been the subject of a require(), + // the lock may already be set. + var locked = module.locked; + module.locked = false; + module.exports = data; + module.locked = locked; } cb(null, module.exports); } module.load(function (er, data) { if (er) return cb(er); if ("string" === typeof data) { - module.compile(data, firstCompile); - } else { - cb(null, module.exports); + return module.compile(data, firstCompile); + } else if (data !== undefined) { + module.exports = data; } + cb(null, module.exports); }); } @@ -286,6 +305,10 @@ Module.prototype.load = function (cb) { loader = exports.extensionCache[ this.ext ]; loader = loader && loader.load; } + if (!loader && (this.ext !== ".js")) { + loader = exports.extensionCache[ ".js" ]; + loader = loader && loader.load; + } if (!loader) { var er = new Error("No loader found for "+this.uri); if (cb) cb(er); @@ -295,19 +318,31 @@ Module.prototype.load = function (cb) { this.load = loader; return this.load(cb); } -Module.prototype.compile = function (code, cb) { - var compiler = exports.extensionCache[ this.ext ]; +// mod.compile(code, cb) +// if ext is provided, then pretend it's that kind of module. +function getCompiler (ext) { + ext = ext || ".js"; + var compiler = exports.extensionCache[ ext ]; compiler = compiler && compiler.compile; - if (!compiler) { - var er = new Error( - "No compiler registered for "+this.ext+" modules"); - if (cb) cb(er); - else throw er; - return er; + if (!compiler && (ext !== ".js")) { + return getCompiler(".js"); } - this.compile = compiler; + return compiler; +} +Module.prototype.compile = function (code, cb) { + this.compile = getCompiler(this.ext); return this.compile(code, cb); } +Module.prototype.compileJS = function (code, cb) { + // if its already been compiled as a JS module, + // then don't compile it again. Set to noop here + // so that the logic is simpler later on. + this.compileJS = getCompiler(".js"); + if (this.compile === this.compileJS) { + this.compileJS = noop; + } + return this.compileJS(code, cb); +} Module.prototype.waitForChildren = function (cb) { var children = this.children, expecting = Object.keys(this.womb),