Skip to content

Commit

Permalink
Revert "Fix"
Browse files Browse the repository at this point in the history
This reverts commit 112589e.
  • Loading branch information
timneutkens committed Sep 28, 2022
1 parent cafb5ff commit 58d4736
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 129 deletions.
24 changes: 11 additions & 13 deletions packages/next-swc/crates/napi/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use serde::Deserialize;
use std::sync::Arc;
use swc_core::{
base::{config::JsMinifyOptions, try_with_handler, TransformOutput},
common::{errors::ColorConfig, sync::Lrc, FileName, SourceFile, SourceMap, GLOBALS},
common::{errors::ColorConfig, sync::Lrc, FileName, SourceFile, SourceMap},
};

struct MinifyTask {
Expand Down Expand Up @@ -85,17 +85,15 @@ impl Task for MinifyTask {
skip_filename: true,
},
|handler| {
GLOBALS.set(&Default::default(), || {
let fm = self.code.to_file(self.c.cm.clone());

self.c.minify(
fm,
handler,
&JsMinifyOptions {
..self.opts.clone()
},
)
})
let fm = self.code.to_file(self.c.cm.clone());

self.c.minify(
fm,
handler,
&JsMinifyOptions {
..self.opts.clone()
},
)
},
)
.convert_err()
Expand Down Expand Up @@ -133,7 +131,7 @@ pub fn minify_sync(cx: CallContext) -> napi::Result<JsObject> {
color: ColorConfig::Never,
skip_filename: true,
},
|handler| GLOBALS.set(&Default::default(), || c.minify(fm, handler, &opts)),
|handler| c.minify(fm, handler, &opts),
)
.convert_err()?;

Expand Down
71 changes: 33 additions & 38 deletions packages/next-swc/crates/napi/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use napi::{CallContext, Either, Env, JsObject, JsString, JsUndefined, Task};
use std::sync::Arc;
use swc_core::{
base::{config::ParseOptions, try_with_handler},
common::{
comments::Comments, errors::ColorConfig, FileName, FilePathMapping, SourceMap, GLOBALS,
},
common::{comments::Comments, errors::ColorConfig, FileName, FilePathMapping, SourceMap},
};

