From 53d711e822ea9c55c23476a13e5198a608426dbf Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Fri, 12 Apr 2024 17:52:44 +0200 Subject: [PATCH] :sparkles: - Fix build specs (#100) --- src/bsconfig.rs | 28 ++++++++++++++++++++++++++-- src/build/build_types.rs | 2 +- src/build/clean.rs | 14 ++------------ src/build/compile.rs | 11 +++-------- src/build/read_compile_state.rs | 2 +- 5 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/bsconfig.rs b/src/bsconfig.rs index fb76739..4224cb6 100644 --- a/src/bsconfig.rs +++ b/src/bsconfig.rs @@ -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 { @@ -83,6 +81,7 @@ pub struct PackageSpec { pub module: String, #[serde(rename = "in-source")] pub in_source: bool, + pub suffix: Option, } #[derive(Deserialize, Debug, Clone)] @@ -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()) + } } diff --git a/src/build/build_types.rs b/src/build/build_types.rs index 5ada3c2..4dc6cfa 100644 --- a/src/build/build_types.rs +++ b/src/build/build_types.rs @@ -132,7 +132,7 @@ pub struct AstModule { pub last_modified: SystemTime, pub ast_file_path: String, pub is_root: bool, - pub suffix: Option, + pub suffix: String, } pub struct CompileAssetsState { diff --git a/src/build/clean.rs b/src/build/clean.rs index 4829966..e4bde12 100644 --- a/src/build/clean.rs +++ b/src/build/clean.rs @@ -1,6 +1,5 @@ use super::build_types::*; use super::packages; -use crate::bsconfig; use crate::helpers; use crate::helpers::emojis::*; use ahash::AHashSet; @@ -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, @@ -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() { diff --git a/src/build/compile.rs b/src/build/compile.rs index f081b86..cdf1695 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -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() ), ] }; diff --git a/src/build/read_compile_state.rs b/src/build/read_compile_state.rs index 59b229a..7d983f5 100644 --- a/src/build/read_compile_state.rs +++ b/src/build/read_compile_state.rs @@ -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);