Skip to content

Commit

Permalink
feat(swc_core): Expand features and use it from wasm and cli (#5369)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj authored Aug 3, 2022
1 parent 12fd737 commit 8b9dd6f
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 90 deletions.
19 changes: 7 additions & 12 deletions Cargo.lock

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

19 changes: 2 additions & 17 deletions crates/binding_core_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@ default = ["swc_v1"]
swc_v1 = []
swc_v2 = []
# This feature exists to allow cargo operations
plugin = [
"swc/plugin",
"swc_plugin_runner/memory_cache",
"swc_plugin_runner/plugin_transform_schema_v1",
"wasmer",
"wasmer-wasi",
"wasmer/js-default",
"wasmer-wasi/js-default",
]
plugin = ["swc/plugin", "swc_core/plugin_transform_host_js"]

[dependencies]
anyhow = "1.0.58"
Expand All @@ -37,20 +29,13 @@ path-clean = "0.1.0"
serde = { version = "1.0.140", features = ["derive"] }
serde_json = "1.0.82"
swc = { path = "../swc" }
swc_common = { path = "../swc_common", features = ["perf"] }
swc_ecma_lints = { path = "../swc_ecma_lints", features = [
"non_critical_lints",
] }
swc_ecmascript = { path = "../swc_ecmascript" }
swc_plugin_runner = { path = "../swc_plugin_runner", default-features = false, optional = true }
tracing = { version = "0.1.35", features = ["release_max_level_off"] }
wasm-bindgen = { version = "0.2.82", features = [
"serde-serialize",
"enable-interning",
] }
wasm-bindgen-futures = "0.4.32"
wasmer = { version = "2.3.0", optional = true, default-features = false }
wasmer-wasi = { version = "2.3.0", optional = true, default-features = false }
swc_core = { path = "../swc_core", features = ["ast", "common", "perf"] }

[package.metadata.wasm-pack.profile.release]
wasm-opt = false
10 changes: 6 additions & 4 deletions crates/binding_core_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ use swc::{
config::{ErrorFormat, JsMinifyOptions, Options, ParseOptions, SourceMapsConfig},
try_with_handler, Compiler,
};
use swc_common::{comments::Comments, FileName, FilePathMapping, SourceMap};
use swc_ecmascript::ast::{EsVersion, Program};
use swc_core::{
ast::{EsVersion, Program},
common::{comments::Comments, FileName, FilePathMapping, SourceMap},
};
use wasm_bindgen::{prelude::*, JsCast};
use wasm_bindgen_futures::{future_to_promise, spawn_local, JsFuture};

Expand Down Expand Up @@ -237,7 +239,7 @@ pub fn transform_sync(
.try_into()
.expect("Resolver should be a js object");

swc_plugin_runner::cache::init_plugin_module_cache_once();
swc_core::plugin_runner::cache::init_plugin_module_cache_once();

let entries = Object::entries(&plugin_bytes_resolver_object);
for entry in entries.iter() {
Expand Down Expand Up @@ -265,7 +267,7 @@ pub fn transform_sync(
// In here we 'inject' externally loaded bytes into the cache, so
// remaining plugin_runner execution path works as much as
// similar between embedded runtime.
swc_plugin_runner::cache::PLUGIN_MODULE_CACHE.store_once(&name, bytes);
swc_core::plugin_runner::cache::PLUGIN_MODULE_CACHE.store_once(&name, bytes);
}
}
}
Expand Down
14 changes: 2 additions & 12 deletions crates/swc_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ path = "./src/main.rs"

[features]
default = []
plugin = [
"swc/plugin",
"swc_plugin_runner/filesystem_cache",
"swc_plugin_runner/plugin_transform_schema_v1",
"wasmer/default",
"wasmer-wasi/default",
]
plugin = ["swc/plugin", "swc_core/plugin_transform_host_native"]

[dependencies]
anyhow = "1.0.53"
Expand All @@ -33,16 +27,12 @@ relative-path = "1.6.1"
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["unbounded_depth"] }
swc = { version = "0.212.0", path = "../swc" }
swc_common = { version = "0.26.0", path = "../swc_common" }
swc_plugin_runner = { version = "0.70.0", path = "../swc_plugin_runner", default-features = false, optional = true }
swc_trace_macro = { version = "0.1.0", path = "../swc_trace_macro" }
swc_core = { path = "../swc_core", features = ["trace_macro", "common"] }
tracing = "0.1.32"
tracing-chrome = "0.5.0"
tracing-futures = "0.2.5"
tracing-subscriber = { version = "0.3.9", features = ["env-filter"] }
walkdir = "2"
wasmer = { version = "2.3.0", optional = true }
wasmer-wasi = { version = "2.3.0", optional = true }

[dependencies.path-absolutize]
features = ["once_cell_cache"]
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_cli/src/commands/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use swc_trace_macro::swc_trace;
use swc_core::trace_macro::swc_trace;

