Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(binding): Create Wasm package for stripping only TypeScript #9124

Merged
merged 24 commits into from
Jul 3, 2024
Merged
7 changes: 5 additions & 2 deletions .github/workflows/publish-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,14 @@ jobs:
matrix:
settings:
- crate: "binding_core_wasm"
npm: "@swc/wasm"
npm: "@swc\\/wasm"
target: nodejs
- crate: "binding_core_wasm"
npm: "@swc/wasm-web"
npm: "@swc\\/wasm-web"
target: web
- crate: "binding_typescript_wasm"
npm: "@swc\\/wasm-typescript"
target: no-modules
steps:
- uses: actions/checkout@v4

Expand Down
22 changes: 20 additions & 2 deletions bindings/Cargo.lock

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

1 change: 1 addition & 0 deletions bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"binding_core_wasm",
"binding_minifier_node",
"binding_minifier_wasm",
"binding_typescript_wasm",
"swc_cli",
]
resolver = "2"
Expand Down
2 changes: 1 addition & 1 deletion bindings/binding_core_node/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
let out_dir = env::var("OUT_DIR").expect("Outdir should exist");
let dest_path = Path::new(&out_dir).join("triple.txt");
let mut f =
BufWriter::new(File::create(&dest_path).expect("Failed to create target triple text"));
BufWriter::new(File::create(dest_path).expect("Failed to create target triple text"));
write!(
f,
"{}",
Expand Down
51 changes: 25 additions & 26 deletions bindings/binding_minifier_node/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use napi::{
};
use serde::Deserialize;
use swc_compiler_base::{
minify_file_comments, parse_js, IdentCollector, IsModule, PrintArgs, SourceMapsConfig,
TransformOutput,
minify_file_comments, parse_js, IdentCollector, PrintArgs, SourceMapsConfig, TransformOutput,
};
use swc_config::config_types::BoolOr;
use swc_core::{
Expand All @@ -23,7 +22,7 @@ use swc_core::{
js::{JsMinifyCommentOption, JsMinifyOptions},
option::{MinifyOptions, TopLevelOptions},
},
parser::{EsConfig, Syntax},
parser::{EsSyntax, Syntax},
transforms::base::{fixer::fixer, hygiene::hygiene, resolver},
visit::{FoldWith, VisitMutWith, VisitWith},
},
Expand Down Expand Up @@ -109,11 +108,30 @@ fn do_work(input: MinifyTarget, options: JsMinifyOptions) -> napi::Result<Transf
..Default::default()
};

let comments = SingleThreadedComments::default();

let module = parse_js(
cm.clone(),
fm.clone(),
handler,
target,
Syntax::Es(EsSyntax {
jsx: true,
decorators: true,
decorators_before_export: true,
import_attributes: true,
..Default::default()
}),
options.module,
Some(&comments),
)
.context("failed to parse input file")?;

// top_level defaults to true if module is true

// https://github.com/swc-project/swc/issues/2254

if options.module {
if module.is_module() {
if let Some(opts) = &mut min_opts.compress {
if opts.top_level.is_none() {
opts.top_level = Some(TopLevelOptions { functions: true });
Expand All @@ -136,25 +154,6 @@ fn do_work(input: MinifyTarget, options: JsMinifyOptions) -> napi::Result<Transf
}
}

let comments = SingleThreadedComments::default();

let module = parse_js(
cm.clone(),
fm.clone(),
handler,
target,
Syntax::Es(EsConfig {
jsx: true,
decorators: true,
decorators_before_export: true,
import_attributes: true,
..Default::default()
}),
IsModule::Bool(options.module),
Some(&comments),
)
.context("failed to parse input file")?;

let source_map_names = if source_map.enabled() {
let mut v = IdentCollector {
names: Default::default(),
Expand All @@ -172,7 +171,7 @@ fn do_work(input: MinifyTarget, options: JsMinifyOptions) -> napi::Result<Transf

let is_mangler_enabled = min_opts.mangle.is_some();

let module = (|| {
let module = {
let module = module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false));

let mut module = swc_core::ecma::minifier::optimize(
Expand All @@ -192,7 +191,7 @@ fn do_work(input: MinifyTarget, options: JsMinifyOptions) -> napi::Result<Transf
}
module.visit_mut_with(&mut fixer(Some(&comments as &dyn Comments)));
module
})();
};

let preserve_comments = options
.format
Expand All @@ -211,7 +210,7 @@ fn do_work(input: MinifyTarget, options: JsMinifyOptions) -> napi::Result<Transf
inline_sources_content: options.inline_sources_content,
source_map,
source_map_names: &source_map_names,
orig: orig.as_ref(),
orig,
comments: Some(&comments),
emit_source_map_columns: options.emit_source_map_columns,
preamble: &options.format.preamble,
Expand Down
1 change: 0 additions & 1 deletion bindings/binding_minifier_wasm/scripts/build.sh

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions bindings/binding_minifier_wasm/scripts/build_web_release.sh

This file was deleted.

6 changes: 0 additions & 6 deletions bindings/binding_minifier_wasm/scripts/test.sh

This file was deleted.

40 changes: 40 additions & 0 deletions bindings/binding_typescript_wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
authors = ["강동윤 <[email protected]>"]
description = "wasm module for swc"
edition = "2021"
license = "Apache-2.0"
name = "binding_typescript_wasm"
publish = false
repository = "https://github.com/swc-project/swc.git"
version = "1.6.6"

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

[features]
default = ["swc_v1"]
swc_v1 = []
swc_v2 = []

[dependencies]
anyhow = "1.0.66"
getrandom = { version = "0.2.10", features = ["js"] }
serde = { version = "1", features = ["derive"] }
serde-wasm-bindgen = "0.4.5"
serde_json = "1.0.120"
swc_core = { version = "0.95.10", features = [
"ecma_ast_serde",
"ecma_codegen",
"ecma_transforms",
"ecma_transforms_typescript",
"ecma_visit",
] }
swc_ecma_codegen = { version = "0.151.1", features = ["serde-impl"] }
swc_error_reporters = "0.18.0"
tracing = { version = "0.1.37", features = ["max_level_off"] }
wasm-bindgen = { version = "0.2.82", features = ["enable-interning"] }
wasm-bindgen-futures = { version = "0.4.41" }

[package.metadata.wasm-pack.profile.release]
wasm-opt = false
5 changes: 5 additions & 0 deletions bindings/binding_typescript_wasm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"jest": "^25.1.0"
}
}
Loading
Loading