Skip to content

✨ - Fix build specs #100

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

Merged
merged 12 commits into from
Apr 12, 2024
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
28 changes: 26 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 @@ -342,4 +341,29 @@ impl Config {
vec![]
}
}

pub fn get_module(&self) -> String {
match &self.package_specs {
Some(OneOrMore::Single(PackageSpec { module, .. })) => module.to_string(),
Some(OneOrMore::Multiple(vec)) => match vec.first() {
Some(PackageSpec { module, .. }) => module.to_string(),
None => "commonjs".to_string(),
},
_ => "commonjs".to_string(),
}
}

pub fn get_suffix(&self) -> String {
match &self.package_specs {
Some(OneOrMore::Single(PackageSpec { suffix, .. })) => suffix.to_owned(),
Some(OneOrMore::Multiple(vec)) => match vec.first() {
Some(PackageSpec { suffix, .. }) => suffix.to_owned(),
None => None,
},

_ => None,
}
.or(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 @@ -132,7 +132,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_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 @@ -476,20 +476,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 @@ -103,7 +103,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