mirror of
https://github.com/swc-project/swc.git
synced 2024-12-22 21:21:31 +03:00
doc(api/rust): Add examples (#4002)
This commit is contained in:
parent
8565a8e8f3
commit
dac84b98e3
@ -97,4 +97,4 @@ testing = {version = "0.19.0", path = "../testing"}
|
|||||||
walkdir = "2"
|
walkdir = "2"
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "usage"
|
name = "transform"
|
||||||
|
3
crates/swc/examples/minify-input.js
Normal file
3
crates/swc/examples/minify-input.js
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
|
||||||
|
const foo = 1 + 1 + 1 + 1 + 1 + 1 + 1;
|
46
crates/swc/examples/minify.rs
Normal file
46
crates/swc/examples/minify.rs
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
use std::{path::Path, sync::Arc};
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
|
use swc::{self, config::JsMinifyOptions, try_with_handler, HandlerOpts};
|
||||||
|
use swc_common::SourceMap;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let cm = Arc::<SourceMap>::default();
|
||||||
|
|
||||||
|
let c = swc::Compiler::new(cm.clone());
|
||||||
|
|
||||||
|
let output = try_with_handler(
|
||||||
|
cm.clone(),
|
||||||
|
HandlerOpts {
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
|handler| {
|
||||||
|
let fm = cm
|
||||||
|
.load_file(Path::new("examples/minify-input.js"))
|
||||||
|
.expect("failed to load file");
|
||||||
|
|
||||||
|
c.minify(
|
||||||
|
fm,
|
||||||
|
handler,
|
||||||
|
&JsMinifyOptions {
|
||||||
|
compress: true.into(),
|
||||||
|
mangle: true.into(),
|
||||||
|
format: Default::default(),
|
||||||
|
ecma: Default::default(),
|
||||||
|
keep_classnames: Default::default(),
|
||||||
|
keep_fnames: Default::default(),
|
||||||
|
module: Default::default(),
|
||||||
|
safari10: Default::default(),
|
||||||
|
toplevel: Default::default(),
|
||||||
|
source_map: Default::default(),
|
||||||
|
output_path: Default::default(),
|
||||||
|
inline_sources_content: Default::default(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.context("failed to minify")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
println!("{}", output.code);
|
||||||
|
}
|
5
crates/swc/examples/transform-input.js
Normal file
5
crates/swc/examples/transform-input.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
|
||||||
|
class Foo { }
|
||||||
|
|
||||||
|
class Bar extends Foo { }
|
35
crates/swc/examples/transform.rs
Normal file
35
crates/swc/examples/transform.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
use std::{path::Path, sync::Arc};
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
|
use swc::{self, config::Options, try_with_handler, HandlerOpts};
|
||||||
|
use swc_common::SourceMap;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let cm = Arc::<SourceMap>::default();
|
||||||
|
|
||||||
|
let c = swc::Compiler::new(cm.clone());
|
||||||
|
|
||||||
|
let output = try_with_handler(
|
||||||
|
cm.clone(),
|
||||||
|
HandlerOpts {
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
|handler| {
|
||||||
|
let fm = cm
|
||||||
|
.load_file(Path::new("examples/transform-input.js"))
|
||||||
|
.expect("failed to load file");
|
||||||
|
|
||||||
|
c.process_js_file(
|
||||||
|
fm,
|
||||||
|
handler,
|
||||||
|
&Options {
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.context("failed to process file")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
println!("{}", output.code);
|
||||||
|
}
|
36
crates/swc/examples/transform_error.rs
Normal file
36
crates/swc/examples/transform_error.rs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
|
use swc::{self, config::Options, try_with_handler};
|
||||||
|
use swc_common::{errors::ColorConfig, FileName, SourceMap};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let cm = Arc::<SourceMap>::default();
|
||||||
|
|
||||||
|
let c = swc::Compiler::new(cm.clone());
|
||||||
|
|
||||||
|
let output = try_with_handler(
|
||||||
|
cm.clone(),
|
||||||
|
swc::HandlerOpts {
|
||||||
|
// Auto is default, but for it's an example.
|
||||||
|
// You can use the env var named `NO_COLOR` to control this.
|
||||||
|
color: ColorConfig::Auto,
|
||||||
|
skip_filename: false,
|
||||||
|
},
|
||||||
|
|handler| {
|
||||||
|
let fm = cm.new_source_file(FileName::Custom("foo.js".into()), "this ?= foo".into());
|
||||||
|
|
||||||
|
c.process_js_file(
|
||||||
|
fm,
|
||||||
|
handler,
|
||||||
|
&Options {
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.context("failed to process file")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.expect("anyhow will give you good error message");
|
||||||
|
|
||||||
|
println!("{}", output.code);
|
||||||
|
}
|
@ -1,31 +0,0 @@
|
|||||||
use std::{path::Path, sync::Arc};
|
|
||||||
|
|
||||||
use swc::{self, config::Options};
|
|
||||||
use swc_common::{
|
|
||||||
errors::{ColorConfig, Handler},
|
|
||||||
SourceMap,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let cm = Arc::<SourceMap>::default();
|
|
||||||
let handler = Arc::new(Handler::with_tty_emitter(
|
|
||||||
ColorConfig::Auto,
|
|
||||||
true,
|
|
||||||
false,
|
|
||||||
Some(cm.clone()),
|
|
||||||
));
|
|
||||||
let c = swc::Compiler::new(cm.clone());
|
|
||||||
|
|
||||||
let fm = cm
|
|
||||||
.load_file(Path::new("foo.js"))
|
|
||||||
.expect("failed to load file");
|
|
||||||
|
|
||||||
c.process_js_file(
|
|
||||||
fm,
|
|
||||||
&handler,
|
|
||||||
&Options {
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.expect("failed to process file");
|
|
||||||
}
|
|
5
crates/swc_ecma_lints/examples/all.js
Normal file
5
crates/swc_ecma_lints/examples/all.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
|
||||||
|
const a = 1;
|
||||||
|
|
||||||
|
a = 2;
|
53
crates/swc_ecma_lints/examples/all.rs
Normal file
53
crates/swc_ecma_lints/examples/all.rs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
use swc_common::{errors::HANDLER, Mark, SyntaxContext};
|
||||||
|
use swc_ecma_ast::*;
|
||||||
|
use swc_ecma_lints::{rule::Rule, rules::LintParams};
|
||||||
|
use swc_ecma_parser::Syntax;
|
||||||
|
use swc_ecma_transforms_base::resolver::resolver_with_mark;
|
||||||
|
use swc_ecma_visit::VisitMutWith;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// testing::run_test creates Lrc<SourceMap> and `&Handler` for you
|
||||||
|
// You can refer to source code of it or try_with_handler of the swc crate.
|
||||||
|
|
||||||
|
let msg = testing::run_test(false, |cm, handler| -> Result<(), _> {
|
||||||
|
HANDLER.set(handler, || {
|
||||||
|
let fm = cm.load_file("examples/all.js".as_ref()).unwrap();
|
||||||
|
|
||||||
|
let module = swc_ecma_parser::parse_file_as_module(
|
||||||
|
&fm,
|
||||||
|
Syntax::default(),
|
||||||
|
EsVersion::latest(),
|
||||||
|
None,
|
||||||
|
&mut vec![],
|
||||||
|
);
|
||||||
|
let mut program = match module {
|
||||||
|
Ok(v) => Program::Module(v),
|
||||||
|
Err(err) => {
|
||||||
|
err.into_diagnostic(handler).emit();
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let top_level_mark = Mark::fresh(Mark::root());
|
||||||
|
let top_level_ctxt = SyntaxContext::empty().apply_mark(top_level_mark);
|
||||||
|
|
||||||
|
program.visit_mut_with(&mut resolver_with_mark(top_level_mark));
|
||||||
|
|
||||||
|
let mut rules = swc_ecma_lints::rules::all(LintParams {
|
||||||
|
program: &program,
|
||||||
|
lint_config: &Default::default(),
|
||||||
|
top_level_ctxt,
|
||||||
|
es_version: EsVersion::latest(),
|
||||||
|
source_map: cm.clone(),
|
||||||
|
});
|
||||||
|
|
||||||
|
let module = program.expect_module();
|
||||||
|
rules.lint_module(&module);
|
||||||
|
|
||||||
|
Err(())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.unwrap_err();
|
||||||
|
|
||||||
|
println!("{}", msg);
|
||||||
|
}
|
@ -3,6 +3,7 @@ authors = ["강동윤 <kdy1997.dev@gmail.com>"]
|
|||||||
description = "rust port of babel and closure compiler."
|
description = "rust port of babel and closure compiler."
|
||||||
documentation = "https://rustdoc.swc.rs/swc_ecma_transforms_compat/"
|
documentation = "https://rustdoc.swc.rs/swc_ecma_transforms_compat/"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
include = ["Cargo.toml", "src/**/*.rs"]
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
name = "swc_ecma_transforms_compat"
|
name = "swc_ecma_transforms_compat"
|
||||||
repository = "https://github.com/swc-project/swc.git"
|
repository = "https://github.com/swc-project/swc.git"
|
||||||
|
@ -3,6 +3,7 @@ authors = ["강동윤 <kdy1997.dev@gmail.com>"]
|
|||||||
description = "rust port of babel and closure compiler."
|
description = "rust port of babel and closure compiler."
|
||||||
documentation = "https://rustdoc.swc.rs/swc_ecma_transforms_optimization/"
|
documentation = "https://rustdoc.swc.rs/swc_ecma_transforms_optimization/"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
include = ["Cargo.toml", "src/**/*.rs"]
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
name = "swc_ecma_transforms_module"
|
name = "swc_ecma_transforms_module"
|
||||||
repository = "https://github.com/swc-project/swc.git"
|
repository = "https://github.com/swc-project/swc.git"
|
||||||
|
@ -3,6 +3,7 @@ authors = ["강동윤 <kdy1997.dev@gmail.com>"]
|
|||||||
description = "rust port of babel and closure compiler."
|
description = "rust port of babel and closure compiler."
|
||||||
documentation = "https://rustdoc.swc.rs/swc_ecma_transforms_optimization/"
|
documentation = "https://rustdoc.swc.rs/swc_ecma_transforms_optimization/"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
include = ["Cargo.toml", "src/**/*.rs"]
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
name = "swc_ecma_transforms_optimization"
|
name = "swc_ecma_transforms_optimization"
|
||||||
repository = "https://github.com/swc-project/swc.git"
|
repository = "https://github.com/swc-project/swc.git"
|
||||||
|
@ -3,6 +3,7 @@ authors = ["강동윤 <kdy1997.dev@gmail.com>"]
|
|||||||
description = "rust port of babel and closure compiler."
|
description = "rust port of babel and closure compiler."
|
||||||
documentation = "https://rustdoc.swc.rs/swc_ecma_transforms_proposal/"
|
documentation = "https://rustdoc.swc.rs/swc_ecma_transforms_proposal/"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
include = ["Cargo.toml", "src/**/*.rs"]
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
name = "swc_ecma_transforms_proposal"
|
name = "swc_ecma_transforms_proposal"
|
||||||
repository = "https://github.com/swc-project/swc.git"
|
repository = "https://github.com/swc-project/swc.git"
|
||||||
|
@ -3,6 +3,7 @@ authors = ["강동윤 <kdy1997.dev@gmail.com>"]
|
|||||||
description = "rust port of babel and closure compiler."
|
description = "rust port of babel and closure compiler."
|
||||||
documentation = "https://rustdoc.swc.rs/swc_ecma_transforms_typescript/"
|
documentation = "https://rustdoc.swc.rs/swc_ecma_transforms_typescript/"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
include = ["Cargo.toml", "src/**/*.rs"]
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
name = "swc_ecma_transforms_typescript"
|
name = "swc_ecma_transforms_typescript"
|
||||||
repository = "https://github.com/swc-project/swc.git"
|
repository = "https://github.com/swc-project/swc.git"
|
||||||
|
65
crates/swc_error_reporters/examples/swc_try.rs
Normal file
65
crates/swc_error_reporters/examples/swc_try.rs
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
//! This is actual code used by swc.
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
fmt,
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
};
|
||||||
|
|
||||||
|
use swc_common::{errors::Handler, sync::Lrc, BytePos, FileName, SourceFile, SourceMap, Span};
|
||||||
|
use swc_error_reporters::{GraphicalReportHandler, PrettyEmitter, PrettyEmitterConfig};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let cm = Lrc::<SourceMap>::default();
|
||||||
|
|
||||||
|
let wr = Box::new(LockedWriter::default());
|
||||||
|
|
||||||
|
let emitter = PrettyEmitter::new(
|
||||||
|
cm.clone(),
|
||||||
|
wr.clone(),
|
||||||
|
GraphicalReportHandler::new(),
|
||||||
|
PrettyEmitterConfig {
|
||||||
|
skip_filename: false,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
// let e_wr = EmitterWriter::new(wr.clone(), Some(cm), false,
|
||||||
|
// true).skip_filename(skip_filename);
|
||||||
|
let handler = Handler::with_emitter(true, false, Box::new(emitter));
|
||||||
|
|
||||||
|
let fm1 = cm.new_source_file(FileName::Custom("foo.js".into()), "13579".into());
|
||||||
|
let fm2 = cm.new_source_file(FileName::Custom("bar.js".into()), "02468".into());
|
||||||
|
|
||||||
|
// This is a simple example.
|
||||||
|
handler
|
||||||
|
.struct_span_err(span(&fm1, 0, 3), "simple message")
|
||||||
|
.emit();
|
||||||
|
|
||||||
|
// We can show other file.
|
||||||
|
// This can be used to show configurable error with the config.
|
||||||
|
|
||||||
|
handler
|
||||||
|
.struct_span_err(span(&fm1, 0, 3), "constraint violation")
|
||||||
|
.span_note(span(&fm2, 0, 1), "this is your config")
|
||||||
|
.emit();
|
||||||
|
|
||||||
|
let s = &**wr.0.lock().unwrap();
|
||||||
|
|
||||||
|
println!("{}", s);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Don't do this in your real app. You should use [Span] created by parser
|
||||||
|
fn span(base: &SourceFile, lo: u32, hi: u32) -> Span {
|
||||||
|
let lo = base.start_pos.0 + lo;
|
||||||
|
let hi = base.start_pos.0 + hi;
|
||||||
|
|
||||||
|
Span::new(BytePos(lo), BytePos(hi), Default::default())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Default)]
|
||||||
|
struct LockedWriter(Arc<Mutex<String>>);
|
||||||
|
|
||||||
|
impl fmt::Write for LockedWriter {
|
||||||
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||||
|
self.0.lock().unwrap().push_str(s);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user