Skip to content

Commit 79afcb5

Browse files
authoredJan 3, 2023
doc(es): Fix example (#6743)
1 parent 3047179 commit 79afcb5

File tree

3 files changed

+80
-73
lines changed

3 files changed

+80
-73
lines changed
 

‎crates/swc/examples/minify.rs

+34-32
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,48 @@ use std::{path::Path, sync::Arc};
22

33
use anyhow::Context;
44
use swc::{self, config::JsMinifyOptions, try_with_handler, BoolOrDataConfig, HandlerOpts};
5-
use swc_common::SourceMap;
5+
use swc_common::{SourceMap, GLOBALS};
66

77
fn main() {
88
let cm = Arc::<SourceMap>::default();
99

1010
let c = swc::Compiler::new(cm.clone());
11+
let output = GLOBALS
12+
.set(&Default::default(), || {
13+
try_with_handler(
14+
cm.clone(),
15+
HandlerOpts {
16+
..Default::default()
17+
},
18+
|handler| {
19+
let fm = cm
20+
.load_file(Path::new("examples/transform-input.js"))
21+
.expect("failed to load file");
1122

12-
let output = try_with_handler(
13-
cm.clone(),
14-
HandlerOpts {
15-
..Default::default()
16-
},
17-
|handler| {
18-
let fm = cm
19-
.load_file(Path::new("examples/minify-input.js"))
20-
.expect("failed to load file");
21-
22-
c.minify(
23-
fm,
24-
handler,
25-
&JsMinifyOptions {
26-
compress: BoolOrDataConfig::from_bool(true),
27-
mangle: BoolOrDataConfig::from_bool(true),
28-
format: Default::default(),
29-
ecma: Default::default(),
30-
keep_classnames: Default::default(),
31-
keep_fnames: Default::default(),
32-
module: Default::default(),
33-
safari10: Default::default(),
34-
toplevel: Default::default(),
35-
source_map: Default::default(),
36-
output_path: Default::default(),
37-
inline_sources_content: Default::default(),
38-
emit_source_map_columns: Default::default(),
23+
c.minify(
24+
fm,
25+
handler,
26+
&JsMinifyOptions {
27+
compress: BoolOrDataConfig::from_bool(true),
28+
mangle: BoolOrDataConfig::from_bool(true),
29+
format: Default::default(),
30+
ecma: Default::default(),
31+
keep_classnames: Default::default(),
32+
keep_fnames: Default::default(),
33+
module: Default::default(),
34+
safari10: Default::default(),
35+
toplevel: Default::default(),
36+
source_map: Default::default(),
37+
output_path: Default::default(),
38+
inline_sources_content: Default::default(),
39+
emit_source_map_columns: Default::default(),
40+
},
41+
)
42+
.context("failed to minify")
3943
},
4044
)
41-
.context("failed to minify")
42-
},
43-
)
44-
.unwrap();
45+
})
46+
.unwrap();
4547

4648
println!("{}", output.code);
4749
}

‎crates/swc/examples/transform.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,36 @@ use std::{path::Path, sync::Arc};
22

33
use anyhow::Context;
44
use swc::{self, config::Options, try_with_handler, HandlerOpts};
5-
use swc_common::SourceMap;
5+
use swc_common::{SourceMap, GLOBALS};
66

77
fn main() {
88
let cm = Arc::<SourceMap>::default();
99

1010
let c = swc::Compiler::new(cm.clone());
11-
12-
let output = try_with_handler(
13-
cm.clone(),
14-
HandlerOpts {
15-
..Default::default()
16-
},
17-
|handler| {
18-
let fm = cm
19-
.load_file(Path::new("examples/transform-input.js"))
20-
.expect("failed to load file");
21-
22-
c.process_js_file(
23-
fm,
24-
handler,
25-
&Options {
11+
let output = GLOBALS
12+
.set(&Default::default(), || {
13+
try_with_handler(
14+
cm.clone(),
15+
HandlerOpts {
2616
..Default::default()
2717
},
18+
|handler| {
19+
let fm = cm
20+
.load_file(Path::new("examples/transform-input.js"))
21+
.expect("failed to load file");
22+
23+
c.process_js_file(
24+
fm,
25+
handler,
26+
&Options {
27+
..Default::default()
28+
},
29+
)
30+
.context("failed to process file")
31+
},
2832
)
29-
.context("failed to process file")
30-
},
31-
)
32-
.unwrap();
33+
})
34+
.unwrap();
3335

3436
println!("{}", output.code);
3537
}

‎crates/swc/examples/transform_error.rs

+24-21
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,38 @@ use std::sync::Arc;
22

33
use anyhow::Context;
44
use swc::{self, config::Options, try_with_handler};
5-
use swc_common::{errors::ColorConfig, FileName, SourceMap};
5+
use swc_common::{errors::ColorConfig, FileName, SourceMap, GLOBALS};
66

77
fn main() {
88
let cm = Arc::<SourceMap>::default();
99

1010
let c = swc::Compiler::new(cm.clone());
11+
let output = GLOBALS
12+
.set(&Default::default(), || {
13+
try_with_handler(
14+
cm.clone(),
15+
swc::HandlerOpts {
16+
// Auto is default, but for it's an example.
17+
// You can use the env var named `NO_COLOR` to control this.
18+
color: ColorConfig::Auto,
19+
skip_filename: false,
20+
},
21+
|handler| {
22+
let fm =
23+
cm.new_source_file(FileName::Custom("foo.js".into()), "this ?= foo".into());
1124

12-
let output = try_with_handler(
13-
cm.clone(),
14-
swc::HandlerOpts {
15-
// Auto is default, but for it's an example.
16-
// You can use the env var named `NO_COLOR` to control this.
17-
color: ColorConfig::Auto,
18-
skip_filename: false,
19-
},
20-
|handler| {
21-
let fm = cm.new_source_file(FileName::Custom("foo.js".into()), "this ?= foo".into());
22-
23-
c.process_js_file(
24-
fm,
25-
handler,
26-
&Options {
27-
..Default::default()
25+
c.process_js_file(
26+
fm,
27+
handler,
28+
&Options {
29+
..Default::default()
30+
},
31+
)
32+
.context("failed to process file")
2833
},
2934
)
30-
.context("failed to process file")
31-
},
32-
)
33-
.expect("anyhow will give you good error message");
35+
})
36+
.expect("anyhow will give you good error message");
3437

3538
println!("{}", output.code);
3639
}

0 commit comments

Comments
 (0)
Please sign in to comment.