diff --git a/crates/swc/examples/minify.rs b/crates/swc/examples/minify.rs index cbd0bae801b0..83ff5ebb93e9 100644 --- a/crates/swc/examples/minify.rs +++ b/crates/swc/examples/minify.rs @@ -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::::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); } diff --git a/crates/swc/examples/transform.rs b/crates/swc/examples/transform.rs index 0e40cccedf32..d45679784069 100644 --- a/crates/swc/examples/transform.rs +++ b/crates/swc/examples/transform.rs @@ -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::::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); } diff --git a/crates/swc/examples/transform_error.rs b/crates/swc/examples/transform_error.rs index 45ec5b7e2f38..05bcc3427c7d 100644 --- a/crates/swc/examples/transform_error.rs +++ b/crates/swc/examples/transform_error.rs @@ -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::::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); }