当前位置:网站首页>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
边栏推荐
- Python implementation of maddpg - (1) openai maddpg environment configuration
- 如何做好功能测试
- 【SLAM】ORB-SLAM3解析——跟踪Track()(3)
- How to do function test well
- NR modulation 1
- Web security SQL injection vulnerability (1)
- 1003 emergency (25 points), "DIJ deformation"
- 蓝色样式商城网站页脚代码
- The real machine cannot access the shooting range of the virtual machine, and the real machine cannot Ping the virtual machine
- Leetcode problem solving -- 108 Convert an ordered array into a binary search tree
猜你喜欢
ERA5再分析资料下载攻略
Tidb ecological tools (backup, migration, import / export) collation
ASU & OSU | model based regularized off-line meta reinforcement learning
Jenkins basic knowledge ----- detailed explanation of 03pipeline code
Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning
Explore pointers and pointer types in depth
Web security SQL injection vulnerability (1)
Analyze menu analysis
Reverse repackaging of wechat applet
mysqldump数据备份
随机推荐
. Net 6 and Net core learning notes: Important issues of net core
深度解析指针与数组笔试题
[pointer training - eight questions]
Lua uses require to load the shared library successfully, but the return is Boolean (always true)
Descriptor implements ORM model
three.js网页背景动画液态js特效
Detailed use of dbutils # yyds dry goods inventory #
银行核心业务系统性能测试方法
NR modulation 1
蓝色样式商城网站页脚代码
Problems encountered in 2022 work IV
Résumé des méthodes de reconnaissance des caractères ocr
I sorted out a classic interview question for my job hopping friends
SWC介绍
Getting started with applet cloud development - getting user search content
SAP ALV颜色代码对应颜色(整理)
mysqldump数据备份
Analyze 菜单分析
适合程序员学习的国外网站推荐
【RISC-V】外部中断