@@ -21,6 +21,33 @@ use std::process::Command;
2121use std:: sync:: OnceLock ;
2222use std:: time:: SystemTime ;
2323
24+ /// Execute js-post-build command for a compiled JavaScript file.
25+ /// Unlike bsb which passes relative paths, rewatch passes absolute paths for clarity.
26+ fn execute_post_build_command ( cmd : & str , js_file_path : & Path ) -> Result < ( ) > {
27+ let full_command = format ! ( "{} {}" , cmd, js_file_path. display( ) ) ;
28+
29+ let output = if cfg ! ( target_os = "windows" ) {
30+ Command :: new ( "cmd" ) . args ( [ "/C" , & full_command] ) . output ( )
31+ } else {
32+ Command :: new ( "sh" ) . args ( [ "-c" , & full_command] ) . output ( )
33+ } ;
34+
35+ match output {
36+ Ok ( output) if !output. status . success ( ) => {
37+ let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
38+ let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
39+ Err ( anyhow ! (
40+ "js-post-build command failed for {}: {}{}" ,
41+ js_file_path. display( ) ,
42+ stderr,
43+ stdout
44+ ) )
45+ }
46+ Err ( e) => Err ( anyhow ! ( "Failed to execute js-post-build command: {}" , e) ) ,
47+ Ok ( _) => Ok ( ( ) ) ,
48+ }
49+ }
50+
2451pub fn compile (
2552 build_state : & mut BuildCommandState ,
2653 show_progress : bool ,
@@ -815,6 +842,29 @@ fn compile_file(
815842 }
816843 } ) ;
817844
845+ // Execute js-post-build command if configured
846+ // Only run for implementation files (not interfaces)
847+ if !is_interface
848+ && let Some ( js_post_build) = & package. config . js_post_build
849+ && let SourceType :: SourceFile ( SourceFile {
850+ implementation : Implementation { path, .. } ,
851+ ..
852+ } ) = & module. source_type
853+ {
854+ // Execute post-build command for each package spec (each output format)
855+ for spec in root_config. get_package_specs ( ) {
856+ let js_file = helpers:: get_source_file_from_rescript_file (
857+ & package. get_build_path ( ) . join ( path) ,
858+ & root_config. get_suffix ( & spec) ,
859+ ) ;
860+
861+ if js_file. exists ( ) {
862+ // Fail the build if post-build command fails (matches bsb behavior with &&)
863+ execute_post_build_command ( & js_post_build. cmd , & js_file) ?;
864+ }
865+ }
866+ }
867+
818868 if helpers:: contains_ascii_characters ( & err) {
819869 if package. is_local_dep {
820870 // suppress warnings of external deps
0 commit comments