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 and compiles non-standard language
extensions (JSX, TypeScript, and Flow) down to standard JavaScript. It also
compiles import
to require
in the same style as Babel. Because of this
smaller scope, Sucrase can get away with an architecture that is much more
performant, but requires more work to implement and maintain each transform.
Current state: The project is under development and you may see bugs if you run it on a large codebase. 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:
Installation:
yarn add sucrase # Or npm install sucrase
Run on a directory:
sucrase ./srcDir --transforms imports,flow -d ./outDir
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: ["imports", "flow"]});
Sucrase currently makes some simplifying assumptions about your code, which are often seen as good practice anyway:
as
. Some steps may run into trouble if you do.Analogous to babel-plugin-transform-react-jsx.
Converts JSX syntax to React.createElement
, e.g. <div a={b} />
becomes
React.createElement('div', {a: b})
.
Analogous to babel-plugin-transform-typescript.
Compiles TypeScript code to JavaScript, removing type annotations and handling features like class fields.
declare
, is not yet supported.f
from a CommonJS module using
import * as f from "f";
, but Babel and Sucrase do not.Analogous to babel-plugin-transform-flow-strip-types.
Removes Flow types, e.g. const f = (x: number): string => "hi";
to
const f = (x) => "hi";
.
declare
, has not been implemented yet.Analogous to babel-plugin-transform-react-display-name
Detect and add display name to React component created using React.createClass
or createReactClass
.
export default
position, since the Sucrase API currently does not accept the
filename.Analogous to babel-plugin-transform-es2015-modules-commonjs
Converts ES Modules (import
/export
) to CommonJS (require
/module.exports
)
using the same approach as Babel.
export let a = 1;
, a = 2;
works, but
[a] = [3];
will not cause imported usages of a
to update.Analogous to babel-plugin-add-module-exports
Add a snippet to emulate the Babel 5 approach to CommonJS interop: if a module
has only a default export, that default export is used as the module body, which
avoids the need for code like require('./MyModule').default
.
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, Flow, or TypeScript. 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
.Currently, Sucrase runs about 4-5x faster than Babel (even when Babel only runs
the relevant transforms). Here's the output of one run of npm run benchmark
:
Simulating transpilation of 100,000 lines of code:
Sucrase: 2298.723ms
TypeScript: 3420.195ms
Babel: 9364.096ms
Previous iterations have been 15-20x faster, and hopefully additional performance work will bring it back to that speed.
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.