Permalink
Browse files

Consolidate file-reading code in underscored.js.

  • Loading branch information...
1 parent bb9746c commit 931cb7d20fc428a31c97e2128e7d41afa659357a @aseemk committed May 26, 2012
Showing with 13 additions and 20 deletions.
  1. +13 −20 lib/compiler/underscored.js
@@ -39,31 +39,26 @@ function run(options) {
var streamliners = {
_js: function(module, filename, code) {
+ if (!code) code = fs.readFileSync(filename, "utf8");
+
+ // If there's a shebang, strip it while preserving line count:
+ var match = /^#!.*([^\u0000]*)$/.exec(code);
+ if (match) code = match[1];
+
var cachedTransform = require("streamline/lib/compiler/compile").cachedTransformSync;
var streamlined = options.cache ? cachedTransform(code, filename, streamline, options) : streamline(code, options);
module._compile(streamlined, filename)
},
_coffee: function(module, filename, code) {
- code = grind(code, filename);
- streamliners._js(module, filename, code, options);
+ if (!code) code = fs.readFileSync(filename, "utf8");
+ var compiled = grind(code, filename);
+ streamliners._js(module, filename, compiled);
}
};
- // If Streamlined CoffeeScript is used, load CoffeeScript and register a new
- // handler for the `._coffee` extension.
- require.extensions['._coffee'] = function(module, filename) {
- var code = fs.readFileSync(filename, "utf8");
- streamliners._coffee(module, filename, code);
- }
-
- // Streamlined JavaScript extension handler.
- require.extensions['._js'] = function(module, filename) {
- var code = fs.readFileSync(filename, "utf8");
- // If there's a shebang, strip it while preserving line count:
- var match = /^#!.*([^\u0000]*)$/.exec(code);
- if (match) code = match[1];
- streamliners._js(module, filename, code);
- }
+ // Register require() extension handlers for ._js and ._coffee:
+ require.extensions['._js'] = streamliners._js;
+ require.extensions['._coffee'] = streamliners._coffee;
// If we were asked to register extension handlers only, we're done:
if (options.registerOnly) return;
@@ -113,9 +108,7 @@ function run(options) {
process.argv.splice(1, 1, mainModule.filename)
// Load the target file and evaluate it.
- fs.readFile(filename, "utf8", function(error, code) {
- streamliners[extension.substring(1)](mainModule, mainModule.filename, code);
- });
+ streamliners[extension.substring(1)](mainModule, mainModule.filename);
}
module.exports.run = run;

0 comments on commit 931cb7d

Please sign in to comment.