mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
Babel 7 (#5105)
* Port to Babel 7 Use the Babel 7 @babel/core transpiler, falling back to the older babel-core version if possible. * Use Babel 7 and preset-env 7 to build the browser compiler and test Babel transpilation * Update docs for Babel 7
This commit is contained in:
parent
98b2a69416
commit
c4245e50c2
15 changed files with 1244 additions and 1371 deletions
2
.babelrc
2
.babelrc
|
@ -1,3 +1,3 @@
|
|||
{
|
||||
"presets": ["env"]
|
||||
"presets": ["@babel/env"]
|
||||
}
|
||||
|
|
4
Cakefile
4
Cakefile
|
@ -67,11 +67,11 @@ build = (callback) ->
|
|||
buildExceptParser callback
|
||||
|
||||
transpile = (code) ->
|
||||
babel = require 'babel-core'
|
||||
babel = require '@babel/core'
|
||||
presets = []
|
||||
# Exclude the `modules` plugin in order to not break the `}(this));`
|
||||
# at the end of the `build:browser` code block.
|
||||
presets.push ['env', {modules: no}] unless process.env.TRANSFORM is 'false'
|
||||
presets.push ['@babel/env', {modules: no}] unless process.env.TRANSFORM is 'false'
|
||||
presets.push ['minify', {mangle: no, evaluate: no, removeUndefined: no}] unless process.env.MINIFY is 'false'
|
||||
babelOptions =
|
||||
compact: process.env.MINIFY isnt 'false'
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -5,7 +5,7 @@ Once installed, you should have access to the `coffee` command, which can execut
|
|||
| Option | Description |
|
||||
| --- | --- |
|
||||
| `-c, --compile` | Compile a `.coffee` script into a `.js` JavaScript file of the same name. |
|
||||
| `-t, --transpile` | Pipe the CoffeeScript compiler’s output through Babel before saving or running the generated JavaScript. Requires `babel-core` to be installed, and options to pass to Babel in a `.babelrc` file or a `package.json` with a `babel` key in the path of the file or folder to be compiled. See [Transpilation](#transpilation).
|
||||
| `-t, --transpile` | Pipe the CoffeeScript compiler’s output through Babel before saving or running the generated JavaScript. Requires `@babel/core` to be installed, and options to pass to Babel in a `.babelrc` file or a `package.json` with a `babel` key in the path of the file or folder to be compiled. See [Transpilation](#transpilation).
|
||||
| `-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` | Launch an interactive CoffeeScript session to try short snippets. Identical to calling `coffee` with no arguments. |
|
||||
|
@ -37,4 +37,4 @@ Once installed, you should have access to the `coffee` command, which can execut
|
|||
* Start the CoffeeScript REPL (`Ctrl-D` to exit, `Ctrl-V`for multi-line):<br>
|
||||
`coffee`
|
||||
|
||||
To use `--transpile`, see [Transpilation](#transpilation).
|
||||
To use `--transpile`, see [Transpilation](#transpilation).
|
||||
|
|
|
@ -18,4 +18,4 @@ npm install --save-dev coffeescript
|
|||
|
||||
The `coffee` and `cake` commands will first look in the current folder to see if CoffeeScript is installed locally, and use that version if so. This allows different versions of CoffeeScript to be installed globally and locally.
|
||||
|
||||
If you plan to use the `--transpile` option (see [Transpilation](#transpilation)) you will need to also install `babel-core` either globally or locally, depending on whether you are running a globally or locally installed version of CoffeeScript.
|
||||
If you plan to use the `--transpile` option (see [Transpilation](#transpilation)) you will need to also install `@babel/core` either globally or locally, depending on whether you are running a globally or locally installed version of CoffeeScript.
|
||||
|
|
|
@ -7,43 +7,43 @@ CoffeeScript 2 generates JavaScript that uses the latest, modern syntax. The run
|
|||
From the root of your project:
|
||||
|
||||
```bash
|
||||
npm install --save-dev babel-core babel-preset-env
|
||||
echo '{ "presets": ["env"] }' > .babelrc
|
||||
npm install --save-dev @babel/core @babel/preset-env
|
||||
echo '{ "presets": ["@babel/env"] }' > .babelrc
|
||||
coffee --compile --transpile --inline-map some-file.coffee
|
||||
```
|
||||
|
||||
#### Transpiling with the CoffeeScript compiler
|
||||
|
||||
To make things easy, CoffeeScript has built-in support for the popular [Babel](http://babeljs.io/) transpiler. You can use it via the `--transpile` command-line option or the `transpile` Node API option. To use either, `babel-core` must be installed in your project:
|
||||
To make things easy, CoffeeScript has built-in support for the popular [Babel](http://babeljs.io/) transpiler. You can use it via the `--transpile` command-line option or the `transpile` Node API option. To use either, `@babel/core` must be installed in your project:
|
||||
|
||||
```bash
|
||||
npm install --save-dev babel-core
|
||||
npm install --save-dev @babel/core
|
||||
```
|
||||
|
||||
Or if you’re running the `coffee` command outside of a project folder, using a globally-installed `coffeescript` module, `babel-core` needs to be installed globally:
|
||||
Or if you’re running the `coffee` command outside of a project folder, using a globally-installed `coffeescript` module, `@babel/core` needs to be installed globally:
|
||||
|
||||
```bash
|
||||
npm install --global babel-core
|
||||
npm install --global @babel/core
|
||||
```
|
||||
|
||||
By default, Babel doesn’t do anything—it doesn’t make assumptions about what you want to transpile to. You need to provide it with a configuration so that it knows what to do. One way to do this is by creating a [`.babelrc` file](https://babeljs.io/docs/usage/babelrc/) in the folder containing the files you’re compiling, or in any parent folder up the path above those files. (Babel supports [other ways](https://babeljs.io/docs/usage/babelrc/), too.) A minimal `.babelrc` file would be just `{ "presets": ["env"] }`. This implies that you have installed [`babel-preset-env`](https://babeljs.io/docs/plugins/preset-env/):
|
||||
By default, Babel doesn’t do anything—it doesn’t make assumptions about what you want to transpile to. You need to provide it with a configuration so that it knows what to do. One way to do this is by creating a [`.babelrc` file](https://babeljs.io/docs/usage/babelrc/) in the folder containing the files you’re compiling, or in any parent folder up the path above those files. (Babel supports [other ways](https://babeljs.io/docs/usage/babelrc/), too.) A minimal `.babelrc` file would be just `{ "presets": ["@babel/env"] }`. This implies that you have installed [`@babel/preset-env`](https://babeljs.io/docs/plugins/preset-env/):
|
||||
|
||||
```bash
|
||||
npm install --save-dev babel-preset-env # Or --global for non-project-based usage
|
||||
npm install --save-dev @babel/preset-env # Or --global for non-project-based usage
|
||||
```
|
||||
|
||||
See [Babel’s website to learn about presets and plugins](https://babeljs.io/docs/plugins/) and the multitude of options you have. Another preset you might need is [`transform-react-jsx`](https://babeljs.io/docs/plugins/transform-react-jsx/) if you’re using JSX with React (JSX can also be used with other frameworks).
|
||||
See [Babel’s website to learn about presets and plugins](https://babeljs.io/docs/plugins/) and the multitude of options you have. Another preset you might need is [`@babel/plugin-transform-react-jsx`](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx/) if you’re using JSX with React (JSX can also be used with other frameworks).
|
||||
|
||||
Once you have `babel-core` and `babel-preset-env` (or other presets or plugins) installed, and a `.babelrc` file (or other equivalent) in place, you can use `coffee --transpile` to pipe CoffeeScript’s output through Babel using the options you’ve saved.
|
||||
Once you have `@babel/core` and `@babel/preset-env` (or other presets or plugins) installed, and a `.babelrc` file (or other equivalent) in place, you can use `coffee --transpile` to pipe CoffeeScript’s output through Babel using the options you’ve saved.
|
||||
|
||||
If you’re using CoffeeScript via the [Node API](nodejs_usage), where you call `CoffeeScript.compile` with a string to be compiled and an `options` object, the `transpile` key of the `options` object should be the Babel options:
|
||||
|
||||
```js
|
||||
CoffeeScript.compile(code, {transpile: {presets: ['env']}})
|
||||
CoffeeScript.compile(code, {transpile: {presets: ['@babel/env']}})
|
||||
```
|
||||
|
||||
You can also transpile CoffeeScript’s output without using the `transpile` option, for example as part of a build chain. This lets you use transpilers other than Babel, and it gives you greater control over the process. There are many great task runners for setting up JavaScript build chains, such as [Gulp](http://gulpjs.com/), [Webpack](https://webpack.github.io/), [Grunt](https://gruntjs.com/) and [Broccoli](http://broccolijs.com/).
|
||||
|
||||
#### Polyfills
|
||||
|
||||
Note that transpiling doesn’t automatically supply [polyfills](https://developer.mozilla.org/en-US/docs/Glossary/Polyfill) for your code. CoffeeScript itself will output [`Array.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) if you use the `in` operator, or destructuring or spread/rest syntax; and [`Function.bind`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) if you use a bound (`=>`) method in a class. Both are supported in Internet Explorer 9+ and all more recent browsers, but you will need to supply polyfills if you need to support Internet Explorer 8 or below and are using features that would cause these methods to be output. You’ll also need to supply polyfills if your own code uses these methods or another method added in recent versions of JavaScript. One polyfill option is [`babel-polyfill`](https://babeljs.io/docs/usage/polyfill/), though there are many [other](https://hackernoon.com/polyfills-everything-you-ever-wanted-to-know-or-maybe-a-bit-less-7c8de164e423) [strategies](https://philipwalton.com/articles/loading-polyfills-only-when-needed/).
|
||||
Note that transpiling doesn’t automatically supply [polyfills](https://developer.mozilla.org/en-US/docs/Glossary/Polyfill) for your code. CoffeeScript itself will output [`Array.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) if you use the `in` operator, or destructuring or spread/rest syntax; and [`Function.bind`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) if you use a bound (`=>`) method in a class. Both are supported in Internet Explorer 9+ and all more recent browsers, but you will need to supply polyfills if you need to support Internet Explorer 8 or below and are using features that would cause these methods to be output. You’ll also need to supply polyfills if your own code uses these methods or another method added in recent versions of JavaScript. One polyfill option is [`@babel/polyfill`](https://babeljs.io/docs/en/babel-polyfill/), though there are many [other](https://hackernoon.com/polyfills-everything-you-ever-wanted-to-know-or-maybe-a-bit-less-7c8de164e423) [strategies](https://philipwalton.com/articles/loading-polyfills-only-when-needed/).
|
||||
|
|
|
@ -619,16 +619,20 @@
|
|||
// on us to transpile for them; we assume most users will probably either
|
||||
// run CoffeeScript’s output without transpilation (modern Node or evergreen
|
||||
// browsers) or use a proper build chain like Gulp or Webpack.
|
||||
require('babel-core');
|
||||
require('@babel/core');
|
||||
} catch (error) {
|
||||
// Give appropriate instructions depending on whether `coffee` was run
|
||||
// locally or globally.
|
||||
if (require.resolve('.').indexOf(process.cwd()) === 0) {
|
||||
console.error('To use --transpile, you must have babel-core installed:\n npm install --save-dev babel-core\nAnd you must save options to configure Babel in one of the places it looks to find its options.\nSee https://coffeescript.org/#transpilation');
|
||||
} else {
|
||||
console.error('To use --transpile with globally-installed CoffeeScript, you must have babel-core installed globally:\n npm install --global babel-core\nAnd you must save options to configure Babel in one of the places it looks to find its options, relative to the file being compiled or to the current folder.\nSee https://coffeescript.org/#transpilation');
|
||||
try {
|
||||
require('babel-core');
|
||||
} catch (error) {
|
||||
// Give appropriate instructions depending on whether `coffee` was run
|
||||
// locally or globally.
|
||||
if (require.resolve('.').indexOf(process.cwd()) === 0) {
|
||||
console.error('To use --transpile, you must have @babel/core installed:\n npm install --save-dev @babel/core\nAnd you must save options to configure Babel in one of the places it looks to find its options.\nSee https://coffeescript.org/#transpilation');
|
||||
} else {
|
||||
console.error('To use --transpile with globally-installed CoffeeScript, you must have @babel/core installed globally:\n npm install --global @babel/core\nAnd you must save options to configure Babel in one of the places it looks to find its options, relative to the file being compiled or to the current folder.\nSee https://coffeescript.org/#transpilation');
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
if (typeof opts.transpile !== 'object') {
|
||||
opts.transpile = {};
|
||||
|
|
|
@ -17,11 +17,15 @@
|
|||
CoffeeScript.transpile = function(js, options) {
|
||||
var babel;
|
||||
try {
|
||||
babel = require('babel-core');
|
||||
babel = require('@babel/core');
|
||||
} catch (error) {
|
||||
// This error is only for Node, as CLI users will see a different error
|
||||
// earlier if they don’t have Babel installed.
|
||||
throw new Error('To use the transpile option, you must have the \'babel-core\' module installed');
|
||||
try {
|
||||
babel = require('babel-core');
|
||||
} catch (error) {
|
||||
// This error is only for Node, as CLI users will see a different error
|
||||
// earlier if they don’t have Babel installed.
|
||||
throw new Error('To use the transpile option, you must have the \'@babel/core\' module installed');
|
||||
}
|
||||
}
|
||||
return babel.transform(js, options);
|
||||
};
|
||||
|
|
|
@ -250,12 +250,16 @@
|
|||
CoffeeScript.register();
|
||||
process.argv = ['coffee'].concat(process.argv.slice(2));
|
||||
if (opts.transpile) {
|
||||
transpile = {};
|
||||
try {
|
||||
transpile = {};
|
||||
transpile.transpile = require('babel-core').transform;
|
||||
transpile.transpile = require('@babel/core').transform;
|
||||
} catch (error) {
|
||||
console.error('To use --transpile with an interactive REPL, babel-core must be installed either in the current folder or globally:\n npm install --save-dev babel-core\nor\n npm install --global babel-core\nAnd you must save options to configure Babel in one of the places it looks to find its options.\nSee https://coffeescript.org/#transpilation');
|
||||
process.exit(1);
|
||||
try {
|
||||
transpile.transpile = require('babel-core').transform;
|
||||
} catch (error) {
|
||||
console.error('To use --transpile with an interactive REPL, @babel/core must be installed either in the current folder or globally:\n npm install --save-dev @babel/core\nor\n npm install --global @babel/core\nAnd you must save options to configure Babel in one of the places it looks to find its options.\nSee https://coffeescript.org/#transpilation');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
transpile.options = {
|
||||
filename: path.resolve(process.cwd(), '<repl>')
|
||||
|
|
2420
package-lock.json
generated
2420
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -39,8 +39,8 @@
|
|||
"url": "git://github.com/jashkenas/coffeescript.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-core": "^6.26.3",
|
||||
"babel-preset-env": "1.7.0",
|
||||
"@babel/core": "^7.0.0",
|
||||
"@babel/preset-env": "^7.0.0",
|
||||
"babel-preset-minify": "^0.4.3",
|
||||
"codemirror": "^5.39.2",
|
||||
"docco": "~0.8.0",
|
||||
|
@ -48,7 +48,7 @@
|
|||
"jison": ">=0.4.18",
|
||||
"markdown-it": "~8.4.2",
|
||||
"underscore": "~1.9.1",
|
||||
"webpack": "~4.16.5"
|
||||
"webpack": "~4.17.2"
|
||||
},
|
||||
"dependencies": {}
|
||||
}
|
||||
|
|
|
@ -454,25 +454,28 @@ compileOptions = (filename, base) ->
|
|||
# run CoffeeScript’s output without transpilation (modern Node or evergreen
|
||||
# browsers) or use a proper build chain like Gulp or Webpack.
|
||||
try
|
||||
require 'babel-core'
|
||||
require '@babel/core'
|
||||
catch
|
||||
# Give appropriate instructions depending on whether `coffee` was run
|
||||
# locally or globally.
|
||||
if require.resolve('.').indexOf(process.cwd()) is 0
|
||||
console.error '''
|
||||
To use --transpile, you must have babel-core installed:
|
||||
npm install --save-dev babel-core
|
||||
And you must save options to configure Babel in one of the places it looks to find its options.
|
||||
See https://coffeescript.org/#transpilation
|
||||
'''
|
||||
else
|
||||
console.error '''
|
||||
To use --transpile with globally-installed CoffeeScript, you must have babel-core installed globally:
|
||||
npm install --global babel-core
|
||||
And you must save options to configure Babel in one of the places it looks to find its options, relative to the file being compiled or to the current folder.
|
||||
See https://coffeescript.org/#transpilation
|
||||
'''
|
||||
process.exit 1
|
||||
try
|
||||
require 'babel-core'
|
||||
catch
|
||||
# Give appropriate instructions depending on whether `coffee` was run
|
||||
# locally or globally.
|
||||
if require.resolve('.').indexOf(process.cwd()) is 0
|
||||
console.error '''
|
||||
To use --transpile, you must have @babel/core installed:
|
||||
npm install --save-dev @babel/core
|
||||
And you must save options to configure Babel in one of the places it looks to find its options.
|
||||
See https://coffeescript.org/#transpilation
|
||||
'''
|
||||
else
|
||||
console.error '''
|
||||
To use --transpile with globally-installed CoffeeScript, you must have @babel/core installed globally:
|
||||
npm install --global @babel/core
|
||||
And you must save options to configure Babel in one of the places it looks to find its options, relative to the file being compiled or to the current folder.
|
||||
See https://coffeescript.org/#transpilation
|
||||
'''
|
||||
process.exit 1
|
||||
|
||||
opts.transpile = {} unless typeof opts.transpile is 'object'
|
||||
|
||||
|
|
|
@ -8,11 +8,14 @@ helpers = CoffeeScript.helpers
|
|||
|
||||
CoffeeScript.transpile = (js, options) ->
|
||||
try
|
||||
babel = require 'babel-core'
|
||||
babel = require '@babel/core'
|
||||
catch
|
||||
# This error is only for Node, as CLI users will see a different error
|
||||
# earlier if they don’t have Babel installed.
|
||||
throw new Error 'To use the transpile option, you must have the \'babel-core\' module installed'
|
||||
try
|
||||
babel = require 'babel-core'
|
||||
catch
|
||||
# This error is only for Node, as CLI users will see a different error
|
||||
# earlier if they don’t have Babel installed.
|
||||
throw new Error 'To use the transpile option, you must have the \'@babel/core\' module installed'
|
||||
babel.transform js, options
|
||||
|
||||
# The `compile` method shared by the CLI, Node and browser APIs.
|
||||
|
|
|
@ -182,19 +182,22 @@ module.exports =
|
|||
CoffeeScript.register()
|
||||
process.argv = ['coffee'].concat process.argv[2..]
|
||||
if opts.transpile
|
||||
transpile = {}
|
||||
try
|
||||
transpile = {}
|
||||
transpile.transpile = require('babel-core').transform
|
||||
transpile.transpile = require('@babel/core').transform
|
||||
catch
|
||||
console.error '''
|
||||
To use --transpile with an interactive REPL, babel-core must be installed either in the current folder or globally:
|
||||
npm install --save-dev babel-core
|
||||
or
|
||||
npm install --global babel-core
|
||||
And you must save options to configure Babel in one of the places it looks to find its options.
|
||||
See https://coffeescript.org/#transpilation
|
||||
'''
|
||||
process.exit 1
|
||||
try
|
||||
transpile.transpile = require('babel-core').transform
|
||||
catch
|
||||
console.error '''
|
||||
To use --transpile with an interactive REPL, @babel/core must be installed either in the current folder or globally:
|
||||
npm install --save-dev @babel/core
|
||||
or
|
||||
npm install --global @babel/core
|
||||
And you must save options to configure Babel in one of the places it looks to find its options.
|
||||
See https://coffeescript.org/#transpilation
|
||||
'''
|
||||
process.exit 1
|
||||
transpile.options =
|
||||
filename: path.resolve process.cwd(), '<repl>'
|
||||
# Since the REPL compilation path is unique (in `eval` above), we need
|
||||
|
|
|
@ -11,7 +11,7 @@ transpile = (method, code, options = {}) ->
|
|||
options.bare = yes
|
||||
options.transpile =
|
||||
# Target Internet Explorer 6, which supports no ES2015+ features.
|
||||
presets: [['env', {targets: browsers: ['ie 6']}]]
|
||||
presets: [['@babel/env', {targets: browsers: ['ie 6']}]]
|
||||
CoffeeScript[method] code, options
|
||||
|
||||
|
||||
|
@ -176,4 +176,4 @@ test "#3306: trailing comma in a function call in the last line", ->
|
|||
foo bar,
|
||||
''', '''
|
||||
foo(bar);
|
||||
'''
|
||||
'''
|
||||
|
|
Loading…
Add table
Reference in a new issue