Skip to content

Commit 2f82b75

Browse files
adrian-gierakowskiGeoffreyBooth
authored andcommitted
implement coffeescript.registerCompiled method (#5130)
* implement coffeescript._addSoucrse method This method enables an external module to implement caching of compilation results. When the compiled js source is loaded from cache, the original coffee code should be added with this method in order to enable the Error.prepareStackTrace below to correctly adjust the stack trace for the corresponding file (the source map will be generated on demand). * replace _addSource with registerCompiled * extract the logic from _compileFile into _compileRawFileContent _compileFile takes care of logging the file and calls _compileRawFileContent this way an external caching implementation which computes cache key based on raw content of the sources file, can reuse the logic of _compileFile and avoid having calling `fs.readFileSync` for the file more twice in case of cache miss * remove 'output' argument from registerCompiled
1 parent 294bb47 commit 2f82b75

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

‎lib/coffeescript/coffeescript.js

+20-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/coffeescript/index.js

+8-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/coffeescript.coffee

+16-4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ sources = {}
5454
# Also save source maps if generated, in form of `(source)`: [`(source map)`].
5555
sourceMaps = {}
5656

57+
# This is exported to enable an external module to implement caching of
58+
# compilation results. When the compiled js source is loaded from cache, the
59+
# original coffee code should be added with this method in order to enable the
60+
# Error.prepareStackTrace below to correctly adjust the stack trace for the
61+
# corresponding file (the source map will be generated on demand).
62+
exports.registerCompiled = registerCompiled = (filename, source, sourcemap) ->
63+
64+
sources[filename] ?= []
65+
sources[filename].push source
66+
67+
if sourcemap?
68+
sourceMaps[filename] ?= []
69+
sourceMaps[filename].push sourcemap
70+
5771
# Compile CoffeeScript code to JavaScript, using the Coffee/Jison compiler.
5872
#
5973
# If `options.sourceMap` is specified, then `options.filename` must also be
@@ -75,8 +89,6 @@ exports.compile = compile = withPrettyErrors (code, options = {}) ->
7589

7690
checkShebangLine filename, code
7791

78-
sources[filename] ?= []
79-
sources[filename].push code
8092
map = new SourceMap if generateSourceMap
8193

8294
tokens = lexer.tokenize code, options
@@ -126,8 +138,6 @@ exports.compile = compile = withPrettyErrors (code, options = {}) ->
126138

127139
if generateSourceMap
128140
v3SourceMap = map.generate options, code
129-
sourceMaps[filename] ?= []
130-
sourceMaps[filename].push map
131141

132142
if options.transpile
133143
if typeof options.transpile isnt 'object'
@@ -158,6 +168,8 @@ exports.compile = compile = withPrettyErrors (code, options = {}) ->
158168
sourceURL = "//# sourceURL=#{options.filename ? 'coffeescript'}"
159169
js = "#{js}\n#{sourceMapDataURI}\n#{sourceURL}"
160170

171+
registerCompiled filename, code, map
172+
161173
if options.sourceMap
162174
{
163175
js

‎src/index.coffee

+7-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ if require.extensions
110110
Use CoffeeScript.register() or require the coffeescript/register module to require #{ext} files.
111111
"""
112112

113-
CoffeeScript._compileFile = (filename, options = {}) ->
114-
raw = fs.readFileSync filename, 'utf8'
113+
CoffeeScript._compileRawFileContent = (raw, filename, options = {}) ->
114+
115115
# Strip the Unicode byte order mark, if this file begins with one.
116116
stripped = if raw.charCodeAt(0) is 0xFEFF then raw.substring 1 else raw
117117

@@ -131,4 +131,9 @@ CoffeeScript._compileFile = (filename, options = {}) ->
131131

132132
answer
133133

134+
CoffeeScript._compileFile = (filename, options = {}) ->
135+
raw = fs.readFileSync filename, 'utf8'
136+
137+
CoffeeScript._compileRawFileContent raw, filename, options
138+
134139
module.exports = CoffeeScript

0 commit comments

Comments
 (0)