@@ -44,6 +44,7 @@ SWITCHES = [
4444 [ ' --no-header' , ' suppress the "Generated by" header' ]
4545 [' -o' , ' --output [DIR]' , ' set the output directory for compiled JavaScript' ]
4646 [' -p' , ' --print' , ' print out the compiled JavaScript' ]
47+ [' -r' , ' --require [MODULE*]' , ' require the given module before eval or REPL' ]
4748 [' -s' , ' --stdio' , ' listen for and compile scripts over stdio' ]
4849 [' -l' , ' --literate' , ' treat stdio as literate style coffee-script' ]
4950 [' -t' , ' --tokens' , ' print out the tokens that the lexer/rewriter produce' ]
@@ -69,6 +70,8 @@ exports.run = ->
6970 # `node` REPL CLI and, therefore, (b) make packages that modify native prototypes
7071 # (such as 'colors' and 'sugar') work as expected.
7172 replCliOpts = useGlobal : yes
73+ opts .prelude = makePrelude opts .require if opts .require
74+ replCliOpts .prelude = opts .prelude
7275 return forkNode () if opts .nodejs
7376 return usage () if opts .help
7477 return version () if opts .version
@@ -101,6 +104,13 @@ exports.run = ->
101104 source = path .resolve source
102105 compilePath source, yes , source
103106
107+ makePrelude = (requires ) ->
108+ requires .map (module ) ->
109+ [_ , name , module ] = match if match = module .match (/ ^ (. * )=(. * )$ / )
110+ name ||= helpers .baseFileName module , yes , useWinPathSep
111+ " #{ name} = require('#{ module } ')"
112+ .join ' ;'
113+
104114# Compile a path, which could be a script or a directory. If a directory
105115# is passed, recursively compile all '.coffee', '.litcoffee', and '.coffee.md'
106116# extension source files in it and all subdirectories.
@@ -167,6 +177,7 @@ compileScript = (file, input, base = null) ->
167177 printLine CoffeeScript .nodes (t .input , t .options ).toString ().trim ()
168178 else if o .run
169179 CoffeeScript .register ()
180+ CoffeeScript .eval opts .prelude , t .options if opts .prelude
170181 CoffeeScript .run t .input , t .options
171182 else if o .join and t .file isnt o .join
172183 t .input = helpers .invertLiterate t .input if helpers .isLiterate file
0 commit comments