Skip to content

WIP: Support .mjs #5026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
raw
presentation
test.coffee
test*.coffee
test.litcoffee
test*.litcoffee
test*.*coffee
test/*.js
parser.output
/node_modules
Expand Down
21 changes: 10 additions & 11 deletions Cakefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ unless process.env.NODE_DISABLE_COLORS
yellow = '\x1B[0;33m'
reset = '\x1B[0m'

# Built file header.
header = """
/**
* CoffeeScript Compiler v#{CoffeeScript.VERSION}
* http://coffeescript.org
*
* Copyright 2011, Jeremy Ashkenas
* Released under the MIT License
*/
"""

# Used in folder names like `docs/v1`.
majorVersion = parseInt CoffeeScript.VERSION.split('.')[0], 10

Expand Down Expand Up @@ -127,6 +116,16 @@ task 'build:full', 'build the CoffeeScript compiler from source twice, and run t
build testBuiltCode

task 'build:browser', 'merge the built scripts into a single file for use in a browser', ->
# Built file header.
header = """
/**
* CoffeeScript Compiler v#{CoffeeScript.VERSION}
* http://coffeescript.org
*
* Copyright #{new Date().getFullYear()}, Jeremy Ashkenas
* Released under the MIT License
*/
"""
code = """
require['../../package.json'] = (function() {
return #{fs.readFileSync "./package.json"};
Expand Down
2 changes: 0 additions & 2 deletions documentation/sections/command_line_interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ Once installed, you should have access to the `coffee` command, which can execut
`coffee --compile --output lib/ src/`
* Watch a file for changes, and recompile it every time the file is saved:<br>
`coffee --watch --compile experimental.coffee`
* Concatenate a list of files into a single script:<br>
`coffee --join project.js --compile src/*.coffee`
* Print out the compiled JS from a one-liner:<br>
`coffee -bpe "alert i for i in [0..10]"`
* All together now, watch and recompile an entire project as you work on it:<br>
Expand Down
62 changes: 17 additions & 45 deletions lib/coffeescript/command.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions lib/coffeescript/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 11 additions & 43 deletions src/command.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ SWITCHES = [
['-e', '--eval', 'pass a string from the command line as input']
['-h', '--help', 'display this help message']
['-i', '--interactive', 'run an interactive CoffeeScript REPL']
['-j', '--join [FILE]', 'concatenate the source CoffeeScript before compiling']
['-m', '--map', 'generate source map and save as .js.map files']
['-M', '--inline-map', 'generate source map and include it directly in output']
['-n', '--nodes', 'print out the parse tree that the parser produces']
Expand Down Expand Up @@ -117,22 +116,6 @@ exports.run = ->
opts.outputFilename = null
opts.outputPath = path.resolve opts.output

if opts.join
opts.join = path.resolve opts.join
console.error '''

The --join option is deprecated and will be removed in a future version.

If for some reason it's necessary to share local variables between files,
replace...

$ coffee --compile --join bundle.js -- a.coffee b.coffee c.coffee

with...

$ cat a.coffee b.coffee c.coffee | coffee --compile --stdio > bundle.js

'''
for source in opts.arguments
source = path.resolve source
compilePath source, yes, source
Expand Down Expand Up @@ -211,10 +194,6 @@ compileScript = (file, input, base = null) ->
CoffeeScript.register()
CoffeeScript.eval opts.prelude, task.options if opts.prelude
CoffeeScript.run task.input, task.options
else if opts.join and task.file isnt opts.join
task.input = helpers.invertLiterate task.input if helpers.isLiterate file
sourceCode[sources.indexOf(task.file)] = task.input
compileJoin()
else
compiled = CoffeeScript.compile task.input, task.options
task.output = compiled
Expand Down Expand Up @@ -254,16 +233,6 @@ compileStdio = ->
stdin.on 'end', ->
compileScript null, Buffer.concat(buffers).toString()

# If all of the source files are done being read, concatenate and compile
# them together.
joinTimeout = null
compileJoin = ->
return unless opts.join
unless sourceCode.some((code) -> code is null)
clearTimeout joinTimeout
joinTimeout = wait 100, ->
compileScript opts.join, sourceCode.join('\n'), opts.join

# Watch a source CoffeeScript file using `fs.watch`, recompiling it every
# time the file is updated. May be used in combination with other options,
# such as `--print`.
Expand All @@ -280,7 +249,6 @@ watch = (source, base) ->
compile()
catch
removeSource source, base
compileJoin()

compile = ->
clearTimeout compileTimeout
Expand Down Expand Up @@ -349,18 +317,16 @@ removeSourceDir = (source, base) ->
for file in sources when source is path.dirname file
removeSource file, base
sourcesChanged = yes
compileJoin() if sourcesChanged

# Remove a file from our source list, and source code cache. Optionally remove
# the compiled JS version as well.
removeSource = (source, base) ->
index = sources.indexOf source
sources.splice index, 1
sourceCode.splice index, 1
unless opts.join
silentUnlink outputPath source, base
silentUnlink outputPath source, base, '.js.map'
timeLog "removed #{source}"
silentUnlink outputPath source, base
silentUnlink outputPath source, base, '.js.map'
timeLog "removed #{source}"

silentUnlink = (path) ->
try
Expand All @@ -369,15 +335,16 @@ silentUnlink = (path) ->
throw err unless err.code in ['ENOENT', 'EPERM']

# Get the corresponding output JavaScript path for a source file.
outputPath = (source, base, extension=".js") ->
basename = helpers.baseFileName source, yes, useWinPathSep
srcDir = path.dirname source
outputPath = (source, base, extension) ->
basename = helpers.baseFileName source, yes, useWinPathSep
srcDir = path.dirname source
dir = unless opts.outputPath
srcDir
else if source is base
opts.outputPath
else
path.join opts.outputPath, path.relative base, srcDir
extension ?= if helpers.isESModule(source) then '.mjs' else '.js'
path.join dir, basename + extension

# Recursively mkdir, like `mkdir -p`.
Expand All @@ -396,13 +363,14 @@ mkdirp = (dir, fn) ->

# Write out a JavaScript source file with the compiled code. By default, files
# are written out in `cwd` as `.js` files with the same name, but the output
# directory can be customized with `--output`.
# directory can be customized with `--output`. If the source file extension is
# `.mcoffee` or `.litmcoffee`, use the `.mjs` extension.
#
# If `generatedSourceMap` is provided, this will write a `.js.map` file into the
# same directory as the `.js` file.
# same directory as the `.js` file; and likewise with `.mjs.map` and `.mjs`.
writeJs = (base, sourcePath, js, jsPath, generatedSourceMap = null) ->
sourceMapPath = "#{jsPath}.map"
jsDir = path.dirname jsPath
jsDir = path.dirname jsPath
compile = ->
if opts.compile
js = ' ' if js.length <= 0
Expand Down
13 changes: 8 additions & 5 deletions src/helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,19 @@ exports.baseFileName = (file, stripExt = no, useWinPathSep = no) ->
parts = file.split(pathSep)
file = parts[parts.length - 1]
return file unless stripExt and file.indexOf('.') >= 0
parts = file.split('.')
parts = file.split '.'
parts.pop()
parts.pop() if parts[parts.length - 1] is 'coffee' and parts.length > 1
parts.join('.')
parts.pop() if parts.length > 1 and parts[parts.length - 1] in ['coffee', 'mcoffee']
parts.join '.'

# Determine if a filename represents a CoffeeScript file.
exports.isCoffee = (file) -> /\.((lit)?coffee|coffee\.md)$/.test file
exports.isCoffee = (file) -> /\.((lit)?m?coffee|m?coffee\.md)$/.test file

# Determine if a filename represents a Literate CoffeeScript file.
exports.isLiterate = (file) -> /\.(litcoffee|coffee\.md)$/.test file
exports.isLiterate = (file) -> /\.(litcoffee|litmcoffee|coffee\.md)$/.test file

# Determine if a filename represents a CoffeeScript file compiling to an ES module.
exports.isESModule = (file) -> /\.((lit)?mcoffee|mcoffee\.md)$/.test file

# Throws a SyntaxError from a given location.
# The error's `toString` will return an error message following the "standard"
Expand Down
1 change: 0 additions & 1 deletion test/argument_parsing.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ If called without options, `coffee` will run your script.
-e, --eval pass a string from the command line as input
-h, --help display this help message
-i, --interactive run an interactive CoffeeScript REPL
-j, --join concatenate the source CoffeeScript before compiling
-m, --map generate source map and save as .js.map files
-M, --inline-map generate source map and include it directly in output
-n, --nodes print out the parse tree that the parser produces
Expand Down