
高性能JavaScript转换工具 专注非标准语言扩展
Sucrase作为一款高效的JavaScript转换工具,主要处理JSX、TypeScript和Flow等非标准语言扩展。通过优化架构,它的性能比Babel提升了20倍,非常适合在现代浏览器和Node.js环境中进行快速开发。该工具支持多种转换功能,包括JSX、TypeScript、Flow和ES模块转换,并能与Webpack、Gulp、Jest等常用开发工具集成。Sucrase为开发者提供了快速的代码转换方案,但主要适用于开发环境,不建议在生产环境或需要兼容旧版浏览器的项目中使用。
yarn add --dev sucrase # Or npm install --save-dev sucrase node -r sucrase/register main.ts
Using the ts-node integration:
yarn add --dev sucrase ts-node typescript ./node_modules/.bin/ts-node --transpiler sucrase/ts-node-plugin main.ts
Sucrase 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!
Sucrase 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.
Sucrase 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 0.57 seconds 636975 lines per second swc 1.19 seconds 304526 lines per second esbuild 1.45 seconds 248692 lines per second TypeScript 8.98 seconds 40240 lines per second Babel 9.18 seconds 39366 lines per second
Details: Measured on July 2022. 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.createClass,
but may be preserved or transformed to _jsx() by setting the jsxRuntime option.
Also adds createReactClass display names and JSX context information.isolatedModules
TypeScript flag so that the typechecker will disallow the few features like
const enums that need cross-file compilation. The Sucrase option keepUnusedImports
can be used to disable all automatic removal of imports and exports, analogous to TS
verbatimModuleSyntax.import/export) to CommonJS
(require/module.exports) using the same approach as Babel and TypeScript
with --esModuleInterop. If preserveDynamicImport is specified in the Sucrase
options, then dynamic import expressions are left alone, which is particularly
useful in Node to load ESM-only libraries. If preserveDynamicImport is not
specified, import expressions are transformed into a promise-wrapped call to
require.react-hot-loader/babel
transform in the react-hot-loader
project. This enables advanced hot reloading use cases such as editing of
bound methods.jest.mock, but the same rules still apply.When the imports transform is not specified (i.e. when targeting ESM), the
injectCreateRequireForImportRequire option can be specified to transform TS
import foo = require("foo"); in a way that matches the
TypeScript 4.7 behavior
with module: nodenext.
These newer JS features are transformed by default:
a?.ba ?? bclass C { x = 1; }.
This includes static fields but not the #x private field syntax.const n = 1_234;try { doThing(); } catch { }.If your target runtime supports these features, you can specify
disableESTransforms: true so that Sucrase preserves the syntax rather than
trying to transform it. Note that transpiled and standard class fields behave
slightly differently; see the
TypeScript 3.7 release notes
for details. If you use TypeScript, you can enable the TypeScript option
useDefineForClassFields to enable error checking related to these differences.
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,
and do expressions are all unsupported in browsers and Node (as of this
writing), and Sucrase doesn't make an attempt to transpile them.By default, JSX is compiled to React functions in development mode. This can be configured with a few options:
"classic" (default): The original JSX transform that calls React.createElement by default.
To configure for non-React use cases, specify:
React.createElement.React.Fragment."automatic": The new JSX transform
introduced with React 17, which calls jsx functions and auto-adds import statements.
To configure for non-React use cases, specify:
react."preserve": Don't transform JSX, and instead emit it as-is in the output code.true, use production version of functions and don't include debugging
information. When using React in production mode with the automatic transform, this must be
set to true to avoid an error about jsxDEV being missing.Two legacy modes can be used with the imports transform:
--esModuleInterop
flag is enabled. For example, if a CJS module exports a function, legacy
TypeScript interop requires you to write import * as add from './add';,
while Babel, Webpack, Node.js, and TypeScript with --esModuleInterop require
you to write import add from './add';. As mentioned in the
docs,
the TypeScript team recommends you always use --esModuleInterop.require('./MyModule') instead of
require('./MyModule').default. Analogous to
babel-plugin-add-module-exports.The most robust way is to use the Sucrase plugin for ts-node,
which has various Node integrations and configures Sucrase via tsconfig.json:
ts-node --transpiler sucrase/ts-node-plugin
For projects that don't target ESM, Sucrase also has a require hook with some reasonable defaults that can be accessed in a few ways:
require("sucrase/register");node -r sucrase/register main.tssucrase-node main.tsOptions can be passed to the require hook via a SUCRASE_OPTIONS environment
variable holding a JSON string of options.
For simple use cases, Sucrase comes with a sucrase CLI that mirrors your
directory structure to an output directory:
sucrase ./srcDir -d ./outDir --transforms typescript,imports
For any advanced use cases, Sucrase can be called 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 regular
enums 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 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.
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


AI一键生成PPT,就用博思AIPPT!
博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种 办公场景。


AI赋能电商视觉革命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无水印视频
最新版Sora2模型免费使用,一键生成无水印视频


实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言 障碍,让全世界的语言交流不再有国界。


选题、配图、成文,一站式创作,让内容运营更高效
讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。


最强AI数据分析助手
小浣熊家族Raccoon,您的AI智能助手,致力于通过先进的人工智能技术,为用户提供高效、便捷的智能服务。无论是日常咨询还是专业问题解答,小浣熊都能以快速、准确的响应满足您的需求,让您的生活更加智能便捷。


像人一样思考的AI智能体
imini 是一款超级AI智能体,能根据人类指令,自主思考、自主完成、并且交付结果的AI智能体。
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号