Skip to content

Instantly share code, notes, and snippets.

@mcpar-land
Created June 29, 2023 05:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcpar-land/b853da913ca7f45faa179704772961ef to your computer and use it in GitHub Desktop.
Save mcpar-land/b853da913ca7f45faa179704772961ef to your computer and use it in GitHub Desktop.
add tailwind to rust build script
use std::process::Command;
fn main() {
let tailwind_cmd = "npx tailwindcss -i input.css -o assets/app.css";
if cfg!(target_os = "windows") {
Command::new("cmd").arg("/C").arg(tailwind_cmd).status()
} else {
Command::new("sh").arg("-c").arg(tailwind_cmd).status()
}
.expect("error running tailwind");
println!("cargo:rerun-if-changed=tailwind.config.js");
println!("cargo:rerun-if-changed=input.css");
}
@bedwards
Copy link

You have the check status.success(), because .status() only returns Err in certain cases (there was something wrong at the system level that disallowed running the command.) Here is my version...

use std::{
    io::{self, Write},
    process,
};

fn main() {
    println!("cargo:rerun-if-changed=tailwind.config.js");
    println!("cargo:rerun-if-changed=src/input.css");

    match process::Command::new("sh")
        .arg("-c")
        .arg("npx tailwindcss -i src/input.css -o target/output.css")
        .output()
    {
        Ok(output) => {
            if !output.status.success() {
                let _ = io::stdout().write_all(&output.stdout);
                let _ = io::stdout().write_all(&output.stderr);
                panic!("Tailwind error");
            }
        }
        Err(e) => panic!("Tailwind error: {:?}", e),
    };
}

Which produces error messages like this...

$ cargo run --release
   Compiling twind1 v0.1.0 (/Users/bedwards/code/twind1)
error: failed to run custom build command for `twind1 v0.1.0 (/Users/bedwards/code/twind1)`

Caused by:
  process didn't exit successfully: `/Users/bedwards/code/twind1/target/release/build/twind1-7b30e541e618a4b0/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-changed=tailwind.config.js
  cargo:rerun-if-changed=src/input.css
  Specified input file src/input.css does not exist.

  --- stderr
  thread 'main' panicked at build.rs:19:17:
  Tailwind error

@mcpar-land
Copy link
Author

excellent!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment