Skip to content

Downstream rescript changes #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.DS_Store
/docs
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

4 changes: 2 additions & 2 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn get_compiler_args(
rescript_version
} else {
let bsc_path = match bsc_path {
Some(bsc_path) => bsc_path,
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
None => helpers::get_bsc(&package_root, workspace_root.to_owned()),
};
helpers::get_rescript_version(&bsc_path)
Expand Down Expand Up @@ -138,7 +138,7 @@ pub fn initialize_build(
let project_root = helpers::get_abs_path(path);
let workspace_root = helpers::get_workspace_root(&project_root);
let bsc_path = match bsc_path {
Some(bsc_path) => bsc_path,
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
None => helpers::get_bsc(&project_root, workspace_root.to_owned()),
};
let root_config_name = packages::read_package_name(&project_root)?;
Expand Down
35 changes: 25 additions & 10 deletions src/build/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,31 @@ pub fn clean_mjs_files(build_state: &BuildState) {
.packages
.get(&build_state.root_config_name)
.expect("Could not find root package");
Some((
std::path::PathBuf::from(package.path.to_string())
.join(&source_file.implementation.path)
.to_string_lossy()
.to_string(),
root_package.config.get_suffix(),
))

Some(
root_package
.config
.get_package_specs()
.iter()
.filter_map(|spec| {
if spec.in_source {
Some((
std::path::PathBuf::from(package.path.to_string())
.join(&source_file.implementation.path)
.to_string_lossy()
.to_string(),
root_package.config.get_suffix(spec),
))
} else {
None
}
})
.collect::<Vec<(String, String)>>(),
)
}
_ => None,
})
.flatten()
.collect::<Vec<(String, String)>>();

rescript_file_locations
Expand Down Expand Up @@ -323,7 +338,7 @@ pub fn cleanup_after_build(build_state: &BuildState) {
});
}

