Skip to content

Commit

Permalink
Fix some odd bugs around compiling to JS and compilers that return an…
Browse files Browse the repository at this point in the history
… object rather than a JS string
  • Loading branch information
isaacs committed Mar 19, 2010
1 parent 803b165 commit 56c529e
Showing 1 changed file with 49 additions and 14 deletions.
63 changes: 49 additions & 14 deletions lib/module.js
Expand Up @@ -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;
}
Expand All @@ -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);
});
}

Expand Down Expand Up @@ -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);
Expand All @@ -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),
Expand Down

0 comments on commit 56c529e

Please sign in to comment.