build.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. use std::collections::hash_map::DefaultHasher;
  2. use std::path::PathBuf;
  3. use std::{hash::Hasher, process::Command};
  4. fn main() {
  5. // If any TS changes, re-run the build script
  6. let watching = std::fs::read_dir("./src/ts").unwrap();
  7. let ts_paths: Vec<_> = watching
  8. .into_iter()
  9. .flatten()
  10. .map(|entry| entry.path())
  11. .filter(|path| path.extension().map(|ext| ext == "ts").unwrap_or(false))
  12. .collect();
  13. for path in &ts_paths {
  14. println!("cargo:rerun-if-changed={}", path.display());
  15. }
  16. // Compute the hash of the ts files
  17. let hash = hash_ts_files(ts_paths);
  18. // If the hash matches the one on disk, we're good and don't need to update bindings
  19. let fs_hash_string = std::fs::read_to_string("src/js/hash.txt");
  20. let expected = fs_hash_string
  21. .as_ref()
  22. .map(|s| s.trim())
  23. .unwrap_or_default();
  24. if expected == hash.to_string() {
  25. return;
  26. }
  27. // Otherwise, generate the bindings and write the new hash to disk
  28. // Generate the bindings for both native and web
  29. gen_bindings("common", "common");
  30. gen_bindings("native", "native");
  31. gen_bindings("core", "core");
  32. gen_bindings("eval", "eval");
  33. gen_bindings("native_eval", "native_eval");
  34. gen_bindings("hydrate", "hydrate");
  35. gen_bindings("initialize_streaming", "initialize_streaming");
  36. std::fs::write("src/js/hash.txt", hash.to_string()).unwrap();
  37. }
  38. /// Hashes the contents of a directory
  39. fn hash_ts_files(mut files: Vec<PathBuf>) -> u64 {
  40. // Different systems will read the files in different orders, so we sort them to make sure the hash is consistent
  41. files.sort();
  42. let mut hash = DefaultHasher::new();
  43. for file in files {
  44. let contents = std::fs::read_to_string(file).unwrap();
  45. // windows + git does a weird thing with line endings, so we need to normalize them
  46. for line in contents.lines() {
  47. hash.write(line.as_bytes());
  48. }
  49. }
  50. hash.finish()
  51. }
  52. // okay...... so tsc might fail if the user doesn't have it installed
  53. // we don't really want to fail if that's the case
  54. // but if you started *editing* the .ts files, you're gonna have a bad time
  55. // so.....
  56. // we need to hash each of the .ts files and add that hash to the JS files
  57. // if the hashes don't match, we need to fail the build
  58. // that way we also don't need
  59. fn gen_bindings(input_name: &str, output_name: &str) {
  60. // If the file is generated, and the hash is different, we need to generate it
  61. let status = Command::new("bun")
  62. .arg("build")
  63. .arg(format!("src/ts/{input_name}.ts"))
  64. .arg("--outfile")
  65. .arg(format!("src/js/{output_name}.js"))
  66. .arg("--minify-whitespace")
  67. .arg("--minify-syntax")
  68. .status()
  69. .unwrap();
  70. if !status.success() {
  71. panic!(
  72. "Failed to generate bindings for {}. Make sure you have tsc installed",
  73. input_name
  74. );
  75. }
  76. }