Skip to content

Commit

Permalink
✨ - Fix build specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrolich committed Apr 12, 2024
1 parent 1838727 commit 67fab5a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
20 changes: 18 additions & 2 deletions src/bsconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use serde::Deserialize;
use std::fs;
use std::path::{Path, PathBuf};

pub static DEFAULT_SUFFIX: &str = ".mjs";

#[derive(Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum OneOrMore<T> {
Expand Down Expand Up @@ -83,6 +81,7 @@ pub struct PackageSpec {
pub module: String,
#[serde(rename = "in-source")]
pub in_source: bool,
pub suffix: Option<String>,
}

#[derive(Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -345,4 +344,21 @@ impl Config {
vec![]
}
}

pub fn get_module(&self) -> String {
match &self.package_specs {
Some(OneOrMore::Single(PackageSpec { module, .. })) => module.to_string(),
Some(OneOrMore::Multiple(_)) => panic!("Multiple package specs not supported"),
_ => "commonjs".to_string(),
}
}

pub fn get_suffix(&self) -> String {
match &self.package_specs {
Some(OneOrMore::Single(PackageSpec { suffix, .. })) => suffix.to_owned(),
Some(OneOrMore::Multiple(_)) => panic!("Multiple package specs not supported"),
_ => self.suffix.to_owned(),
}
.unwrap_or(".js".to_string())
}
}
2 changes: 1 addition & 1 deletion src/build/build_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub struct AstModule {
pub last_modified: SystemTime,
pub ast_file_path: String,
pub is_root: bool,
pub suffix: Option<String>,
pub suffix: String,
}

pub struct CompileAssetsState {
Expand Down
14 changes: 2 additions & 12 deletions src/build/clean.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::build_types::*;
use super::packages;
use crate::bsconfig;
use crate::helpers;
use crate::helpers::emojis::*;
use ahash::AHashSet;
Expand Down Expand Up @@ -75,11 +74,7 @@ pub fn clean_mjs_files(build_state: &BuildState) {
.join(source_file.implementation.path.to_string())
.to_string_lossy()
.to_string(),
root_package
.bsconfig
.suffix
.to_owned()
.unwrap_or(String::from(bsconfig::DEFAULT_SUFFIX)),
root_package.bsconfig.get_suffix(),
))
}
_ => None,
Expand Down Expand Up @@ -130,12 +125,7 @@ pub fn cleanup_previous_build(
.get(package_name)
.expect("Could not find package");
remove_compile_assets(package, res_file_location);
remove_mjs_file(
&res_file_location,
&suffix
.to_owned()
.unwrap_or(String::from(bsconfig::DEFAULT_SUFFIX)),
);
remove_mjs_file(&res_file_location, &suffix);
remove_iast(package, res_file_location);
remove_ast(package, res_file_location);
match helpers::get_extension(ast_file_path).as_str() {
Expand Down
11 changes: 3 additions & 8 deletions src/build/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,20 +474,15 @@ pub fn compiler_args(
} else {
debug!("Compiling file: {}", &module_name);

// TODO: Also read suffix from package-spec.
let suffix = match root_config.suffix.to_owned() {
Some(suffix) => suffix,
None => String::from(bsconfig::DEFAULT_SUFFIX),
};

vec![
"-bs-package-name".to_string(),
config.name.to_owned(),
"-bs-package-output".to_string(),
format!(
"es6:{}:{}",
"{}:{}:{}",
root_config.get_module(),
Path::new(file_path).parent().unwrap().to_str().unwrap(),
suffix
root_config.get_suffix()
),
]
};
Expand Down
2 changes: 1 addition & 1 deletion src/build/read_compile_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState {
last_modified: last_modified.to_owned(),
ast_file_path,
is_root: *package_is_root,
suffix: root_package.bsconfig.suffix.to_owned(),
suffix: root_package.bsconfig.get_suffix(),
},
);
let _ = ast_rescript_file_locations.insert(res_file_path);
Expand Down

0 comments on commit 67fab5a

Please sign in to comment.