Updated: Migrating from CoffeeScript to ES6

Posted . Visible to the public. Auto-destruct in 16 days
  • Simplified setup
  • Recommend using eslint to clean up styling

Changes

  • It is quite easy to migrate from CoffeeScript to ES6. You can use [decaffeinate](https://github.com/decaffeinate/decaffeinate) to convert your CoffeeScript source to modern JavaScript.
  • -## Installation
  • -
  • -* Install the NPM module globally `yarn global add decaffeinate`
  • -* Add the following line to your `~/.bashrc` to export the Yarn binaries in your Bash
  • +Install decaffeinate globally:
  • +```bash
  • +npm install -g decaffeinate
  • ```
  • -export PATH="$PATH:$(yarn global bin)"
  • +
  • +Call `decaffeinate` on each `.coffee` file, relaxing some options to get the most modern (and concise) JS:
  • +
  • +```bash
  • +decaffeinate file.coffee --use-cs2 --loose --optional-chaining --logical-assignment
  • ```
  • -* Run `decaffeinate some.coffee` to get the converted modern JavaScript file.
  • +> [tip]
  • +> If you use [Babel](https://babeljs.io/) and see errors while decaffeinating, try deleting your `babel.config.js`, run `decaffeinate`, then restore the babel config file.
  • -## Notes
  • +If your project [uses eslint](https://makandracards.com/makandra/509642-integrating-eslint), have it fix style issues (like semicolons):
  • +
  • +```bash
  • +npx eslint --fix file.js
  • +```
  • -* decaffeinate will add some [suggestions](https://github.com/decaffeinate/decaffeinate/blob/4da99ddc0f3c30f724e1db747fedc5c0f8a702af/docs/suggestions.md) to your newly generated JS file with advises that makes you JS cleaner. This action need to be done manually and carefully checked.
  • -* You might want to use [the options](https://github.com/decaffeinate/decaffeinate#other-options) --prefer-let and --use-js-modules
  • -* If you get errors while decaffeinating, try deleting your `babel.config.js`, run decaffeinate, restore the babel config file.
  • +Decaffeinate will add some [suggestions](https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md) to your newly generated JS file with advises that makes you JS cleaner. This action need to be done manually and carefully checked. In particular you will need to clean up a lot of unnecessary `return` statements.
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2024-07-26 11:30)