当前位置:网站首页>SWC介绍
SWC介绍
2022-07-06 03:17:00 【phial03】
SWC介绍
我们来看一下swc的官方介绍:
SWC (stands for Speedy Web Compiler) is a super-fast TypeScript / JavaScript compiler written in Rust.
swc的出现其实很大一部分原因是要替换掉工程中的babel,所以babel有的功能他几乎都有。
与babel最大的区别可能就是: swc is super-fast
swc官网中还有这样一句话,体现了他的速度:

除了swc的官方宣传外,Next.js基于swc实现了一个Rust编译器,用来解析编译、打包代码。下面是Next.js结合swc之后给出的一个数据:

所以从上面这些数据也可以简单看出来swc的优势:可以提高开发效率,提升开发体验。
Github
SWC 开源地址
这是一位名叫 kdy1 的韩国97年的小伙子开源的,很牛逼呀!
swc原理分析
(1)新建一个 debug-swc 项目
$ cargo new debug-swc
$ cd debug-swc
目录结构如下:
debug-swc
├── .gitignore
├── Cargo.toml
└── src
└── main.rs
(2)添加依赖Cargo.toml 文件,[dependencies] 配置项下面添加如下内容:
swc_ecma_parser = "0.31.2"
swc_common = "0.7.0"
swc_ecma_ast = "0.26.0"
swc_atoms = "0.2.2"

(3)新增 test.js 文件
根目录下的这个文件,用于 debug swc 的解析之用,内容如下,
const i = 1;
(4)编写 src/main.rs
官方例子 Crate: swc_ecma_parser 几乎不用做任何修改。
为了能展示 swc 解析 JavaScript 代码的结果,我增加了一个 get_identifier_name 函数,用于获取 test.js 代码中的变量名 i。
extern crate swc_common;
extern crate swc_ecma_parser;
use std::path::Path;
use swc_atoms::JsWord;
use swc_common::{
errors::{
ColorConfig, Handler},
sync::Lrc,
SourceMap,
};
use swc_ecma_ast::{
Decl, Module, ModuleItem, Pat, Stmt};
use swc_ecma_parser::{
lexer::Lexer, Parser, StringInput, Syntax};
fn main() {
swc_common::GLOBALS.set(&swc_common::Globals::new(), || {
let cm: Lrc<SourceMap> = Default::default();
let handler = Handler::with_tty_emitter(
ColorConfig::Auto, true, false, Some(cm.clone()));
let fm = cm
.load_file(Path::new("./test.js"))
.expect("failed to load test.js");
let lexer = Lexer::new(
// We want to parse ecmascript
Syntax::Es(Default::default()),
// JscTarget defaults to es5
Default::default(),
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
for e in parser.take_errors() {
e.into_diagnostic(&handler).emit();
}
let module = parser
.parse_module()
.map_err(|e| {
// Unrecoverable fatal error occurred
e.into_diagnostic(&handler).emit()
})
.expect("failed to parser module");
if let Ok(identifier_name) = get_identifier_name(&module) {
println!("identifier name: {}", identifier_name);
}
});
}
fn get_identifier_name(module: &Module) -> Result<&JsWord, ()> {
for module_item in &module.body {
if let ModuleItem::Stmt(Stmt::Decl(Decl::Var(var))) = module_item {
for decl in &var.decls {
if let Pat::Ident(identifier) = &decl.name {
return Ok(&identifier.id.sym);
}
}
}
}
Err(())
}
src/main.rs 的完整源码可以看这里:github: debug-swc/src/main.rs
(4)来看一下执行结果
$ cargo run
执行 cargo run 会先进行 cargo build,最终结果如下,
我们经过解析 AST 从 test.js 源码中拿到了变量名 i。
边栏推荐
- Research on cooperative control of industrial robots
- 出现Permission denied的解决办法(750权限谨慎使用)
- Leetcode problem solving -- 108 Convert an ordered array into a binary search tree
- Mysqldump data backup
- Sign SSL certificate as Ca
- OCR文字識別方法綜述
- My C language learning records (blue bridge) -- files and file input and output
- Tidb ecological tools (backup, migration, import / export) collation
- MySQL advanced notes
- 【概念】Web 基础概念认知
猜你喜欢

1.16 - 校验码

2022工作中遇到的问题四

C # create self host webservice

OCR文字識別方法綜述

【概念】Web 基础概念认知
![[concept] Web basic concept cognition](/img/27/14bcd73ca70d136436a4382a1b4bd1.jpg)
[concept] Web basic concept cognition
![Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]](/img/3b/385d19e51340ecd6281df47b39f40c.png)
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]

IPv6 comprehensive experiment

Who is the winner of PTA

The real machine cannot access the shooting range of the virtual machine, and the real machine cannot Ping the virtual machine
随机推荐
SAP ALV单元格级别设置颜色
How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)
February 14, 2022 Daily: Google long article summarizes the experience of building four generations of TPU
【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
建模规范:命名规范
手写数据库客户端
Leetcode problem solving -- 98 Validate binary search tree
Selenium share
StrError & PERROR use yyds dry inventory
Getting started with applet cloud development - getting user search content
Some problem records of AGP gradle
Performance analysis of user login TPS low and CPU full
Problems encountered in 2022 work IV
Taobao focus map layout practice
Leetcode problem solving -- 108 Convert an ordered array into a binary search tree
The difference between sizeof and strlen in C language
Era5 reanalysis data download strategy
What is the investment value of iFLYTEK, which does not make money?
Zhang Lijun: penetrating uncertainty depends on four "invariants"
【paddle】加载模型权重后预测报错AttributeError: ‘Model‘ object has no attribute ‘_place‘