Dynamic safelisting #12812
Replies: 3 comments 3 replies
-
You could try creating PostCSS plugin that wraps Tailwind, conceptually like: const tailwindcss = require('tailwindcss');
const base = require('tailwind.config');
module.exports = () => {
let dynamicSafelist = [];
return {
postcssPlugin: 'my-plugin-name',
plugins: [
(root, result) => {
// Update `dynamicSafelist` here
},
tailwindcss({
presets: [base],
safelist: dynamicSafelist,
}),
],
};
}; |
Beta Was this translation helpful? Give feedback.
-
@wongjn So, I got everything to work. The repo does now look for (changing) The code sample does:
For reference, here is the postcss config file: https://github.com/frontline-hq/tailwind-dynamic-safelist/blob/main/postcss.config.js#L14 I am still using a shitty way to restart the vite dev server (just rewriting the vite config file to the file tree, but without changes) |
Beta Was this translation helpful? Give feedback.
-
So there is an even better way to do this, which only requires a vite plugin and a tiny change to frontline-hq/tailwind-dynamic-safelist@a969acd It adds the tailwind safelist directly to the tailwind config and utilizes rewriting the config to the filesystem to restart the server (instead of server.restart()). |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
As already discussed here, it is currently not possible to enable dynamic safelisting in tailwind.
Just so that you can understand what I am talking about: With dynamic safelisting I mean safelists that depend on the content of the processed files.
Currently, the only way to do something like that: Write the safelist to a file and include that file in the safelist configuration in tailwind.config.
But that might suffer from a double build problem, as the safelist can only be generated after the bundler (vite e.g.) has run which will then generate the safelist files which might trigger a rebuild.
Now I have an idea about how one could enable dynamic safelisting nevertheless:
Couldn't one write a tailwind plugin that reads the relevant files from the filesystem (e.g.
**/*.svelte
files), processes them to determine the dynamic safelist and then returns the dynamic safelist as part of the configuration?Problems / questions:
Beta Was this translation helpful? Give feedback.
All reactions