@@ -769,181 +809,110 @@
Overview
CoffeeScript on the top
- CoffeeScript 2
Why CoffeeScript When There’s ES2015+?
CoffeeScript introduced many new features to the JavaScript world, such as =>
and destructuring and classes. We are happy that ECMA has seen their utility and adopted them into ECMAScript.
+ CoffeeScript 2
+Why CoffeeScript When There’s ES2015+?
+CoffeeScript introduced many new features to the JavaScript world, such as =>
and destructuring and classes. We are happy that ECMA has seen their utility and adopted them into ECMAScript.
CoffeeScript’s intent, however, was never to be a superset of JavaScript. One of the guiding principles of CoffeeScript has been simplicity: not just removing JavaScript’s “bad parts,” but providing a cleaner, terser syntax that uses less punctuation and enforces indentation, to make code easier to read and reason about. Increased clarity leads to increased quality, and fewer bugs. This benefit of CoffeeScript remains, even in an ES2015+ world.
-ES2015+ Output
CoffeeScript 2 supports many of the latest ES2015+ features, output using ES2015+ syntax. If you’re looking for a single tool that takes CoffeeScript input and generates JavaScript output that runs in any JavaScript runtime, assuming you opt out of certain newer features, stick to the CoffeeScript 1.x branch. CoffeeScript 2 breaks compatibility with certain CoffeeScript 1.x features in order to conform with the ES2015+ specifications, and generate more idiomatic output (a CoffeeScript =>
becomes an ES =>
; a CoffeeScript class
becomes an ES class
; and so on).
+ES2015+ Output
+CoffeeScript 2 supports many of the latest ES2015+ features, output using ES2015+ syntax. If you’re looking for a single tool that takes CoffeeScript input and generates JavaScript output that runs in any JavaScript runtime, assuming you opt out of certain newer features, stick to the CoffeeScript 1.x branch. CoffeeScript 2 breaks compatibility with certain CoffeeScript 1.x features in order to conform with the ES2015+ specifications, and generate more idiomatic output (a CoffeeScript =>
becomes an ES =>
; a CoffeeScript class
becomes an ES class
; and so on).
Since the CoffeeScript 2 compiler outputs ES2015+ syntax, it is your responsibility to either ensure that your target JavaScript runtime(s) support all these features, or that you pass the output through another transpiler like Babel, Rollup or Traceur Compiler. In general, CoffeeScript 2’s output is supported as is by Node.js 7.6+, except for modules which require transpilation.
There are many great task runners for setting up JavaScript build chains, such as Gulp, Webpack, Grunt and Broccoli. If you’re looking for a very minimal solution to get started, you can use babel-preset-env and the command line:
-
-npm install --global coffeescript@next
+npm install --global coffeescript@next
npm install --save-dev coffeescript@next babel-cli babel-preset-env
-coffee -p *.coffee | babel --presets env > app.js
-
+coffee -p *.coffee | babel --presets env > app.js
+
+
- Installation
The command-line version of coffee
is available as a Node.js utility. The core compiler however, does not depend on Node, and can be run in any JavaScript environment, or in the browser (see Try CoffeeScript).
+ Installation
+The command-line version of coffee
is available as a Node.js utility. The core compiler however, does not depend on Node, and can be run in any JavaScript environment, or in the browser (see Try CoffeeScript).
To install, first make sure you have a working copy of the latest stable version of Node.js. You can then install CoffeeScript globally with npm:
-
-npm install --global coffeescript@next
-When you need CoffeeScript as a dependency of a project, within that project’s folder you can install it locally:
-
-npm install --save coffeescript@next
-
+npm install --global coffeescript@next
+
+
When you need CoffeeScript as a dependency of a project, within that project’s folder you can install it locally:
+npm install --save coffeescript@next
+
+
- Usage
Once installed, you should have access to the coffee
command, which can execute scripts, compile .coffee
files into .js
, and provide an interactive REPL. The coffee
command takes the following options:
+ Usage
+Once installed, you should have access to the coffee
command, which can execute scripts, compile .coffee
files into .js
, and provide an interactive REPL. The coffee
command takes the following options:
-
+
+
+Option |
+Description |
+
+
-
-
-c, --compile |
-
Compile a .coffee script into a .js JavaScript file of the same name. |
-
-
-
-m, --map |
-
Generate source maps alongside the compiled JavaScript files. Adds sourceMappingURL directives to the JavaScript as well. |
-
-
-
-M, --inline-map |
-
Just like --map , but include the source map directly in the compiled JavaScript files, rather than in a separate file. |
-
-
-
--i, --interactive |
-
+-i, --interactive |
Launch an interactive CoffeeScript session to try short snippets. Identical to calling coffee with no arguments. |
-
-
-
-o, --output [DIR] |
-
Write out all compiled JavaScript files into the specified directory. Use in conjunction with --compile or --watch . |
-
-
-
-w, --watch |
-
Watch files for changes, rerunning the specified command when any file is updated. |
-
-
-
-p, --print |
-
Instead of writing out the JavaScript as a file, print it directly to stdout. |
-
-
-
-s, --stdio |
-
-Pipe in CoffeeScript to STDIN and get back JavaScript over STDOUT. Good for use with processes written in other languages. An example:
-cat src/cake.coffee | coffee -sc |
-
+Pipe in CoffeeScript to STDIN and get back JavaScript over STDOUT. Good for use with processes written in other languages. An example:
cat src/cake.coffee | coffee -sc |
-
-
-l, --literate |
-
Parses the code as Literate CoffeeScript. You only need to specify this when passing in code directly over stdio, or using some sort of extension-less file name. |
-
-
-
-e, --eval |
-
-Compile and print a little snippet of CoffeeScript directly from the command line. For example:
-coffee -e "console.log num for num in [10..1]" |
-
+Compile and print a little snippet of CoffeeScript directly from the command line. For example:
coffee -e "console.log num for num in [10..1]" |
-
-
--r, --require [MODULE] |
-
+-r, --require [MODULE] |
require() the given module before starting the REPL or evaluating the code given with the --eval flag. |
-
-
-
-b, --bare |
-
Compile the JavaScript without the top-level function safety wrapper. |
-
-
-
-t, --tokens |
-
-Instead of parsing the CoffeeScript, just lex it, and print out the token stream:
-[IDENTIFIER square] [= =] [PARAM_START (] ... |
-
+Instead of parsing the CoffeeScript, just lex it, and print out the token stream. Used for debugging the compiler. |
-
-
-n, --nodes |
-
-Instead of compiling the CoffeeScript, just lex and parse it, and print out the parse tree:
-
-Block
- Assign
- Value IdentifierLiteral: square
- Code
- Param IdentifierLiteral: x
- Block
- Op *
- Value IdentifierLiteral: x
- Value IdentifierLiteral: x
- |
-
+Instead of compiling the CoffeeScript, just lex and parse it, and print out the parse tree. Used for debugging the compiler. |
-
-
--nodejs |
-
-The node executable has some useful options you can set, such as
---debug , --debug-brk , --max-stack-size , and --expose-gc . Use this flag to forward options directly to Node.js. To pass multiple flags, use --nodejs multiple times. |
-
+The node executable has some useful options you can set, such as --debug , --debug-brk , --max-stack-size , and --expose-gc . Use this flag to forward options directly to Node.js. To pass multiple flags, use --nodejs multiple times. |
-
-
--no-header |
-
Suppress the “Generated by CoffeeScript” header. |
-
-
-
-
-Examples:
+Examples:
+
- Compile a directory tree of
.coffee
files in src
into a parallel tree of .js
files in lib
:
coffee --compile --output lib/ src/
- Watch a file for changes, and recompile it every time the file is saved:
@@ -960,14 +929,16 @@ Examples:
- Language Reference
This reference is structured so that it can be read from top to bottom, if you like. Later sections use ideas and syntax previously introduced. Familiarity with JavaScript is assumed. In all of the following examples, the source CoffeeScript is provided on the left, and the direct compilation into JavaScript is on the right.
+ Language Reference
+This reference is structured so that it can be read from top to bottom, if you like. Later sections use ideas and syntax previously introduced. Familiarity with JavaScript is assumed. In all of the following examples, the source CoffeeScript is provided on the left, and the direct compilation into JavaScript is on the right.
Many of the examples can be run (where it makes sense) by pressing the ▶ button on the right. The CoffeeScript on the left is editable, and the JavaScript will update as you edit.
First, the basics: CoffeeScript uses significant whitespace to delimit blocks of code. You don’t need to use semicolons ;
to terminate expressions, ending the line will do just as well (although semicolons can still be used to fit multiple expressions onto a single line). Instead of using curly braces { }
to surround blocks of code in functions, if-statements, switch, and try/catch, use indentation.
You don’t need to use parentheses to invoke a function if you’re passing arguments. The implicit call wraps forward to the end of the line or block expression.
console.log sys.inspect object
→ console.log(sys.inspect(object));
- Functions
Functions are defined by an optional list of parameters in parentheses, an arrow, and the function body. The empty function looks like this: ->
+ Functions
+Functions are defined by an optional list of parameters in parentheses, an arrow, and the function body. The empty function looks like this: ->