高性能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 enum
s 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?.b
a ?? b
class 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.ts
sucrase-node main.ts
Options 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 enum
s are treated as regular
enum
s 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小说写作助手,一站式润色、改写、扩写
蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。
字节跳动发布的AI编程神器IDE
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。
全能AI智能助手,随时解答生活与工作的多样问题
问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。
实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。
一键生成PPT和Word,让学习生活更轻松
讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。
深度推理能力全新升级,全面对标OpenAI o1
科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件 和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。
一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型
Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。
AI助力,做PPT更简单!
咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。
选题、配图、成文,一站式创作,让内容运营更高效
讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。
专业的AI公文写作平台,公文写作神器
AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完 成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。