Skip to content

Commit cf66494

Browse files
committed
Handle the possibility of compiling multiple scripts with the same filename, or multiple anonymous scripts
1 parent 774725f commit cf66494

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/coffeescript.coffee

+13-11
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ withPrettyErrors = (fn) ->
4949
# a stack trace. Assuming that most of the time, code isn’t throwing
5050
# exceptions, it’s probably more efficient to compile twice only when we
5151
# need a stack trace, rather than always generating a source map even when
52-
# it’s not likely to be used. Save in form of `filename`: `(source)`
52+
# it’s not likely to be used. Save in form of `filename`: [`(source)`]
5353
sources = {}
54-
# Also save source maps if generated, in form of `filename`: `(source map)`.
54+
# Also save source maps if generated, in form of `(source)`: [`(source map)`].
5555
sourceMaps = {}
5656

5757
# Compile CoffeeScript code to JavaScript, using the Coffee/Jison compiler.
@@ -75,7 +75,8 @@ exports.compile = compile = withPrettyErrors (code, options) ->
7575

7676
checkShebangLine filename, code
7777

78-
sources[filename] = code
78+
sources[filename] ?= []
79+
sources[filename].push code
7980
map = new SourceMap if generateSourceMap
8081

8182
tokens = lexer.tokenize code, options
@@ -124,8 +125,9 @@ exports.compile = compile = withPrettyErrors (code, options) ->
124125
js = "// #{header}\n#{js}"
125126

126127
if generateSourceMap
127-
v3SourceMap = map.generate(options, code)
128-
sourceMaps[filename] = map
128+
v3SourceMap = map.generate options, code
129+
sourceMaps[filename] ?= []
130+
sourceMaps[filename].push map
129131

130132
if options.inlineMap
131133
encoded = base64encode JSON.stringify v3SourceMap
@@ -266,14 +268,14 @@ formatSourcePosition = (frame, getSourceMapping) ->
266268

267269
getSourceMap = (filename) ->
268270
if sourceMaps[filename]?
269-
sourceMaps[filename]
270-
# CoffeeScript compiled in a browser may get compiled with `options.filename`
271-
# of `<anonymous>`, but the browser may request the stack trace with the
272-
# filename of the script file.
271+
sourceMaps[filename][sourceMaps[filename].length - 1]
273272
else if sourceMaps['<anonymous>']?
274-
sourceMaps['<anonymous>']
273+
# CoffeeScript compiled in a browser may get compiled with `options.filename`
274+
# of `<anonymous>`, but the browser may request the stack trace with the
275+
# filename of the script file.
276+
sourceMaps['<anonymous>'][sourceMaps['<anonymous>'].length - 1]
275277
else if sources[filename]?
276-
answer = compile sources[filename],
278+
answer = compile sources[filename][sources[filename].length - 1],
277279
filename: filename
278280
sourceMap: yes
279281
literate: helpers.isLiterate filename

0 commit comments

Comments
 (0)