Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SWC plugin (improve Vite / Nest.js support) #185

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 0 additions & 82 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,94 +6,21 @@ node_modules

# Lock files
package-lock.json
# This package-lock is used as cache key and needs to be comitted
!documentation/package-lock.json

############
# iOS
############

# Build generated
dist
ReactNativeExample/ios/build/
ReactNativeExample/ios/DerivedData/

# Various settings
ReactNativeExample/ios/xcuserdata/

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
ReactNativeExample/ios/Pods/

############
# Android
############
# Built application files
*.apk
*.ap_

# Files for the Dalvik VM
*.dex

# Java class files
*.class

# Generated files
ReactNativeExample/android/bin/
ReactNativeExample/android/gen/
ReactNativeExample/android/out/

# Gradle files
ReactNativeExample/android/.gradle/
ReactNativeExample/android/build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
ReactNativeExample/android/proguard/

# Log Files
*.log

# Android Studio Navigation editor temp files
ReactNativeExample/android/.navigation/

# Android Studio captures folder
ReactNativeExample/android/captures/

# Intellij
*.iml

##################
# React-Native
##################
# OSX
#
.DS_Store

# Xcode
#
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate

# Android/IJ
#
build/
Expand All @@ -109,15 +36,6 @@ node_modules/
npm-debug.log
yarn-error.log

# BUCK
buck-out/
\.buckd/
*.keystore
!debug.keystore

main.jsbundle.map
index.android.bundle.map

.jest_cache
.eslintcache
**/.docusaurus
Expand Down
5 changes: 5 additions & 0 deletions packages/swc-plugin-obsidian/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# These command aliases are not final, may change
[alias]
# Alias to build actual plugin binary for the specified target.
build-wasi = "build --target wasm32-wasi"
build-wasm32 = "build --target wasm32-unknown-unknown"
18 changes: 18 additions & 0 deletions packages/swc-plugin-obsidian/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "swc-plugin-obsidian"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[profile.release]
lto = true

[dependencies]
serde = "1"
swc_core = { version = "0.101.*", features = ["ecma_plugin_transform"] }

# .cargo/config defines few alias to build plugin.
# cargo build-wasi generates wasm-wasi32 binary
# cargo build-wasm32 generates wasm32-unknown-unknown binary.
14 changes: 14 additions & 0 deletions packages/swc-plugin-obsidian/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "swc-plugin-obsidian",
"version": "0.1.0",
"description": "",
"author": "",
"license": "ISC",
"keywords": ["swc-plugin"],
"main": "target/wasm32-wasi/release/swc_plugin_obsidian.wasm",
"scripts": {
"prepublishOnly": "cargo build-wasi --release"
},
"files": [],
"preferUnplugged": true
}
48 changes: 48 additions & 0 deletions packages/swc-plugin-obsidian/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use swc_core::ecma::{
ast::Program,
transforms::testing::test_inline,
visit::{as_folder, FoldWith, VisitMut},
};
use swc_core::plugin::{plugin_transform, proxies::TransformPluginProgramMetadata};

pub struct TransformVisitor;

impl VisitMut for TransformVisitor {
// Implement necessary visit_mut_* methods for actual custom transform.
// A comprehensive list of possible visitor methods can be found here:
// https://rustdoc.swc.rs/swc_ecma_visit/trait.VisitMut.html
}

/// An example plugin function with macro support.
/// `plugin_transform` macro interop pointers into deserialized structs, as well
/// as returning ptr back to host.
///
/// It is possible to opt out from macro by writing transform fn manually
/// if plugin need to handle low-level ptr directly via
/// `__transform_plugin_process_impl(
/// ast_ptr: *const u8, ast_ptr_len: i32,
/// unresolved_mark: u32, should_enable_comments_proxy: i32) ->
/// i32 /* 0 for success, fail otherwise.
/// Note this is only for internal pointer interop result,
/// not actual transform result */`
///
/// This requires manual handling of serialization / deserialization from ptrs.
/// Refer swc_plugin_macro to see how does it work internally.
#[plugin_transform]
pub fn process_transform(program: Program, _metadata: TransformPluginProgramMetadata) -> Program {
program.fold_with(&mut as_folder(TransformVisitor))
}

// An example to test plugin transform.
// Recommended strategy to test plugin's transform is verify
// the Visitor's behavior, instead of trying to run `process_transform` with mocks
// unless explicitly required to do so.
test_inline!(
Default::default(),
|_| as_folder(TransformVisitor),
boo,
// Input codes
r#"console.log("transform");"#,
// Output codes after transformed with plugin
r#"console.log("transform");"#
);
Loading