Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(swc/cli): implements few compile flags #4125

Merged
merged 2 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/swc_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ tracing-subscriber = { version = "0.3.9", features = ["env-filter"] }
walkdir = "2"
wasmer = { version = "2.2.1", optional = true }
wasmer-wasi = { version = "2.2.1", optional = true }
glob = "0.3.0"

[dependencies.path-absolutize]
features = ["once_cell_cache"]
Expand Down
108 changes: 61 additions & 47 deletions crates/swc_cli/src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{

use anyhow::Context;
use clap::Parser;
use glob::glob;
use path_absolutize::Absolutize;
use rayon::prelude::*;
use relative_path::RelativePath;
Expand Down Expand Up @@ -116,6 +117,7 @@ static DEFAULT_EXTENSIONS: &[&str] = &["js", "jsx", "es6", "es", "mjs", "ts", "t
fn get_files_list(
raw_files_input: &[PathBuf],
extensions: &[String],
ignore_pattern: Option<&str>,
_include_dotfiles: bool,
) -> anyhow::Result<Vec<PathBuf>> {
let input_dir = raw_files_input.iter().find(|p| p.is_dir());
Expand All @@ -141,36 +143,16 @@ fn get_files_list(
raw_files_input.to_owned()
};

Ok(files)
}

fn build_transform_options(
config_file: &Option<PathBuf>,
file_path: &Option<&Path>,
) -> anyhow::Result<Options> {
let base_options = Options::default();
let base_config = Config::default();

let config_file = if let Some(config_file_path) = config_file {
let config_file_contents = fs::read(config_file_path)?;
serde_json::from_slice(&config_file_contents)
.map_err(|e| anyhow::anyhow!("{}", e))
.context("Failed to parse config file")?
} else {
None
};

let mut ret = Options {
config: Config { ..base_config },
config_file,
..base_options
};
if let Some(ignore_pattern) = ignore_pattern {
let pattern: Vec<PathBuf> = glob(ignore_pattern)?.filter_map(|p| p.ok()).collect();

if let Some(file_path) = file_path {
ret.filename = file_path.to_str().unwrap_or_default().to_owned();
return Ok(files
.into_iter()
.filter(|file_path| !pattern.iter().any(|p| p.eq(file_path)))
.collect());
}

Ok(ret)
Ok(files)
}

/// Calculate full, absolute path to the file to emit.
Expand Down Expand Up @@ -271,6 +253,34 @@ struct InputContext {

#[swc_trace]
impl CompileOptions {
fn build_transform_options(&self, file_path: &Option<&Path>) -> anyhow::Result<Options> {
let base_options = Options::default();
let base_config = Config::default();

let config_file = if let Some(config_file_path) = &self.config_file {
let config_file_contents = fs::read(config_file_path)?;
serde_json::from_slice(&config_file_contents).context("Failed to parse config file")?
} else {
None
};

let mut ret = Options {
config: Config { ..base_config },
config_file,
..base_options
};

if let Some(file_path) = *file_path {
ret.filename = file_path.to_str().unwrap_or_default().to_owned();
}

if let Some(env_name) = &self.env_name {
ret.env_name = env_name.to_string();
}

Ok(ret)
}

/// Create canonical list of inputs to be processed across stdin / single
/// file / multiple files.
fn collect_inputs(&self) -> anyhow::Result<Vec<InputContext>> {
Expand All @@ -282,7 +292,7 @@ impl CompileOptions {
}

if let Some(stdin_input) = stdin_input {
let options = build_transform_options(&self.config_file, &self.filename.as_deref())?;
let options = self.build_transform_options(&self.filename.as_deref())?;

let fm = compiler.cm.new_source_file(
if options.filename.is_empty() {
Expand All @@ -309,25 +319,29 @@ impl CompileOptions {
DEFAULT_EXTENSIONS.iter().map(|v| v.to_string()).collect()
};

return get_files_list(&self.files, &included_extensions, false)?
.iter()
.map(|file_path| {
build_transform_options(&self.config_file, &Some(file_path)).and_then(
|options| {
let fm = compiler
.cm
.load_file(file_path)
.context("failed to load file");
fm.map(|fm| InputContext {
options,
fm,
compiler: compiler.clone(),
file_path: file_path.to_path_buf(),
})
},
)
})
.collect::<anyhow::Result<Vec<InputContext>>>();
return get_files_list(
&self.files,
&included_extensions,
self.ignore.as_deref(),
false,
)?
.iter()
.map(|file_path| {
self.build_transform_options(&Some(file_path))
.and_then(|options| {
let fm = compiler
.cm
.load_file(file_path)
.context("failed to load file");
fm.map(|fm| InputContext {
options,
fm,
compiler: compiler.clone(),
file_path: file_path.to_path_buf(),
})
})
})
.collect::<anyhow::Result<Vec<InputContext>>>();
}

anyhow::bail!("Input is empty");
Expand Down