Skip to content

Commit 7d57ea9

Browse files
authored
Downstream rescript changes (#160)
1 parent 9798cad commit 7d57ea9

24 files changed

+435
-125
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
22
.DS_Store
3+
/docs

.gitmodules

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn get_compiler_args(
7272
rescript_version
7373
} else {
7474
let bsc_path = match bsc_path {
75-
Some(bsc_path) => bsc_path,
75+
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
7676
None => helpers::get_bsc(&package_root, workspace_root.to_owned()),
7777
};
7878
helpers::get_rescript_version(&bsc_path)
@@ -138,7 +138,7 @@ pub fn initialize_build(
138138
let project_root = helpers::get_abs_path(path);
139139
let workspace_root = helpers::get_workspace_root(&project_root);
140140
let bsc_path = match bsc_path {
141-
Some(bsc_path) => bsc_path,
141+
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
142142
None => helpers::get_bsc(&project_root, workspace_root.to_owned()),
143143
};
144144
let root_config_name = packages::read_package_name(&project_root)?;

src/build/clean.rs

Lines changed: 25 additions & 10 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+
root_package.config.get_suffix(spec),
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
@@ -323,7 +338,7 @@ pub fn cleanup_after_build(build_state: &BuildState) {
323338
});
324339
}
325340

326-
pub fn clean(path: &str, show_progress: bool, bsc_path: Option<String>) -> Result<()> {
341+
pub fn clean(path: &str, show_progress: bool, bsc_path: Option<String>, build_dev_deps: bool) -> Result<()> {
327342
let project_root = helpers::get_abs_path(path);
328343
let workspace_root = helpers::get_workspace_root(&project_root);
329344
let packages = packages::make(
@@ -332,11 +347,11 @@ pub fn clean(path: &str, show_progress: bool, bsc_path: Option<String>) -> Resul
332347
&workspace_root,
333348
show_progress,
334349
// Always clean dev dependencies
335-
true,
350+
build_dev_deps,
336351
)?;
337352
let root_config_name = packages::read_package_name(&project_root)?;
338353
let bsc_path = match bsc_path {
339-
Some(bsc_path) => bsc_path,
354+
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
340355
None => helpers::get_bsc(&project_root, workspace_root.to_owned()),
341356
};
342357

src/build/compile.rs

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -430,23 +430,47 @@ pub fn compiler_args(
430430
false => vec![],
431431
};
432432

433+
let package_name_arg = vec!["-bs-package-name".to_string(), config.name.to_owned()];
434+
433435
let implementation_args = if is_interface {
434436
debug!("Compiling interface file: {}", &module_name);
435437
vec![]
436438
} else {
437439
debug!("Compiling file: {}", &module_name);
438-
439-
vec![
440-
"-bs-package-name".to_string(),
441-
config.name.to_owned(),
442-
"-bs-package-output".to_string(),
443-
format!(
444-
"{}:{}:{}",
445-
root_config.get_module(),
446-
Path::new(file_path).parent().unwrap().to_str().unwrap(),
447-
root_config.get_suffix()
448-
),
449-
]
440+
let specs = root_config.get_package_specs();
441+
442+
specs
443+
.iter()
444+
.map(|spec| {
445+
return vec![
446+
"-bs-package-output".to_string(),
447+
format!(
448+
"{}:{}:{}",
449+
spec.module,
450+
if spec.in_source {
451+
Path::new(file_path)
452+
.parent()
453+
.unwrap()
454+
.to_str()
455+
.unwrap()
456+
.to_string()
457+
} else {
458+
format!(
459+
"lib/{}",
460+
Path::join(
461+
Path::new(&spec.get_out_of_source_dir()),
462+
Path::new(file_path).parent().unwrap()
463+
)
464+
.to_str()
465+
.unwrap()
466+
)
467+
},
468+
root_config.get_suffix(spec),
469+
),
470+
];
471+
})
472+
.flatten()
473+
.collect()
450474
};
451475

452476
vec![
@@ -463,6 +487,7 @@ pub fn compiler_args(
463487
// this is the default
464488
// we should probably parse the right ones from the package config
465489
// vec!["-w".to_string(), "a".to_string()],
490+
package_name_arg,
466491
implementation_args,
467492
// vec![
468493
// "-I".to_string(),
@@ -588,6 +613,7 @@ fn compile_file(
588613
&Some(packages),
589614
build_dev_deps,
590615
);
616+
591617
let to_mjs = Command::new(bsc_path)
592618
.current_dir(helpers::canonicalize_string_path(&build_path_abs.to_owned()).unwrap())
593619
.args(to_mjs_args)
@@ -699,26 +725,31 @@ fn compile_file(
699725
}
700726

701727
// copy js file
702-
match &module.source_type {
703-
SourceType::SourceFile(SourceFile {
704-
implementation: Implementation { path, .. },
705-
..
706-
}) => {
707-
let source = helpers::get_source_file_from_rescript_file(
708-
&std::path::Path::new(&package.path).join(path),
709-
&root_package.config.get_suffix(),
710-
);
711-
let destination = helpers::get_source_file_from_rescript_file(
712-
&std::path::Path::new(&package.get_build_path()).join(path),
713-
&root_package.config.get_suffix(),
714-
);
715-
716-
if source.exists() {
717-
let _ = std::fs::copy(&source, &destination).expect("copying source file failed");
728+
root_package.config.get_package_specs().iter().for_each(|spec| {
729+
if spec.in_source {
730+
match &module.source_type {
731+
SourceType::SourceFile(SourceFile {
732+
implementation: Implementation { path, .. },
733+
..
734+
}) => {
735+
let source = helpers::get_source_file_from_rescript_file(
736+
&std::path::Path::new(&package.path).join(path),
737+
&root_package.config.get_suffix(spec),
738+
);
739+
let destination = helpers::get_source_file_from_rescript_file(
740+
&std::path::Path::new(&package.get_build_path()).join(path),
741+
&root_package.config.get_suffix(spec),
742+
);
743+
744+
if source.exists() {
745+
let _ =
746+
std::fs::copy(&source, &destination).expect("copying source file failed");
747+
}
748+
}
749+
_ => (),
718750
}
719751
}
720-
_ => (),
721-
}
752+
});
722753

723754
if helpers::contains_ascii_characters(&err) {
724755
if package.is_pinned_dep || package.is_local_dep {

src/build/packages.rs

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ pub fn get_build_path(canonical_path: &str) -> String {
6767
format!("{}/lib/bs", canonical_path)
6868
}
6969

70+
pub fn get_js_path(canonical_path: &str) -> String {
71+
format!("{}/lib/js", canonical_path)
72+
}
73+
74+
pub fn get_es6_path(canonical_path: &str) -> String {
75+
format!("{}/lib/es6", canonical_path)
76+
}
77+
7078
pub fn get_ocaml_build_path(canonical_path: &str) -> String {
7179
format!("{}/lib/ocaml", canonical_path)
7280
}
@@ -80,6 +88,14 @@ impl Package {
8088
get_build_path(&self.path)
8189
}
8290

91+
pub fn get_js_path(&self) -> String {
92+
get_js_path(&self.path)
93+
}
94+
95+
pub fn get_es6_path(&self) -> String {
96+
get_es6_path(&self.path)
97+
}
98+
8399
pub fn get_mlmap_path(&self) -> String {
84100
self.get_build_path()
85101
+ "/"
@@ -494,17 +510,19 @@ pub fn get_source_files(
494510
};
495511

496512
let path_dir = Path::new(&source.dir);
497-
if (build_dev_deps && type_ == &Some("dev".to_string())) || type_ != &Some("dev".to_string()) {
498-
match read_folders(filter, package_dir, path_dir, recurse) {
513+
match (build_dev_deps, type_) {
514+
(false, Some(type_)) if type_ == "dev" => (),
515+
_ => match read_folders(filter, package_dir, path_dir, recurse) {
499516
Ok(files) => map.extend(files),
517+
500518
Err(_e) => log::error!(
501519
"Could not read folder: {:?}. Specified in dependency: {}, located {:?}...",
502520
path_dir.to_path_buf().into_os_string(),
503521
package_name,
504522
package_dir
505523
),
506-
}
507-
}
524+
},
525+
};
508526

509527
map
510528
}
@@ -594,8 +612,48 @@ pub fn parse_packages(build_state: &mut BuildState) {
594612
}
595613
let build_path_abs = package.get_build_path();
596614
let bs_build_path = package.get_ocaml_build_path();
597-
helpers::create_build_path(&build_path_abs);
598-
helpers::create_build_path(&bs_build_path);
615+
helpers::create_path(&build_path_abs);
616+
helpers::create_path(&bs_build_path);
617+
let root_config = build_state
618+
.get_package(&build_state.root_config_name)
619+
.expect("cannot find root config");
620+
621+
root_config.config.get_package_specs().iter().for_each(|spec| {
622+
if !spec.in_source {
623+
// we don't want to calculate this if we don't have out of source specs
624+
// we do this twice, but we almost never have multiple package specs
625+
// so this optimization is less important
626+
let relative_dirs: AHashSet<PathBuf> = match &package.source_files {
627+
Some(source_files) => source_files
628+
.keys()
629+
.map(|source_file| {
630+
Path::new(source_file)
631+
.parent()
632+
.expect("parent dir not found")
633+
.to_owned()
634+
})
635+
.collect(),
636+
_ => AHashSet::new(),
637+
};
638+
if spec.is_common_js() {
639+
helpers::create_path(&package.get_js_path());
640+
relative_dirs.iter().for_each(|path_buf| {
641+
helpers::create_path_for_path(&Path::join(
642+
&PathBuf::from(package.get_js_path()),
643+
path_buf,
644+
))
645+
})
646+
} else {
647+
helpers::create_path(&package.get_es6_path());
648+
relative_dirs.iter().for_each(|path_buf| {
649+
helpers::create_path_for_path(&Path::join(
650+
&PathBuf::from(package.get_es6_path()),
651+
path_buf,
652+
))
653+
})
654+
}
655+
}
656+
});
599657

600658
package.namespace.to_suffix().iter().for_each(|namespace| {
601659
// generate the mlmap "AST" file for modules that have a namespace configured

src/build/parse.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,7 @@ fn generate_ast(
325325
);
326326

327327
// generate the dir of the ast_path (it mirrors the source file dir)
328-
helpers::create_build_path(
329-
&(package.get_build_path() + "/" + &ast_path.parent().unwrap().to_string_lossy()),
330-
);
328+
helpers::create_path(&(package.get_build_path() + "/" + &ast_path.parent().unwrap().to_string_lossy()));
331329

332330
/* Create .ast */
333331
let result = if let Some(res_to_ast) = Some(

src/build/read_compile_state.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ 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_suffix(root_package.config.get_package_specs().first().unwrap()),
107109
},
108110
);
109111
let _ = ast_rescript_file_locations.insert(res_file_path);

0 commit comments

Comments
 (0)