SlideShare ist ein Scribd-Unternehmen logo
1 von 19
A glance at the SWC
Speedy web compiler - written in Rust
About
- An extensible Rust-based platform for the next generation of fast developer
tools
- Main features: transform (to ecma targets, add new syntaxes,... ),
bundling(swcpack - still in development), ast parser,
- Used by tools like Next.js, Parcel, and Deno, the list goes on
- Used by companies like Vercel, ByteDance, Tencent, Shopify, etc.
*** SWC is 20x faster than Babel on a single thread and 70x faster on four cores.
(according to swc.rc)
Why SWC (pros)
Extensibility: allow extend features with plugin, in js(have bottleneck issue) or in rust
Performance: see page 2. (picture below: benchmarks for transform to es5)
WebAssembly: support for WASM => super fast even in browser environment
Community: ecosystem are bing and still expanding.
Why not SWC (cons)
Rust is new to learn:
Challenging learning curve —>
Bottleneck with other tools:
It can make the build process faster, but it may
not, if we have still Webpack, legacy babel
loaders, or swc js plugin in the pipe, to do
some work.
Source: humb1t
Why not SWC (cons)
Rust is new to learn:
Challenging learning curve —>
Bottleneck with other tools:
It can make the build process faster, but it may
not, if we have still Webpack (or with some
legacy babel loaders) in the pipe, to do some
work.
Source: humb1t
How SWC runs in Node/ Browser
On Nodejs:
using Foreign Function Interface (via Node C++ AddOn, https://nodejs.org/api/addons.html)
On Browser:
Rust core => Wasm => run directly in browser(see @swc/wasm )
plugin: javascript plugin
- Like babel plugin, but interfaces are not
- Have bottleneck issue: because the core which is written in rust has a high
performance but js plugin, which traveling on AST, is not
- Showcase: @formatjs/swc-plugin
Native Rust plugins
Extend from babel: see https://swc.rs/docs/migrating-from-babel
Provide from swc_project: see github.com/swc-project/plugins
Custom plugins:
=> To have a glance: let’s make one simple plugin
Example and practice
Setup env:
# install rust via rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# install rust cli
cargo install swc_cli
# VSCODE setup rust language server
code --install-extension rust-lang.rust-analyzer
Example and practice
clone source plugin source code
# for quicker setup, clone this repository:
github.com/cute-me-on-repos/test-ts-miniapp.git
git checkout -b main/<.>
Example and practice
Init source plugin source code (target wasm32-wasi):
# init plugin with the name “tini-swc-plugin”:
swc plugin new --target-type wasm32-wasi tini-swc-plugin;
rustup target add wasm32-wasi # add target
cd tini-swc-plugin
Example and practice
Explaining simple example from document:
https://swc.rs/docs/plugin/ecmascript/getting-started
Then write our own one —> next page
Example and practice
# Practice problem:
Problem:
We want to limit some of the web Apis that users want to access in tini app;
Write a swc rust plugin to travel the AST of 1 js file and replace all statements
(include [`this`, `self`, `global`]) that refer to the global scope, except `this`
of Component/Page/App, with `(void 0)`(undefined).
Requirement: quick docs researching skill =))
Example and practice
# input:
```js
this;global; // thoses should be remove
global.unAllowWebApiCall();
(function unknow(){
this.unAllowWebApiCall();
})();
Page({
onLoad(){this.setData({value:”hi”})}
})
```
Example and practice
# output:
```js
(void 0);(void 0);
(void 0).unAllowWebApiCall();
(function unknow(){
(void 0).unAllowWebApiCall();
})();
Page({
onLoad(){this.setData({value:”hi”})}
})
```
Speed up Jest
In jest-transform pipe.
Addition: Rust to Wasm
- Light weight: rust have no GC -> not need to re-create GC in wasm like others
- Fast, safe, simple -> compare to cpp
? should
ArrayBuffer to transferring data between web contexts
(Q&&A)||SIGTERM
Gracias ♡ ~

Weitere ähnliche Inhalte

Ähnlich wie A glance at the Rust SWC

How to start using Scala
How to start using ScalaHow to start using Scala
How to start using Scala
Ngoc Dao
 
Andrew Mykhaliuk - Sorry, I need to make a build for frontend
Andrew Mykhaliuk - Sorry, I need to make a build for frontendAndrew Mykhaliuk - Sorry, I need to make a build for frontend
Andrew Mykhaliuk - Sorry, I need to make a build for frontend
OdessaJS Conf
 