pub fn clean(path: &str, show_progress: bool, bsc_path: Option<String>) -> Result<()> {
pub fn clean(path: &str, show_progress: bool, bsc_path: Option<String>, build_dev_deps: bool) -> Result<()> {
let project_root = helpers::get_abs_path(path);
let workspace_root = helpers::get_workspace_root(&project_root);
let packages = packages::make(
Expand All @@ -332,11 +347,11 @@ pub fn clean(path: &str, show_progress: bool, bsc_path: Option<String>) -> Resul
&workspace_root,
show_progress,
// Always clean dev dependencies
true,
build_dev_deps,
)?;
let root_config_name = packages::read_package_name(&project_root)?;
let bsc_path = match bsc_path {
Some(bsc_path) => bsc_path,
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
None => helpers::get_bsc(&project_root, workspace_root.to_owned()),
};

Expand Down
91 changes: 61 additions & 30 deletions src/build/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,23 +430,47 @@ pub fn compiler_args(
false => vec![],
};

let package_name_arg = vec!["-bs-package-name".to_string(), config.name.to_owned()];

let implementation_args = if is_interface {
debug!("Compiling interface file: {}", &module_name);
vec![]
} else {
debug!("Compiling file: {}", &module_name);

vec![
"-bs-package-name".to_string(),
config.name.to_owned(),
"-bs-package-output".to_string(),
format!(
"{}:{}:{}",
root_config.get_module(),
Path::new(file_path).parent().unwrap().to_str().unwrap(),
root_config.get_suffix()
),
]
let specs = root_config.get_package_specs();

specs
.iter()
.map(|spec| {
return vec![
"-bs-package-output".to_string(),
format!(
"{}:{}:{}",
spec.module,
if spec.in_source {
Path::new(file_path)
.parent()
.unwrap()
.to_str()
.unwrap()
.to_string()
} else {
format!(
"lib/{}",
Path::join(
Path::new(&spec.get_out_of_source_dir()),
Path::new(file_path).parent().unwrap()
)
.to_str()
.unwrap()
)
},
root_config.get_suffix(spec),
),
];
})
.flatten()
.collect()
};

vec![
Expand All @@ -463,6 +487,7 @@ pub fn compiler_args(
// this is the default
// we should probably parse the right ones from the package config
// vec!["-w".to_string(), "a".to_string()],
package_name_arg,
implementation_args,
// vec![
// "-I".to_string(),
Expand Down Expand Up @@ -588,6 +613,7 @@ fn compile_file(
&Some(packages),
build_dev_deps,
);

let to_mjs = Command::new(bsc_path)
.current_dir(helpers::canonicalize_string_path(&build_path_abs.to_owned()).unwrap())
.args(to_mjs_args)
Expand Down Expand Up @@ -699,26 +725,31 @@ fn compile_file(
}

// copy js file
match &module.source_type {
SourceType::SourceFile(SourceFile {
implementation: Implementation { path, .. },
..
}) => {
let source = helpers::get_source_file_from_rescript_file(
&std::path::Path::new(&package.path).join(path),
&root_package.config.get_suffix(),
);
let destination = helpers::get_source_file_from_rescript_file(
&std::path::Path::new(&package.get_build_path()).join(path),
&root_package.config.get_suffix(),
);

if source.exists() {
let _ = std::fs::copy(&source, &destination).expect("copying source file failed");
root_package.config.get_package_specs().iter().for_each(|spec| {
if spec.in_source {
match &module.source_type {
SourceType::SourceFile(SourceFile {
implementation: Implementation { path, .. },
..
}) => {
let source = helpers::get_source_file_from_rescript_file(
&std::path::Path::new(&package.path).join(path),
&root_package.config.get_suffix(spec),
);
let destination = helpers::get_source_file_from_rescript_file(
&std::path::Path::new(&package.get_build_path()).join(path),
&root_package.config.get_suffix(spec),
);

if source.exists() {
let _ =
std::fs::copy(&source, &destination).expect("copying source file failed");
}
}
_ => (),
}
}
_ => (),
}
});

if helpers::contains_ascii_characters(&err) {
if package.is_pinned_dep || package.is_local_dep {
Expand Down
70 changes: 64 additions & 6 deletions src/build/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ pub fn get_build_path(canonical_path: &str) -> String {
format!("{}/lib/bs", canonical_path)
}

pub fn get_js_path(canonical_path: &str) -> String {
format!("{}/lib/js", canonical_path)
}

pub fn get_es6_path(canonical_path: &str) -> String {
format!("{}/lib/es6", canonical_path)
}

pub fn get_ocaml_build_path(canonical_path: &str) -> String {
format!("{}/lib/ocaml", canonical_path)
}
Expand All @@ -80,6 +88,14 @@ impl Package {
get_build_path(&self.path)
}

pub fn get_js_path(&self) -> String {
get_js_path(&self.path)
}

pub fn get_es6_path(&self) -> String {
get_es6_path(&self.path)
}

pub fn get_mlmap_path(&self) -> String {
self.get_build_path()
+ "/"
Expand Down Expand Up @@ -494,17 +510,19 @@ pub fn get_source_files(
};

let path_dir = Path::new(&source.dir);
if (build_dev_deps && type_ == &Some("dev".to_string())) || type_ != &Some("dev".to_string()) {
match read_folders(filter, package_dir, path_dir, recurse) {
match (build_dev_deps, type_) {
(false, Some(type_)) if type_ == "dev" => (),
_ => match read_folders(filter, package_dir, path_dir, recurse) {
Ok(files) => map.extend(files),

Err(_e) => log::error!(
"Could not read folder: {:?}. Specified in dependency: {}, located {:?}...",
path_dir.to_path_buf().into_os_string(),
package_name,
package_dir
),
}
}
},
};

map
}
Expand Down Expand Up @@ -594,8 +612,48 @@ pub fn parse_packages(build_state: &mut BuildState) {
}
let build_path_abs = package.get_build_path();
let bs_build_path = package.get_ocaml_build_path();
helpers::create_build_path(&build_path_abs);
helpers::create_build_path(&bs_build_path);
helpers::create_path(&build_path_abs);
helpers::create_path(&bs_build_path);
let root_config = build_state
.get_package(&build_state.root_config_name)
.expect("cannot find root config");

root_config.config.get_package_specs().iter().for_each(|spec| {
if !spec.in_source {
// we don't want to calculate this if we don't have out of source specs
// we do this twice, but we almost never have multiple package specs
// so this optimization is less important
let relative_dirs: AHashSet<PathBuf> = match &package.source_files {
Some(source_files) => source_files
.keys()
.map(|source_file| {
Path::new(source_file)
.parent()
.expect("parent dir not found")
.to_owned()
})
.collect(),
_ => AHashSet::new(),
};
if spec.is_common_js() {
helpers::create_path(&package.get_js_path());
relative_dirs.iter().for_each(|path_buf| {
helpers::create_path_for_path(&Path::join(
&PathBuf::from(package.get_js_path()),
path_buf,
))
})
} else {
helpers::create_path(&package.get_es6_path());
relative_dirs.iter().for_each(|path_buf| {
helpers::create_path_for_path(&Path::join(
&PathBuf::from(package.get_es6_path()),
path_buf,
))
})
}
}
});

package.namespace.to_suffix().iter().for_each(|namespace| {
// generate the mlmap "AST" file for modules that have a namespace configured
Expand Down
4 changes: 1 addition & 3 deletions src/build/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,7 @@ fn generate_ast(
);

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

/* Create .ast */
let result = if let Some(res_to_ast) = Some(
Expand Down
4 changes: 3 additions & 1 deletion src/build/read_compile_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ 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.config.get_suffix(),
suffix: root_package
.config
.get_suffix(root_package.config.get_package_specs().first().unwrap()),
},
);
let _ = ast_rescript_file_locations.insert(res_file_path);
Expand Down
Loading