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

Pre process Slim templates embedded in Ruby files #17336

Merged
merged 8 commits into from
Mar 24, 2025
Merged

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Mar 22, 2025

This PR fixes an issue where embedded Slim templates inside of Ruby files are not pre processed because we pre process based on a file extension.

This PR also handles embedded SLIM templates using the following syntax:

slim_template <<~SLIM
  .flex
    .flex-1
      h1 Hello World
    .flex-1
      p This is a test
SLIM

As far as I can tell, this is only a Slim template thing and not a Haml template thing but I could be wrong here. See: https://viewcomponent.org/guide/templates.html#interpolations

The ViewComponent package handles anything that looks like {lang}_template, so the lang here will be used as the pre processing language for now.

Fixes: #17334

Test plan

  1. Added test for this
  2. Existing tests pass
  3. Made sure that the snippet from the issue works as expected:

Added an example where we have a slim_template and a svelte_template to prove that it embeds based on the language. I also added a html_template with Svelte syntax to really make sure that that doesn't work.

image

This builds on top of the regex but also has support for back
references.

Note: backreferences make it much slower than using the regex crate
directly, but we need to make sure that the heredoc is properly closed.
We were hard coding `slim`, but with this change anything works. Verified
it by adding a `svelte_template` test.
Let's make the captures lazy instead of greedy to capture as few
characters as possible.
CHANGELOG.md Outdated
@@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Increase Standalone hardware compatibility on macOS x64 builds ([#17267](https://github.com/tailwindlabs/tailwindcss/pull/17267))
- Ensure that the CSS file rebuilds if a new CSS variable is used from templates ([#17301](https://github.com/tailwindlabs/tailwindcss/pull/17301))
- Fix class extraction followed by `(` in Pug ([#17320](https://github.com/tailwindlabs/tailwindcss/pull/17320))
- Pre process `Slim` templates embedded in Ruby files ([#17336](https://github.com/tailwindlabs/tailwindcss/pull/17336))
Copy link
Member

Choose a reason for hiding this comment

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

looks like this was added to the 4.0.15 section too 🙈

Copy link
Member Author

Choose a reason for hiding this comment

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

Woopsie, yep, fixed

@@ -19,6 +19,7 @@ bexpand = "1.2.0"
fast-glob = "0.4.3"
classification-macros = { path = "../classification-macros" }
regex = "1.11.1"
fancy-regex = "0.14.0"
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure why the regex library alone is not enough?

Copy link
Member Author

Choose a reason for hiding this comment

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

use std::sync;

static TEMPLATE_REGEX: sync::LazyLock<Regex> = sync::LazyLock::new(|| {
Regex::new(r#"\s*(.*?)_template\s*<<[-~]?([A-Z]+?)\n([\s\S]*?)\2"#).unwrap()
Copy link
Member

Choose a reason for hiding this comment

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

TIL about \2 🤯

@RobinMalfait RobinMalfait merged commit 711b9cd into main Mar 24, 2025
6 checks passed
@RobinMalfait RobinMalfait deleted the fix/issue-17334 branch March 24, 2025 10:58
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.

Tailwind v4 is not seeing classes in slim templates
2 participants