Permalink
Browse files

Use modules' local instances of CoffeeScript too.

When we handle ._coffee files, use the module's local CoffeeScript dependency
for compiling the CoffeeScript to JS.

Added a loader example showing this in action. Failed before, works now.
  • Loading branch information...
1 parent 8b05d42 commit 7dee195cbe9434f5b42682afa72508041a641b83 @aseemk aseemk committed May 27, 2012
@@ -0,0 +1,3 @@
+console.log 'hello ...'
+setTimeout _, 1000
+console.log '... world!'
View
@@ -0,0 +1,13 @@
+// Before running this, run the following in this directory:
+// $ mkdir node_modules
+// $ npm install coffee-script
+//
+// Then run this file from the root directory as follows:
+// $ node examples/loader/loader2
+//
+// Streamline should find the local CoffeeScript instance for this module.
+
+require('coffee-script');
+require('../..').register();
+
+require('./hello2');
View
@@ -91,7 +91,7 @@ exports.compileFile = function(_, js_, options) {
content = shebangparse[1];
if (language === "coffee") {
- var coffee = require("../util/coffee-script");
+ var coffee = require("../util/coffee-loader").load();
content = coffee.compile(content, {
filename: js_
});
@@ -383,7 +383,7 @@ exports.compile = function(_, paths, options) {
var jsPath = path.substring(0, path.length - ext.length) + ".js";
if (options.force || mtime(_, path) > mtime(_, jsPath)) {
var source = fs.readFile(path, "utf8", _);
- var coffee = require("../util/coffee-script");
+ var coffee = require("../util/coffee-loader").load();
var compiled = coffee.compile(source, {
filename: path
});
View
@@ -91,7 +91,7 @@ exports.compileFile = function exports_compileFile__1(_, js_, options) { var ext
content = shebangparse[1];
if ((language === "coffee")) {
- coffee = require("../util/coffee-script");
+ coffee = require("../util/coffee-loader").load();
content = coffee.compile(content, {
filename: js_ }); } ;
@@ -383,7 +383,7 @@ exports.compile = function exports_compile__3(_, paths, options) { var failed, t
jsPath = (path.substring(0, (path.length - ext.length)) + ".js"); return (function __$_compile(_) {
var __2 = options.force; if (__2) { return _(null, __2); } ; return mtime(__cb(_, __frame, 14, 26, function ___(__0, __4) { return mtime(__cb(_, __frame, 14, 43, function ___(__0, __5) { var __3 = (__4 > __5); return _(null, __3); }, true), jsPath); }, true), path); })(__cb(_, __frame, -369, 6, function ___(__0, __5) { return (function __$_compile(__then) { if (__5) {
return fs.readFile(path, "utf8", __cb(_, __frame, 15, 19, function ___(__0, __6) { source = __6;
- coffee = require("../util/coffee-script");
+ coffee = require("../util/coffee-loader").load();
compiled = coffee.compile(source, {
filename: path });
@@ -30,16 +30,15 @@ function run(options) {
_coffee: function(module, filename, code) {
if (!code) code = fs.readFileSync(filename, "utf8");
- // Compile the source CoffeeScript to regular JS.
- // TODO The require() of CoffeeScript should happen relative to
- // wherever CoffeeScript was originally required() ideally. How?
- // Right now it's happening relative to the process's cwd.
- code = require("../util/coffee-script").compile(code, {
+ // Compile the source CoffeeScript to regular JS. We make sure to
+ // use the module's local instance of CoffeeScript if possible.
+ var coffee = require("../util/coffee-loader").load(module.filename);
+ var ground = coffee.compile(code, {
filename: filename
});
// Then transform it like regular JS.
- streamliners._js(module, filename, code);
+ streamliners._js(module, filename, ground);
}
};
@@ -61,8 +60,9 @@ function run(options) {
var filename = process.argv[1];
// If we're running via _coffee, we should run CoffeeScript ourselves so
- // that it can register its regular .coffee handler.
- if (coffeePresent) require("../util/coffee-script");
+ // that it can register its regular .coffee handler. We make sure to do
+ // this relative to the caller's working directory instead of from here.
+ if (coffeePresent) require("../util/coffee-loader").load();
// We'll make that file the "main" module by reusing the current one.
var mainModule = require.main;
View
@@ -0,0 +1,14 @@
+// Helper file to require CoffeeScript relative to the either the caller's
+// working directory or the calling module's directory.
+
+var Module = require("module");
+
+exports.load = function(fromDir) {
+ if (!fromDir) fromDir = process.cwd();
+
+ var paths = Module._nodeModulePaths(fromDir);
+ var cspath = Module._findPath('coffee-script', paths);
+
+ if (cspath) return require(cspath);
+ else throw new Error("Failed to find CoffeeScript from " + fromDir);
+};
@@ -1,8 +0,0 @@
-// Helper file to require CoffeeScript relative to the calling working dir.
-
-var Module = require("module");
-var paths = Module._nodeModulePaths(process.cwd());
-var cspath = Module._findPath('coffee-script', paths);
-
-if (cspath) module.exports = require(cspath);
-else throw new Error("Failed to find CoffeeScript from " + process.cwd());

0 comments on commit 7dee195

Please sign in to comment.