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

Support dynamic (Jinja-generated) classes #8

Merged
merged 4 commits into from
Mar 8, 2024

Conversation

mathrick
Copy link
Contributor

@mathrick mathrick commented Feb 1, 2024

Run tailwindcss on the build dir, rather than input templates, so that dynamically-generated values are properly seen by tailwind.

Fixes #7.

Comment on lines 61 to 64
subprocess.run(
[
self.tailwind_bin,
"-i",
self.input_css,
"-o",
os.path.join(output_path, self.css_path),
"--minify",
],
self._get_tailwind_args(output_path, "--minify"),
check=True,
cwd=self.env.root_path,
cwd=output_path,
Copy link
Owner

Choose a reason for hiding this comment

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

But this method is run before_build, I doubt if it works

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, fixed.

Run tailwindcss on the build dir, rather than input templates, so that dynamically-generated values are properly seen by tailwind.
Running Tailwind watcher from within Lektor watcher is unnecessary,
since we already have a watcher and know when to rebuild things, and
it leads to bugs, such as the Tailwind watcher leaking memory or
kicking in before the build is fully done.
@mathrick
Copy link
Contributor Author

mathrick commented Mar 1, 2024

I've removed the use of tailwindcss -w, since it never worked reliably, and it doesn't really make sense to use it in the first place. We already have a watcher built in.

@mathrick
Copy link
Contributor Author

mathrick commented Mar 6, 2024

Is there anything else you'd like to see for this PR to be acceptable?

@frostming
Copy link
Owner

frostming commented Mar 7, 2024

Is there anything else you'd like to see for this PR to be acceptable?

Since you removed the tailwind live server, does it work in dev mode? Can it update the stylesheet on class changes? I just didn't find the time to verify it myself

@mathrick
Copy link
Contributor Author

mathrick commented Mar 7, 2024

Since you removed the tailwind live server, does it work in dev mode? Can it update the stylesheet on class changes? I just didn't find the time to verify it myself

Yes, I use it primarily in dev mode. It actually works better now, because the tailwind watcher no longer leaks memory, and it's more reliable. I believe the watcher would previously race lektor sometimes and not get everything updated. I've not had that happen since I removed -w, it's now very solid.

@frostming
Copy link
Owner

@mathrick Can you check the failing tests?

@mathrick
Copy link
Contributor Author

mathrick commented Mar 7, 2024

@frostming will do

@mathrick
Copy link
Contributor Author

mathrick commented Mar 7, 2024

@frostming: fixed, could you retry?

I've been running into weird timeouts in test_server_rebuilds_css_when_input_css_updated() that I really can't see any reason for, and it shouldn't be any of my changes anyway. I had it pass once for no visible reason and then it went back to timing out, so I'm hoping it will just not do that when run through GitHub.

@mathrick
Copy link
Contributor Author

mathrick commented Mar 7, 2024

This is really weird: for some reason, writing the sentinel value in test_server_rebuilds_css_when_input_css_updated() does not trigger a rebuild. But if I watch the test and manually change the file from another terminal before the timeout hits, the server happily picks up the change.

@mathrick
Copy link
Contributor Author

mathrick commented Mar 7, 2024

To make things even weirder, this does not happen if I enter the debugger from within pytest. Which makes me think this is some odd artefact of how the server is run within the tests, and also makes it incredibly difficult to debug. Maybe something with buffering? But I'm not sure what would be buffered exactly that could cause that.

This was caused by the switch from having two watchers (lektor server and
tailwindcss) to just the server. Because we made writes as soon as
wait_for_server() returns, this would essentially catch the server in
a short window before it was done returning from a build, and it
wouldn't pick up the changes, so the rebuild would never
happen. Previously this was not a concern, because the CSS was watched
by one process, and other files by the other, and they delayed each
other, so there was never a chance to write immediately after the
rebuild was done. Now we sleep for a second before writing to prevent
that.

Note that this is purely an artefact of the testing process, and the
fact we can do it within milliseconds. This is not possible for a
human, so it doesn't come up in normal usage.
@mathrick
Copy link
Contributor Author

mathrick commented Mar 7, 2024

Alright, this was extremely painful to debug, but I finally found the issue. To quote from the commit message:

This was caused by the switch from having two watchers (lektor server and tailwindcss) to just the server. Because we made writes as soon as wait_for_server() returns, this would essentially catch the server in a short window before it was done returning from a build, and it wouldn't pick up the changes, so the rebuild would never happen. Previously this was not a concern, because the CSS was watched by one process, and other files by the other, and they delayed each other, so there was never a chance to write immediately after the rebuild was done. Now we sleep for a second before writing to prevent that.

Note that this is purely an artefact of the testing process, and the fact we can do it within milliseconds. This is not possible for a human, so it doesn't come up in normal usage.

@frostming frostming merged commit ea66d2a into frostming:main Mar 8, 2024
10 checks passed
@mathrick mathrick deleted the dynamic-classes branch March 12, 2024 19:05
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.

Dynamic css classes at render time
2 participants