Skip to content

Commit

Permalink
🎨 - Allow arbitrary suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
Roland Peelen committed Jan 17, 2024
1 parent 860d66c commit 233aed8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 31 deletions.
30 changes: 5 additions & 25 deletions src/bsconfig.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};
use serde::Deserialize;
use std::fs;
use std::path::{Path, PathBuf};
use std::{fmt, fs};

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

#[derive(Deserialize, Debug, Clone)]
#[serde(untagged)]
Expand Down Expand Up @@ -121,28 +123,6 @@ pub enum JsxModule {
React,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum Suffix {
#[serde(rename = ".js")]
Js,
#[serde(rename = ".mjs")]
Mjs,
#[serde(rename = ".cjs")]
Cjs,
#[serde(rename = ".bs.js")]
BsJs,
#[serde(rename = ".bs.mjs")]
BsMjs,
#[serde(rename = ".bs.cjs")]
BsCjs,
}

impl fmt::Display for Suffix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", serde_json::to_value(self).unwrap().as_str().unwrap())
}
}

#[derive(Deserialize, Debug, Clone)]
pub struct JsxSpecs {
pub version: Option<i32>,
Expand All @@ -161,7 +141,7 @@ pub struct T {
#[serde(rename = "package-specs")]
pub package_specs: Option<OneOrMore<PackageSpec>>,
pub warnings: Option<Warnings>,
pub suffix: Option<Suffix>,
pub suffix: Option<String>,
#[serde(rename = "pinned-dependencies")]
pub pinned_dependencies: Option<Vec<String>>,
#[serde(rename = "bs-dependencies")]
Expand Down
2 changes: 1 addition & 1 deletion src/build/build_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub struct AstModule {
pub last_modified: SystemTime,
pub ast_file_path: String,
pub is_root: bool,
pub suffix: Option<crate::bsconfig::Suffix>,
pub suffix: Option<String>,
}

pub struct CompileAssetsState {
Expand Down
10 changes: 6 additions & 4 deletions src/build/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn remove_iast(source_file: &str, package_path: &str, root_path: &str, is_root:
));
}

fn remove_mjs_file(source_file: &str, suffix: &bsconfig::Suffix) {
fn remove_mjs_file(source_file: &str, suffix: &String) {
let _ = std::fs::remove_file(helpers::change_extension(
source_file,
// suffix.to_string includes the ., so we need to remove it
Expand Down Expand Up @@ -107,12 +107,12 @@ pub fn clean_mjs_files(build_state: &BuildState) {
.bsconfig
.suffix
.to_owned()
.unwrap_or(bsconfig::Suffix::Mjs),
.unwrap_or(String::from(bsconfig::DEFAULT_SUFFIX)),
))
}
_ => None,
})
.collect::<Vec<(String, bsconfig::Suffix)>>();
.collect::<Vec<(String, String)>>();

rescript_file_locations
.par_iter()
Expand Down Expand Up @@ -168,7 +168,9 @@ pub fn cleanup_previous_build(
);
remove_mjs_file(
&res_file_location,
&suffix.to_owned().unwrap_or(bsconfig::Suffix::Mjs),
&suffix
.to_owned()
.unwrap_or(String::from(bsconfig::DEFAULT_SUFFIX)),
);
remove_iast(
res_file_location,
Expand Down
2 changes: 1 addition & 1 deletion src/build/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ fn compile_file(
// TODO: Also read suffix from package-spec.
let suffix = match root_package.bsconfig.suffix.to_owned() {
Some(suffix) => suffix,
None => bsconfig::Suffix::Mjs,
None => String::from(bsconfig::DEFAULT_SUFFIX)
};

vec![
Expand Down

0 comments on commit 233aed8

Please sign in to comment.