Skip to content

Commit 64fe73b

Browse files
committed
do everything proper from the package specs
1 parent b035fde commit 64fe73b

File tree

4 files changed

+61
-53
lines changed

4 files changed

+61
-53
lines changed

src/build/clean.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,31 @@ pub fn clean_mjs_files(build_state: &BuildState) {
7070
.packages
7171
.get(&build_state.root_config_name)
7272
.expect("Could not find root package");
73-
Some((
74-
std::path::PathBuf::from(package.path.to_string())
75-
.join(&source_file.implementation.path)
76-
.to_string_lossy()
77-
.to_string(),
78-
root_package.config.get_suffix(),
79-
))
73+
74+
Some(
75+
root_package
76+
.config
77+
.get_package_specs()
78+
.iter()
79+
.filter_map(|spec| {
80+
if spec.in_source {
81+
Some((
82+
std::path::PathBuf::from(package.path.to_string())
83+
.join(&source_file.implementation.path)
84+
.to_string_lossy()
85+
.to_string(),
86+
spec.get_suffix(),
87+
))
88+
} else {
89+
None
90+
}
91+
})
92+
.collect::<Vec<(String, String)>>(),
93+
)
8094
}
8195
_ => None,
8296
})
97+
.flatten()
8398
.collect::<Vec<(String, String)>>();
8499

85100
rescript_file_locations

src/build/compile.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ pub fn compiler_args(
488488
.unwrap()
489489
)
490490
},
491-
root_config.get_suffix()
491+
spec.get_suffix()
492492
),
493493
];
494494
})
@@ -671,26 +671,31 @@ fn compile_file(
671671
}
672672

673673
// copy js file
674-
match &module.source_type {
675-
SourceType::SourceFile(SourceFile {
676-
implementation: Implementation { path, .. },
677-
..
678-
}) => {
679-
let source = helpers::get_source_file_from_rescript_file(
680-
&std::path::Path::new(&package.path).join(path),
681-
&root_package.config.get_suffix(),
682-
);
683-
let destination = helpers::get_source_file_from_rescript_file(
684-
&std::path::Path::new(&package.get_build_path()).join(path),
685-
&root_package.config.get_suffix(),
686-
);
687-
688-
if source.exists() {
689-
let _ = std::fs::copy(&source, &destination).expect("copying source file failed");
674+
root_package.config.get_package_specs().iter().for_each(|spec| {
675+
if spec.in_source {
676+
match &module.source_type {
677+
SourceType::SourceFile(SourceFile {
678+
implementation: Implementation { path, .. },
679+
..
680+
}) => {
681+
let source = helpers::get_source_file_from_rescript_file(
682+
&std::path::Path::new(&package.path).join(path),
683+
&spec.get_suffix(),
684+
);
685+
let destination = helpers::get_source_file_from_rescript_file(
686+
&std::path::Path::new(&package.get_build_path()).join(path),
687+
&spec.get_suffix(),
688+
);
689+
690+
if source.exists() {
691+
let _ =
692+
std::fs::copy(&source, &destination).expect("copying source file failed");
693+
}
694+
}
695+
_ => (),
690696
}
691697
}
692-
_ => (),
693-
}
698+
});
694699

695700
if helpers::contains_ascii_characters(&err) {
696701
if package.is_pinned_dep || package.is_local_dep {

src/build/read_compile_state.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ 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.config.get_suffix(),
106+
suffix: root_package
107+
.config
108+
.get_package_specs()
109+
.first()
110+
.map(|spec| spec.get_suffix())
111+
.unwrap(),
107112
},
108113
);
109114
let _ = ast_rescript_file_locations.insert(res_file_path);

src/config.rs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ impl PackageSpec {
126126
_ => false,
127127
}
128128
}
129+
130+
pub fn get_suffix(&self) -> String {
131+
self.suffix.to_owned().unwrap_or(".js".to_string())
132+
}
129133
}
130134

131135
#[derive(Deserialize, Debug, Clone)]
@@ -413,31 +417,6 @@ impl Config {
413417
}
414418
}
415419

416-
pub fn get_module(&self) -> String {
417-
match &self.package_specs {
418-
Some(OneOrMore::Single(PackageSpec { module, .. })) => module.to_string(),
419-
Some(OneOrMore::Multiple(vec)) => match vec.first() {
420-
Some(PackageSpec { module, .. }) => module.to_string(),
421-
None => "commonjs".to_string(),
422-
},
423-
_ => "commonjs".to_string(),
424-
}
425-
}
426-
427-
pub fn get_suffix(&self) -> String {
428-
match &self.package_specs {
429-
Some(OneOrMore::Single(PackageSpec { suffix, .. })) => suffix.to_owned(),
430-
Some(OneOrMore::Multiple(vec)) => match vec.first() {
431-
Some(PackageSpec { suffix, .. }) => suffix.to_owned(),
432-
None => None,
433-
},
434-
435-
_ => None,
436-
}
437-
.or(self.suffix.to_owned())
438-
.unwrap_or(".js".to_string())
439-
}
440-
441420
pub fn get_gentype_arg(&self) -> Vec<String> {
442421
match &self.gentype_config {
443422
Some(_) => vec!["-bs-gentype".to_string()],
@@ -447,7 +426,11 @@ impl Config {
447426

448427
pub fn get_package_specs(&self) -> Vec<PackageSpec> {
449428
match self.package_specs.clone() {
450-
None => vec![],
429+
None => vec![PackageSpec {
430+
module: "commonjs".to_string(),
431+
in_source: true,
432+
suffix: Some(".js".to_string()),
433+
}],
451434
Some(OneOrMore::Single(spec)) => vec![spec],
452435
Some(OneOrMore::Multiple(vec)) => vec,
453436
}

0 commit comments

Comments
 (0)