Building Server Applications Using ObjectiveC And GNUstep
Building Server Applications Using ObjectiveC And GNUstepBuilding Server Applications Using ObjectiveC And GNUstep
Building Server Applications Using ObjectiveC And GNUstep
guest9efd1a1
 
Building Server Applications Using Objective C And Gn Ustep
Building Server Applications Using Objective C And Gn UstepBuilding Server Applications Using Objective C And Gn Ustep
Building Server Applications Using Objective C And Gn Ustep
wangii
 

Ähnlich wie A glance at the Rust SWC (20)

Lightning fast sass
Lightning fast sassLightning fast sass
Lightning fast sass
 
Node.js an Exectutive View
Node.js an Exectutive ViewNode.js an Exectutive View
Node.js an Exectutive View
 
How to start using Scala
How to start using ScalaHow to start using Scala
How to start using Scala
 
Andrew Mykhaliuk - Sorry, I need to make a build for frontend
Andrew Mykhaliuk - Sorry, I need to make a build for frontendAndrew Mykhaliuk - Sorry, I need to make a build for frontend
Andrew Mykhaliuk - Sorry, I need to make a build for frontend
 
Deploying Symfony2 app with Ansible
Deploying Symfony2 app with AnsibleDeploying Symfony2 app with Ansible
Deploying Symfony2 app with Ansible
 
Introduction of webpack 4
Introduction of webpack 4Introduction of webpack 4
Introduction of webpack 4
 
Rust Embedded Development on ESP32 and basics of Async with Embassy
Rust Embedded Development on ESP32 and basics of Async with EmbassyRust Embedded Development on ESP32 and basics of Async with Embassy
Rust Embedded Development on ESP32 and basics of Async with Embassy
 
De-centralised Version Control with Bazaar
De-centralised Version Control with BazaarDe-centralised Version Control with Bazaar
De-centralised Version Control with Bazaar
 
CollabSphere 2021 - DEV114 - The Nuts and Bolts of CI/CD With a Large XPages ...
CollabSphere 2021 - DEV114 - The Nuts and Bolts of CI/CD With a Large XPages ...CollabSphere 2021 - DEV114 - The Nuts and Bolts of CI/CD With a Large XPages ...
CollabSphere 2021 - DEV114 - The Nuts and Bolts of CI/CD With a Large XPages ...
 
Building Server Applications Using ObjectiveC And GNUstep
Building Server Applications Using ObjectiveC And GNUstepBuilding Server Applications Using ObjectiveC And GNUstep
Building Server Applications Using ObjectiveC And GNUstep
 
Building Server Applications Using Objective C And Gn Ustep
Building Server Applications Using Objective C And Gn UstepBuilding Server Applications Using Objective C And Gn Ustep
Building Server Applications Using Objective C And Gn Ustep
 
Rock Solid WordPress
Rock Solid WordPressRock Solid WordPress
Rock Solid WordPress
 
WebSocket on client & server using websocket-sharp & ASP.NET Core
WebSocket on client & server using websocket-sharp & ASP.NET CoreWebSocket on client & server using websocket-sharp & ASP.NET Core
WebSocket on client & server using websocket-sharp & ASP.NET Core
 
IBM ConnectED 2015 - BP106 From XPages Hero To OSGi Guru: Taking The Scary Ou...
IBM ConnectED 2015 - BP106 From XPages Hero To OSGi Guru: Taking The Scary Ou...IBM ConnectED 2015 - BP106 From XPages Hero To OSGi Guru: Taking The Scary Ou...
IBM ConnectED 2015 - BP106 From XPages Hero To OSGi Guru: Taking The Scary Ou...
 
Care and feeding notes
Care and feeding notesCare and feeding notes
Care and feeding notes
 
stackconf 2020 | Enterprise CI/CD Integration Testing Environments Done Right...
stackconf 2020 | Enterprise CI/CD Integration Testing Environments Done Right...stackconf 2020 | Enterprise CI/CD Integration Testing Environments Done Right...
stackconf 2020 | Enterprise CI/CD Integration Testing Environments Done Right...
 
