@@ -6,6 +6,7 @@ CoffeeScript = require './'
6
6
{merge , updateSyntaxError } = require ' ./helpers'
7
7
8
8
sawSIGINT = no
9
+ transpile = no
9
10
10
11
replDefaults =
11
12
prompt : ' coffee> ' ,
@@ -35,14 +36,19 @@ replDefaults =
35
36
ast = CoffeeScript .nodes tokens
36
37
# Add assignment to `__` variable to force the input to be an expression.
37
38
ast = new Block [new Assign (new Value new Literal ' __' ), ast, ' =' ]
38
- # Wrap the expression in a closure to support top-level `await`
39
+ # Wrap the expression in a closure to support top-level `await`.
39
40
ast = new Code [], ast
40
41
isAsync = ast .isAsync
41
- # Invoke the wrapping closure
42
+ # Invoke the wrapping closure.
42
43
ast = new Block [new Call ast]
43
44
js = ast .compile {bare : yes , locals : Object .keys (context), referencedVars, sharedScope : yes }
45
+ if transpile
46
+ js = transpile .transpile (js, transpile .options ).code
47
+ # Strip `"use strict"`, to avoid an exception on assigning to
48
+ # undeclared variable `__`.
49
+ js = js .replace / ^ "use strict"| ^ 'use strict'/ , ' '
44
50
result = runInContext js, context, filename
45
- # Await an async result, if necessary
51
+ # Await an async result, if necessary.
46
52
if isAsync
47
53
result .then (resolvedResult) ->
48
54
cb null , resolvedResult unless sawSIGINT
@@ -167,6 +173,31 @@ module.exports =
167
173
168
174
CoffeeScript .register ()
169
175
process .argv = [' coffee' ].concat process .argv [2 .. ]
176
+ if opts .transpile
177
+ try
178
+ transpile = {}
179
+ transpile .transpile = require (' babel-core' ).transform
180
+ catch
181
+ console .error '''
182
+ To use --transpile with an interactive REPL, babel-core must be installed either in the current folder or globally:
183
+ npm install --save-dev babel-core
184
+ or
185
+ npm install --global babel-core
186
+ And you must save options to configure Babel in one of the places it looks to find its options.
187
+ See http://coffeescript.org/#transpilation
188
+ '''
189
+ process .exit 1
190
+ transpile .options =
191
+ filename : path .resolve process .cwd (), ' <repl>'
192
+ # Since the REPL compilation path is unique (in `eval` above), we need
193
+ # another way to get the `options` object attached to a module so that
194
+ # it knows later on whether it needs to be transpiled. In the case of
195
+ # the REPL, the only applicable option is `transpile`.
196
+ Module = require ' module'
197
+ originalModuleLoad = Module :: load
198
+ Module :: load = (filename ) ->
199
+ @options = transpile : transpile .options
200
+ originalModuleLoad .call @ , filename
170
201
opts = merge replDefaults, opts
171
202
repl = nodeREPL .start opts
172
203
runInContext opts .prelude , repl .context , ' prelude' if opts .prelude
0 commit comments