Super-fast alternative to Babel for when you can target modern JS runtimes
This project is fork of sucrase compiler
import {transform} from "@mizchi/sucrase";
// typescript is default
const code = transform("export const x: number = 1;", {
transforms: ["jsx"],
keepUnusedImports: true,
});
console.log(code.code);
--experimental-loader instead of hookSucrase 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!
undefinedSucrase 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.
undefinedSucrase 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:
Time Speed
Sucrase 1.64 seconds 220221 lines per second
swc 2.13 seconds 169502 lines per second
esbuild 3.02 seconds 119738 lines per second
TypeScript 24.18 seconds 14937 lines per second
Babel 27.22 seconds 13270 lines per second
Details: Measured on January 2021. Tools run in single-threaded mode without warm-up. See the
benchmark code
for methodology and caveats.
The main configuration option in Sucrase is an array of transform names. These
transforms are available:
React.createElement, e.g. <div a={b} />React.createElement('div', {a: b}). Behaves like Babel 7’screateReactClass display names and JSX context information.isolatedModulesconst enums that need cross-file compilation.import/export) to CommonJSrequire/module.exports) using the same approach as Babel and TypeScript--esModuleInterop. Also includes dynamic import.react-hot-loader/babeljest.mock, but the same rules still apply.These proposed JS features are built-in and always transformed:
a?.ba ?? bclass C { x = 1; }.#x private field syntax.export * as a from 'a';const n = 1_234;try { doThing(); } catch { }.All JS syntax not mentioned above will “pass through” and needs to be supported
by your JS runtime. For example:
throw expressions, generator arrow functions,do expressions are all unsupported in browsers and Node (as of thisLike Babel, Sucrase compiles JSX to React functions by default, but can be
configured for any JSX use case.
React.createElement.React.Fragment.Two legacy modes can be used with the import transform:
--esModuleInteropimport * as add from './add';,--esModuleInterop requireimport add from './add';. As mentioned in the--esModuleInterop.require('./MyModule') instead ofrequire('./MyModule').default. Analogous toInstallation:
yarn add --dev sucrase # Or npm install --save-dev sucrase
Often, you’ll want to use one of the build tool integrations:
Webpack,
Gulp,
Jest,
Rollup,
Broccoli.
Compile on-the-fly via a require hook with some reasonable defaults:
// Register just one extension.
require("sucrase/register/ts");
// Or register all at once.
require("sucrase/register");
Compile on-the-fly via a drop-in replacement for node:
sucrase-node index.ts
Run on a directory:
sucrase ./srcDir -d ./outDir --transforms typescript,imports
Call from JS directly:
import {transform} from "sucrase";
const compiledCode = transform(code, {transforms: ["typescript", "imports"]}).code;
Sucrase is intended to be useful for the most common cases, but it does not aim
to have nearly the scope and versatility of Babel. Some specific examples:
const enums are treated as regularenums rather than inlining across files.See the Project Vision document for more details on
the philosophy behind Sucrase.
As JavaScript implementations mature, it becomes more and more reasonable to
disable Babel transforms, especially in development when you know that you’re
targeting a modern runtime. You might hope that you could simplify and speed up
the build step by eventually disabling Babel entirely, but this isn’t possible
if you’re using a non-standard language extension like JSX, TypeScript, or Flow.
Unfortunately, disabling most transforms in Babel doesn’t speed it up as much as
you might expect. To understand, let’s take a look at how Babel works:
Only step 4 gets faster when disabling plugins, so there’s always a fixed cost
to running Babel regardless of how many transforms are enabled.
Sucrase bypasses most of these steps, and works like this:
<Foo withReact.createElement(Foo.Because Sucrase works on a lower level and uses a custom parser for its use
case, it is much faster than Babel.
Contributions are welcome, whether they be bug reports, PRs, docs, tests, or
anything else! Please take a look through the Contributing Guide
to learn how to get started.
Sucrase is MIT-licensed. A large part of Sucrase is based on a fork of the
Babel parser,
which is also MIT-licensed.
Sucrase is an enzyme that processes sugar. Get it?