当前位置:网站首页>SWC introduction
SWC introduction
2022-07-06 03:19:00 【phial03】
SWC Introduce
Let's take a look swc
Of official Introduce :
SWC (stands for Speedy Web Compiler) is a super-fast TypeScript / JavaScript compiler written in Rust.
swc
In fact, a large part of the reason for the emergence of the project is to replace babel
, therefore babel
yes , we have function He has almost all of them .
And babel The biggest difference may be : swc is super-fast
swc There is another sentence on the official website , It reflects his speed :
except swc
In addition to the official publicity ,Next.js
be based on swc
Implemented a Rust
compiler , Used to parse and compile 、 Packaging code . Here is Next.js
combination swc
A data given later :
So it can be seen simply from the above data swc
The advantages of : Can improve development efficiency , Enhance the development experience .
Github
SWC Open source address
This is a man named kdy1 South Korea 97 The young man of... Is open source , It's awesome !
swc Principle analysis
(1) Create a new one debug-swc project
$ cargo new debug-swc
$ cd debug-swc
The directory structure is as follows :
debug-swc
├── .gitignore
├── Cargo.toml
└── src
└── main.rs
(2) Add dependency Cargo.toml
file ,[dependencies]
Add the following content under the configuration item :
swc_ecma_parser = "0.31.2"
swc_common = "0.7.0"
swc_ecma_ast = "0.26.0"
swc_atoms = "0.2.2"
(3) newly added test.js
file
This file in the root directory , be used for debug swc For parsing , The contents are as follows ,
const i = 1;
(4) To write src/main.rs
The official example Crate: swc_ecma_parser Hardly need to make any changes .
In order to show swc analysis JavaScript Code results , I added a get_identifier_name
function , Used to get test.js
Variable names in the code 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
The complete source code of can be seen here :github: debug-swc/src/main.rs
(4) Take a look at the execution results
$ cargo run
perform cargo run
We'll do it first cargo build
, The final results are as follows ,
We analyzed AST from test.js Get the variable name in the source code i
.
Reference article :
swc–babel Replacement
Easy to use SWC
Next generation build tools
SWC Experience
Compiler tools SWC
边栏推荐
- [ruoyi] enable Mini navigation bar
- Tomb. Weekly update of Finance (February 7 - February 13)
- Arabellacpc 2019 (supplementary question)
- Selenium share
- I sorted out a classic interview question for my job hopping friends
- Crazy, thousands of netizens are exploding the company's salary
- Polymorphic day02
- Self made CA certificate and SSL certificate using OpenSSL
- jsscript
- 蓝色样式商城网站页脚代码
猜你喜欢
Linear programming matlab
指针笔试题~走近大厂
SAP ALV单元格级别设置颜色
下一个行业风口:NFT 数字藏品,是机遇还是泡沫?
[risc-v] external interrupt
Jenkins basic knowledge ----- detailed explanation of 03pipeline code
Performance test method of bank core business system
js 正则过滤和增加富文本中图片前缀
Exness foreign exchange: the governor of the Bank of Canada said that the interest rate hike would be more moderate, and the United States and Canada fell slightly to maintain range volatility
Who is the winner of PTA
随机推荐
Jenkins basic knowledge ----- detailed explanation of 03pipeline code
OCR文字识别方法综述
【概念】Web 基础概念认知
An article about liquid template engine
记录一下逆向任务管理器的过程
Deno介绍
Item 10: Prefer scoped enums to unscoped enums.
XSS challenges绕过防护策略进行 XSS 注入
NR modulation 1
Recommended foreign websites for programmers to learn
EDCircles: A real-time circle detector with a false detection control 翻译
Era5 reanalysis data download strategy
Redo file corruption repair
Data and Introspection__ dict__ Attributes and__ slots__ attribute
jsscript
Daily question brushing plan-2-13 fingertip life
How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)
多态day02
MySQL Server层四个日志
【RISC-V】外部中断