From 7089b02a74c860169978b5631a5b1be6547436e6 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sun, 2 Jun 2013 09:03:54 +0400 Subject: [PATCH] Merging in a monkey-patching of Module::load, unfortunately. --- lib/coffee-script/coffee-script.js | 50 +++++++++++++----------------- src/coffee-script.coffee | 46 ++++++++++++--------------- 2 files changed, 42 insertions(+), 54 deletions(-) diff --git a/lib/coffee-script/coffee-script.js b/lib/coffee-script/coffee-script.js index 40807c11..2c8e09cd 100644 --- a/lib/coffee-script/coffee-script.js +++ b/lib/coffee-script/coffee-script.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript 1.6.2 (function() { - var Lexer, SourceMap, child_process, compile, ext, fork, formatSourcePosition, fs, helpers, lexer, loadFile, parser, patchStackTrace, patched, path, vm, _i, _len, _ref, + var Lexer, Module, SourceMap, child_process, compile, ext, findExtension, fork, formatSourcePosition, fs, helpers, lexer, loadFile, parser, patchStackTrace, patched, path, vm, _i, _len, _ref, __hasProp = {}.hasOwnProperty; fs = require('fs'); @@ -185,35 +185,29 @@ ext = _ref[_i]; require.extensions[ext] = loadFile; } - (function(Module) { - var NATIVELOAD, findExtension; - NATIVELOAD = "function(filename){debug('load'+JSON.stringify(filename)+'formodule'+JSON.stringify(this.id));assert(!this.loaded);this.filename=filename;this.paths=Module._nodeModulePaths(path.dirname(filename));varextension=path.extname(filename)||'.js';if(!Module._extensions[extension])extension='.js';Module._extensions[extension](this,filename);this.loaded=true;}"; - if (Module.prototype.load.toString().replace(/\s+/g, '') !== NATIVELOAD) { - return; + Module = require('module'); + findExtension = function(filename) { + var curExtension, extensions; + extensions = path.basename(filename).split('.'); + if (extensions[0] === '') { + extensions.shift(); } - findExtension = function(filename) { - var curExtension, extensions; - extensions = path.basename(filename).split('.'); - if (extensions[0] === '') { - extensions.shift(); + while (extensions.shift()) { + curExtension = '.' + extensions.join('.'); + if (Module._extensions[curExtension]) { + return curExtension; } - while (extensions.shift()) { - curExtension = '.' + extensions.join('.'); - if (Module._extensions[curExtension]) { - return curExtension; - } - } - return '.js'; - }; - return Module.prototype.load = function(filename) { - var extension; - this.filename = filename; - this.paths = Module._nodeModulePaths(path.dirname(filename)); - extension = findExtension(filename); - Module._extensions[extension](this, filename); - return this.loaded = true; - }; - })(require('module')); + } + return '.js'; + }; + Module.prototype.load = function(filename) { + var extension; + this.filename = filename; + this.paths = Module._nodeModulePaths(path.dirname(filename)); + extension = findExtension(filename); + Module._extensions[extension](this, filename); + return this.loaded = true; + }; } if (child_process) { diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index be7e4ab5..30679297 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -154,34 +154,28 @@ if require.extensions for ext in ['.coffee', '.litcoffee', '.coffee.md'] require.extensions[ext] = loadFile - # Patch Node's module loader to be able to handle .coffee.md in require.extensions - do (Module = require 'module') -> - NATIVELOAD = "function(filename){debug('load'+JSON.stringify(filename)+'formodule'+JSON.stringify(this.id));assert(!this.loaded);this.filename=filename;this.paths=Module._nodeModulePaths(path.dirname(filename));varextension=path.extname(filename)||'.js';if(!Module._extensions[extension])extension='.js';Module._extensions[extension](this,filename);this.loaded=true;}" + # Patch Node's module loader to be able to handle mult-dot extensions. + # This is a horrible thing that should not be required. Perhaps, one day, + # when a truly benevolent dictator comes to rule over the Republik of Node, + # it won't be. + Module = require 'module' - # Only keep going if we're sure module.prototype.load is what we expect - return unless Module::load.toString().replace(/\s+/g, '') is NATIVELOAD - - findExtension = (filename) -> - extensions = path.basename(filename).split '.' - - # Remove initial dot from dotfiles - extensions.shift() if extensions[0] is '' - - # Start with the longest extension and work our way shortwards - while extensions.shift() - curExtension = '.' + extensions.join '.' - return curExtension if Module._extensions[curExtension] - - '.js' - - Module::load = (filename) -> - @filename = filename - @paths = Module._nodeModulePaths path.dirname filename - - extension = findExtension filename - Module._extensions[extension](this, filename) - @loaded = true + findExtension = (filename) -> + extensions = path.basename(filename).split '.' + # Remove the initial dot from dotfiles. + extensions.shift() if extensions[0] is '' + # Start with the longest possible extension and work our way shortwards. + while extensions.shift() + curExtension = '.' + extensions.join '.' + return curExtension if Module._extensions[curExtension] + '.js' + Module::load = (filename) -> + @filename = filename + @paths = Module._nodeModulePaths path.dirname filename + extension = findExtension filename + Module._extensions[extension](this, filename) + @loaded = true # If we're on Node, patch `child_process.fork` so that Coffee scripts are able # to fork both CoffeeScript files, and JavaScript files, directly.