A Introduction to the World of Node, Javascript & Selenium
A Introduction to the World of Node, Javascript & SeleniumA Introduction to the World of Node, Javascript & Selenium
A Introduction to the World of Node, Javascript & Selenium
 
Postgres the hardway
Postgres the hardwayPostgres the hardway
Postgres the hardway
 
Ruby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developerRuby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developer
 
Building For Mer
Building For MerBuilding For Mer
Building For Mer
 

Kürzlich hochgeladen

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Kürzlich hochgeladen (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 

A glance at the Rust SWC

  • 1. A glance at the SWC Speedy web compiler - written in Rust
  • 2. About - An extensible Rust-based platform for the next generation of fast developer tools - Main features: transform (to ecma targets, add new syntaxes,... ), bundling(swcpack - still in development), ast parser, - Used by tools like Next.js, Parcel, and Deno, the list goes on - Used by companies like Vercel, ByteDance, Tencent, Shopify, etc. *** SWC is 20x faster than Babel on a single thread and 70x faster on four cores. (according to swc.rc)
  • 3. Why SWC (pros) Extensibility: allow extend features with plugin, in js(have bottleneck issue) or in rust Performance: see page 2. (picture below: benchmarks for transform to es5) WebAssembly: support for WASM => super fast even in browser environment Community: ecosystem are bing and still expanding.
  • 4. Why not SWC (cons) Rust is new to learn: Challenging learning curve —> Bottleneck with other tools: It can make the build process faster, but it may not, if we have still Webpack, legacy babel loaders, or swc js plugin in the pipe, to do some work. Source: humb1t
  • 5. Why not SWC (cons) Rust is new to learn: Challenging learning curve —> Bottleneck with other tools: It can make the build process faster, but it may not, if we have still Webpack (or with some legacy babel loaders) in the pipe, to do some work. Source: humb1t
  • 6. How SWC runs in Node/ Browser On Nodejs: using Foreign Function Interface (via Node C++ AddOn, https://nodejs.org/api/addons.html) On Browser: Rust core => Wasm => run directly in browser(see @swc/wasm )
  • 7. plugin: javascript plugin - Like babel plugin, but interfaces are not - Have bottleneck issue: because the core which is written in rust has a high performance but js plugin, which traveling on AST, is not - Showcase: @formatjs/swc-plugin
  • 8. Native Rust plugins Extend from babel: see https://swc.rs/docs/migrating-from-babel Provide from swc_project: see github.com/swc-project/plugins Custom plugins: => To have a glance: let’s make one simple plugin
  • 9. Example and practice Setup env: # install rust via rustup curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh # install rust cli cargo install swc_cli # VSCODE setup rust language server code --install-extension rust-lang.rust-analyzer
  • 10. Example and practice clone source plugin source code # for quicker setup, clone this repository: github.com/cute-me-on-repos/test-ts-miniapp.git git checkout -b main/<.>
  • 11. Example and practice Init source plugin source code (target wasm32-wasi): # init plugin with the name “tini-swc-plugin”: swc plugin new --target-type wasm32-wasi tini-swc-plugin; rustup target add wasm32-wasi # add target cd tini-swc-plugin
  • 12. Example and practice Explaining simple example from document: https://swc.rs/docs/plugin/ecmascript/getting-started Then write our own one —> next page
  • 13. Example and practice # Practice problem: Problem: We want to limit some of the web Apis that users want to access in tini app; Write a swc rust plugin to travel the AST of 1 js file and replace all statements (include [`this`, `self`, `global`]) that refer to the global scope, except `this` of Component/Page/App, with `(void 0)`(undefined). Requirement: quick docs researching skill =))
  • 14. Example and practice # input: ```js this;global; // thoses should be remove global.unAllowWebApiCall(); (function unknow(){ this.unAllowWebApiCall(); })(); Page({ onLoad(){this.setData({value:”hi”})} }) ```
  • 15. Example and practice # output: ```js (void 0);(void 0); (void 0).unAllowWebApiCall(); (function unknow(){ (void 0).unAllowWebApiCall(); })(); Page({ onLoad(){this.setData({value:”hi”})} }) ```
  • 16. Speed up Jest In jest-transform pipe.
  • 17. Addition: Rust to Wasm - Light weight: rust have no GC -> not need to re-create GC in wasm like others - Fast, safe, simple -> compare to cpp
  • 18. ? should ArrayBuffer to transferring data between web contexts