pub struct ParseTask {
Expand All @@ -24,44 +22,41 @@ impl Task for ParseTask {
type JsValue = JsString;

fn compute(&mut self) -> napi::Result<Self::Output> {
GLOBALS.set(&Default::default(), || {
let c =
swc_core::base::Compiler::new(Arc::new(SourceMap::new(FilePathMapping::empty())));
let c = swc_core::base::Compiler::new(Arc::new(SourceMap::new(FilePathMapping::empty())));

let options: ParseOptions = deserialize_json(&self.options).convert_err()?;
let comments = c.comments().clone();
let comments: Option<&dyn Comments> = if options.comments {
Some(&comments)
} else {
None
};
let fm =
c.cm.new_source_file(self.filename.clone(), self.src.clone());
let program = try_with_handler(
c.cm.clone(),
swc_core::base::HandlerOpts {
color: ColorConfig::Never,
skip_filename: false,
},
|handler| {
c.parse_js(
fm,
handler,
options.target,
options.syntax,
options.is_module,
comments,
)
},
)
.convert_err()?;
let options: ParseOptions = deserialize_json(&self.options).convert_err()?;
let comments = c.comments().clone();
let comments: Option<&dyn Comments> = if options.comments {
Some(&comments)
} else {
None
};
let fm =
c.cm.new_source_file(self.filename.clone(), self.src.clone());
let program = try_with_handler(
c.cm.clone(),
swc_core::base::HandlerOpts {
color: ColorConfig::Never,
skip_filename: false,
},
|handler| {
c.parse_js(
fm,
handler,
options.target,
options.syntax,
options.is_module,
comments,
)
},
)
.convert_err()?;

let ast_json = serde_json::to_string(&program)
.context("failed to serialize Program")
.convert_err()?;
let ast_json = serde_json::to_string(&program)
.context("failed to serialize Program")
.convert_err()?;

Ok(ast_json)
})
Ok(ast_json)
}

fn resolve(self, env: Env, result: Self::Output) -> napi::Result<Self::JsValue> {
Expand Down
154 changes: 76 additions & 78 deletions packages/next-swc/crates/napi/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use std::{
};
use swc_core::{
base::{try_with_handler, Compiler, TransformOutput},
common::{errors::ColorConfig, FileName, GLOBALS},
common::{errors::ColorConfig, FileName},
ecma::transforms::base::pass::noop,
};

Expand All @@ -68,85 +68,83 @@ impl Task for TransformTask {
type JsValue = JsObject;

fn compute(&mut self) -> napi::Result<Self::Output> {
GLOBALS.set(&Default::default(), || {
let eliminated_packages: Rc<RefCell<fxhash::FxHashSet<String>>> = Default::default();
let res = catch_unwind(AssertUnwindSafe(|| {
try_with_handler(
self.c.cm.clone(),
swc_core::base::HandlerOpts {
color: ColorConfig::Never,
skip_filename: true,
},
|handler| {
self.c.run(|| {
let options: TransformOptions = deserialize_json(&self.options)?;
let fm = match &self.input {
Input::Source { src } => {
let filename = if options.swc.filename.is_empty() {
FileName::Anon
} else {
FileName::Real(options.swc.filename.clone().into())
};

self.c.cm.new_source_file(filename, src.to_string())
let eliminated_packages: Rc<RefCell<fxhash::FxHashSet<String>>> = Default::default();
let res = catch_unwind(AssertUnwindSafe(|| {
try_with_handler(
self.c.cm.clone(),
swc_core::base::HandlerOpts {
color: ColorConfig::Never,
skip_filename: true,
},
|handler| {
self.c.run(|| {
let options: TransformOptions = deserialize_json(&self.options)?;
let fm = match &self.input {
Input::Source { src } => {
let filename = if options.swc.filename.is_empty() {
FileName::Anon
} else {
FileName::Real(options.swc.filename.clone().into())
};

self.c.cm.new_source_file(filename, src.to_string())
}
Input::FromFilename => {
let filename = &options.swc.filename;
if filename.is_empty() {
bail!("no filename is provided via options");
}
Input::FromFilename => {
let filename = &options.swc.filename;
if filename.is_empty() {
bail!("no filename is provided via options");
}

self.c.cm.new_source_file(
FileName::Real(filename.into()),
read_to_string(filename).with_context(|| {
format!("Failed to read source code from {}", filename)
})?,
)
}
};
let options = options.patch(&fm);

let cm = self.c.cm.clone();
let file = fm.clone();

self.c.process_js_with_custom_pass(
fm,
None,
handler,
&options.swc,
|_, comments| {
custom_before_pass(
cm,
file,
&options,
comments.clone(),
eliminated_packages.clone(),
)
},
|_, _| noop(),
)
})
},
)
}))
.map_err(|err| {
if let Some(s) = err.downcast_ref::<String>() {
anyhow!("failed to process {}", s)
} else {
anyhow!("failed to process")
}
});

match res {
Ok(res) => res
.map(|o| (o, eliminated_packages.replace(Default::default())))
.convert_err(),
Err(err) => Err(napi::Error::new(
Status::GenericFailure,
format!("{:?}", err),
)),

self.c.cm.new_source_file(
FileName::Real(filename.into()),
read_to_string(filename).with_context(|| {
format!("Failed to read source code from {}", filename)
})?,
)
}
};
let options = options.patch(&fm);

let cm = self.c.cm.clone();
let file = fm.clone();

self.c.process_js_with_custom_pass(
fm,
None,
handler,
&options.swc,
|_, comments| {
custom_before_pass(
cm,
file,
&options,
comments.clone(),
eliminated_packages.clone(),
)
},
|_, _| noop(),
)
})
},
)
}))
.map_err(|err| {
if let Some(s) = err.downcast_ref::<String>() {
anyhow!("failed to process {}", s)
} else {
anyhow!("failed to process")
}
})
});

match res {
Ok(res) => res
.map(|o| (o, eliminated_packages.replace(Default::default())))
.convert_err(),
Err(err) => Err(napi::Error::new(
Status::GenericFailure,
format!("{:?}", err),
)),
}
}

fn resolve(
Expand Down

0 comments on commit 58d4736

Please sign in to comment.