UNPKG

13 kBMarkdownView Raw
1# Sucrase
2
3[![Build Status](https://github.com/alangpierce/sucrase/workflows/All%20tests/badge.svg)](https://github.com/alangpierce/sucrase/actions)
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
11Sucrase is an alternative to Babel that allows super-fast development builds.
12Instead of compiling a large range of JS features to be able to work in Internet
13Explorer, Sucrase assumes that you're developing with a recent browser or recent
14Node.js version, so it focuses on compiling non-standard language extensions:
15JSX, TypeScript, and Flow. Because of this smaller scope, Sucrase can get away
16with an architecture that is much more performant but less extensible and
17maintainable. Sucrase's parser is forked from Babel's parser (so Sucrase is
18indebted to Babel and wouldn't be possible without it) and trims it down to a
19focused subset of what Babel solves. If it fits your use case, hopefully Sucrase
20can speed up your development experience!
21
22**Sucrase has been extensively tested.** It can successfully build
23the [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)
29with 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
32Sucrase compares with other tools when compiling the Jest codebase 3 times,
33about 360k lines of code total:
34
35```text
36 Time Speed
37Sucrase 0.57 seconds 636975 lines per second
38swc 1.19 seconds 304526 lines per second
39esbuild 1.45 seconds 248692 lines per second
40TypeScript 8.98 seconds 40240 lines per second
41Babel 9.18 seconds 39366 lines per second
42```
43
44Details: Measured on July 2022. Tools run in single-threaded mode without warm-up. See the
45[benchmark code](https://github.com/alangpierce/sucrase/blob/main/benchmark/benchmark.ts)
46for methodology and caveats.
47
48## Transforms
49
50The main configuration option in Sucrase is an array of transform names. These
51transforms are available:
52
53* **jsx**: Transforms JSX syntax to `React.createElement`, e.g. `<div a={b} />`
54 becomes `React.createElement('div', {a: b})`. Behaves like Babel 7's
55 [React preset](https://github.com/babel/babel/tree/main/packages/babel-preset-react),
56 including adding `createReactClass` display names and JSX context information.
57* **typescript**: Compiles TypeScript code to JavaScript, removing type
58 annotations and handling features like enums. Does not check types. Sucrase
59 transforms each file independently, so you should enable the `isolatedModules`
60 TypeScript flag so that the typechecker will disallow the few features like
61 `const enum`s that need cross-file compilation.
62* **flow**: Removes Flow type annotations. Does not check types.
63* **imports**: Transforms ES Modules (`import`/`export`) to CommonJS
64 (`require`/`module.exports`) using the same approach as Babel and TypeScript
65 with `--esModuleInterop`. If `preserveDynamicImport` is specified in the Sucrase
66 options, then dynamic `import` expressions are left alone, which is particularly
67 useful in Node to load ESM-only libraries. If `preserveDynamicImport` is not
68 specified, `import` expressions are transformed into a promise-wrapped call to
69 `require`.
70* **react-hot-loader**: Performs the equivalent of the `react-hot-loader/babel`
71 transform in the [react-hot-loader](https://github.com/gaearon/react-hot-loader)
72 project. This enables advanced hot reloading use cases such as editing of
73 bound methods.
74* **jest**: Hoist desired [jest](https://jestjs.io/) method calls above imports in
75 the same way as [babel-plugin-jest-hoist](https://github.com/facebook/jest/tree/master/packages/babel-plugin-jest-hoist).
76 Does not validate the arguments passed to `jest.mock`, but the same rules still apply.
77
78When the `imports` transform is *not* specified (i.e. when targeting ESM), the
79`injectCreateRequireForImportRequire` option can be specified to transform TS
80`import foo = require("foo");` in a way that matches the
81[TypeScript 4.7 behavior](https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#commonjs-interoperability)
82with `module: nodenext`.
83
84These newer JS features are transformed by default:
85
86* [Optional chaining](https://github.com/tc39/proposal-optional-chaining): `a?.b`
87* [Nullish coalescing](https://github.com/tc39/proposal-nullish-coalescing): `a ?? b`
88* [Class fields](https://github.com/tc39/proposal-class-fields): `class C { x = 1; }`.
89 This includes static fields but not the `#x` private field syntax.
90* [Numeric separators](https://github.com/tc39/proposal-numeric-separator):
91 `const n = 1_234;`
92* [Optional catch binding](https://github.com/tc39/proposal-optional-catch-binding):
93 `try { doThing(); } catch { }`.
94
95If your target runtime supports these features, you can specify
96`disableESTransforms: true` so that Sucrase preserves the syntax rather than
97trying to transform it. Note that transpiled and standard class fields behave
98slightly differently; see the
99[TypeScript 3.7 release notes](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#the-usedefineforclassfields-flag-and-the-declare-property-modifier)
100for details. If you use TypeScript, you can enable the TypeScript option
101`useDefineForClassFields` to enable error checking related to these differences.
102
103### Unsupported syntax
104
105All JS syntax not mentioned above will "pass through" and needs to be supported
106by your JS runtime. For example:
107
108* Decorators, private fields, `throw` expressions, generator arrow functions,
109 and `do` expressions are all unsupported in browsers and Node (as of this
110 writing), and Sucrase doesn't make an attempt to transpile them.
111* Object rest/spread, async functions, and async iterators are all recent
112 features that should work fine, but might cause issues if you use older
113 versions of tools like webpack. BigInt and newer regex features may or may not
114 work, based on your tooling.
115
116### JSX Options
117
118Like Babel, Sucrase compiles JSX to React functions by default, but can be
119configured for any JSX use case.
120
121* **jsxPragma**: Element creation function, defaults to `React.createElement`.
122* **jsxFragmentPragma**: Fragment component, defaults to `React.Fragment`.
123
124### Legacy CommonJS interop
125
126Two legacy modes can be used with the `imports` transform:
127
128* **enableLegacyTypeScriptModuleInterop**: Use the default TypeScript approach
129 to CommonJS interop instead of assuming that TypeScript's `--esModuleInterop`
130 flag is enabled. For example, if a CJS module exports a function, legacy
131 TypeScript interop requires you to write `import * as add from './add';`,
132 while Babel, Webpack, Node.js, and TypeScript with `--esModuleInterop` require
133 you to write `import add from './add';`. As mentioned in the
134 [docs](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#support-for-import-d-from-cjs-form-commonjs-modules-with---esmoduleinterop),
135 the TypeScript team recommends you always use `--esModuleInterop`.
136* **enableLegacyBabel5ModuleInterop**: Use the Babel 5 approach to CommonJS
137 interop, so that you can run `require('./MyModule')` instead of
138 `require('./MyModule').default`. Analogous to
139 [babel-plugin-add-module-exports](https://github.com/59naga/babel-plugin-add-module-exports).
140
141## Usage
142
143Installation:
144
145```bash
146yarn add --dev sucrase # Or npm install --save-dev sucrase
147```
148
149Often, you'll want to use one of the build tool integrations:
150[Webpack](https://github.com/alangpierce/sucrase/tree/main/integrations/webpack-loader),
151[Gulp](https://github.com/alangpierce/sucrase/tree/main/integrations/gulp-plugin),
152[Jest](https://github.com/alangpierce/sucrase/tree/main/integrations/jest-plugin),
153[Rollup](https://github.com/rollup/plugins/tree/master/packages/sucrase),
154[Broccoli](https://github.com/stefanpenner/broccoli-sucrase).
155
156Compile on-the-fly via a require hook with some [reasonable defaults](src/register.ts):
157
158```js
159// Register just one extension.
160require("sucrase/register/ts");
161// Or register all at once.
162require("sucrase/register");
163```
164
165Compile on-the-fly via a drop-in replacement for node:
166
167```bash
168sucrase-node index.ts
169```
170
171Run on a directory:
172
173```bash
174sucrase ./srcDir -d ./outDir --transforms typescript,imports
175```
176
177Call from JS directly:
178
179```js
180import {transform} from "sucrase";
181const compiledCode = transform(code, {transforms: ["typescript", "imports"]}).code;
182```
183
184## What Sucrase is not
185
186Sucrase is intended to be useful for the most common cases, but it does not aim
187to have nearly the scope and versatility of Babel. Some specific examples:
188
189* Sucrase does not check your code for errors. Sucrase's contract is that if you
190 give it valid code, it will produce valid JS code. If you give it invalid
191 code, it might produce invalid code, it might produce valid code, or it might
192 give an error. Always use Sucrase with a linter or typechecker, which is more
193 suited for error-checking.
194* Sucrase is not pluginizable. With the current architecture, transforms need to
195 be explicitly written to cooperate with each other, so each additional
196 transform takes significant extra work.
197* Sucrase is not good for prototyping language extensions and upcoming language
198 features. Its faster architecture makes new transforms more difficult to write
199 and more fragile.
200* Sucrase will never produce code for old browsers like IE. Compiling code down
201 to ES5 is much more complicated than any transformation that Sucrase needs to
202 do.
203* Sucrase is hesitant to implement upcoming JS features, although some of them
204 make sense to implement for pragmatic reasons. Its main focus is on language
205 extensions (JSX, TypeScript, Flow) that will never be supported by JS
206 runtimes.
207* Like Babel, Sucrase is not a typechecker, and must process each file in
208 isolation. For example, TypeScript `const enum`s are treated as regular
209 `enum`s rather than inlining across files.
210* You should think carefully before using Sucrase in production. Sucrase is
211 mostly beneficial in development, and in many cases, Babel or tsc will be more
212 suitable for production builds.
213
214See the [Project Vision](./docs/PROJECT_VISION.md) document for more details on
215the philosophy behind Sucrase.
216
217## Motivation
218
219As JavaScript implementations mature, it becomes more and more reasonable to
220disable Babel transforms, especially in development when you know that you're
221targeting a modern runtime. You might hope that you could simplify and speed up
222the build step by eventually disabling Babel entirely, but this isn't possible
223if you're using a non-standard language extension like JSX, TypeScript, or Flow.
224Unfortunately, disabling most transforms in Babel doesn't speed it up as much as
225you might expect. To understand, let's take a look at how Babel works:
226
2271. Tokenize the input source code into a token stream.
2282. Parse the token stream into an AST.
2293. Walk the AST to compute the scope information for each variable.
2304. Apply all transform plugins in a single traversal, resulting in a new AST.
2315. Print the resulting AST.
232
233Only step 4 gets faster when disabling plugins, so there's always a fixed cost
234to running Babel regardless of how many transforms are enabled.
235
236Sucrase bypasses most of these steps, and works like this:
237
2381. Tokenize the input source code into a token stream using a trimmed-down fork
239 of the Babel parser. This fork does not produce a full AST, but still
240 produces meaningful token metadata specifically designed for the later
241 transforms.
2422. Scan through the tokens, computing preliminary information like all
243 imported/exported names.
2443. Run the transform by doing a pass through the tokens and performing a number
245 of careful find-and-replace operations, like replacing `<Foo` with
246 `React.createElement(Foo`.
247
248Because Sucrase works on a lower level and uses a custom parser for its use
249case, it is much faster than Babel.
250
251## Contributing
252
253Contributions are welcome, whether they be bug reports, PRs, docs, tests, or
254anything else! Please take a look through the [Contributing Guide](./CONTRIBUTING.md)
255to learn how to get started.
256
257## License and attribution
258
259Sucrase is MIT-licensed. A large part of Sucrase is based on a fork of the
260[Babel parser](https://github.com/babel/babel/tree/main/packages/babel-parser),
261which is also MIT-licensed.
262
263## Why the name?
264
265Sucrase is an enzyme that processes sugar. Get it?