Skip to content

Commit

Permalink
Options.plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Nov 29, 2019
1 parent 55b473b commit cc3623a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
8 changes: 6 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct ParseOptions {
pub syntax: Syntax,
}

#[derive(Default, Clone, Serialize, Deserialize)]
#[derive(Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct Options {
#[serde(flatten, default)]
Expand Down Expand Up @@ -79,6 +79,9 @@ pub struct Options {

#[serde(default)]
pub source_root: Option<String>,

#[serde(skip)]
pub hook: Option<Box<dyn Pass + 'static>>,
}

#[derive(Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -109,7 +112,7 @@ impl Default for InputSourceMap {

impl Options {
pub fn build(
&self,
self,
cm: &Arc<SourceMap>,
handler: &Handler,
config: Option<Config>,
Expand Down Expand Up @@ -149,6 +152,7 @@ impl Options {

let pass = chain_at!(
Module,
self.hook.unwrap_or_else(|| box noop()),
// handle jsx
Optional::new(react::react(cm.clone(), transform.react), syntax.jsx()),
Optional::new(typescript::strip(), syntax.typescript()),
Expand Down
22 changes: 8 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Compiler {
where
F: FnOnce() -> R,
{
GLOBALS.set(&self.globals, op)
GLOBALS.set(&self.globals, || common::CM.set(&self.cm, || op()))
}

/// This method parses a javascript / typescript file
Expand Down Expand Up @@ -165,21 +165,15 @@ impl Compiler {
/// This method handles merging of config.
pub fn config_for_file(
&self,
opts: &Options,
opts: Options,
fm: &SourceFile,
) -> Result<BuiltConfig<impl Pass>, Error> {
let Options {
ref root,
root_mode,
swcrc,
config_file,
..
} = opts;
let root = root
let root = opts
.root
.clone()
.unwrap_or_else(|| ::std::env::current_dir().unwrap());

let config_file = match config_file {
let config_file = match opts.config_file {
Some(ConfigFile::Str(ref s)) => {
let path = Path::new(s);
let r = File::open(&path).map_err(|err| Error::FailedToReadConfigFile { err })?;
Expand All @@ -192,7 +186,7 @@ impl Compiler {

match fm.name {
FileName::Real(ref path) => {
if *swcrc {
if opts.swcrc {
let mut parent = path.parent();
while let Some(dir) = parent {
let swcrc = dir.join(".swcrc");
Expand All @@ -210,7 +204,7 @@ impl Compiler {
return Ok(built);
}

if dir == root && *root_mode == RootMode::Root {
if dir == root && opts.root_mode == RootMode::Root {
break;
}
parent = dir.parent();
Expand Down Expand Up @@ -245,7 +239,7 @@ impl Compiler {
fm: Arc<SourceFile>,
opts: Options,
) -> Result<TransformOutput, Error> {
let config = self.run(|| self.config_for_file(&opts, &*fm))?;
let config = self.run(|| self.config_for_file(opts, &*fm))?;

self.process_js(fm, config)
}
Expand Down

0 comments on commit cc3623a

Please sign in to comment.