alangpierce /sucrase
Super-fast alternative to Babel for when you can target modern JS runtimes
★ 5,878⑂ 161👁 43⑃ 606● 250▤ 0
+0
日增长
5,878
stars
161
forks
43
watchers
606
prs
250
issues
0
releases
README
Sucrase
Try it out
Quick usage
bash
yarn add --dev sucrase Or npm install --save-dev sucrase
node -r sucrase/register main.ts
Using the ts-node integration:
bash
yarn add --dev sucrase ts-node typescript
./node_modules/.bin/ts-node --transpiler sucrase/ts-node-plugin main.ts
Project overview
Sucrase is an alternative to Babel that allows super-fast development builds.
Instead of compiling a large range of JS features to be able to work in Internet
Explorer, Sucrase assumes that you're developing with a recent browser or recent
Node.js version, so it focuses on compiling non-standard language extensions:
JSX, TypeScript, and Flow. Because of this smaller scope, Sucrase can get away
with an architecture that is much more performant but less extensible and
maintainable. Sucrase's parser is forked from Babel's parser (so Sucrase is
indebted to Babel and wouldn't be possible without it) and trims it down to a
focused subset of what Babel solves. If it fits your use case, hopefully Sucrase
can speed up your development experience!
Sucrase has been extensively tested. It can successfully build
the Benchling frontend code,
Babel,
React,
TSLint,
Apollo client, and
decaffeinate
with all tests passing, about 1 million lines of code total.
Sucrase is about 20x faster than Babel. Here's one measurement of how
Sucrase compares with other tools when compiling the Jest codebase 3 times,
about 360k lines of code total:
text
Time Speed
Sucrase 0.57 seconds 636975 lines per second
swc 1.19 seconds 304526 lines per second
esbuild 1.45 seconds 248692 lines per second
TypeScript 8.98 seconds 40240 lines per second
Babel 9.18 seconds 39366 lines per second
Details: Measured on July 2022. Tools run in single-threaded mode without warm-up. See the
benchmark code
for methodology and caveats.
Transforms
The main configuration option in Sucrase is an array of transform names. These
transforms are available:
jsx: Enables JSX syntax. By default, JSX is transformed to React.createClass,
but may be preserved or transformed to _jsx() by setting the jsxRuntime option.
Also adds createReactClass display names and JSX context information.
typescript: Compiles TypeScript code to JavaScript, removing type
annotations and handling features like enums. Does not check types. Sucrase
transforms each file independently, so you should enable the isolatedModules
TypeScript flag so that the typechecker will disallow the few features like
const enums that need cross-file compilation. The Sucrase option keepUnusedImports
can be used to disable all automatic removal of imports and exports, analogous to TS
verbatimModuleSyntax.
flow: Removes Flow type annotations. Does not check types.
imports: Transforms ES Modules (import/export) to CommonJS
(require/module.exports) using the same approach as Babel and TypeScript
with --esModuleInterop. If preserveDynamicImport is specified in theTop issues
#361Survey: How do you use Sucrase, what would make it better?22 comments · 2018-12-01#330Decorator Support19 comments · 2018-11-09#507Can we run sucrase in the browser like babel-standalone?16 comments · 2020-02-12#161Project roadmap and ways others can contribute9 comments · 2018-01-23#436HMR on non-web react project6 comments · 2019-03-16#216Improve performance by compiling to webassembly6 comments · 2018-05-28#476Can't import the named export 'parse' from non EcmaScript module (only default export is available)5 comments · 2019-12-03#302Eventually is almost here: Class Fields5 comments · 2018-07-20