1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Decoupling --require flag processing from file compilation

This change allows files to be `--require`d before entering the REPL. It's also
an opimization, since files are `--require`d only once, rather than being
required again every time a file is compiled.

A secondary change is that `module.filename` is temporarily modified. This is
somewhat less aesthetically appealing than the old approach of using
fs.realpathSync, but it allows you to run `coffee -r ./foo` rather than having
to write `coffee -r ./foo.coffee`, since Node does not accept absolute paths
without a file extension.
This commit is contained in:
Trevor Burnham 2011-01-13 14:50:00 -05:00
parent 7815138386
commit ba45dedbd5
2 changed files with 17 additions and 10 deletions

View file

@ -22,6 +22,7 @@
contents = [];
optionParser = null;
exports.run = function() {
var oldModuleFilename, req, _i, _len, _ref;
parseOptions();
if (opts.nodejs) {
return forkNode();
@ -32,6 +33,16 @@
if (opts.version) {
return version();
}
if (opts.require) {
oldModuleFilename = module.filename;
module.filename = '.';
_ref = opts.require;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
req = _ref[_i];
require(req);
}
module.filename = oldModuleFilename;
}
if (opts.interactive) {
return require('./repl');
}
@ -95,16 +106,9 @@
return _results;
};
compileScript = function(file, input, base) {
var o, options, req, t, task, _i, _len, _ref;
var o, options, t, task;
o = opts;
options = compileOptions(file);
if (o.require) {
_ref = o.require;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
req = _ref[_i];
require(helpers.starts(req, '.') ? fs.realpathSync(req) : req);
}
}
try {
t = task = {
file: file,

View file

@ -59,6 +59,11 @@ exports.run = ->
return forkNode() if opts.nodejs
return usage() if opts.help
return version() if opts.version
if opts.require
oldModuleFilename = module.filename
module.filename = '.'
require req for req in opts.require
module.filename = oldModuleFilename
return require './repl' if opts.interactive
return compileStdio() if opts.stdio
return compileScript null, sources[0] if opts.eval
@ -98,8 +103,6 @@ compileScripts = ->
compileScript = (file, input, base) ->
o = opts
options = compileOptions file
if o.require
require(if helpers.starts(req, '.') then fs.realpathSync(req) else req) for req in o.require
try
t = task = {file, input, options}
CoffeeScript.emit 'compile', task