Skip to content

Commit

Permalink
doc(es): Fix example (#6743)
Browse files Browse the repository at this point in the history
  • Loading branch information
RiESAEX committed Jan 3, 2023
1 parent 3047179 commit 79afcb5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 73 deletions.
66 changes: 34 additions & 32 deletions crates/swc/examples/minify.rs
Expand Up @@ -2,46 +2,48 @@ use std::{path::Path, sync::Arc};

use anyhow::Context;
use swc::{self, config::JsMinifyOptions, try_with_handler, BoolOrDataConfig, HandlerOpts};
use swc_common::SourceMap;
use swc_common::{SourceMap, GLOBALS};

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

let c = swc::Compiler::new(cm.clone());
let output = GLOBALS
.set(&Default::default(), || {
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");

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: BoolOrDataConfig::from_bool(true),
mangle: BoolOrDataConfig::from_bool(true),
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(),
emit_source_map_columns: Default::default(),
c.minify(
fm,
handler,
&JsMinifyOptions {
compress: BoolOrDataConfig::from_bool(true),
mangle: BoolOrDataConfig::from_bool(true),
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(),
emit_source_map_columns: Default::default(),
},
)
.context("failed to minify")
},
)
.context("failed to minify")
},
)
.unwrap();
})
.unwrap();

println!("{}", output.code);
}
42 changes: 22 additions & 20 deletions crates/swc/examples/transform.rs
Expand Up @@ -2,34 +2,36 @@ use std::{path::Path, sync::Arc};

use anyhow::Context;
use swc::{self, config::Options, try_with_handler, HandlerOpts};
use swc_common::SourceMap;
use swc_common::{SourceMap, GLOBALS};

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 {
let output = GLOBALS
.set(&Default::default(), || {
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")
},
)
.context("failed to process file")
},
)
.unwrap();
})
.unwrap();

println!("{}", output.code);
}
45 changes: 24 additions & 21 deletions crates/swc/examples/transform_error.rs
Expand Up @@ -2,35 +2,38 @@ use std::sync::Arc;

use anyhow::Context;
use swc::{self, config::Options, try_with_handler};
use swc_common::{errors::ColorConfig, FileName, SourceMap};
use swc_common::{errors::ColorConfig, FileName, SourceMap, GLOBALS};

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

let c = swc::Compiler::new(cm.clone());
let output = GLOBALS
.set(&Default::default(), || {
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());

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()
c.process_js_file(
fm,
handler,
&Options {
..Default::default()
},
)
.context("failed to process file")
},
)
.context("failed to process file")
},
)
.expect("anyhow will give you good error message");
})
.expect("anyhow will give you good error message");

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

0 comments on commit 79afcb5

Please sign in to comment.