From 18e2232dbca69649963e076cc63f7829d18ec555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 26 Aug 2021 00:07:09 +0900 Subject: [PATCH] feat(plugin): Groundwork for rust plugin system (#1893) --- .github/workflows/cargo.yml | 3 + Cargo.lock | 283 +++++++++++++++++++++++++++++++----- Cargo.toml | 13 +- plugin/Cargo.toml | 22 +++ plugin/README.md | 3 + plugin/build/Cargo.toml | 14 ++ plugin/build/src/lib.rs | 1 + plugin/runner/Cargo.toml | 26 ++++ plugin/runner/src/lib.rs | 41 ++++++ plugin/scripts/cli.sh | 7 + plugin/scripts/test.sh | 12 ++ plugin/src/lib.rs | 32 ++++ plugin/testing/Cargo.toml | 21 +++ plugin/testing/src/lib.rs | 1 + 14 files changed, 442 insertions(+), 37 deletions(-) create mode 100644 plugin/Cargo.toml create mode 100644 plugin/README.md create mode 100644 plugin/build/Cargo.toml create mode 100644 plugin/build/src/lib.rs create mode 100644 plugin/runner/Cargo.toml create mode 100644 plugin/runner/src/lib.rs create mode 100755 plugin/scripts/cli.sh create mode 100755 plugin/scripts/test.sh create mode 100644 plugin/src/lib.rs create mode 100644 plugin/testing/Cargo.toml create mode 100644 plugin/testing/src/lib.rs diff --git a/.github/workflows/cargo.yml b/.github/workflows/cargo.yml index 5ff1a9a9adde..e8e7dad84096 100644 --- a/.github/workflows/cargo.yml +++ b/.github/workflows/cargo.yml @@ -144,6 +144,9 @@ jobs: - swc_macros_common - swc_node_base - swc_node_bundler + - swc_plugin + - swc_plugin_runner + - swc_plugin_testing - swc_stylis - swc_visit - swc_visit_macros diff --git a/Cargo.lock b/Cargo.lock index 9284d7d3e2c5..1a21b7cda9c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,6 +12,52 @@ dependencies = [ "regex", ] +[[package]] +name = "abi_stable" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c14e9b355e56a614082b552bf24c28b9db1ac50325c96125a1f2723575b39653" +dependencies = [ + "abi_stable_derive", + "abi_stable_shared", + "core_extensions", + "crossbeam-channel", + "generational-arena", + "libloading 0.6.7", + "lock_api 0.4.4", + "parking_lot 0.11.1", + "repr_offset", + "rustc_version", + "serde", + "serde_derive", + "serde_json", +] + +[[package]] +name = "abi_stable_derive" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3434fe2abd3d4c780fb4c23b747cdb8bc2fc1b52dc2329b7b40c1b62ae9f1ab" +dependencies = [ + "abi_stable_shared", + "as_derive_utils", + "core_extensions", + "proc-macro2", + "quote", + "rustc_version", + "syn", + "typed-arena", +] + +[[package]] +name = "abi_stable_shared" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a96112d6a13f37dd1cc2a2f07f7f12b7c3d176b6eec36e7987432a70fece8b48" +dependencies = [ + "core_extensions", +] + [[package]] name = "addr2line" version = "0.16.0" @@ -87,6 +133,18 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "as_derive_utils" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffc3613a62c7b739739a2cb1ee166ac275f16c5b86caf454ba21a2f79f04b025" +dependencies = [ + "core_extensions", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "ast_node" version = "0.7.3" @@ -294,6 +352,15 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" +[[package]] +name = "core_extensions" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a117e1ca5d83ff939487e48e143c6a642cebd01a88f472a4ec7dc4773690fda" +dependencies = [ + "rustc_version", +] + [[package]] name = "cpufeatures" version = "0.1.5" @@ -617,6 +684,15 @@ dependencies = [ "byteorder", ] +[[package]] +name = "generational-arena" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e1d3b771574f62d0548cee0ad9057857e9fc25d7a3335f140c84f6acd0bf601" +dependencies = [ + "cfg-if 0.1.10", +] + [[package]] name = "generic-array" version = "0.14.4" @@ -818,6 +894,15 @@ dependencies = [ "rayon", ] +[[package]] +name = "instant" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bee0328b1209d157ef001c94dd85b4f8f64139adb0eac2659f4b08382b2f474d" +dependencies = [ + "cfg-if 1.0.0", +] + [[package]] name = "ipnet" version = "2.3.1" @@ -860,7 +945,7 @@ dependencies = [ "dashmap", "nom", "serde", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_parser", @@ -903,6 +988,26 @@ version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7f823d141fe0a24df1e23b4af4e3c7ba9e5966ec514ea068c93024aa7deb765" +[[package]] +name = "libloading" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "351a32417a12d5f7e82c368a66781e307834dae04c6ce0cd4456d52989229883" +dependencies = [ + "cfg-if 1.0.0", + "winapi", +] + +[[package]] +name = "libloading" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f84d96438c15fcd6c3f244c8fce01d1e2b9c6b5623e9c711dc9286d8fc92d6a" +dependencies = [ + "cfg-if 1.0.0", + "winapi", +] + [[package]] name = "lock_api" version = "0.1.5" @@ -913,6 +1018,15 @@ dependencies = [ "scopeguard 0.3.3", ] +[[package]] +name = "lock_api" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb" +dependencies = [ + "scopeguard 1.1.0", +] + [[package]] name = "log" version = "0.4.14" @@ -1090,7 +1204,7 @@ dependencies = [ "serde", "serde_json", "swc", - "swc_atoms", + "swc_atoms 0.2.7", "swc_bundler", "swc_common", "swc_ecma_ast", @@ -1298,8 +1412,19 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337" dependencies = [ - "lock_api", - "parking_lot_core", + "lock_api 0.1.5", + "parking_lot_core 0.4.0", +] + +[[package]] +name = "parking_lot" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" +dependencies = [ + "instant", + "lock_api 0.4.4", + "parking_lot_core 0.8.3", ] [[package]] @@ -1315,6 +1440,20 @@ dependencies = [ "winapi", ] +[[package]] +name = "parking_lot_core" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018" +dependencies = [ + "cfg-if 1.0.0", + "instant", + "libc", + "redox_syscall", + "smallvec 1.6.1", + "winapi", +] + [[package]] name = "path-clean" version = "0.1.0" @@ -1804,6 +1943,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "repr_offset" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "422498868d83340d45980a20132f29cbdcf8a84bf6e9e95b151a0b47f1288df4" +dependencies = [ + "rustc_version", +] + [[package]] name = "reqwest" version = "0.11.4" @@ -2156,7 +2304,7 @@ dependencies = [ "serde", "serde_json", "sourcemap", - "swc_atoms", + "swc_atoms 0.2.7", "swc_bundler", "swc_common", "swc_ecma_ast", @@ -2185,13 +2333,23 @@ dependencies = [ "string_cache_codegen", ] +[[package]] +name = "swc_atoms" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "837a3ef86c2817228e733b6f173c821fd76f9eb21a0bc9001a826be48b00b4e7" +dependencies = [ + "string_cache", + "string_cache_codegen", +] + [[package]] name = "swc_babel_ast" version = "0.1.0" dependencies = [ "serde", "serde_json", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_node_base", ] @@ -2208,7 +2366,7 @@ dependencies = [ "serde", "serde_json", "swc", - "swc_atoms", + "swc_atoms 0.2.7", "swc_babel_ast", "swc_babel_visit", "swc_common", @@ -2227,7 +2385,7 @@ version = "0.1.0" dependencies = [ "serde", "serde_json", - "swc_atoms", + "swc_atoms 0.2.7", "swc_babel_ast", "swc_visit", ] @@ -2254,7 +2412,7 @@ dependencies = [ "reqwest", "retain_mut", "sha-1", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_codegen", @@ -2285,7 +2443,7 @@ dependencies = [ "num-bigint", "once_cell", "owning_ref", - "parking_lot", + "parking_lot 0.7.1", "rayon", "scoped-tls", "serde", @@ -2316,7 +2474,7 @@ dependencies = [ "is-macro", "serde", "string_enum", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", ] @@ -2326,7 +2484,7 @@ version = "0.2.0" dependencies = [ "auto_impl", "bitflags", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_css_ast", "swc_css_codegen_macros", @@ -2354,7 +2512,7 @@ dependencies = [ "lexical", "serde", "serde_json", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_css_ast", "swc_css_visit", @@ -2366,7 +2524,7 @@ dependencies = [ name = "swc_css_utils" version = "0.1.0" dependencies = [ - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_css_ast", "swc_css_visit", @@ -2376,7 +2534,7 @@ dependencies = [ name = "swc_css_visit" version = "0.3.0" dependencies = [ - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_css_ast", "swc_visit", @@ -2392,7 +2550,7 @@ dependencies = [ "serde", "serde_json", "string_enum", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", ] @@ -2403,7 +2561,7 @@ dependencies = [ "bitflags", "num-bigint", "sourcemap", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_codegen_macros", @@ -2426,7 +2584,7 @@ dependencies = [ name = "swc_ecma_dep_graph" version = "0.35.0" dependencies = [ - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_parser", @@ -2439,7 +2597,7 @@ name = "swc_ecma_ext_transforms" version = "0.25.0" dependencies = [ "phf", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_parser", @@ -2461,7 +2619,7 @@ dependencies = [ "regex", "serde", "serde_json", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_visit", @@ -2485,7 +2643,7 @@ dependencies = [ "serde", "serde_json", "serde_regex", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_codegen", @@ -2515,7 +2673,7 @@ dependencies = [ "serde", "serde_json", "smallvec 1.6.1", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_visit", @@ -2538,7 +2696,7 @@ dependencies = [ "serde_json", "st-map", "string_enum", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_codegen", @@ -2556,7 +2714,7 @@ version = "0.67.0" dependencies = [ "pretty_assertions 0.6.1", "sourcemap", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_codegen", @@ -2586,7 +2744,7 @@ dependencies = [ "phf", "scoped-tls", "smallvec 1.6.1", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_codegen", @@ -2600,7 +2758,7 @@ dependencies = [ name = "swc_ecma_transforms_classes" version = "0.13.0" dependencies = [ - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_transforms_base", @@ -2620,7 +2778,7 @@ dependencies = [ "ordered-float", "serde", "smallvec 1.6.1", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_parser", @@ -2654,7 +2812,7 @@ dependencies = [ "indexmap", "pathdiff", "serde", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_loader", @@ -2678,7 +2836,7 @@ dependencies = [ "once_cell", "retain_mut", "serde_json", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_parser", @@ -2702,7 +2860,7 @@ dependencies = [ "fxhash", "serde", "smallvec 1.6.1", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_loader", @@ -2728,7 +2886,7 @@ dependencies = [ "serde", "sha-1", "string_enum", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_codegen", @@ -2767,7 +2925,7 @@ version = "0.36.0" dependencies = [ "fxhash", "serde", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_codegen", @@ -2789,7 +2947,7 @@ version = "0.41.3" dependencies = [ "once_cell", "scoped-tls", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_ecma_visit", @@ -2802,7 +2960,7 @@ name = "swc_ecma_visit" version = "0.35.2" dependencies = [ "num-bigint", - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_ecma_ast", "swc_visit", @@ -2869,7 +3027,7 @@ dependencies = [ "serde_json", "string_enum", "swc", - "swc_atoms", + "swc_atoms 0.2.7", "swc_bundler", "swc_common", "swc_ecma_ast", @@ -2885,11 +3043,58 @@ dependencies = [ "walkdir", ] +[[package]] +name = "swc_plugin" +version = "0.1.0" +dependencies = [ + "abi_stable", + "anyhow", + "serde", + "serde_json", + "swc_atoms 0.2.7", + "swc_common", + "swc_ecma_ast", + "swc_ecma_utils", + "swc_ecma_visit", +] + +[[package]] +name = "swc_plugin_runner" +version = "0.1.0" +dependencies = [ + "abi_stable", + "anyhow", + "libloading 0.7.0", + "serde", + "serde_json", + "swc_atoms 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "swc_common", + "swc_ecma_ast", + "swc_ecma_codegen", + "swc_ecma_parser", + "swc_plugin", + "testing", +] + +[[package]] +name = "swc_plugin_testing" +version = "0.1.0" +dependencies = [ + "anyhow", + "swc_atoms 0.2.7", + "swc_common", + "swc_ecma_ast", + "swc_ecma_codegen", + "swc_ecma_utils", + "swc_ecma_visit", + "swc_plugin", +] + [[package]] name = "swc_stylis" version = "0.1.0" dependencies = [ - "swc_atoms", + "swc_atoms 0.2.7", "swc_common", "swc_css_ast", "swc_css_codegen", @@ -3097,6 +3302,12 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" +[[package]] +name = "typed-arena" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0685c84d5d54d1c26f7d3eb96cd41550adb97baed141a761cf335d3d33bcd0ae" + [[package]] name = "typenum" version = "1.13.0" diff --git a/Cargo.toml b/Cargo.toml index ce9a8452a77f..3f3e13a4aab3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,16 @@ [workspace] -members = ["css", "css/stylis", "ecmascript", "ecmascript/babel/compat", "ecmascript/jsdoc", "node/binding", "wasm"] +members = [ + "css", + "css/stylis", + "ecmascript", + "ecmascript/babel/compat", + "ecmascript/jsdoc", + "node/binding", + "plugin", + "plugin/runner", + "plugin/testing", + "wasm", +] [package] authors = ["강동윤 "] diff --git a/plugin/Cargo.toml b/plugin/Cargo.toml new file mode 100644 index 000000000000..6550a4b56d4d --- /dev/null +++ b/plugin/Cargo.toml @@ -0,0 +1,22 @@ +[package] +authors = ["강동윤 "] +description = "Base for swc plugins" +documentation = "https://rustdoc.swc.rs/swc_plugin/" +edition = "2018" +license = "Apache-2.0/MIT" +name = "swc_plugin" +repository = "https://github.com/swc-project/swc.git" +version = "0.1.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +abi_stable = "0.9.3" +anyhow = "1.0.41" +serde = "1.0.126" +serde_json = "1.0.64" +swc_atoms = {version = "0.2.7", path = "../atoms"} +swc_common = {version = "0.11.6", path = "../common"} +swc_ecma_ast = {version = "0.49.5", path = "../ecmascript/ast"} +swc_ecma_utils = {version = "0.41.3", path = "../ecmascript/utils"} +swc_ecma_visit = {version = "0.35.2", path = "../ecmascript/visit"} diff --git a/plugin/README.md b/plugin/README.md new file mode 100644 index 000000000000..bddfadc24e1c --- /dev/null +++ b/plugin/README.md @@ -0,0 +1,3 @@ +# swc_plugin + +Base runtime for swc plugins, written in rust. diff --git a/plugin/build/Cargo.toml b/plugin/build/Cargo.toml new file mode 100644 index 000000000000..2fd0340e9f96 --- /dev/null +++ b/plugin/build/Cargo.toml @@ -0,0 +1,14 @@ +[package] +authors = ["강동윤 "] +description = "Base for swc plugins" +documentation = "https://rustdoc.swc.rs/swc_plugin_build/" +edition = "2018" +license = "Apache-2.0/MIT" +name = "swc_plugin_build" +repository = "https://github.com/swc-project/swc.git" +version = "0.1.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.41" diff --git a/plugin/build/src/lib.rs b/plugin/build/src/lib.rs new file mode 100644 index 000000000000..f37176037eef --- /dev/null +++ b/plugin/build/src/lib.rs @@ -0,0 +1 @@ +pub fn setup() {} diff --git a/plugin/runner/Cargo.toml b/plugin/runner/Cargo.toml new file mode 100644 index 000000000000..fdaa008ad1b6 --- /dev/null +++ b/plugin/runner/Cargo.toml @@ -0,0 +1,26 @@ +[package] +authors = ["강동윤 "] +description = "Runner for swc plugins. This crate is INTERRNAL crate and used by swc itself." +documentation = "https://rustdoc.swc.rs/swc_plugin_runner/" +edition = "2018" +license = "Apache-2.0/MIT" +name = "swc_plugin_runner" +repository = "https://github.com/swc-project/swc.git" +version = "0.1.0" +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +abi_stable = "0.9.3" +anyhow = "1.0.42" +libloading = "0.7.0" +serde = {version = "1.0.126", features = ["derive"]} +serde_json = "1.0.64" +swc_atoms = "0.2.7" +swc_common = {version = "0.11.6", path = "../../common"} +swc_ecma_ast = {version = "0.49.5", path = "../../ecmascript/ast"} +swc_ecma_parser = {version = "0.67.1", path = "../../ecmascript/parser"} +swc_plugin = {version = "0.1", path = "../"} + +[dev-dependencies] +swc_ecma_codegen = {version = "0.67.2", path = "../../ecmascript/codegen"} +testing = {version = "0.12.2", path = "../../testing"} diff --git a/plugin/runner/src/lib.rs b/plugin/runner/src/lib.rs new file mode 100644 index 000000000000..adde970aa80a --- /dev/null +++ b/plugin/runner/src/lib.rs @@ -0,0 +1,41 @@ +use abi_stable::{ + library::RootModule, + std_types::{RResult, RStr, RString}, +}; +use anyhow::{anyhow, Context, Error}; +use std::path::Path; +use swc_ecma_ast::Program; +use swc_plugin::SwcPluginRef; + +pub fn apply_js_plugin(program: &Program, path: &Path) -> Result { + (|| -> Result<_, Error> { + let plugin = SwcPluginRef::load_from_file(path).context("failed to load plugin")?; + + let config_json = "{}"; + let ast_json = + serde_json::to_string(&program).context("failed to serialize program as json")?; + + let plugin_fn = plugin + .process_js() + .ok_or_else(|| anyhow!("the plugin does not support transforming js"))?; + + let new_ast = plugin_fn(RStr::from(config_json), RString::from(ast_json)); + + let new = match new_ast { + RResult::ROk(v) => v, + RResult::RErr(err) => return Err(anyhow!("plugin returned an errror\n{}", err)), + }; + let new = new.into_string(); + + let new = serde_json::from_str(&new) + .with_context(|| format!("plugin generated invalid ast: `{}`", new))?; + + Ok(new) + })() + .with_context(|| { + format!( + "failed to invoke `{}` as js transform plugin", + path.display() + ) + }) +} diff --git a/plugin/scripts/cli.sh b/plugin/scripts/cli.sh new file mode 100755 index 000000000000..0ad1bcae84f9 --- /dev/null +++ b/plugin/scripts/cli.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +set -eu + +cargo install --path ./cli + +(cd plugins && swc-dev $@) \ No newline at end of file diff --git a/plugin/scripts/test.sh b/plugin/scripts/test.sh new file mode 100755 index 000000000000..deebd5e17a35 --- /dev/null +++ b/plugin/scripts/test.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +## This script assumes `plugins` is in `$CDPATH` + +set -eu + +(cd plugins && cargo build && ls -al target/debug) + +export PLUGINS_DIR=$(cd plugins > /dev/null && pwd) +echo "Using plugins at $PLUGINS_DIR" + +(cd runner && cargo test) \ No newline at end of file diff --git a/plugin/src/lib.rs b/plugin/src/lib.rs new file mode 100644 index 000000000000..1cc37b2df3f1 --- /dev/null +++ b/plugin/src/lib.rs @@ -0,0 +1,32 @@ +/// Reexported for convinience. +use abi_stable::{ + library::RootModule, + package_version_strings, + sabi_types::VersionStrings, + std_types::{RResult, RStr, RString}, + StableAbi, +}; + +pub mod ecmascript { + pub extern crate swc_ecma_ast as ast; + pub extern crate swc_ecma_utils as utils; + pub extern crate swc_ecma_visit as visit; +} + +#[repr(C)] +#[derive(StableAbi)] +#[sabi(kind(Prefix(prefix_ref = "SwcPluginRef")))] +#[sabi(missing_field(panic))] +pub struct SwcPlugin { + #[sabi(last_prefix_field)] + pub process_js: + Option RResult>, +} + +impl RootModule for SwcPluginRef { + abi_stable::declare_root_module_statics! {SwcPluginRef} + + const BASE_NAME: &'static str = "swc_plugin"; + const NAME: &'static str = "swc_plugin"; + const VERSION_STRINGS: VersionStrings = package_version_strings!(); +} diff --git a/plugin/testing/Cargo.toml b/plugin/testing/Cargo.toml new file mode 100644 index 000000000000..e360e60b97cd --- /dev/null +++ b/plugin/testing/Cargo.toml @@ -0,0 +1,21 @@ +[package] +authors = ["강동윤 "] +description = "Base for swc plugins" +documentation = "https://rustdoc.swc.rs/swc_plugin_testing/" +edition = "2018" +license = "Apache-2.0/MIT" +name = "swc_plugin_testing" +repository = "https://github.com/swc-project/swc.git" +version = "0.1.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.41" +swc_atoms = {version = "0.2.7", path = "../../atoms"} +swc_common = {version = "0.11.6", path = "../../common"} +swc_ecma_ast = {version = "0.49.5", path = "../../ecmascript/ast"} +swc_ecma_codegen = {version = "0.67.2", path = "../../ecmascript/codegen"} +swc_ecma_utils = {version = "0.41.3", path = "../../ecmascript/utils"} +swc_ecma_visit = {version = "0.35.2", path = "../../ecmascript/visit"} +swc_plugin = {version = "0.1.0", path = "../"} diff --git a/plugin/testing/src/lib.rs b/plugin/testing/src/lib.rs new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/plugin/testing/src/lib.rs @@ -0,0 +1 @@ +