Skip to content

Commit

Permalink
feat(css/plugin): Support Wasm plugin (#6568)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Dec 2, 2022
1 parent 2d36c3f commit fa8f7b0
Show file tree
Hide file tree
Showing 14 changed files with 2,352 additions and 146 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

216 changes: 116 additions & 100 deletions crates/swc_core/Cargo.toml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions crates/swc_core/src/lib.rs
Expand Up @@ -12,8 +12,8 @@ pub mod quote;
pub extern crate swc_ecma_quote_macros;

// Plugins
#[cfg(feature = "__plugin_transform")]
#[cfg_attr(docsrs, doc(cfg(feature = "__plugin_transform")))]
#[cfg(feature = "__common_plugin_transform")]
#[cfg_attr(docsrs, doc(cfg(feature = "__common_plugin_transform")))]
pub mod plugin;

#[cfg(feature = "__ecma")]
Expand Down
20 changes: 10 additions & 10 deletions crates/swc_core/src/plugin.rs
@@ -1,20 +1,20 @@
// #[plugin_transform] macro
#[cfg(any(docsrs, feature = "__plugin_transform"))]
#[cfg_attr(docsrs, doc(cfg(feature = "__plugin_transform")))]
pub use swc_plugin_macro::plugin_transform;
#[cfg(any(docsrs, feature = "__common_plugin_transform"))]
#[cfg_attr(docsrs, doc(cfg(feature = "__common_plugin_transform")))]
pub use swc_plugin_macro::{css_plugin_transform, plugin_transform};

/// exported __alloc / __free fn for the guest (plugin)
/// allows to allocate memory from the host side.
/// This should not be directly referenced.
#[cfg(all(feature = "__plugin_transform", target_arch = "wasm32"))]
#[cfg(all(feature = "__common_plugin_transform", target_arch = "wasm32"))]
pub mod memory {
pub use swc_plugin::allocation::*;
}

/// Global HANDLER implementation for the plugin
/// for error reporting.
#[cfg(any(docsrs, feature = "__plugin_transform"))]
#[cfg_attr(docsrs, doc(cfg(feature = "__plugin_transform")))]
#[cfg(any(docsrs, feature = "__common_plugin_transform"))]
#[cfg_attr(docsrs, doc(cfg(feature = "__common_plugin_transform")))]
pub mod errors {
/// global context HANDLER in plugin's transform function.
pub static HANDLER: swc_plugin::pseudo_scoped_key::PseudoScopedKey<
Expand All @@ -25,16 +25,16 @@ pub mod errors {
}

/// Plugin's environment metadata context.
#[cfg(any(docsrs, feature = "__plugin_transform"))]
#[cfg_attr(docsrs, doc(cfg(feature = "__plugin_transform")))]
#[cfg(any(docsrs, feature = "__common_plugin_transform"))]
#[cfg_attr(docsrs, doc(cfg(feature = "__common_plugin_transform")))]
pub mod metadata {
pub use swc_common::plugin::metadata::TransformPluginMetadataContextKind;
pub use swc_plugin_proxy::TransformPluginProgramMetadata;
}

/// Proxy to the host's data not attached to the AST, like sourcemap / comments.
#[cfg(any(docsrs, feature = "__plugin_transform"))]
#[cfg_attr(docsrs, doc(cfg(feature = "__plugin_transform")))]
#[cfg(any(docsrs, feature = "__common_plugin_transform"))]
#[cfg_attr(docsrs, doc(cfg(feature = "__common_plugin_transform")))]
pub mod proxies {
pub use swc_plugin_proxy::*;
}
21 changes: 18 additions & 3 deletions crates/swc_plugin_macro/src/lib.rs
Expand Up @@ -12,12 +12,25 @@ pub fn plugin_transform(
let token = proc_macro2::TokenStream::from(input);
let parsed_results = syn::parse2::<SynItem>(token).expect("Failed to parse tokens");
match parsed_results {
SynItem::Fn(func) => handle_func(func),
SynItem::Fn(func) => handle_func(func, Ident::new("Program", Span::call_site())),
_ => panic!("Please confirm if plugin macro is specified for the function"),
}
}

fn handle_func(func: ItemFn) -> TokenStream {
#[proc_macro_attribute]
pub fn css_plugin_transform(
_args: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let token = proc_macro2::TokenStream::from(input);
let parsed_results = syn::parse2::<SynItem>(token).expect("Failed to parse tokens");
match parsed_results {
SynItem::Fn(func) => handle_func(func, Ident::new("Stylesheet", Span::call_site())),
_ => panic!("Please confirm if plugin macro is specified for the function"),
}
}

fn handle_func(func: ItemFn, ast_type: Ident) -> TokenStream {
let ident = func.sig.ident.clone();
let transform_process_impl_ident =
Ident::new("__transform_plugin_process_impl", Span::call_site());
Expand Down Expand Up @@ -80,6 +93,7 @@ fn handle_func(func: ItemFn) -> TokenStream {
}

#[no_mangle]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn #transform_core_pkg_diag_ident() -> u32 {
let schema_version = swc_core::common::plugin::PLUGIN_TRANSFORM_AST_SCHEMA_VERSION;
let core_pkg_diag = swc_core::diagnostics::get_core_engine_diagnostics();
Expand Down Expand Up @@ -109,6 +123,7 @@ fn handle_func(func: ItemFn) -> TokenStream {
// There are some cases error won't be wrapped up however - for example, we expect
// serialization of PluginError itself should succeed.
#[no_mangle]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn #transform_process_impl_ident(
ast_ptr: *const u8, ast_ptr_len: u32,
unresolved_mark: u32, should_enable_comments_proxy: i32) -> u32 {
Expand All @@ -119,7 +134,7 @@ fn handle_func(func: ItemFn) -> TokenStream {
let err = swc_core::common::plugin::serialized::PluginError::Deserialize("Failed to deserialize program received from host".to_string());
return construct_error_ptr(err);
}
let program: Program = program.expect("Should be a program");
let program: #ast_type = program.expect("Should be a program");

// Create a handler wired with plugin's diagnostic emitter, set it for global context.
let handler = swc_core::common::errors::Handler::with_emitter(
Expand Down
21 changes: 12 additions & 9 deletions crates/swc_plugin_runner/Cargo.toml
Expand Up @@ -58,17 +58,20 @@ wasmer-compiler-cranelift = { version = "2.3.0" }
wasmer-engine-universal = { version = "2.3.0" }

[dev-dependencies]
criterion = "0.3"
swc_atoms = { version = "0.4.25", path = '../swc_atoms' }
swc_ecma_loader = { version = "0.41.18", path = "../swc_ecma_loader" }
swc_ecma_parser = { version = "0.123.2", path = "../swc_ecma_parser" }
swc_ecma_visit = { version = "0.81.1", path = "../swc_ecma_visit" }
swc_node_base = { version = "0.5.8", path = "../swc_node_base" }
testing = { version = "0.31.18", path = "../testing" }
# This allows we can run non-wasm32 target build command while some pkg select features for wasmer/js omits its transitive deps
swc_ecma_ast = { version = "0.95.1", path = "../swc_ecma_ast", feature = [
criterion = "0.3"
swc_atoms = { version = "0.4.25", path = '../swc_atoms' }
swc_css_ast = { version = "0.128.1", path = "../swc_css_ast", features = [
"rkyv-impl",
] }
swc_css_parser = { version = "0.137.3", path = "../swc_css_parser" }
swc_ecma_ast = { version = "0.95.1", path = "../swc_ecma_ast", features = [
"rkyv-impl",
] }
swc_ecma_loader = { version = "0.41.18", path = "../swc_ecma_loader" }
swc_ecma_parser = { version = "0.123.2", path = "../swc_ecma_parser" }
swc_ecma_visit = { version = "0.81.1", path = "../swc_ecma_visit" }
swc_node_base = { version = "0.5.8", path = "../swc_node_base" }
testing = { version = "0.31.18", path = "../testing" }
wasmer = "2.3.0"
wasmer-wasi = "2.3.0"

Expand Down

1 comment on commit fa8f7b0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: fa8f7b0 Previous: 8cdc28c Ratio
es/full/bugs-1 353239 ns/iter (± 39445) 358089 ns/iter (± 76574) 0.99
es/full/minify/libraries/antd 2078251310 ns/iter (± 27895652) 2276050437 ns/iter (± 26620525) 0.91
es/full/minify/libraries/d3 456454566 ns/iter (± 24957236) 508166180 ns/iter (± 21506236) 0.90
es/full/minify/libraries/echarts 1860832278 ns/iter (± 273792038) 1903803414 ns/iter (± 55789374) 0.98
es/full/minify/libraries/jquery 127488036 ns/iter (± 19543741) 133132505 ns/iter (± 9621702) 0.96
es/full/minify/libraries/lodash 146296630 ns/iter (± 5873202) 155105922 ns/iter (± 9825492) 0.94
es/full/minify/libraries/moment 76142469 ns/iter (± 10048933) 74941028 ns/iter (± 2207589) 1.02
es/full/minify/libraries/react 23984111 ns/iter (± 2864805) 24320886 ns/iter (± 939304) 0.99
es/full/minify/libraries/terser 391689879 ns/iter (± 11489362) 396720397 ns/iter (± 7228508) 0.99
es/full/minify/libraries/three 680315768 ns/iter (± 50673572) 696597179 ns/iter (± 20296114) 0.98
es/full/minify/libraries/typescript 4165509204 ns/iter (± 772377694) 4131786600 ns/iter (± 84194408) 1.01
es/full/minify/libraries/victory 999466221 ns/iter (± 87948207) 999228771 ns/iter (± 34553774) 1.00
es/full/minify/libraries/vue 189639080 ns/iter (± 14866423) 198179218 ns/iter (± 7050970) 0.96
es/full/codegen/es3 34227 ns/iter (± 2058) 36583 ns/iter (± 3662) 0.94
es/full/codegen/es5 34356 ns/iter (± 1149) 34833 ns/iter (± 1741) 0.99
es/full/codegen/es2015 34241 ns/iter (± 3424) 33796 ns/iter (± 2025) 1.01
es/full/codegen/es2016 34078 ns/iter (± 2532) 34111 ns/iter (± 1452) 1.00
es/full/codegen/es2017 33193 ns/iter (± 1439) 33793 ns/iter (± 2914) 0.98
es/full/codegen/es2018 33384 ns/iter (± 938) 33757 ns/iter (± 993) 0.99
es/full/codegen/es2019 33240 ns/iter (± 1294) 33634 ns/iter (± 2286) 0.99
es/full/codegen/es2020 34740 ns/iter (± 3285) 33719 ns/iter (± 1219) 1.03
es/full/all/es3 200755501 ns/iter (± 16424703) 195636704 ns/iter (± 5400805) 1.03
es/full/all/es5 186831116 ns/iter (± 21854864) 185522466 ns/iter (± 5742440) 1.01
es/full/all/es2015 154262824 ns/iter (± 20845504) 148209196 ns/iter (± 5570362) 1.04
es/full/all/es2016 153665360 ns/iter (± 19825682) 152713049 ns/iter (± 9353884) 1.01
es/full/all/es2017 154896418 ns/iter (± 19178490) 151192116 ns/iter (± 8752166) 1.02
es/full/all/es2018 151192080 ns/iter (± 18049025) 150219541 ns/iter (± 9058792) 1.01
es/full/all/es2019 150965646 ns/iter (± 18227895) 145006616 ns/iter (± 7534774) 1.04
es/full/all/es2020 140647028 ns/iter (± 18889302) 140259464 ns/iter (± 10692626) 1.00
es/full/parser 744440 ns/iter (± 56755) 733676 ns/iter (± 74048) 1.01
es/full/base/fixer 26906 ns/iter (± 2918) 26974 ns/iter (± 1913) 1.00
es/full/base/resolver_and_hygiene 93229 ns/iter (± 7273) 95821 ns/iter (± 4365) 0.97
serialization of ast node 216 ns/iter (± 8) 219 ns/iter (± 24) 0.99
serialization of serde 232 ns/iter (± 29) 228 ns/iter (± 22) 1.02

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.