Currently I'm developing a website using the following stack:
- vue.js
- @vue/server-renderer
- vite
- tailwind CSS
SSG was chosen as the rendering mode.
Tailwind, as described in the documentation, allows you to specify directories and file extensions (content
property) , in which he will try to find the classes, in order to leave only those that are actually used in the project.
As a result, the 'main.css'
file is formed, in which only those classes that are used remain.
Next, I just take this file and include it in every page that was rendered during the build phase of the project.
This results in:
index.html - main.css
about.html - main.css
blog.html - main.css
It turns out that main.css
file may contain classes that are needed only for one of the pages and are not needed for others.
I would like to implement the following:
- Take main.css which turned out
- Render a page, for examle about.html
- take only those styles that are needed for about.html page from the main.css file
- create a new about.css file
- link the resulting about.css styles to about.html
I’ve already tried to implement this using an awesome PurgeCSS tool as following:
- render page content usind @vue/server-renderer's renderToString() method;
- Pass an resulting css and html sources to PurgeCSS here is an example
But there are too many corner cases around this solution, such as:
- Dynamic classes which can be added to the html on the client side
- Some components may be missing in rendered html and their content will be added later (for example, v-if directive was used on the component)
A few takeaways:
bg-red-500
forindex
orabout
page, both are referencing the same unique declaration)bg-[#ccc]
but it goes on the opposite side of what Tailwind is achieving and should be used exceptionallycontent
, still the defaults on this page, sectionConfigure your template paths
, no need to do anything crazy or complicatedstyle scoped
, you can still use Tailwind inside of those tags thoindex
,about
,blog
etc, then Tailwind is probably not the best approach because this is not how it is supposed to workscreens
,colors
etc that you're not using