#[derive(Parser)]
pub struct BundleOptions {}
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_cli/src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use swc::{
config::{Config, ConfigFile, Options},
try_with_handler, Compiler, HandlerOpts, TransformOutput,
};
use swc_common::{
errors::ColorConfig, sync::Lazy, FileName, FilePathMapping, SourceFile, SourceMap,
use swc_core::{
common::{errors::ColorConfig, sync::Lazy, FileName, FilePathMapping, SourceFile, SourceMap},
trace_macro::swc_trace,
};
use swc_trace_macro::swc_trace;
use walkdir::WalkDir;

use crate::util::trace::init_trace;
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_cli/src/commands/lint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use swc_trace_macro::swc_trace;
use swc_core::trace_macro::swc_trace;

#[derive(Parser)]
pub struct LintOptions {}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_cli/src/commands/minify.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use swc_trace_macro::swc_trace;
use swc_core::trace_macro::swc_trace;

#[derive(Parser)]
pub struct MinifyOptions {}
Expand Down
105 changes: 79 additions & 26 deletions crates/swc_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,64 @@ version = "0.1.1"
all-features = true
rustdoc-args = ["--cfg", "docsrs"]


[features]
### Public features
### Users should opt in necessary features explicitly

## Flags to opt into certain optimizations. In most cases this won't expose new interfaces,
## but changes internal logics to perform differently. These flag should be turned on in combination with
## actual features. Refer build.rs for more details.
perf = ["swc_common/perf"]

## General
# Enable swc_common reexports.
#
# In most cases, top-level features (i.e plugin, or plugin_host) will include this, so no need to
# explicitly opt in. However, if top-level feature should be controlled by feature as opt-in
# and some other dependent feature need this, can be manually selected.
# refer binding_core_wasm for example: it has plugin_transform_host feature, but
# it has to be a feature to be enabled by compile time flag to avoid cargo's feature collision,
# while it also need to access 'common' exports by default for cargo check / or clippy.
common = ["swc_common"]

# Enable `quote!` macro support.
quote = ["swc_ecma_quote"]

# Enable swc_ecma_ast / swc_atoms support.
# TODO: currently both are enabled at once, we may want to separate them.
ast = ["swc_ecma_ast", "swc_atoms"]

# Enable trace macro support.
# TODO: Once all top-level package (node, wasm, cli..) imports swc_core,
# we may encapsulate `tracing` package into swc_core.
trace_macro = ["swc_trace_macro"]

## Plugins
# Top level features should be enabled to write plugins for the custom transform.
plugin_transform = [
plugin_transform = ["__plugin_transform", "__plugin_transform_schema_v1"]

# Host features to enable plugin `runner` runtime.
# native feature is for the host environment does not have, or cannot access
# to the wasm runtime (i.e cli, or @swc/core node bindings).
# js feature is for the environment can access wasm runtime (i.e @swc/wasm)
plugin_transform_host_native = [
# Dependent features
"__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",
"once_cell",
"__plugin_transform_host",
"__plugin_transform_host_schema_v1",
# Enable optional packages
"swc_plugin_runner/filesystem_cache",
"wasmer/default",
"wasmer-wasi/default",
]
plugin_transform_host_js = [
# Dependent features
"__plugin_transform_host",
"__plugin_transform_host_schema_v1",
# Enable optional packages
"swc_plugin_runner/memory_cache",
"wasmer/js-default",
"wasmer-wasi/js-default",
]

### Internal features that public features are relying on.
Expand All @@ -44,46 +80,63 @@ plugin_transform = [
# 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_host_schema_v1 = [
"__plugin_transform_schema_v1",
"swc_plugin_runner/plugin_transform_schema_v1",
]

# Do not use: testing purpose only
__plugin_transform_schema_vtest = ["swc_common/plugin_transform_schema_vtest"]
__plugin_transform_host_schema_vtest = [
"__plugin_transform_schema_vtest",
"swc_plugin_runner/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 = [
__plugin_transform = [
# Dependent features
"__plugin_transform",
"__common",
"__ast",
"__visit",
"__plugin_transform_schema_vtest", # Enable optional packages
"common",
"ast",
# Enable optional packages
"swc_ecma_ast/rkyv-impl",
"swc_common/plugin-mode",
"swc_plugin_proxy/plugin-mode",
"swc_plugin_macro",
"swc_plugin",
"once_cell",
]

## Common
__common = ["swc_common"]
# Internal flags for any transform plugin host feature
__plugin_transform_host = ["common"]

## TODO: Should this be public?
## AST
__ast = ["swc_ecma_ast/rkyv-impl", "swc_atoms"]
# Do not use: testing purpose only
# Force enable different version of AST schema
__plugin_transform_schema_test = [
"__plugin_transform",
"__plugin_transform_schema_vtest",
]

## Common
__visit = ["swc_ecma_visit"]

[dependencies]
# 3rd party dependencies
once_cell = { optional = true, version = "1.13.0" }
wasmer = { optional = true, version = "2.3.0", default-features = false }
wasmer-wasi = { optional = true, version = "2.3.0", default-features = false }

# swc_* 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 = { optional = true, version = "0.89.0", path = "../swc_plugin" }
swc_plugin_macro = { optional = true, version = "0.9.0", path = "../swc_plugin_macro" }
swc_plugin_proxy = { optional = true, version = "0.17.0", path = "../swc_plugin_proxy" }
swc_trace_macro = { optional = true, version = "0.1.0", path = "../swc_trace_macro" }
# TODO: eventually swc_plugin_runner needs to remove default features
swc_plugin_runner = { optional = true, version = "0.70.0", path = "../swc_plugin_runner", default-features = false }
17 changes: 17 additions & 0 deletions crates/swc_core/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Validate conflict between host / plugin features
#[cfg(all(
feature = "plugin_transform",
any(
feature = "plugin_transform_host_native",
feature = "plugin_transform_host_js"
)
))]
compile_error!(
"'plugin_transform' and 'plugin_transform_host*' features are mutually exclusive. If you're \
writing a plugin, use 'plugin_transform' feature. If you're writing a custom SWC binary to \
run plugin, use 'plugin_transform_host_*' instead."
);

