From cf902cfd02c9515d8a37c5893c94f368a997965e Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 1 Aug 2022 13:57:36 -0700 Subject: [PATCH 01/12] build(cargo): define new pkg --- Cargo.lock | 4 ++++ Cargo.toml | 1 + crates/swc_core/Cargo.toml | 12 ++++++++++++ crates/swc_core/src/lib.rs | 1 + 4 files changed, 18 insertions(+) create mode 100644 crates/swc_core/Cargo.toml create mode 100644 crates/swc_core/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 9f45b3666a77..de051b051308 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3146,6 +3146,10 @@ dependencies = [ "syn", ] +[[package]] +name = "swc_core" +version = "0.1.0" + [[package]] name = "swc_css" version = "0.117.0" diff --git a/Cargo.toml b/Cargo.toml index 8a1a712d9e60..a88abf420778 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ + "crates/swc_core", "crates/dbg-swc", "crates/jsdoc", "crates/binding_core_node", diff --git a/crates/swc_core/Cargo.toml b/crates/swc_core/Cargo.toml new file mode 100644 index 000000000000..2ddf086797cb --- /dev/null +++ b/crates/swc_core/Cargo.toml @@ -0,0 +1,12 @@ +[package] +authors = ["강동윤 ", "OJ Kwon "] +description = "TBD" +name = "swc_core" +license = "Apache-2.0" +edition = "2021" +repository = "https://github.com/swc-project/swc.git" +version = "0.1.0" + +[features] + +[dependencies] diff --git a/crates/swc_core/src/lib.rs b/crates/swc_core/src/lib.rs new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/crates/swc_core/src/lib.rs @@ -0,0 +1 @@ + From cb4fa0736901a66b7f097904b17afe8d5f18d54b Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 1 Aug 2022 15:27:03 -0700 Subject: [PATCH 02/12] feat(swc/core): initial plugin features --- Cargo.lock | 10 +++++ crates/swc_core/Cargo.toml | 41 +++++++++++++++++++ crates/swc_core/src/lib.rs | 25 +++++++++++ crates/swc_core/src/plugin.rs | 25 +++++++++++ crates/swc_plugin/src/lib.rs | 4 +- .../fixture/swc_internal_plugin/Cargo.lock | 35 +++++++++++----- .../fixture/swc_internal_plugin/Cargo.toml | 2 +- .../fixture/swc_internal_plugin/src/lib.rs | 13 +++--- 8 files changed, 136 insertions(+), 19 deletions(-) create mode 100644 crates/swc_core/src/plugin.rs diff --git a/Cargo.lock b/Cargo.lock index de051b051308..987ab49e06f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3149,6 +3149,16 @@ dependencies = [ [[package]] name = "swc_core" version = "0.1.0" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_quote", + "swc_ecma_visit", + "swc_plugin", + "swc_plugin_macro", + "swc_plugin_proxy", +] [[package]] name = "swc_css" diff --git a/crates/swc_core/Cargo.toml b/crates/swc_core/Cargo.toml index 2ddf086797cb..a42989ff16b5 100644 --- a/crates/swc_core/Cargo.toml +++ b/crates/swc_core/Cargo.toml @@ -8,5 +8,46 @@ repository = "https://github.com/swc-project/swc.git" version = "0.1.0" [features] +### Public features +### Users should opt in necessary features explicitly + +## General +# Enable `quote!` macro support. +quote = ["swc_ecma_quote"] + +## Plugins +# Top level features should be enabled to write plugins. +plugin = [ + # Dependent features + "__common", + "__ast", + "__visit", + # Enable optional packages + "swc_common/plugin-mode", + "swc_plugin_proxy/plugin-mode", + "swc_plugin_macro", + "swc_plugin" +] + +### Internal features +### Feature sets public features rely on. +### This is not supposed to be used directly, and does not gaurantee +### stability across each versions. + +# Common +__common = ["swc_common"] + +# TODO: Should this be public? +# AST +__ast = ["swc_ecma_ast/rkyv-impl", "swc_atoms"] +__visit = ["swc_ecma_visit"] [dependencies] +swc_atoms = { optional = true, version = "0.3.1", path = "../swc_atoms" } +swc_common = { optional = true, version = "0.26.0", path = "../swc_common" } +swc_ecma_ast = { optional = true, version = "0.89.1", path = "../swc_ecma_ast" } +swc_ecma_quote = { optional = true, version = "0.30.0", path = "../swc_ecma_quote" } +swc_ecma_visit = { optional = true, version = "0.75.0", path = "../swc_ecma_visit" } +swc_plugin = { optional = true, version = "0.88.2", path = "../swc_plugin" } +swc_plugin_macro = { optional = true, version = "0.8.1", path = "../swc_plugin_macro" } +swc_plugin_proxy = { optional = true, version = "0.17.0", path = "../swc_plugin_proxy" } diff --git a/crates/swc_core/src/lib.rs b/crates/swc_core/src/lib.rs index 8b137891791f..43e9435df4b4 100644 --- a/crates/swc_core/src/lib.rs +++ b/crates/swc_core/src/lib.rs @@ -1 +1,26 @@ +#![cfg_attr(docsrs, feature(doc_cfg))] +// Plugins +#[cfg(feature = "plugin")] +pub mod plugin; + +// ast exposed via swc_ecma_ast +// TODO: Can dependency tree simplified +// by swc_ecma_ast reexports swc_atoms? +#[cfg(any(feature = "__ast"))] +pub mod ast { + pub use swc_atoms::*; + pub use swc_ecma_ast::*; +} + +// visit* interfaces +#[cfg(any(feature = "__visit"))] +pub mod visit { + pub use swc_ecma_visit::*; +} + +// swc_common features +#[cfg(any(feature = "__common"))] +pub mod common { + pub use swc_common::*; +} diff --git a/crates/swc_core/src/plugin.rs b/crates/swc_core/src/plugin.rs new file mode 100644 index 000000000000..298c6811885b --- /dev/null +++ b/crates/swc_core/src/plugin.rs @@ -0,0 +1,25 @@ +// #[plugin_transform] macro +#[cfg(feature = "plugin")] +pub use swc_plugin_macro::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", target_arch = "wasm32"))] +pub mod memory { + pub use swc_plugin::allocation::*; +} + +/// Global HANDLER implementation for the plugin +/// for error reporting. +#[cfg(feature = "plugin")] +pub mod errors { + pub use swc_plugin::handler::HANDLER; +} + +/// Plugin's environment metadata context. +#[cfg(feature = "plugin")] +pub mod metadata { + pub use swc_common::plugin::metadata::TransformPluginMetadataContextKind; + pub use swc_plugin_proxy::TransformPluginProgramMetadata; +} diff --git a/crates/swc_plugin/src/lib.rs b/crates/swc_plugin/src/lib.rs index be4994cc0806..237f6f8fccfb 100644 --- a/crates/swc_plugin/src/lib.rs +++ b/crates/swc_plugin/src/lib.rs @@ -51,7 +51,7 @@ pub mod syntax_pos { pub use swc_common::{Mark, Span, DUMMY_SP}; } -mod handler; +pub mod handler; pub mod errors { pub use swc_common::errors::{Diagnostic, Handler, Level}; @@ -65,7 +65,7 @@ pub mod environment { // in ide (i.e rust-analyzer) or non-wasm target `cargo check` pub use swc_plugin_macro::plugin_transform; #[cfg(target_arch = "wasm32")] -mod allocation; +pub mod allocation; #[cfg(target_arch = "wasm32")] pub mod memory { pub use crate::allocation::*; diff --git a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock index b27ad3e7e587..7693fef65bce 100644 --- a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock +++ b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock @@ -704,7 +704,7 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "swc_atoms" -version = "0.3.0" +version = "0.3.1" dependencies = [ "bytecheck", "once_cell", @@ -717,7 +717,7 @@ dependencies = [ [[package]] name = "swc_common" -version = "0.25.0" +version = "0.26.0" dependencies = [ "ahash", "anyhow", @@ -743,9 +743,22 @@ dependencies = [ "url", ] +[[package]] +name = "swc_core" +version = "0.1.0" +dependencies = [ + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_visit", + "swc_plugin", + "swc_plugin_macro", + "swc_plugin_proxy", +] + [[package]] name = "swc_ecma_ast" -version = "0.88.1" +version = "0.89.1" dependencies = [ "bitflags", "bytecheck", @@ -762,7 +775,7 @@ dependencies = [ [[package]] name = "swc_ecma_parser" -version = "0.115.1" +version = "0.116.0" dependencies = [ "either", "enum_kind", @@ -779,7 +792,7 @@ dependencies = [ [[package]] name = "swc_ecma_utils" -version = "0.97.0" +version = "0.98.1" dependencies = [ "indexmap", "once_cell", @@ -793,7 +806,7 @@ dependencies = [ [[package]] name = "swc_ecma_visit" -version = "0.74.1" +version = "0.75.0" dependencies = [ "num-bigint", "swc_atoms", @@ -805,7 +818,7 @@ dependencies = [ [[package]] name = "swc_ecmascript" -version = "0.185.0" +version = "0.186.0" dependencies = [ "swc_ecma_ast", "swc_ecma_parser", @@ -828,7 +841,7 @@ name = "swc_internal_plugin" version = "0.1.0" dependencies = [ "serde", - "swc_plugin", + "swc_core", ] [[package]] @@ -843,7 +856,7 @@ dependencies = [ [[package]] name = "swc_plugin" -version = "0.87.0" +version = "0.88.2" dependencies = [ "swc_atoms", "swc_common", @@ -854,7 +867,7 @@ dependencies = [ [[package]] name = "swc_plugin_macro" -version = "0.8.0" +version = "0.8.1" dependencies = [ "proc-macro2", "quote", @@ -863,7 +876,7 @@ dependencies = [ [[package]] name = "swc_plugin_proxy" -version = "0.16.0" +version = "0.17.0" dependencies = [ "better_scoped_tls", "bytecheck", diff --git a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.toml b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.toml index b559f27c56e4..69dbdf7fe827 100644 --- a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.toml +++ b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.toml @@ -11,4 +11,4 @@ crate-type = ["cdylib"] [dependencies] serde = "1" -swc_plugin = { path = "../../../../swc_plugin" } +swc_core = { path = "../../../../swc_core", features = ["plugin"] } diff --git a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/src/lib.rs b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/src/lib.rs index 65f92e16d6ea..c7efe8bddbc0 100644 --- a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/src/lib.rs +++ b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/src/lib.rs @@ -1,9 +1,12 @@ -use swc_plugin::{ +use swc_core::{ ast::*, - errors::HANDLER, - metadata::{TransformPluginMetadataContextKind, TransformPluginProgramMetadata}, - plugin_transform, - syntax_pos::DUMMY_SP, + common::DUMMY_SP, + plugin::{ + errors::HANDLER, + metadata::{TransformPluginMetadataContextKind, TransformPluginProgramMetadata}, + plugin_transform, + }, + visit::*, }; struct ConsoleOutputReplacer; From a9da0c0694a4845d00a13cc312480afea79fe17d Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 1 Aug 2022 16:37:33 -0700 Subject: [PATCH 03/12] refactor(swc/plugin_macro): update references to core --- crates/swc_core/src/plugin.rs | 8 ++++++- crates/swc_plugin_macro/src/lib.rs | 34 ++++++++++++++++-------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/crates/swc_core/src/plugin.rs b/crates/swc_core/src/plugin.rs index 298c6811885b..6805ffaf1447 100644 --- a/crates/swc_core/src/plugin.rs +++ b/crates/swc_core/src/plugin.rs @@ -14,7 +14,7 @@ pub mod memory { /// for error reporting. #[cfg(feature = "plugin")] pub mod errors { - pub use swc_plugin::handler::HANDLER; + pub use swc_plugin::handler::{PluginDiagnosticsEmitter, HANDLER}; } /// Plugin's environment metadata context. @@ -23,3 +23,9 @@ 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(feature = "plugin")] +pub mod proxies { + pub use swc_plugin_proxy::*; +} diff --git a/crates/swc_plugin_macro/src/lib.rs b/crates/swc_plugin_macro/src/lib.rs index eb76557f1c5c..c0f164e1a25e 100644 --- a/crates/swc_plugin_macro/src/lib.rs +++ b/crates/swc_plugin_macro/src/lib.rs @@ -45,9 +45,9 @@ fn handle_func(func: ItemFn) -> TokenStream { } /// Internal function plugin_macro uses to create ptr to PluginError. - fn construct_error_ptr(plugin_error: swc_plugin::PluginError) -> i32 { - let plugin_error = swc_plugin::VersionedSerializable::new(plugin_error); - let ret = swc_plugin::PluginSerializedBytes::try_serialize(&plugin_error).expect("Should able to serialize PluginError"); + fn construct_error_ptr(plugin_error: swc_core::common::plugin::serialized::PluginError) -> i32 { + let plugin_error = swc_core::common::plugin::serialized::VersionedSerializable::new(plugin_error); + let ret = swc_core::common::plugin::serialized::PluginSerializedBytes::try_serialize(&plugin_error).expect("Should able to serialize PluginError"); let (ptr, len) = ret.as_ptr(); send_transform_result_to_host( @@ -59,7 +59,7 @@ fn handle_func(func: ItemFn) -> TokenStream { #[no_mangle] pub fn #transform_schema_version_ident() -> u32 { - swc_plugin::PLUGIN_TRANSFORM_AST_SCHEMA_VERSION + swc_core::common::plugin::serialized::PLUGIN_TRANSFORM_AST_SCHEMA_VERSION } // Macro to allow compose plugin's transform function without manual pointer operation. @@ -72,44 +72,46 @@ fn handle_func(func: ItemFn) -> TokenStream { unresolved_mark: u32, should_enable_comments_proxy: i32) -> i32 { // Reconstruct `Program` & config string from serialized program // Host (SWC) should allocate memory, copy bytes and pass ptr to plugin. - let program = unsafe { swc_plugin::deserialize_from_ptr(ast_ptr, ast_ptr_len).map(|v| v.into_inner()) }; + let program = unsafe { swc_core::common::plugin::serialized::deserialize_from_ptr(ast_ptr, ast_ptr_len).map(|v| v.into_inner()) }; if program.is_err() { - let err = swc_plugin::PluginError::Deserialize("Failed to deserialize program received from host".to_string()); + 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"); // Create a handler wired with plugin's diagnostic emitter, set it for global context. - let handler = swc_plugin::errors::Handler::with_emitter( + let handler = swc_core::common::errors::Handler::with_emitter( true, false, - Box::new(swc_plugin::environment::PluginDiagnosticsEmitter) + Box::new(swc_core::plugin::errors::PluginDiagnosticsEmitter) ); - let handler_set_result = swc_plugin::errors::HANDLER.inner.set(handler); + let handler_set_result = swc_core::plugin::errors::HANDLER.inner.set(handler); if handler_set_result.is_err() { - let err = swc_plugin::PluginError::Serialize( + let err = swc_core::common::plugin::serialized::PluginError::Serialize( "Failed to set handler for plugin".to_string() ); return construct_error_ptr(err); } // Construct metadata to the `Program` for the transform plugin. - let plugin_comments_proxy = if should_enable_comments_proxy == 1 { Some(swc_plugin::comments::PluginCommentsProxy) } else { None }; - let mut metadata = swc_plugin::metadata::TransformPluginProgramMetadata { + let plugin_comments_proxy = if should_enable_comments_proxy == 1 { Some(swc_core::plugin::proxies::PluginCommentsProxy) } else { None }; + let mut metadata = swc_core::plugin::metadata::TransformPluginProgramMetadata { comments: plugin_comments_proxy, - source_map: swc_plugin::source_map::PluginSourceMapProxy, - unresolved_mark: swc_plugin::syntax_pos::Mark::from_u32(unresolved_mark), + source_map: swc_core::plugin::proxies::PluginSourceMapProxy, + unresolved_mark: swc_core::common::Mark::from_u32(unresolved_mark), }; // Take original plugin fn ident, then call it with interop'ed args let transformed_program = #ident(program, metadata); // Serialize transformed result, return back to the host. - let serialized_result = swc_plugin::PluginSerializedBytes::try_serialize(&swc_plugin::VersionedSerializable::new(transformed_program)); + let serialized_result = swc_core::common::plugin::serialized::PluginSerializedBytes::try_serialize( + &swc_core::common::plugin::serialized::VersionedSerializable::new(transformed_program) + ); if serialized_result.is_err() { - let err = swc_plugin::PluginError::Serialize("Failed to serialize transformed program".to_string()); + let err = swc_core::common::plugin::serialized::PluginError::Serialize("Failed to serialize transformed program".to_string()); return construct_error_ptr(err); } From 6b0a9a4ef638eac3fe286fd39d4a4718979a13bf Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 1 Aug 2022 16:50:49 -0700 Subject: [PATCH 04/12] refactor(swc/plugin): strip out reexports with deps --- Cargo.lock | 5 - crates/swc_core/src/lib.rs | 6 + crates/swc_plugin/Cargo.toml | 16 +- crates/swc_plugin/src/lib.rs | 70 +------- .../fixture/swc_internal_plugin/Cargo.lock | 150 ------------------ 5 files changed, 9 insertions(+), 238 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 987ab49e06f3..c7ed2e4f1a43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4134,12 +4134,7 @@ dependencies = [ name = "swc_plugin" version = "0.88.2" dependencies = [ - "swc_atoms", "swc_common", - "swc_ecma_quote", - "swc_ecmascript", - "swc_plugin_macro", - "swc_plugin_proxy", ] [[package]] diff --git a/crates/swc_core/src/lib.rs b/crates/swc_core/src/lib.rs index 43e9435df4b4..4921569dbebe 100644 --- a/crates/swc_core/src/lib.rs +++ b/crates/swc_core/src/lib.rs @@ -1,5 +1,11 @@ #![cfg_attr(docsrs, feature(doc_cfg))] +// Quote +#[cfg(feature = "quote")] +pub mod quote { + pub use swc_ecma_quote::*; +} + // Plugins #[cfg(feature = "plugin")] pub mod plugin; diff --git a/crates/swc_plugin/Cargo.toml b/crates/swc_plugin/Cargo.toml index 468afc7cf20e..85618b9b50bd 100644 --- a/crates/swc_plugin/Cargo.toml +++ b/crates/swc_plugin/Cargo.toml @@ -17,22 +17,10 @@ bench = false [features] default = ["plugin_transform_schema_v1"] -quote = ["swc_ecma_quote"] -plugin_transform_schema_v1 = [ - "swc_common/plugin_transform_schema_v1" -] -plugin_transform_schema_vtest = [ - "swc_common/plugin_transform_schema_vtest", -] +plugin_transform_schema_v1 = ["swc_common/plugin_transform_schema_v1"] +plugin_transform_schema_vtest = ["swc_common/plugin_transform_schema_vtest"] [dependencies] -swc_atoms = { version = "0.3.1", path = "../swc_atoms" } swc_common = { version = "0.26.0", path = "../swc_common", features = [ "plugin-mode", ] } -swc_ecma_quote = { version = "0.30.0", path = "../swc_ecma_quote", optional = true } -swc_ecmascript = { version = "0.186.0", path = "../swc_ecmascript", features = ["utils", "visit", "rkyv-impl"] } -swc_plugin_proxy = { version = "0.17.0", path = "../swc_plugin_proxy", features = [ - "plugin-mode", -] } -swc_plugin_macro = { version = "0.8.1", path = "../swc_plugin_macro" } diff --git a/crates/swc_plugin/src/lib.rs b/crates/swc_plugin/src/lib.rs index 237f6f8fccfb..4c7874ebf609 100644 --- a/crates/swc_plugin/src/lib.rs +++ b/crates/swc_plugin/src/lib.rs @@ -1,73 +1,5 @@ #![cfg_attr(docsrs, feature(doc_cfg))] - -// Reexports -pub use swc_common::{ - chain, - plugin::serialized::{ - deserialize_from_ptr, PluginError, PluginSerializedBytes, VersionedSerializable, - PLUGIN_TRANSFORM_AST_SCHEMA_VERSION, - }, -}; - -pub mod collections { - pub use swc_common::collections::AHashMap; -} - -pub mod comments { - pub use swc_common::comments::{Comment, CommentKind, Comments}; - pub use swc_plugin_proxy::PluginCommentsProxy; -} - -pub mod source_map { - pub use swc_common::{ - source_map::{ - BytePos, CharPos, FileLinesResult, FileName, Loc, MultiByteChar, NonNarrowChar, Pos, - SourceFile, - }, - SourceMapper, - }; - pub use swc_plugin_proxy::PluginSourceMapProxy; -} - -pub mod metadata { - pub use swc_common::plugin::metadata::TransformPluginMetadataContextKind; - pub use swc_plugin_proxy::TransformPluginProgramMetadata; -} - -pub mod utils { - pub use swc_common::util::take; - #[cfg(feature = "swc_ecma_quote")] - #[cfg_attr(docsrs, doc(cfg(feature = "quote")))] - pub use swc_ecma_quote::*; - pub use swc_ecmascript::utils::*; -} - -pub mod ast { - pub use swc_atoms::*; - pub use swc_ecmascript::{ast::*, visit::*}; -} - -pub mod syntax_pos { - pub use swc_common::{Mark, Span, DUMMY_SP}; -} - -pub mod handler; -pub mod errors { - pub use swc_common::errors::{Diagnostic, Handler, Level}; - - pub use crate::handler::HANDLER; -} - -pub mod environment { - pub use crate::handler::*; -} -// We don't set target cfg as it'll block macro expansions -// in ide (i.e rust-analyzer) or non-wasm target `cargo check` -pub use swc_plugin_macro::plugin_transform; #[cfg(target_arch = "wasm32")] pub mod allocation; -#[cfg(target_arch = "wasm32")] -pub mod memory { - pub use crate::allocation::*; -} +pub mod handler; mod pseudo_scoped_key; diff --git a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock index 7693fef65bce..87568ff7b0d5 100644 --- a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock +++ b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock @@ -146,16 +146,6 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f107b87b6afc2a64fd13cac55fe06d6c8859f12d4b14cbcdd2c67d0976781be" -[[package]] -name = "enum_kind" -version = "0.2.1" -dependencies = [ - "pmutil", - "proc-macro2", - "swc_macros_common", - "syn", -] - [[package]] name = "fnv" version = "1.0.7" @@ -219,16 +209,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "indexmap" -version = "1.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" -dependencies = [ - "autocfg", - "hashbrown", -] - [[package]] name = "is-macro" version = "0.2.1" @@ -248,79 +228,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" -[[package]] -name = "lexical" -version = "6.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7aefb36fd43fef7003334742cbf77b243fcd36418a1d1bdd480d613a67968f6" -dependencies = [ - "lexical-core", -] - -[[package]] -name = "lexical-core" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cde5de06e8d4c2faabc400238f9ae1c74d5412d03a7bd067645ccbc47070e46" -dependencies = [ - "lexical-parse-float", - "lexical-parse-integer", - "lexical-util", - "lexical-write-float", - "lexical-write-integer", -] - -[[package]] -name = "lexical-parse-float" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "683b3a5ebd0130b8fb52ba0bdc718cc56815b6a097e28ae5a6997d0ad17dc05f" -dependencies = [ - "lexical-parse-integer", - "lexical-util", - "static_assertions", -] - -[[package]] -name = "lexical-parse-integer" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d0994485ed0c312f6d965766754ea177d07f9c00c9b82a5ee62ed5b47945ee9" -dependencies = [ - "lexical-util", - "static_assertions", -] - -[[package]] -name = "lexical-util" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5255b9ff16ff898710eb9eb63cb39248ea8a5bb036bea8085b1a767ff6c4e3fc" -dependencies = [ - "static_assertions", -] - -[[package]] -name = "lexical-write-float" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accabaa1c4581f05a3923d1b4cfd124c329352288b7b9da09e766b0668116862" -dependencies = [ - "lexical-util", - "lexical-write-integer", - "static_assertions", -] - -[[package]] -name = "lexical-write-integer" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1b6f3d1f4422866b68192d62f77bc5c700bee84f3069f2469d7bc8c77852446" -dependencies = [ - "lexical-util", - "static_assertions", -] - [[package]] name = "libc" version = "0.2.126" @@ -653,12 +560,6 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - [[package]] name = "string_cache" version = "0.8.4" @@ -773,37 +674,6 @@ dependencies = [ "unicode-id", ] -[[package]] -name = "swc_ecma_parser" -version = "0.116.0" -dependencies = [ - "either", - "enum_kind", - "lexical", - "num-bigint", - "serde", - "smallvec", - "swc_atoms", - "swc_common", - "swc_ecma_ast", - "tracing", - "typed-arena", -] - -[[package]] -name = "swc_ecma_utils" -version = "0.98.1" -dependencies = [ - "indexmap", - "once_cell", - "swc_atoms", - "swc_common", - "swc_ecma_ast", - "swc_ecma_visit", - "tracing", - "unicode-id", -] - [[package]] name = "swc_ecma_visit" version = "0.75.0" @@ -816,16 +686,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "swc_ecmascript" -version = "0.186.0" -dependencies = [ - "swc_ecma_ast", - "swc_ecma_parser", - "swc_ecma_utils", - "swc_ecma_visit", -] - [[package]] name = "swc_eq_ignore_macros" version = "0.1.0" @@ -858,11 +718,7 @@ dependencies = [ name = "swc_plugin" version = "0.88.2" dependencies = [ - "swc_atoms", "swc_common", - "swc_ecmascript", - "swc_plugin_macro", - "swc_plugin_proxy", ] [[package]] @@ -963,12 +819,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "typed-arena" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0685c84d5d54d1c26f7d3eb96cd41550adb97baed141a761cf335d3d33bcd0ae" - [[package]] name = "unicode-bidi" version = "0.3.8" From 545b80e782f5ef831cc700e5443c06a83cff49ca Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 1 Aug 2022 17:10:27 -0700 Subject: [PATCH 05/12] refactor(swc/plugin_macro): encapsulate plugindiagemitter --- crates/swc_core/src/plugin.rs | 2 +- crates/swc_plugin/README.md | 25 +++------------------- crates/swc_plugin/src/handler.rs | 33 +----------------------------- crates/swc_plugin_macro/src/lib.rs | 26 ++++++++++++++++++++++- 4 files changed, 30 insertions(+), 56 deletions(-) diff --git a/crates/swc_core/src/plugin.rs b/crates/swc_core/src/plugin.rs index 6805ffaf1447..c63b3dc5185c 100644 --- a/crates/swc_core/src/plugin.rs +++ b/crates/swc_core/src/plugin.rs @@ -14,7 +14,7 @@ pub mod memory { /// for error reporting. #[cfg(feature = "plugin")] pub mod errors { - pub use swc_plugin::handler::{PluginDiagnosticsEmitter, HANDLER}; + pub use swc_plugin::handler::HANDLER; } /// Plugin's environment metadata context. diff --git a/crates/swc_plugin/README.md b/crates/swc_plugin/README.md index bbdc3c1041f7..cd23ec9f04aa 100644 --- a/crates/swc_plugin/README.md +++ b/crates/swc_plugin/README.md @@ -1,24 +1,5 @@ -# SWC Plugin +## swc_plugin (PUBLIC INTERFACE DEPRECATED) -This crate provides necessary types and macros for creating a custom plugin for SWC (https://swc.rs/), which are WebAssembly modules that may be used to modify behavior of SWC. Currently only `transform` (https://swc.rs/docs/usage/core#transform) is supported. +Provides internal implementation detail to communicate between `@swc/core` to the plugin. -**Disclaimer: currently SWC plugin support is experimental, there may be possible breaking changes or unexpected behaviors. Please provide issues, feedbacks to https://github.com/swc-project/swc/discussions .** - -## Writing a plugin - -All plugin require adding crate-type = ['cdylib'] to the Cargo.toml. For a quick setup, SWC provides a simple commandline to create project for the plugin. - -``` -// if you haven't, add build targets for webassembly -rustup target add wasm32-wasi wasm32-unknown-unknown - -cargo install swc_cli - -swc plugin new ${plugin_name} --target-type wasm32-wasi -``` - -When create a new project cli require to specify target type between `wasm32-wasi` or `wasm32-unknown-unknown`. `wasm32-unknown-unknown` will generate slighly smaller binary, but it doesn't support system interfaces as well as macros like `printn!()`. - -Generated project will contain a fn with macro `#[plugin_transform]` which internally does necessary interop for communication between host to the plugin. - -There are few references like SWC's [jest transform](https://github.com/swc-project/swc/blob/90d080c16b41b73f34151c1f5ecbcbf0b5d8e236/crates/swc_ecma_ext_transforms/src/jest.rs) for writing actual `VisitMut`. +NOTE: This was previously entrypoint sdk to authoring a plugin, but `swc_core` becomes a meta entrypoint to any features SWC provides (https://github.com/swc-project/swc/discussions/5244). \ No newline at end of file diff --git a/crates/swc_plugin/src/handler.rs b/crates/swc_plugin/src/handler.rs index 52c5c32c19aa..714c38b9589a 100644 --- a/crates/swc_plugin/src/handler.rs +++ b/crates/swc_plugin/src/handler.rs @@ -1,38 +1,7 @@ -use swc_common::{ - plugin::serialized::{PluginSerializedBytes, VersionedSerializable}, - sync::OnceCell, -}; +use swc_common::sync::OnceCell; use crate::pseudo_scoped_key::PseudoScopedKey; -#[cfg(target_arch = "wasm32")] // Allow testing -extern "C" { - fn __emit_diagnostics(bytes_ptr: i32, bytes_ptr_len: i32); - fn __free(bytes_ptr: i32, size: i32) -> i32; -} - -/// An emitter for the Diagnostic in plugin's context by borrowing host's -/// environment context. -/// -/// It is not expected to call this directly inside of plugin transform. -/// Instead, it is encouraged to use global HANDLER. -pub struct PluginDiagnosticsEmitter; - -impl swc_common::errors::Emitter for PluginDiagnosticsEmitter { - #[cfg_attr(not(target_arch = "wasm32"), allow(unused))] - fn emit(&mut self, db: &swc_common::errors::DiagnosticBuilder<'_>) { - let diagnostic = VersionedSerializable::new((*db.diagnostic).clone()); - let diag = PluginSerializedBytes::try_serialize(&diagnostic) - .expect("Should able to serialize Diagnostic"); - let (ptr, len) = diag.as_ptr(); - - #[cfg(target_arch = "wasm32")] // Allow testing - unsafe { - __emit_diagnostics(ptr as i32, len as i32); - } - } -} - /// global context HANDLER in plugin's transform function. pub static HANDLER: PseudoScopedKey = PseudoScopedKey { inner: OnceCell::new(), diff --git a/crates/swc_plugin_macro/src/lib.rs b/crates/swc_plugin_macro/src/lib.rs index c0f164e1a25e..e746bb9e19a1 100644 --- a/crates/swc_plugin_macro/src/lib.rs +++ b/crates/swc_plugin_macro/src/lib.rs @@ -32,6 +32,30 @@ fn handle_func(func: ItemFn) -> TokenStream { #[cfg(target_arch = "wasm32")] // Allow testing extern "C" { fn __set_transform_result(bytes_ptr: i32, bytes_ptr_len: i32); + fn __emit_diagnostics(bytes_ptr: i32, bytes_ptr_len: i32); + fn __free(bytes_ptr: i32, size: i32) -> i32; + } + + /// An emitter for the Diagnostic in plugin's context by borrowing host's + /// environment context. + /// + /// It is not expected to call this directly inside of plugin transform. + /// Instead, it is encouraged to use global HANDLER. + pub struct PluginDiagnosticsEmitter; + + impl swc_core::common::errors::Emitter for PluginDiagnosticsEmitter { + #[cfg_attr(not(target_arch = "wasm32"), allow(unused))] + fn emit(&mut self, db: &swc_core::common::errors::DiagnosticBuilder<'_>) { + let diagnostic = swc_core::common::plugin::serialized::VersionedSerializable::new((*db.diagnostic).clone()); + let diag = swc_core::common::plugin::serialized::PluginSerializedBytes::try_serialize(&diagnostic) + .expect("Should able to serialize Diagnostic"); + let (ptr, len) = diag.as_ptr(); + + #[cfg(target_arch = "wasm32")] // Allow testing + unsafe { + __emit_diagnostics(ptr as i32, len as i32); + } + } } @@ -83,7 +107,7 @@ fn handle_func(func: ItemFn) -> TokenStream { let handler = swc_core::common::errors::Handler::with_emitter( true, false, - Box::new(swc_core::plugin::errors::PluginDiagnosticsEmitter) + Box::new(PluginDiagnosticsEmitter) ); let handler_set_result = swc_core::plugin::errors::HANDLER.inner.set(handler); From 6f6c10feeb8c96fd8cf2ed11125f80e0c98370db Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 1 Aug 2022 20:04:45 -0700 Subject: [PATCH 06/12] refactor(swc/core): clarify plugin feature --- crates/swc_core/Cargo.toml | 4 ++-- crates/swc_core/src/lib.rs | 2 +- crates/swc_core/src/plugin.rs | 10 +++++----- .../tests/fixture/swc_internal_plugin/Cargo.toml | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/swc_core/Cargo.toml b/crates/swc_core/Cargo.toml index a42989ff16b5..a866d04e50e1 100644 --- a/crates/swc_core/Cargo.toml +++ b/crates/swc_core/Cargo.toml @@ -16,8 +16,8 @@ version = "0.1.0" quote = ["swc_ecma_quote"] ## Plugins -# Top level features should be enabled to write plugins. -plugin = [ +# Top level features should be enabled to write plugins for the custom transform. +plugin_transform = [ # Dependent features "__common", "__ast", diff --git a/crates/swc_core/src/lib.rs b/crates/swc_core/src/lib.rs index 4921569dbebe..26429e1e90e7 100644 --- a/crates/swc_core/src/lib.rs +++ b/crates/swc_core/src/lib.rs @@ -7,7 +7,7 @@ pub mod quote { } // Plugins -#[cfg(feature = "plugin")] +#[cfg(feature = "plugin_transform")] pub mod plugin; // ast exposed via swc_ecma_ast diff --git a/crates/swc_core/src/plugin.rs b/crates/swc_core/src/plugin.rs index c63b3dc5185c..3d9f560a0a94 100644 --- a/crates/swc_core/src/plugin.rs +++ b/crates/swc_core/src/plugin.rs @@ -1,31 +1,31 @@ // #[plugin_transform] macro -#[cfg(feature = "plugin")] +#[cfg(feature = "plugin_transform")] pub use swc_plugin_macro::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", target_arch = "wasm32"))] +#[cfg(all(feature = "plugin_transform", target_arch = "wasm32"))] pub mod memory { pub use swc_plugin::allocation::*; } /// Global HANDLER implementation for the plugin /// for error reporting. -#[cfg(feature = "plugin")] +#[cfg(feature = "plugin_transform")] pub mod errors { pub use swc_plugin::handler::HANDLER; } /// Plugin's environment metadata context. -#[cfg(feature = "plugin")] +#[cfg(feature = "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(feature = "plugin")] +#[cfg(feature = "plugin_transform")] pub mod proxies { pub use swc_plugin_proxy::*; } diff --git a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.toml b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.toml index 69dbdf7fe827..d1fdc0b158ce 100644 --- a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.toml +++ b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.toml @@ -11,4 +11,4 @@ crate-type = ["cdylib"] [dependencies] serde = "1" -swc_core = { path = "../../../../swc_core", features = ["plugin"] } +swc_core = { path = "../../../../swc_core", features = ["plugin_transform"] } From 3e10ae579b34964cc7209a2f31701a4a9432d696 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 1 Aug 2022 20:24:27 -0700 Subject: [PATCH 07/12] refactor(swc/plugin): flatten dep tree to exclude swc_common --- Cargo.lock | 3 ++- crates/swc_core/Cargo.toml | 13 ++++++++++++- crates/swc_core/src/plugin.rs | 7 ++++++- crates/swc_plugin/Cargo.toml | 9 +-------- crates/swc_plugin/src/handler.rs | 8 -------- crates/swc_plugin/src/lib.rs | 3 +-- crates/swc_plugin/src/pseudo_scoped_key.rs | 12 +++++++----- .../tests/fixture/swc_internal_plugin/Cargo.lock | 3 ++- 8 files changed, 31 insertions(+), 27 deletions(-) delete mode 100644 crates/swc_plugin/src/handler.rs diff --git a/Cargo.lock b/Cargo.lock index c7ed2e4f1a43..8d4f1c6268e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3150,6 +3150,7 @@ dependencies = [ name = "swc_core" version = "0.1.0" dependencies = [ + "once_cell", "swc_atoms", "swc_common", "swc_ecma_ast", @@ -4134,7 +4135,7 @@ dependencies = [ name = "swc_plugin" version = "0.88.2" dependencies = [ - "swc_common", + "once_cell", ] [[package]] diff --git a/crates/swc_core/Cargo.toml b/crates/swc_core/Cargo.toml index a866d04e50e1..74a818985b0a 100644 --- a/crates/swc_core/Cargo.toml +++ b/crates/swc_core/Cargo.toml @@ -22,11 +22,13 @@ plugin_transform = [ "__common", "__ast", "__visit", + "__plugin_transform_schema_v1", # Enable optional packages "swc_common/plugin-mode", "swc_plugin_proxy/plugin-mode", "swc_plugin_macro", - "swc_plugin" + "swc_plugin", + "once_cell" ] ### Internal features @@ -34,6 +36,14 @@ plugin_transform = [ ### This is not supposed to be used directly, and does not gaurantee ### stability across each versions. +# Specify version of AST schema will be used. +# This'll be automatically selected via transform_plugin features, +# SWC upstream decides which version to be used for specific version of +# swc_core. +__plugin_transform_schema_v1 = [ + "swc_common/plugin_transform_schema_v1" +] + # Common __common = ["swc_common"] @@ -43,6 +53,7 @@ __ast = ["swc_ecma_ast/rkyv-impl", "swc_atoms"] __visit = ["swc_ecma_visit"] [dependencies] +once_cell = { optional = true, version = "1.13.0"} swc_atoms = { optional = true, version = "0.3.1", path = "../swc_atoms" } swc_common = { optional = true, version = "0.26.0", path = "../swc_common" } swc_ecma_ast = { optional = true, version = "0.89.1", path = "../swc_ecma_ast" } diff --git a/crates/swc_core/src/plugin.rs b/crates/swc_core/src/plugin.rs index 3d9f560a0a94..55aaff0ebb93 100644 --- a/crates/swc_core/src/plugin.rs +++ b/crates/swc_core/src/plugin.rs @@ -14,7 +14,12 @@ pub mod memory { /// for error reporting. #[cfg(feature = "plugin_transform")] pub mod errors { - pub use swc_plugin::handler::HANDLER; + /// global context HANDLER in plugin's transform function. + pub static HANDLER: swc_plugin::pseudo_scoped_key::PseudoScopedKey< + swc_common::errors::Handler, + > = swc_plugin::pseudo_scoped_key::PseudoScopedKey { + inner: once_cell::sync::OnceCell::new(), + }; } /// Plugin's environment metadata context. diff --git a/crates/swc_plugin/Cargo.toml b/crates/swc_plugin/Cargo.toml index 85618b9b50bd..898278c4f24a 100644 --- a/crates/swc_plugin/Cargo.toml +++ b/crates/swc_plugin/Cargo.toml @@ -15,12 +15,5 @@ rustdoc-args = ["--cfg", "docsrs"] [lib] bench = false -[features] -default = ["plugin_transform_schema_v1"] -plugin_transform_schema_v1 = ["swc_common/plugin_transform_schema_v1"] -plugin_transform_schema_vtest = ["swc_common/plugin_transform_schema_vtest"] - [dependencies] -swc_common = { version = "0.26.0", path = "../swc_common", features = [ - "plugin-mode", -] } +once_cell = "1.13.0" \ No newline at end of file diff --git a/crates/swc_plugin/src/handler.rs b/crates/swc_plugin/src/handler.rs deleted file mode 100644 index 714c38b9589a..000000000000 --- a/crates/swc_plugin/src/handler.rs +++ /dev/null @@ -1,8 +0,0 @@ -use swc_common::sync::OnceCell; - -use crate::pseudo_scoped_key::PseudoScopedKey; - -/// global context HANDLER in plugin's transform function. -pub static HANDLER: PseudoScopedKey = PseudoScopedKey { - inner: OnceCell::new(), -}; diff --git a/crates/swc_plugin/src/lib.rs b/crates/swc_plugin/src/lib.rs index 4c7874ebf609..45cdf6a29576 100644 --- a/crates/swc_plugin/src/lib.rs +++ b/crates/swc_plugin/src/lib.rs @@ -1,5 +1,4 @@ #![cfg_attr(docsrs, feature(doc_cfg))] #[cfg(target_arch = "wasm32")] pub mod allocation; -pub mod handler; -mod pseudo_scoped_key; +pub mod pseudo_scoped_key; diff --git a/crates/swc_plugin/src/pseudo_scoped_key.rs b/crates/swc_plugin/src/pseudo_scoped_key.rs index a777abf76ca6..844b769f659c 100644 --- a/crates/swc_plugin/src/pseudo_scoped_key.rs +++ b/crates/swc_plugin/src/pseudo_scoped_key.rs @@ -1,19 +1,21 @@ +use once_cell::sync::OnceCell; + /// Simple substitution for scoped_thread_local with limited interface parity. /// The only available fn in this struct is `with`, which is being used for the /// consumers when they need to access global scope handler (HANDLER.with()). /// Any other interfaces to support thread local is not available. -pub struct PseudoScopedKey { +pub struct PseudoScopedKey { // As we can't use scoped_thread_local for the global HANDLER, it is challenging // to set its inner handler for each plugin scope's diagnostic emitter. // Via lazy init OnceCell we keep static HANDLER as immutable, also allows to set // plugin specific values when proc_macro expands plugin's transform helper. - pub inner: swc_common::sync::OnceCell, + pub inner: OnceCell, } -impl PseudoScopedKey { +impl PseudoScopedKey { pub fn with(&self, f: F) -> R where - F: FnOnce(&swc_common::errors::Handler) -> R, + F: FnOnce(&T) -> R, { f(self.inner.get().expect("Should set handler before call")) } @@ -25,4 +27,4 @@ impl PseudoScopedKey { // NOTE: This only works cause we know each plugin transform doesn't need any // thread safety. However, if wasm gets thread support and if we're going to // support it this should be revisited. -unsafe impl std::marker::Sync for PseudoScopedKey {} +unsafe impl std::marker::Sync for PseudoScopedKey {} diff --git a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock index 87568ff7b0d5..743c3b17e7d1 100644 --- a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock +++ b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock @@ -648,6 +648,7 @@ dependencies = [ name = "swc_core" version = "0.1.0" dependencies = [ + "once_cell", "swc_atoms", "swc_common", "swc_ecma_ast", @@ -718,7 +719,7 @@ dependencies = [ name = "swc_plugin" version = "0.88.2" dependencies = [ - "swc_common", + "once_cell", ] [[package]] From 2935542aace20131ec4349f041969961f828dd88 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 1 Aug 2022 22:32:34 -0700 Subject: [PATCH 08/12] test(swc): update references --- crates/swc_core/Cargo.toml | 37 ++++++++++++++++--- crates/swc_core/src/lib.rs | 2 + crates/swc_core/src/plugin.rs | 10 ++++- .../plugin_transform_schema_vtest/Cargo.toml | 2 +- .../plugin_transform_schema_vtest/src/lib.rs | 7 +++- 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/crates/swc_core/Cargo.toml b/crates/swc_core/Cargo.toml index 74a818985b0a..bf2dbb6d3bce 100644 --- a/crates/swc_core/Cargo.toml +++ b/crates/swc_core/Cargo.toml @@ -19,6 +19,7 @@ quote = ["swc_ecma_quote"] # Top level features should be enabled to write plugins for the custom transform. plugin_transform = [ # Dependent features + "__plugin_transform", "__common", "__ast", "__visit", @@ -31,8 +32,7 @@ plugin_transform = [ "once_cell" ] -### Internal features -### Feature sets public features rely on. +### Internal features that public features are relying on. ### This is not supposed to be used directly, and does not gaurantee ### stability across each versions. @@ -44,11 +44,38 @@ __plugin_transform_schema_v1 = [ "swc_common/plugin_transform_schema_v1" ] -# Common +# Do not use: testing purpose only +__plugin_transform_schema_vtest = [ + "swc_common/plugin_transform_schema_vtest" +] + +## Plugins + +# Internal flags for any transform plugin feature +__plugin_transform = [] + +# Do not use: testing purpose only +# Force enable different version of AST schema +__plugin_transform_schema_test = [ + # Dependent features + "__plugin_transform", + "__common", + "__ast", + "__visit", + "__plugin_transform_schema_vtest", + # Enable optional packages + "swc_common/plugin-mode", + "swc_plugin_proxy/plugin-mode", + "swc_plugin_macro", + "swc_plugin", + "once_cell" +] + +## Common __common = ["swc_common"] -# TODO: Should this be public? -# AST +## TODO: Should this be public? +## AST __ast = ["swc_ecma_ast/rkyv-impl", "swc_atoms"] __visit = ["swc_ecma_visit"] diff --git a/crates/swc_core/src/lib.rs b/crates/swc_core/src/lib.rs index 26429e1e90e7..0e363de8ee66 100644 --- a/crates/swc_core/src/lib.rs +++ b/crates/swc_core/src/lib.rs @@ -8,6 +8,8 @@ pub mod quote { // Plugins #[cfg(feature = "plugin_transform")] +#[cfg(any(docsrs, feature = "__plugin_transform"))] +#[cfg(feature = "__plugin_transform")] pub mod plugin; // ast exposed via swc_ecma_ast diff --git a/crates/swc_core/src/plugin.rs b/crates/swc_core/src/plugin.rs index 55aaff0ebb93..b2f54f4e8d2b 100644 --- a/crates/swc_core/src/plugin.rs +++ b/crates/swc_core/src/plugin.rs @@ -1,11 +1,13 @@ // #[plugin_transform] macro #[cfg(feature = "plugin_transform")] +#[cfg(any(docsrs, feature = "__plugin_transform"))] +#[cfg(feature = "__plugin_transform")] pub use swc_plugin_macro::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 = "__plugin_transform", target_arch = "wasm32"))] pub mod memory { pub use swc_plugin::allocation::*; } @@ -13,6 +15,8 @@ pub mod memory { /// Global HANDLER implementation for the plugin /// for error reporting. #[cfg(feature = "plugin_transform")] +#[cfg(any(docsrs, feature = "__plugin_transform"))] +#[cfg(feature = "__plugin_transform")] pub mod errors { /// global context HANDLER in plugin's transform function. pub static HANDLER: swc_plugin::pseudo_scoped_key::PseudoScopedKey< @@ -24,6 +28,8 @@ pub mod errors { /// Plugin's environment metadata context. #[cfg(feature = "plugin_transform")] +#[cfg(any(docsrs, feature = "__plugin_transform"))] +#[cfg(feature = "__plugin_transform")] pub mod metadata { pub use swc_common::plugin::metadata::TransformPluginMetadataContextKind; pub use swc_plugin_proxy::TransformPluginProgramMetadata; @@ -31,6 +37,8 @@ pub mod metadata { /// Proxy to the host's data not attached to the AST, like sourcemap / comments. #[cfg(feature = "plugin_transform")] +#[cfg(any(docsrs, feature = "__plugin_transform"))] +#[cfg(feature = "__plugin_transform")] pub mod proxies { pub use swc_plugin_proxy::*; } diff --git a/node-swc/e2e/fixtures/plugin_transform_schema_vtest/Cargo.toml b/node-swc/e2e/fixtures/plugin_transform_schema_vtest/Cargo.toml index d5a370f68381..86030b010bbe 100644 --- a/node-swc/e2e/fixtures/plugin_transform_schema_vtest/Cargo.toml +++ b/node-swc/e2e/fixtures/plugin_transform_schema_vtest/Cargo.toml @@ -11,4 +11,4 @@ crate-type = ["cdylib"] [dependencies] serde = "1.0.140" -swc_plugin = { path = "../../../../crates/swc_plugin", default-features = false, features = ["plugin_transform_schema_vtest"] } +swc_core = { path = "../../../../crates/swc_core", features = ["__plugin_transform_schema_test"] } diff --git a/node-swc/e2e/fixtures/plugin_transform_schema_vtest/src/lib.rs b/node-swc/e2e/fixtures/plugin_transform_schema_vtest/src/lib.rs index 793f3d570ce8..c434c8ddbd08 100644 --- a/node-swc/e2e/fixtures/plugin_transform_schema_vtest/src/lib.rs +++ b/node-swc/e2e/fixtures/plugin_transform_schema_vtest/src/lib.rs @@ -1,5 +1,8 @@ -use swc_plugin::{ - ast::*, metadata::TransformPluginProgramMetadata, plugin_transform, syntax_pos::DUMMY_SP, +use swc_core::{ + ast::*, + common::DUMMY_SP, + plugin::{metadata::TransformPluginProgramMetadata, plugin_transform}, + visit::*, }; struct ConsoleOutputReplacer; From 9fa8c5be4ce20be090994616269138f8acdcbed2 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 1 Aug 2022 22:30:03 -0700 Subject: [PATCH 09/12] refactor(swc/core): align feature cfg --- crates/swc_core/src/lib.rs | 10 ++++------ crates/swc_core/src/plugin.rs | 8 -------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/crates/swc_core/src/lib.rs b/crates/swc_core/src/lib.rs index 0e363de8ee66..b9b65546734a 100644 --- a/crates/swc_core/src/lib.rs +++ b/crates/swc_core/src/lib.rs @@ -1,34 +1,32 @@ #![cfg_attr(docsrs, feature(doc_cfg))] // Quote -#[cfg(feature = "quote")] +#[cfg(any(docsrs, feature = "quote"))] pub mod quote { pub use swc_ecma_quote::*; } // Plugins -#[cfg(feature = "plugin_transform")] #[cfg(any(docsrs, feature = "__plugin_transform"))] -#[cfg(feature = "__plugin_transform")] pub mod plugin; // ast exposed via swc_ecma_ast // TODO: Can dependency tree simplified // by swc_ecma_ast reexports swc_atoms? -#[cfg(any(feature = "__ast"))] +#[cfg(any(docsrs, feature = "__ast"))] pub mod ast { pub use swc_atoms::*; pub use swc_ecma_ast::*; } // visit* interfaces -#[cfg(any(feature = "__visit"))] +#[cfg(any(docsrs, feature = "__visit"))] pub mod visit { pub use swc_ecma_visit::*; } // swc_common features -#[cfg(any(feature = "__common"))] +#[cfg(any(docsrs, feature = "__common"))] pub mod common { pub use swc_common::*; } diff --git a/crates/swc_core/src/plugin.rs b/crates/swc_core/src/plugin.rs index b2f54f4e8d2b..15e604c5a3e2 100644 --- a/crates/swc_core/src/plugin.rs +++ b/crates/swc_core/src/plugin.rs @@ -1,7 +1,5 @@ // #[plugin_transform] macro -#[cfg(feature = "plugin_transform")] #[cfg(any(docsrs, feature = "__plugin_transform"))] -#[cfg(feature = "__plugin_transform")] pub use swc_plugin_macro::plugin_transform; /// exported __alloc / __free fn for the guest (plugin) @@ -14,9 +12,7 @@ pub mod memory { /// Global HANDLER implementation for the plugin /// for error reporting. -#[cfg(feature = "plugin_transform")] #[cfg(any(docsrs, feature = "__plugin_transform"))] -#[cfg(feature = "__plugin_transform")] pub mod errors { /// global context HANDLER in plugin's transform function. pub static HANDLER: swc_plugin::pseudo_scoped_key::PseudoScopedKey< @@ -27,18 +23,14 @@ pub mod errors { } /// Plugin's environment metadata context. -#[cfg(feature = "plugin_transform")] #[cfg(any(docsrs, feature = "__plugin_transform"))] -#[cfg(feature = "__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(feature = "plugin_transform")] #[cfg(any(docsrs, feature = "__plugin_transform"))] -#[cfg(feature = "__plugin_transform")] pub mod proxies { pub use swc_plugin_proxy::*; } From 2cfc7d832683539f80977cbb0a2f3d5125779087 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 1 Aug 2022 22:37:55 -0700 Subject: [PATCH 10/12] build(swc/core): realign version --- crates/swc_core/Cargo.toml | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/crates/swc_core/Cargo.toml b/crates/swc_core/Cargo.toml index bf2dbb6d3bce..60ac2ca2fca8 100644 --- a/crates/swc_core/Cargo.toml +++ b/crates/swc_core/Cargo.toml @@ -5,7 +5,7 @@ name = "swc_core" license = "Apache-2.0" edition = "2021" repository = "https://github.com/swc-project/swc.git" -version = "0.1.0" +version = "0.0.1" [features] ### Public features @@ -29,7 +29,7 @@ plugin_transform = [ "swc_plugin_proxy/plugin-mode", "swc_plugin_macro", "swc_plugin", - "once_cell" + "once_cell", ] ### Internal features that public features are relying on. @@ -40,14 +40,10 @@ plugin_transform = [ # This'll be automatically selected via transform_plugin features, # SWC upstream decides which version to be used for specific version of # swc_core. -__plugin_transform_schema_v1 = [ - "swc_common/plugin_transform_schema_v1" -] +__plugin_transform_schema_v1 = ["swc_common/plugin_transform_schema_v1"] # Do not use: testing purpose only -__plugin_transform_schema_vtest = [ - "swc_common/plugin_transform_schema_vtest" -] +__plugin_transform_schema_vtest = ["swc_common/plugin_transform_schema_vtest"] ## Plugins @@ -68,7 +64,7 @@ __plugin_transform_schema_test = [ "swc_plugin_proxy/plugin-mode", "swc_plugin_macro", "swc_plugin", - "once_cell" + "once_cell", ] ## Common @@ -80,7 +76,7 @@ __ast = ["swc_ecma_ast/rkyv-impl", "swc_atoms"] __visit = ["swc_ecma_visit"] [dependencies] -once_cell = { optional = true, version = "1.13.0"} +once_cell = { optional = true, version = "1.13.0" } swc_atoms = { optional = true, version = "0.3.1", path = "../swc_atoms" } swc_common = { optional = true, version = "0.26.0", path = "../swc_common" } swc_ecma_ast = { optional = true, version = "0.89.1", path = "../swc_ecma_ast" } From c00b4d1d0ed9a46ab4928015050539277ebade1c Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 1 Aug 2022 22:39:10 -0700 Subject: [PATCH 11/12] build(cargo): update lockfile --- Cargo.lock | 2 +- .../fixture/swc_internal_plugin/Cargo.lock | 2 +- .../plugin_transform_schema_vtest/Cargo.lock | 168 ++---------------- 3 files changed, 18 insertions(+), 154 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8d4f1c6268e8..71742d8fc594 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3148,7 +3148,7 @@ dependencies = [ [[package]] name = "swc_core" -version = "0.1.0" +version = "0.0.1" dependencies = [ "once_cell", "swc_atoms", diff --git a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock index 743c3b17e7d1..45e604b1495b 100644 --- a/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock +++ b/crates/swc_plugin_runner/tests/fixture/swc_internal_plugin/Cargo.lock @@ -646,7 +646,7 @@ dependencies = [ [[package]] name = "swc_core" -version = "0.1.0" +version = "0.0.1" dependencies = [ "once_cell", "swc_atoms", diff --git a/node-swc/e2e/fixtures/plugin_transform_schema_vtest/Cargo.lock b/node-swc/e2e/fixtures/plugin_transform_schema_vtest/Cargo.lock index d81d99c1b177..4cb71842fe6d 100644 --- a/node-swc/e2e/fixtures/plugin_transform_schema_vtest/Cargo.lock +++ b/node-swc/e2e/fixtures/plugin_transform_schema_vtest/Cargo.lock @@ -146,16 +146,6 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f107b87b6afc2a64fd13cac55fe06d6c8859f12d4b14cbcdd2c67d0976781be" -[[package]] -name = "enum_kind" -version = "0.2.1" -dependencies = [ - "pmutil", - "proc-macro2", - "swc_macros_common", - "syn", -] - [[package]] name = "fnv" version = "1.0.7" @@ -219,16 +209,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "indexmap" -version = "1.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" -dependencies = [ - "autocfg", - "hashbrown", -] - [[package]] name = "is-macro" version = "0.2.1" @@ -248,79 +228,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" -[[package]] -name = "lexical" -version = "6.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7aefb36fd43fef7003334742cbf77b243fcd36418a1d1bdd480d613a67968f6" -dependencies = [ - "lexical-core", -] - -[[package]] -name = "lexical-core" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cde5de06e8d4c2faabc400238f9ae1c74d5412d03a7bd067645ccbc47070e46" -dependencies = [ - "lexical-parse-float", - "lexical-parse-integer", - "lexical-util", - "lexical-write-float", - "lexical-write-integer", -] - -[[package]] -name = "lexical-parse-float" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "683b3a5ebd0130b8fb52ba0bdc718cc56815b6a097e28ae5a6997d0ad17dc05f" -dependencies = [ - "lexical-parse-integer", - "lexical-util", - "static_assertions", -] - -[[package]] -name = "lexical-parse-integer" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d0994485ed0c312f6d965766754ea177d07f9c00c9b82a5ee62ed5b47945ee9" -dependencies = [ - "lexical-util", - "static_assertions", -] - -[[package]] -name = "lexical-util" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5255b9ff16ff898710eb9eb63cb39248ea8a5bb036bea8085b1a767ff6c4e3fc" -dependencies = [ - "static_assertions", -] - -[[package]] -name = "lexical-write-float" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accabaa1c4581f05a3923d1b4cfd124c329352288b7b9da09e766b0668116862" -dependencies = [ - "lexical-util", - "lexical-write-integer", - "static_assertions", -] - -[[package]] -name = "lexical-write-integer" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1b6f3d1f4422866b68192d62f77bc5c700bee84f3069f2469d7bc8c77852446" -dependencies = [ - "lexical-util", - "static_assertions", -] - [[package]] name = "libc" version = "0.2.126" @@ -451,7 +358,7 @@ name = "plugin_transform_schema_vtest" version = "0.1.0" dependencies = [ "serde", - "swc_plugin", + "swc_core", ] [[package]] @@ -661,12 +568,6 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - [[package]] name = "string_cache" version = "0.8.4" @@ -751,6 +652,20 @@ dependencies = [ "url", ] +[[package]] +name = "swc_core" +version = "0.0.1" +dependencies = [ + "once_cell", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "swc_ecma_visit", + "swc_plugin", + "swc_plugin_macro", + "swc_plugin_proxy", +] + [[package]] name = "swc_ecma_ast" version = "0.89.1" @@ -768,37 +683,6 @@ dependencies = [ "unicode-id", ] -[[package]] -name = "swc_ecma_parser" -version = "0.116.0" -dependencies = [ - "either", - "enum_kind", - "lexical", - "num-bigint", - "serde", - "smallvec", - "swc_atoms", - "swc_common", - "swc_ecma_ast", - "tracing", - "typed-arena", -] - -[[package]] -name = "swc_ecma_utils" -version = "0.98.1" -dependencies = [ - "indexmap", - "once_cell", - "swc_atoms", - "swc_common", - "swc_ecma_ast", - "swc_ecma_visit", - "tracing", - "unicode-id", -] - [[package]] name = "swc_ecma_visit" version = "0.75.0" @@ -811,16 +695,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "swc_ecmascript" -version = "0.186.0" -dependencies = [ - "swc_ecma_ast", - "swc_ecma_parser", - "swc_ecma_utils", - "swc_ecma_visit", -] - [[package]] name = "swc_eq_ignore_macros" version = "0.1.0" @@ -845,11 +719,7 @@ dependencies = [ name = "swc_plugin" version = "0.88.2" dependencies = [ - "swc_atoms", - "swc_common", - "swc_ecmascript", - "swc_plugin_macro", - "swc_plugin_proxy", + "once_cell", ] [[package]] @@ -950,12 +820,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "typed-arena" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0685c84d5d54d1c26f7d3eb96cd41550adb97baed141a761cf335d3d33bcd0ae" - [[package]] name = "unicode-bidi" version = "0.3.8" From 46da5d3e2724e2926fbb987133ff5fd4f173792a Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 1 Aug 2022 23:26:31 -0700 Subject: [PATCH 12/12] refactor(swc/core): align feature cfg --- crates/swc_core/src/lib.rs | 5 +++++ crates/swc_core/src/plugin.rs | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/crates/swc_core/src/lib.rs b/crates/swc_core/src/lib.rs index b9b65546734a..9b49e0bc18e5 100644 --- a/crates/swc_core/src/lib.rs +++ b/crates/swc_core/src/lib.rs @@ -2,18 +2,21 @@ // Quote #[cfg(any(docsrs, feature = "quote"))] +#[cfg_attr(docsrs, doc(cfg(feature = "quote")))] pub mod quote { pub use swc_ecma_quote::*; } // Plugins #[cfg(any(docsrs, feature = "__plugin_transform"))] +#[cfg_attr(docsrs, doc(cfg(feature = "__plugin_transform")))] pub mod plugin; // ast exposed via swc_ecma_ast // TODO: Can dependency tree simplified // by swc_ecma_ast reexports swc_atoms? #[cfg(any(docsrs, feature = "__ast"))] +#[cfg_attr(docsrs, doc(cfg(feature = "__ast")))] pub mod ast { pub use swc_atoms::*; pub use swc_ecma_ast::*; @@ -21,12 +24,14 @@ pub mod ast { // visit* interfaces #[cfg(any(docsrs, feature = "__visit"))] +#[cfg_attr(docsrs, doc(cfg(feature = "__visit")))] pub mod visit { pub use swc_ecma_visit::*; } // swc_common features #[cfg(any(docsrs, feature = "__common"))] +#[cfg_attr(docsrs, doc(cfg(feature = "__common")))] pub mod common { pub use swc_common::*; } diff --git a/crates/swc_core/src/plugin.rs b/crates/swc_core/src/plugin.rs index 15e604c5a3e2..8246ccd56d1a 100644 --- a/crates/swc_core/src/plugin.rs +++ b/crates/swc_core/src/plugin.rs @@ -1,5 +1,6 @@ // #[plugin_transform] macro #[cfg(any(docsrs, feature = "__plugin_transform"))] +#[cfg_attr(docsrs, doc(cfg(feature = "__plugin_transform")))] pub use swc_plugin_macro::plugin_transform; /// exported __alloc / __free fn for the guest (plugin) @@ -13,6 +14,7 @@ pub mod memory { /// Global HANDLER implementation for the plugin /// for error reporting. #[cfg(any(docsrs, feature = "__plugin_transform"))] +#[cfg_attr(docsrs, doc(cfg(feature = "__plugin_transform")))] pub mod errors { /// global context HANDLER in plugin's transform function. pub static HANDLER: swc_plugin::pseudo_scoped_key::PseudoScopedKey< @@ -24,6 +26,7 @@ pub mod errors { /// Plugin's environment metadata context. #[cfg(any(docsrs, feature = "__plugin_transform"))] +#[cfg_attr(docsrs, doc(cfg(feature = "__plugin_transform")))] pub mod metadata { pub use swc_common::plugin::metadata::TransformPluginMetadataContextKind; pub use swc_plugin_proxy::TransformPluginProgramMetadata; @@ -31,6 +34,7 @@ pub mod metadata { /// 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")))] pub mod proxies { pub use swc_plugin_proxy::*; }