Skip to content

Commit 53d711e

Browse files
authored
✨ - Fix build specs (#100)
1 parent 02fc6c7 commit 53d711e

File tree

5 files changed

+33
-24
lines changed

5 files changed

+33
-24
lines changed

src/bsconfig.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use serde::Deserialize;
44
use std::fs;
55
use std::path::{Path, PathBuf};
66

7-
pub static DEFAULT_SUFFIX: &str = ".mjs";
8-
97
#[derive(Deserialize, Debug, Clone)]
108
#[serde(untagged)]
119
pub enum OneOrMore<T> {
@@ -83,6 +81,7 @@ pub struct PackageSpec {
8381
pub module: String,
8482
#[serde(rename = "in-source")]
8583
pub in_source: bool,
84+
pub suffix: Option<String>,
8685
}
8786

8887
#[derive(Deserialize, Debug, Clone)]
@@ -342,4 +341,29 @@ impl Config {
342341
vec![]
343342
}
344343
}
344+
345+
pub fn get_module(&self) -> String {
346+
match &self.package_specs {
347+
Some(OneOrMore::Single(PackageSpec { module, .. })) => module.to_string(),
348+
Some(OneOrMore::Multiple(vec)) => match vec.first() {
349+
Some(PackageSpec { module, .. }) => module.to_string(),
350+
None => "commonjs".to_string(),
351+
},
352+
_ => "commonjs".to_string(),
353+
}
354+
}
355+
356+
pub fn get_suffix(&self) -> String {
357+
match &self.package_specs {
358+
Some(OneOrMore::Single(PackageSpec { suffix, .. })) => suffix.to_owned(),
359+
Some(OneOrMore::Multiple(vec)) => match vec.first() {
360+
Some(PackageSpec { suffix, .. }) => suffix.to_owned(),
361+
None => None,
362+
},
363+
364+
_ => None,
365+
}
366+
.or(self.suffix.to_owned())
367+
.unwrap_or(".js".to_string())
368+
}
345369
}

src/build/build_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub struct AstModule {
132132
pub last_modified: SystemTime,
133133
pub ast_file_path: String,
134134
pub is_root: bool,
135-
pub suffix: Option<String>,
135+
pub suffix: String,
136136
}
137137

138138
pub struct CompileAssetsState {

src/build/clean.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::build_types::*;
22
use super::packages;
3-
use crate::bsconfig;
43
use crate::helpers;
54
use crate::helpers::emojis::*;
65
use ahash::AHashSet;
@@ -75,11 +74,7 @@ pub fn clean_mjs_files(build_state: &BuildState) {
7574
.join(&source_file.implementation.path)
7675
.to_string_lossy()
7776
.to_string(),
78-
root_package
79-
.bsconfig
80-
.suffix
81-
.to_owned()
82-
.unwrap_or(String::from(bsconfig::DEFAULT_SUFFIX)),
77+
root_package.bsconfig.get_suffix(),
8378
))
8479
}
8580
_ => None,
@@ -130,12 +125,7 @@ pub fn cleanup_previous_build(
130125
.get(package_name)
131126
.expect("Could not find package");
132127
remove_compile_assets(package, res_file_location);
133-
remove_mjs_file(
134-
res_file_location,
135-
&suffix
136-
.to_owned()
137-
.unwrap_or(String::from(bsconfig::DEFAULT_SUFFIX)),
138-
);
128+
remove_mjs_file(res_file_location, &suffix);
139129
remove_iast(package, res_file_location);
140130
remove_ast(package, res_file_location);
141131
match helpers::get_extension(ast_file_path).as_str() {

src/build/compile.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -476,20 +476,15 @@ pub fn compiler_args(
476476
} else {
477477
debug!("Compiling file: {}", &module_name);
478478

479-
// TODO: Also read suffix from package-spec.
480-
let suffix = match root_config.suffix.to_owned() {
481-
Some(suffix) => suffix,
482-
None => String::from(bsconfig::DEFAULT_SUFFIX),
483-
};
484-
485479
vec![
486480
"-bs-package-name".to_string(),
487481
config.name.to_owned(),
488482
"-bs-package-output".to_string(),
489483
format!(
490-
"es6:{}:{}",
484+
"{}:{}:{}",
485+
root_config.get_module(),
491486
Path::new(file_path).parent().unwrap().to_str().unwrap(),
492-
suffix
487+
root_config.get_suffix()
493488
),
494489
]
495490
};

src/build/read_compile_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState {
103103
last_modified: last_modified.to_owned(),
104104
ast_file_path,
105105
is_root: *package_is_root,
106-
suffix: root_package.bsconfig.suffix.to_owned(),
106+
suffix: root_package.bsconfig.get_suffix(),
107107
},
108108
);
109109
let _ = ast_rescript_file_locations.insert(res_file_path);

0 commit comments

Comments
 (0)