Skip to content
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- `Int.fromString` and `Float.fromString` use stricter number parsing and no longer uses an explicit radix argument, but instead supports parsing hexadecimal, binary and exponential notation.
- Remove `external-stdlib` configuration option from `rescript.json`. This option was rarely used and is no longer supported.
- `js-post-build` now passes the correct output file path based on `in-source` configuration: when `in-source: true`, the path next to the source file is passed; when `in-source: false`, the path in the `lib/<module>/` directory is passed. Additionally, stdout and stderr from the post-build command are now logged. https://github.com/rescript-lang/rescript/pull/8190
- `js-post-build` command now runs in the directory containing the `rescript.json` where it is defined, instead of the unpredictable build invocation directory. This provides consistent behavior in monorepos. https://github.com/rescript-lang/rescript/pull/8195

#### :eyeglasses: Spec Compliance

Expand Down
4 changes: 2 additions & 2 deletions docs/docson/build-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
"properties": {
"cmd": {
"type": "string",
"description": "Shell command to run after each JS file is compiled. The absolute path to the output JS file is appended as an argument. The path respects the `in-source` setting."
"description": "Shell command to run after each JS file is compiled. The absolute path to the output JS file is appended as an argument. The path respects the `in-source` setting. The command runs in the directory containing the rescript.json where it is defined."
}
}
},
Expand Down Expand Up @@ -454,7 +454,7 @@
},
"js-post-build": {
"$ref": "#/definitions/js-post-build",
"description": "Post-processing hook. The build system will invoke `cmd <absolute-path-to-js-file>` after each JS file is compiled. The path respects the `in-source` setting: when true, the path is next to the source file; when false, the path is in the `lib/<module>/` directory. The command runs with the same working directory as the build process (typically the project root)."
"description": "Post-processing hook. The build system will invoke `cmd <absolute-path-to-js-file>` after each JS file is compiled. The path respects the `in-source` setting: when true, the path is next to the source file; when false, the path is in the `lib/<module>/` directory. The command runs in the directory containing the rescript.json where it is defined."
},
"package-specs": {
"$ref": "#/definitions/package-specs",
Expand Down
24 changes: 18 additions & 6 deletions rewatch/src/build/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,27 @@ use std::sync::OnceLock;
use std::time::SystemTime;

/// Execute js-post-build command for a compiled JavaScript file.
/// Unlike bsb which passes relative paths, rewatch passes absolute paths for clarity.
fn execute_post_build_command(cmd: &str, js_file_path: &Path) -> Result<()> {
/// The command runs in the directory containing the rescript.json that defines it.
/// The absolute path to the JS file is passed as an argument.
fn execute_post_build_command(cmd: &str, js_file_path: &Path, working_dir: &Path) -> Result<()> {
let full_command = format!("{} {}", cmd, js_file_path.display());

debug!("Executing js-post-build: {}", full_command);
debug!(
"Executing js-post-build: {} (in {})",
full_command,
working_dir.display()
);

let output = if cfg!(target_os = "windows") {
Command::new("cmd").args(["/C", &full_command]).output()
Command::new("cmd")
.args(["/C", &full_command])
.current_dir(working_dir)
.output()
} else {
Command::new("sh").args(["-c", &full_command]).output()
Command::new("sh")
.args(["-c", &full_command])
.current_dir(working_dir)
.output()
};

match output {
Expand Down Expand Up @@ -886,7 +897,8 @@ fn compile_file(

if js_file.exists() {
// Fail the build if post-build command fails (matches bsb behavior with &&)
execute_post_build_command(&js_post_build.cmd, &js_file)?;
// Run in the package's directory (where rescript.json is defined)
execute_post_build_command(&js_post_build.cmd, &js_file, &package.path)?;
}
}
}
Expand Down
Loading