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(bindings/cli): Implement --config #6835

Merged
merged 3 commits into from Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 30 additions & 4 deletions bindings/swc_cli/src/commands/compile.rs
@@ -1,7 +1,7 @@
use std::{
fs::{self, File},
io::{self, Read, Write},
path::{Path, PathBuf},
path::{Component, Path, PathBuf},
sync::Arc,
};

Expand All @@ -13,7 +13,7 @@ use rayon::prelude::*;
use relative_path::RelativePath;
use swc_core::{
base::{
config::{ConfigFile, Options, SourceMapsConfig},
config::{Config, ConfigFile, Options, PluginConfig, SourceMapsConfig},
try_with_handler, Compiler, HandlerOpts, TransformOutput,
},
common::{
Expand All @@ -29,8 +29,8 @@ use crate::util::trace::init_trace;
#[derive(Parser)]
pub struct CompileOptions {
/// Override a config from .swcrc file.
#[clap(long)]
config: Option<Vec<String>>,
#[clap(long, value_parser = parse_config)]
config: Option<Config>,

/// Path to a .swcrc file to use
#[clap(long)]
Expand Down Expand Up @@ -107,6 +107,10 @@ pub struct CompileOptions {
*no_swcrc: bool, */
}

fn parse_config(s: &str) -> Result<Config, serde_json::Error> {
serde_json::from_str(s)
}

static COMPILER: Lazy<Arc<Compiler>> = Lazy::new(|| {
let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));

Expand Down Expand Up @@ -271,10 +275,32 @@ impl CompileOptions {
});

let mut options = Options {
config: self.config.to_owned().unwrap_or_default(),
config_file,
..Options::default()
};

options.config.jsc.experimental.plugins =
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
options.config.jsc.experimental.plugins.map(|plugins| {
plugins
.into_iter()
.map(|p| {
// if the path starts with . or .., then turn it into an absolute path using
// the current working directory as the base
let path = Path::new(&p.0);
PluginConfig(
match path.components().next() {
Some(Component::CurDir) | Some(Component::ParentDir) => {
path.absolutize().unwrap().display().to_string()
}
_ => p.0,
},
p.1,
)
})
.collect()
});

if let Some(file_path) = *file_path {
options.filename = file_path.to_str().unwrap_or_default().to_owned();
}
Expand Down
4 changes: 2 additions & 2 deletions crates/swc/src/config/mod.rs
Expand Up @@ -70,9 +70,9 @@ use swc_ecma_transforms_compat::es2015::regenerator;
use swc_ecma_transforms_optimization::{inline_globals2, GlobalExprMap};
use swc_ecma_visit::{Fold, VisitMutWith};

pub use crate::plugin::PluginConfig;
use crate::{
builder::PassBuilder, dropped_comments_preserver::dropped_comments_preserver,
plugin::PluginConfig, SwcImportResolver,
builder::PassBuilder, dropped_comments_preserver::dropped_comments_preserver, SwcImportResolver,
};

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/src/plugin.rs
Expand Up @@ -25,7 +25,7 @@ use swc_ecma_visit::{noop_fold_type, Fold};
/// plugin's entrypoint function.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct PluginConfig(String, serde_json::Value);
pub struct PluginConfig(pub String, pub serde_json::Value);

#[cfg(any(feature = "plugin", feature = "plugin-bytecheck"))]
pub fn plugins(
Expand Down