How to Prevent Duplicate CSS When Merging Tailwind 4 Output at Build and Runtime? #18526
Replies: 2 comments 6 replies
-
|
You didn't provide the source file for the build-time CSS, but presumably, if it has @import "tailwindcss/theme.css" source(none) layer(theme);
@import "tailwindcss/utilities.css" source(none) layer(utilities);
@source "#{temp_dir}/*.template";This removes the |
Beta Was this translation helpful? Give feedback.
-
|
@SlavaMryasov i re-check it last night, it is my elixir backend code defp build_css_content(site_name) do
temp_dir = get_temp_dir()
app_root = Application.app_dir(:mishka_cms)
app_css_path = Path.expand("priv/static/assets/css/app.css", app_root)
heroicons_path = Path.expand("priv/static/temp/js_assets/vendor/heroicons.js", app_root)
# Read the app.css content if it exists, otherwise use empty string
app_css_content =
case File.read(app_css_path) do
{:ok, content} ->
Logger.debug("Reading app.css from #{app_css_path}, size: #{byte_size(content)} bytes")
content
{:error, _reason} ->
Logger.warning("app.css not found at #{app_css_path}, using empty CSS")
""
end
"""
@import "tailwindcss" source(none);
/* Scan runtime templates */
@source "#{temp_dir}/tailwind_classes.template";
@source "#{temp_dir}/runtime/#{site_name}/**/*.template";
@source "#{temp_dir}/runtime/__global__/**/*.template";
/* Scan JS/TS assets */
@source "#{temp_dir}/js_assets/**/*.js";
@source "#{temp_dir}/js_assets/**/*.ts";
/* Add plugins and custom variants */
@plugin "#{heroicons_path}";
@custom-variant phx-click-loading (.phx-click-loading&, .phx-click-loading &);
@custom-variant phx-submit-loading (.phx-submit-loading&, .phx-submit-loading &);
@custom-variant phx-change-loading (.phx-change-loading&, .phx-change-loading &);
/* Use the data attribute for dark mode */
@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));
/* Make LiveView wrapper divs transparent for layout */
[data-phx-session], [data-phx-teleported-src] { display: contents }
/* App CSS content directly embedded */
#{app_css_content}
"""
endAs you see first it loads it is my app.css for example It solves your problem? |
Beta Was this translation helpful? Give feedback.


Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi. I've recently been working on implementing a system that allows me to merge the Tailwind CSS classes generated at build time with additional CSS classes generated at runtime using the Tailwind binary. (I am using Tailwind 4)
Unfortunately, the issue I'm facing is CSS duplication in my source code, which increases the overall size of the output.
For example, here's a snippet of the CSS that's generated at build time by Tailwind by scanning all of my non-compiled source files:
Now, at runtime, based on the user's request, I generate temporary files with the
.templateextension like this:build_css_content()functionThen I pass the arguments like so: (imagine it is a JS code) i just mixed old css and new css
Here’s the problem:
If I don’t include
@import "tailwindcss" source(none);, the CLI doesn’t produce any output at runtime. But when I include it, it works — except that many parts of the CSS are duplicated.What I really need is a way to completely eliminate duplicated CSS.
Note: Since the source code is compiled (it's a compiled language), I can't scan the source files again at runtime.
What solution would you suggest?
Beta Was this translation helpful? Give feedback.
All reactions