1 | # Sucrase
|
2 |
|
3 | [![Build Status](https://travis-ci.org/alangpierce/sucrase.svg?branch=master)](https://travis-ci.org/alangpierce/sucrase)
|
4 | [![npm version](https://img.shields.io/npm/v/sucrase.svg)](https://www.npmjs.com/package/sucrase)
|
5 | [![Install Size](https://packagephobia.now.sh/badge?p=sucrase)](https://packagephobia.now.sh/result?p=sucrase)
|
6 | [![MIT License](https://img.shields.io/npm/l/express.svg?maxAge=2592000)](LICENSE)
|
7 | [![Join the chat at https://gitter.im/sucrasejs](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/sucrasejs/Lobby)
|
8 |
|
9 | ### [Try it out](https://sucrase.io)
|
10 |
|
11 | Sucrase is an alternative to Babel that allows super-fast development builds.
|
12 | Instead of compiling a large range of JS features to be able to work in Internet
|
13 | Explorer, Sucrase assumes that you're developing with a recent browser or recent
|
14 | Node.js version, so it focuses on compiling non-standard language extensions:
|
15 | JSX, TypeScript, and Flow. Because of this smaller scope, Sucrase can get away
|
16 | with an architecture that is much more performant but less extensible and
|
17 | maintainable. Sucrase's parser is forked from Babel's parser (so Sucrase is
|
18 | indebted to Babel and wouldn't be possible without it) and trims it down to a
|
19 | focused subset of what Babel solves. If it fits your use case, hopefully Sucrase
|
20 | can speed up your development experience!
|
21 |
|
22 | **Sucrase has been extensively tested.** It can successfully build
|
23 | the [Benchling](https://benchling.com/) frontend code,
|
24 | [Babel](https://github.com/babel/babel),
|
25 | [React](https://github.com/facebook/react),
|
26 | [TSLint](https://github.com/palantir/tslint),
|
27 | [Apollo client](https://github.com/apollographql/apollo-client), and
|
28 | [decaffeinate](https://github.com/decaffeinate/decaffeinate)
|
29 | with all tests passing, about 1 million lines of code total.
|
30 |
|
31 | **Sucrase is about 20x faster than Babel.** Here's one measurement of how Sucrase
|
32 | compares with other tools on a large TypeScript codebase with 4045 files and
|
33 | 661081 lines of code:
|
34 | ```
|
35 | Time Speed
|
36 | Sucrase 2.928s 225752 lines per second
|
37 | swc 13.782s 47966 lines per second
|
38 | TypeScript 39.603s 16693 lines per second
|
39 | Babel 52.598s 12569 lines per second
|
40 | ```
|
41 |
|
42 | ## Transforms
|
43 |
|
44 | The main configuration option in Sucrase is an array of transform names. These
|
45 | transforms are available:
|
46 | * **jsx**: Transforms JSX syntax to `React.createElement`, e.g. `<div a={b} />`
|
47 | becomes `React.createElement('div', {a: b})`. Behaves like Babel 7's
|
48 | [React preset](https://github.com/babel/babel/tree/master/packages/babel-preset-react),
|
49 | including adding `createReactClass` display names and JSX context information.
|
50 | * **typescript**: Compiles TypeScript code to JavaScript, removing type
|
51 | annotations and handling features like enums. Does not check types. Sucrase
|
52 | transforms each file independently, so you should enable the `isolatedModules`
|
53 | TypeScript flag so that the typechecker will disallow the few features like
|
54 | `const enum`s that need cross-file compilation.
|
55 | * **flow**: Removes Flow type annotations. Does not check types.
|
56 | * **imports**: Transforms ES Modules (`import`/`export`) to CommonJS
|
57 | (`require`/`module.exports`) using the same approach as Babel and TypeScript
|
58 | with `--esModuleInterop`. Also includes dynamic `import`.
|
59 | * **react-hot-loader**: Performs the equivalent of the `react-hot-loader/babel`
|
60 | transform in the [react-hot-loader](https://github.com/gaearon/react-hot-loader)
|
61 | project. This enables advanced hot reloading use cases such as editing of
|
62 | bound methods.
|
63 |
|
64 | These proposed JS features are built-in and always transformed:
|
65 | * [Class fields](https://github.com/tc39/proposal-class-fields): `class C { x = 1; }`.
|
66 | This includes static fields but not the `#x` private field syntax.
|
67 | * [Export namespace syntax](https://github.com/tc39/proposal-export-ns-from):
|
68 | `export * as a from 'a';`
|
69 | * [Numeric separators](https://github.com/tc39/proposal-numeric-separator):
|
70 | `const n = 1_234;`
|
71 | * [Optional catch binding](https://github.com/tc39/proposal-optional-catch-binding):
|
72 | `try { doThing(); } catch { }`.
|
73 |
|
74 | ### Unsupported syntax
|
75 |
|
76 | All JS syntax not mentioned above will "pass through" and needs to be supported
|
77 | by your JS runtime. For example:
|
78 | * Decorators, private fields, `throw` expressions, optional chaining, generator
|
79 | arrow functions, and `do` expressions are all unsupported in browsers and Node
|
80 | (as of this writing), and Sucrase doesn't make an attempt to transpile them.
|
81 | * Object rest/spread, async functions, and async iterators are all recent
|
82 | features that should work fine, but might cause issues if you use older
|
83 | versions of tools like webpack. BigInt and newer regex features may or may not
|
84 | work, based on your tooling.
|
85 |
|
86 | ### JSX Options
|
87 | Like Babel, Sucrase compiles JSX to React functions by default, but can be
|
88 | configured for any JSX use case.
|
89 | * **jsxPragma**: Element creation function, defaults to `React.createElement`.
|
90 | * **jsxFragmentPragma**: Fragment component, defaults to `React.Fragment`.
|
91 |
|
92 | ### Legacy CommonJS interop
|
93 | Two legacy modes can be used with the `import` transform:
|
94 | * **enableLegacyTypeScriptModuleInterop**: Use the default TypeScript approach
|
95 | to CommonJS interop instead of assuming that TypeScript's `--esModuleInterop`
|
96 | flag is enabled. For example, if a CJS module exports a function, legacy
|
97 | TypeScript interop requires you to write `import * as add from './add';`,
|
98 | while Babel, Webpack, Node.js, and TypeScript with `--esModuleInterop` require
|
99 | you to write `import add from './add';`. As mentioned in the
|
100 | [docs](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#support-for-import-d-from-cjs-form-commonjs-modules-with---esmoduleinterop),
|
101 | the TypeScript team recommends you always use `--esModuleInterop`.
|
102 | * **enableLegacyBabel5ModuleInterop**: Use the Babel 5 approach to CommonJS
|
103 | interop, so that you can run `require('./MyModule')` instead of
|
104 | `require('./MyModule').default`. Analogous to
|
105 | [babel-plugin-add-module-exports](https://github.com/59naga/babel-plugin-add-module-exports).
|
106 |
|
107 | ## Usage
|
108 |
|
109 | Installation:
|
110 |
|
111 | ```
|
112 | yarn add --dev sucrase # Or npm install --save-dev sucrase
|
113 | ```
|
114 |
|
115 | Often, you'll want to use one of the build tool integrations:
|
116 | [Webpack](https://github.com/alangpierce/sucrase/tree/master/integrations/webpack-loader),
|
117 | [Gulp](https://github.com/alangpierce/sucrase/tree/master/integrations/gulp-plugin),
|
118 | [Jest](https://github.com/alangpierce/sucrase/tree/master/integrations/jest-plugin),
|
119 | [Rollup](https://github.com/rollup/rollup-plugin-sucrase),
|
120 | [Broccoli](https://github.com/stefanpenner/broccoli-sucrase).
|
121 |
|
122 | Compile on-the-fly via a require hook with some [reasonable defaults](src/register.ts):
|
123 |
|
124 | ```js
|
125 | // Register just one extension.
|
126 | require("sucrase/register/ts");
|
127 | // Or register all at once.
|
128 | require("sucrase/register");
|
129 | ```
|
130 |
|
131 | Compile on-the-fly via a drop-in replacement for node:
|
132 |
|
133 | ```
|
134 | sucrase-node index.ts
|
135 | ```
|
136 |
|
137 | Run on a directory:
|
138 |
|
139 | ```
|
140 | sucrase ./srcDir -d ./outDir --transforms typescript,imports
|
141 | ```
|
142 |
|
143 | Call from JS directly:
|
144 |
|
145 | ```js
|
146 | import {transform} from "sucrase";
|
147 | const compiledCode = transform(code, {transforms: ["typescript", "imports"]}).code;
|
148 | ```
|
149 |
|
150 | ## What Sucrase is not
|
151 |
|
152 | Sucrase is intended to be useful for the most common cases, but it does not aim
|
153 | to have nearly the scope and versatility of Babel. Some specific examples:
|
154 |
|
155 | * Sucrase does not check your code for errors. Sucrase's contract is that if you
|
156 | give it valid code, it will produce valid JS code. If you give it invalid
|
157 | code, it might produce invalid code, it might produce valid code, or it might
|
158 | give an error. Always use Sucrase with a linter or typechecker, which is more
|
159 | suited for error-checking.
|
160 | * Sucrase is not pluginizable. With the current architecture, transforms need to
|
161 | be explicitly written to cooperate with each other, so each additional
|
162 | transform takes significant extra work.
|
163 | * Sucrase is not good for prototyping language extensions and upcoming language
|
164 | features. Its faster architecture makes new transforms more difficult to write
|
165 | and more fragile.
|
166 | * Sucrase will never produce code for old browsers like IE. Compiling code down
|
167 | to ES5 is much more complicated than any transformation that Sucrase needs to
|
168 | do.
|
169 | * Sucrase is hesitant to implement upcoming JS features, although some of them
|
170 | make sense to implement for pragmatic reasons. Its main focus is on language
|
171 | extensions (JSX, TypeScript, Flow) that will never be supported by JS
|
172 | runtimes.
|
173 | * Like Babel, Sucrase is not a typechecker, and must process each file in
|
174 | isolation. For example, TypeScript `const enum`s are treated as regular
|
175 | `enum`s rather than inlining across files.
|
176 | * You should think carefully before using Sucrase in production. Sucrase is
|
177 | mostly beneficial in development, and in many cases, Babel or tsc will be more
|
178 | suitable for production builds.
|
179 |
|
180 | See the [Project Vision](./docs/PROJECT_VISION.md) document for more details on
|
181 | the philosophy behind Sucrase.
|
182 |
|
183 | ## Motivation
|
184 |
|
185 | As JavaScript implementations mature, it becomes more and more reasonable to
|
186 | disable Babel transforms, especially in development when you know that you're
|
187 | targeting a modern runtime. You might hope that you could simplify and speed up
|
188 | the build step by eventually disabling Babel entirely, but this isn't possible
|
189 | if you're using a non-standard language extension like JSX, TypeScript, or Flow.
|
190 | Unfortunately, disabling most transforms in Babel doesn't speed it up as much as
|
191 | you might expect. To understand, let's take a look at how Babel works:
|
192 |
|
193 | 1. Tokenize the input source code into a token stream.
|
194 | 2. Parse the token stream into an AST.
|
195 | 3. Walk the AST to compute the scope information for each variable.
|
196 | 4. Apply all transform plugins in a single traversal, resulting in a new AST.
|
197 | 5. Print the resulting AST.
|
198 |
|
199 | Only step 4 gets faster when disabling plugins, so there's always a fixed cost
|
200 | to running Babel regardless of how many transforms are enabled.
|
201 |
|
202 | Sucrase bypasses most of these steps, and works like this:
|
203 | 1. Tokenize the input source code into a token stream using a trimmed-down fork
|
204 | of the Babel parser. This fork does not produce a full AST, but still
|
205 | produces meaningful token metadata specifically designed for the later
|
206 | transforms.
|
207 | 2. Scan through the tokens, computing preliminary information like all
|
208 | imported/exported names.
|
209 | 3. Run the transform by doing a pass through the tokens and performing a number
|
210 | of careful find-and-replace operations, like replacing `<Foo` with
|
211 | `React.createElement(Foo`.
|
212 |
|
213 | Because Sucrase works on a lower level and uses a custom parser for its use
|
214 | case, it is much faster than Babel.
|
215 |
|
216 | ## Contributing
|
217 |
|
218 | Contributions are welcome, whether they be bug reports, PRs, docs, tests, or
|
219 | anything else! Please take a look through the [Contributing Guide](./CONTRIBUTING.md)
|
220 | to learn how to get started.
|
221 |
|
222 | ## License and attribution
|
223 |
|
224 | Sucrase is MIT-licensed. A large part of Sucrase is based on a fork of the
|
225 | [Babel parser](https://github.com/babel/babel/tree/master/packages/babel-parser),
|
226 | which is also MIT-licensed.
|
227 |
|
228 | ## Why the name?
|
229 |
|
230 | Sucrase is an enzyme that processes sugar. Get it?
|