hacklang-sucrase

Super-fast alternative to Babel for when you can target modern JS runtimes

Usage no npm install needed!

<script type="module">
  import hacklangSucrase from 'https://cdn.skypack.dev/hacklang-sucrase';
</script>

README

hacklang-sucrose

An actual working implementation of hacklang. This repository is a minimal-change adaptation of Sucrase

Usage

See ./examples/ to use it with webpack, with a node project (coming soon) or with CRA

Contributing

Setup

git clone https://github.com/eankeen/hacklang-sucrose
cd hacklang-sucrase

yarn i
yarn i -D tslib
yarn generate
yarn build

Adding Your Keyword

Note: For reference, see the var -> rice, const -> yuto, class -> aditya (partially), and throw -> carrot

  1. Edit keyword entry in KEYWORDS variable in generator/generateReadWordTree.ts. Make sure to prepend a comment!

  2. Add if statement for your new keyword in the second loop over ALL_KEYWORDS (starts around line 130) in generator/generateReadWordTree.ts

  • This is done because the property (ex. _var) used to access the real name of the token (ex. rice). Is autogenerated from the changed token we specify. We want to make the accessor stay as _var (instead of changing to _rice) so we don't have to refactor the rest of the codebase
  1. Edit KeywordTokenType instantiation for your keyword in generator/generateTokenTypes.ts

  2. Generate the proper tokens from your changes

yarn generate
  1. Add your token to the Hacklang transformer in src/parser/transformers/HacklangTransformer.ts

  2. Build project

  • The build pipeline is different compared to Sucrose because the multi-step self-build check to ensure compiler output integrity has been removed
yarn build
  1. Test your keyword :)
node examples/sucrase-cli/input.js
( cd examples/webpack && yarn webpack )
( cd examples/create-react-app && yarn start )

  1. Commit changes and make a PR 😝

Note that although the setup for above should work for most of the keywords, entities such as =, super(), constructor() and possibly this may require a custom transformation under src/parser/transformers/HacklangTransformer.ts

Roadmap

  • create webpack plugin to auto modify CRA config
  • create example for node using our @sucrase/register
  • add more keywords