Skip to content
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

Tailwind v4 support #428

Merged
merged 13 commits into from
Feb 14, 2025
Merged

Tailwind v4 support #428

merged 13 commits into from
Feb 14, 2025

Conversation

SleeplessOne1917
Copy link
Contributor

@SleeplessOne1917 SleeplessOne1917 commented Feb 1, 2025

Closes #426 and closes #417.

Judging by the linked issues, I'm not the only one interested in using Tailwind v4 with Leptos. I added code to support use on linux musl, though I'm not sure how to test that it's correct. I also haven't figured out how to prevent the generation of a JS config file (which isn't needed in Tailwind v4) when using v4. Because of all of this, I'm opening this PR as a draft.

target_arch: &str,
version: Option<&str>,
) -> Result<String>;
fn executable_name(&self, target_os: &str, target_arch: &str, version: &str) -> Result<String>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made version a &str instead of an Option<&str> since the only place the function is called, the version is guarunteed to be known. It also doesn't seem like this function will be called in a different place.

@@ -331,6 +331,8 @@ impl Command for CommandTailwind {

/// Tool binary download url for the given OS and platform arch
fn download_url(&self, target_os: &str, target_arch: &str, version: &str) -> Result<String> {
let use_musl = is_linux_musl_env() && version.starts_with("v4");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I should put in the effort to make the Tailwind version check more robust. I doubt they'll be coming out with a new major version any time soon.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might consider making it error if the version is greater than 4 and provide a useful error message

@Eveeifyeve
Copy link

I also haven't figured out how to prevent the generation of a JS config file (which isn't needed in Tailwind v4) when using v4.

Here:

pub fn new(conf: &ProjectConfig) -> Result<Option<Self>> {
let input_file = if let Some(input_file) = conf.tailwind_input_file.clone() {
conf.config_dir.join(input_file)
} else {
if conf.tailwind_config_file.is_some() {
bail!("The Cargo.toml `tailwind-input-file` is required when using `tailwind-config-file`]");
}
return Ok(None);
};
let config_file = conf.config_dir.join(
conf.tailwind_config_file
.clone()
.unwrap_or_else(|| Utf8PathBuf::from("tailwind.config.js")),
);

Because of all of this, I'm opening this PR as a draft.

Please mark it as a draft pr and not ready for review just to communicate that this is still work in progress and not ready to review.

@benwis
Copy link
Contributor

benwis commented Feb 1, 2025

How are you planning on telling Tailwind to scan .rs files without a tailwind config file?

@benwis benwis marked this pull request as draft February 1, 2025 23:20
@SleeplessOne1917
Copy link
Contributor Author

@Eveeifyeve

Please mark it as a draft pr and not ready for review just to communicate that this is still work in progress and not ready to review.

My apologies. I accidentally opened it as ready for review and was unable to find out how to revert it to a draft PR after the fact.

@benwis

How are you planning on telling Tailwind to scan .rs files without a tailwind config file?

According to the Tailwind CSS docs, almost every type of file gets scanned:

Tailwind will scan every file in your project for class names, except in the following cases:

  • Files that are in your .gitignore file
  • Binary files like images, videos, or zip files
  • CSS files
  • Common package manager lock files

If you need to scan any files that Tailwind is ignoring by default, you can explicitly register those sources.

If the rust files still get ignored, we can still add sources explicity wiith @source in the CSS file.

@benwis
Copy link
Contributor

benwis commented Feb 2, 2025

@Eveeifyeve

Please mark it as a draft pr and not ready for review just to communicate that this is still work in progress and not ready to review.

My apologies. I accidentally opened it as ready for review and was unable to find out how to revert it to a draft PR after the fact.

@benwis

How are you planning on telling Tailwind to scan .rs files without a tailwind config file?

According to the Tailwind CSS docs, almost every type of file gets scanned:

Tailwind will scan every file in your project for class names, except in the following cases:

  • Files that are in your .gitignore file
  • Binary files like images, videos, or zip files
  • CSS files
  • Common package manager lock files

If you need to scan any files that Tailwind is ignoring by default, you can explicitly register those sources.

If the rust files still get ignored, we can still add sources explicity wiith @source in the CSS file.

Good to know, I have not read their new docs

@SleeplessOne1917
Copy link
Contributor Author

SleeplessOne1917 commented Feb 2, 2025

From what I can tell, the JS config file is autogenerated in src/compile/tailwind.rs as opposed to src/config/tailwind.rs as linked in a previous comment. The code in src/config/tailwind.rs does generate a TailwindConfig instance, but the only info TailwindConfig::new has to generate that is a &ProjectConfig, which doesn't contain any info directly about which tailwind version is being used. The current version is used in src/ext/exe.rs, although I couldn't find a clean way of making that accessible to code in other modules.

I could use the environment variable to tell what the version is, although that will break if the env var isn't specified and the user is using the default version. Alternatively, since the &ProjectConfig being passed to TailwindConfig::new has the path of the input tailwindcss file, I could read the first line of the file to see if it equals @import "tailwindcss";. Neither of these solutions are ideal, though.

Edit: Another idea occurred to me. Users could specify which version of Tailwind they want to use in the Cargo.toml compilation/site parameters. This could take precedence over the default version hardcoded in this codebase, but be overrideable the environment variable.

Yet another approach could be scanning a package.json file, if present, for the version it uses.

@SleeplessOne1917
Copy link
Contributor Author

I decided to handle it with the VersionConfig enum in src/config/version.rs. Let me know if this is an acceptable way of managing it.

@SleeplessOne1917
Copy link
Contributor Author

Just remembered that preventing the JS config from generating when using v4 was the reason I marked this as draft. Now that that is handled, I'm marking this as ready for review again.

@SleeplessOne1917 SleeplessOne1917 marked this pull request as ready for review February 14, 2025 00:12
@benwis
Copy link
Contributor

benwis commented Feb 14, 2025

I think this is ready to merge once we make it error on tailwind v5. Nice work @SleeplessOne1917

Copy link
Contributor

@benwis benwis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@benwis
Copy link
Contributor

benwis commented Feb 14, 2025

Thanks @SleeplessOne1917 I know the Tailwind using folks appreciate ya

@benwis benwis merged commit 4964e88 into leptos-rs:main Feb 14, 2025
8 checks passed
@SleeplessOne1917 SleeplessOne1917 deleted the tailwind-v4 branch February 14, 2025 03:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cargo leptos build loads wrong version of the tailwingcss binary Tailwindcss v0.4 support
3 participants