Skip to content

Commit a2037e7

Browse files
Fix #4725: apply transpile option to require’d .coffee files (#4728)
* Fix #4725: apply transpile option to require’d .coffee files * Use the current module’s options if it has any, before going searching up the tree * Don’t mutate passed-in options object
1 parent 694e69d commit a2037e7

File tree

6 files changed

+47
-18
lines changed

6 files changed

+47
-18
lines changed

lib/coffeescript/index.js

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/coffeescript/register.js

Lines changed: 13 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.coffee

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ CoffeeScript.run = (code, options = {}) ->
4646
fs.realpathSync '.'
4747
mainModule.paths = require('module')._nodeModulePaths dir
4848

49+
# Save the options for compiling child imports.
50+
mainModule.options = options
51+
4952
# Compile.
5053
if not helpers.isCoffee(mainModule.filename) or require.extensions
5154
answer = CoffeeScript.compile code, options
@@ -104,17 +107,19 @@ if require.extensions
104107
Use CoffeeScript.register() or require the coffeescript/register module to require #{ext} files.
105108
"""
106109

107-
CoffeeScript._compileFile = (filename, sourceMap = no, inlineMap = no) ->
110+
CoffeeScript._compileFile = (filename, options = {}) ->
108111
raw = fs.readFileSync filename, 'utf8'
109112
# Strip the Unicode byte order mark, if this file begins with one.
110113
stripped = if raw.charCodeAt(0) is 0xFEFF then raw.substring 1 else raw
111114

115+
options = Object.assign {}, options,
116+
filename: filename
117+
literate: helpers.isLiterate filename
118+
sourceFiles: [filename]
119+
inlineMap: yes # Always generate a source map, so that stack traces line up.
120+
112121
try
113-
answer = CoffeeScript.compile stripped, {
114-
filename, sourceMap, inlineMap
115-
sourceFiles: [filename]
116-
literate: helpers.isLiterate filename
117-
}
122+
answer = CoffeeScript.compile stripped, options
118123
catch err
119124
# As the filename and code of a dynamically loaded file will be different
120125
# from the original file compiled with CoffeeScript.run, add that

src/register.coffee

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ path = require 'path'
55

66
# Load and run a CoffeeScript file for Node, stripping any `BOM`s.
77
loadFile = (module, filename) ->
8-
answer = CoffeeScript._compileFile filename, no, yes
8+
options = module.options or getRootModule(module).options
9+
answer = CoffeeScript._compileFile filename, options
910
module._compile answer, filename
1011

1112
# If the installed version of Node supports `require.extensions`, register
@@ -48,3 +49,7 @@ if child_process
4849
args = [path].concat args
4950
path = binary
5051
fork path, args, options
52+
53+
# Utility function to find the `options` object attached to the topmost module.
54+
getRootModule = (module) ->
55+
if module.parent then getRootModule module.parent else module

test/compilation.coffee

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,7 @@ test "using transpile from the Node API requires an object", ->
166166
CoffeeScript.compile '', transpile: yes
167167
catch exception
168168
eq exception.message, 'The transpile option must be given an object with options to pass to Babel'
169+
170+
test "transpile option applies to imported .coffee files", ->
171+
return if global.testingBrowser
172+
doesNotThrow -> transpile 'run', "import { getSep } from './test/importing/transpile_import'\ngetSep()"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import path from 'path'
2+
3+
export getSep = -> path.sep

0 commit comments

Comments
 (0)