Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Sucrase is a super-fast alternative to Babel for compiling modern JavaScript into older versions that are more widely supported. It focuses on compiling non-standard syntax like JSX, TypeScript, and Flow into standard JavaScript, offering significant speed improvements over Babel.
JSX Compilation
Sucrase can compile JSX syntax used in React applications into standard JavaScript, making it easier to run in environments that do not support JSX natively.
import React from 'react';
const App = () => <div>Hello, Sucrase!</div>;
TypeScript Compilation
Sucrase can compile TypeScript code into plain JavaScript, allowing developers to use TypeScript's type checking features without worrying about compatibility.
import express from 'express';
const app: express.Application = express();
Flow Compilation
Sucrase provides support for Flow, a static type checker for JavaScript. It can strip Flow type annotations and compile the code into standard JavaScript.
/* @flow */
function square(n: number): number {
return n * n;
}
Babel is a widely used compiler for writing next generation JavaScript. It's more flexible and configurable than Sucrase, but generally slower due to its comprehensive feature set.
The TypeScript compiler not only compiles TypeScript into JavaScript but also provides type checking. It's similar to Sucrase's TypeScript compilation feature but includes type checking, which Sucrase does not.
esbuild is an extremely fast JavaScript bundler and minifier. It offers similar compilation features to Sucrase but also includes bundling and minification, making it a more comprehensive tool for building web applications.
Sucrase is an alternative to Babel that allows super-fast development builds. Instead of compiling a large range of JS features down to ES5, Sucrase assumes that you're targeting a modern JS runtime (e.g. Node.js 8 or latest Chrome) and 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.
Current state: The project is in active development. It is about 13x faster than Babel and about 5x faster than TypeScript, and it has been tested on hundreds of thousands of lines of code. You may still find bugs when running on your code, though. You probably shouldn't use it in production, but you may find it useful in development. Feel free to file issues!
Sucrase can convert the following codebases with all tests passing:
The main configuration option in Sucrase is an array of transform names. There are four main transforms that you may want to enable:
React.createElement
, e.g. <div a={b} />
becomes React.createElement('div', {a: b})
. Behaves like Babel 7's
babel-preset-react,
including adding createReactClass
display names and JSX context information.const f = (x: number): string => "hi";
to const f = (x) => "hi";
. Does not check types.import
/export
) to CommonJS
(require
/module.exports
) using the same approach as Babel. With the
typescript
transform enabled, the import conversion uses the behavior of the
TypeScript compiler (which is slightly more lenient). Also includes dynamic
import
.The following proposed JS features are built-in and always transformed:
class C { x = 1; }
.
This includes static fields but not the #x
private field syntax.export * as a from 'a';
const n = 1_234;
try { doThing(); } catch { }
.There are some additional opt-in transforms that are useful in legacy situations:
require('./MyModule')
instead of require('./MyModule').default
.
Analogous to
babel-plugin-add-module-exports.Installation:
yarn add --dev sucrase # Or npm install --save-dev sucrase
Run on a directory:
sucrase ./srcDir -d ./outDir --transforms typescript,imports
Register a require hook with some reasonable defaults:
// Register just one extension.
import "sucrase/register/ts";
// Or register all at once.
import "sucrase/register";
Call from JS directly:
import {transform} from "sucrase";
const compiledCode = transform(code, {transforms: ["typescript", "imports"]});
There are also integrations for Webpack, Gulp, and Jest.
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
with
React.createElement(Foo
.Because Sucrase works on a lower level and uses a custom parser for its use case, it is much faster than Babel.
Currently, Sucrase runs about 13x faster than Babel (even when Babel only runs
the relevant transforms) and 5x faster than TypeScript. Here's the output of
one run of npm run benchmark
:
Simulating transpilation of 100,000 lines of code:
Sucrase: 777.638ms
TypeScript: 3820.914ms
Babel: 10041.368ms
Sucrase is MIT-licensed. A large part of Sucrase is based on a fork of Babylon, which is also MIT-licensed.
Sucrase is an enzyme that processes sugar. Get it?
FAQs
Super-fast alternative to Babel for when you can target modern JS runtimes
The npm package sucrase receives a total of 9,321,896 weekly downloads. As such, sucrase popularity was classified as popular.
We found that sucrase demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.