fn main() {
/* noop */
}
Loading

1 comment on commit 8b9dd6f

@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: 8b9dd6f Previous: cf3de12 Ratio
es/full/minify/libraries/antd 1675716868 ns/iter (± 56816192) 1740651081 ns/iter (± 86430952) 0.96
es/full/minify/libraries/d3 371820491 ns/iter (± 19030402) 408377303 ns/iter (± 11951247) 0.91
es/full/minify/libraries/echarts 1411517142 ns/iter (± 30997730) 1520390988 ns/iter (± 88223673) 0.93
es/full/minify/libraries/jquery 85750941 ns/iter (± 4603107) 121210350 ns/iter (± 21089364) 0.71
es/full/minify/libraries/lodash 109734611 ns/iter (± 5316429) 142478744 ns/iter (± 34322153) 0.77
es/full/minify/libraries/moment 51911500 ns/iter (± 1693512) 62076112 ns/iter (± 6201625) 0.84
es/full/minify/libraries/react 17493526 ns/iter (± 808346) 19613867 ns/iter (± 833381) 0.89
es/full/minify/libraries/terser 283552971 ns/iter (± 13528734) 319606930 ns/iter (± 3584724) 0.89
es/full/minify/libraries/three 539146452 ns/iter (± 45475469) 571240186 ns/iter (± 5821961) 0.94
es/full/minify/libraries/typescript 3652179576 ns/iter (± 417269986) 3651206518 ns/iter (± 175653258) 1.00
es/full/minify/libraries/victory 805867664 ns/iter (± 99565253) 799917911 ns/iter (± 73916310) 1.01
es/full/minify/libraries/vue 146595844 ns/iter (± 24013243) 162867512 ns/iter (± 16449844) 0.90
es/full/codegen/es3 35327 ns/iter (± 7777) 33524 ns/iter (± 1208) 1.05
es/full/codegen/es5 35479 ns/iter (± 5621) 33539 ns/iter (± 1293) 1.06
es/full/codegen/es2015 34357 ns/iter (± 4082) 33882 ns/iter (± 1489) 1.01
es/full/codegen/es2016 33860 ns/iter (± 1018) 33909 ns/iter (± 2681) 1.00
es/full/codegen/es2017 34253 ns/iter (± 1669) 34378 ns/iter (± 4149) 1.00
es/full/codegen/es2018 34061 ns/iter (± 1454) 34753 ns/iter (± 5292) 0.98
es/full/codegen/es2019 34308 ns/iter (± 1885) 34490 ns/iter (± 4269) 0.99
es/full/codegen/es2020 35511 ns/iter (± 5520) 36650 ns/iter (± 4143) 0.97
es/full/all/es3 198585742 ns/iter (± 21932660) 221141882 ns/iter (± 23828918) 0.90
es/full/all/es5 201144748 ns/iter (± 114246354) 206295278 ns/iter (± 18632360) 0.98
es/full/all/es2015 152072598 ns/iter (± 14482777) 168607742 ns/iter (± 17324665) 0.90
es/full/all/es2016 147693646 ns/iter (± 19681956) 165135079 ns/iter (± 14402921) 0.89
es/full/all/es2017 145157274 ns/iter (± 6938751) 165533677 ns/iter (± 16231745) 0.88
es/full/all/es2018 144334112 ns/iter (± 7035150) 162673808 ns/iter (± 22348641) 0.89
es/full/all/es2019 145999749 ns/iter (± 11111682) 145497126 ns/iter (± 14756129) 1.00
es/full/all/es2020 140029812 ns/iter (± 6664682) 161659906 ns/iter (± 26737176) 0.87
es/full/parser 746515 ns/iter (± 18108) 782111 ns/iter (± 76485) 0.95
es/full/base/fixer 29416 ns/iter (± 718) 30972 ns/iter (± 5642) 0.95
es/full/base/resolver_and_hygiene 87515 ns/iter (± 1933) 97772 ns/iter (± 13758) 0.90
serialization of ast node 214 ns/iter (± 3) 240 ns/iter (± 51) 0.89
serialization of serde 236 ns/iter (± 4) 249 ns/iter (± 23) 0.95

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

Please sign in to comment.