Tailwind CSS v3 content path resolution in nested project layouts
0 reputation · 18 Feb 2023, 01:03 UTC
0 reputation · 18 Feb 2023, 01:03 UTC
Tailwind CSS v3 runs the Just-In-Time engine by default and generates utility CSS only from class names found in files matched by the content array. The goal is to scan utility classes used across multiple packages in a monorepo where tailwind.config.js lives in a subdirectory rather than the repository root.
Because content globs resolve relative to the configuration file location, it is unclear whether patterns can reach sibling packages outside that directory, and whether dynamically assembled class names in those packages require the safelist option to appear in the built CSS. This question assumes Tailwind CSS v3.x and should be confirmed against the installed version before changing build configuration.
Can content globs reference directories above or beside the configuration file? What relative path syntax reliably includes sibling packages without breaking on different operating systems? Must runtime-assembled class names be added to safelist even when the files containing them are already scanned?
34775 reputation · 18 Feb 2023, 04:29 UTC
In Tailwind CSS v3, content array globs resolve relative to the location of the tailwind.config.js file. If your configuration is located in a subdirectory, you can reference sibling or parent directories using standard relative path syntax (../).
Yes, you can reference directories above or beside the configuration file. To ensure reliability across different operating systems (Windows, macOS, Linux), use forward slashes; Node.js, which Tailwind uses, resolves these correctly on all platforms.
// tailwind.config.js located in /packages/app/
module.exports = {
content: [
'./src/**/*.{js,ts,jsx,tsx}', // Current package
'../sibling-package/src/**/*.{js,ts,jsx,tsx}', // Sibling package
],
// ... rest of config
};
The JIT engine works by scanning source files for literal string matches. It does not execute your JavaScript code to determine what classes might be built.
text-${color}), Tailwind will not find the literal text-red-500 or text-blue-500.safelist option to ensure they are included in the final CSS.module.exports = {
content: ['./src/**/*.{js}'],
safelist: [
'text-(red|blue|green)-500', // Regex or string to match dynamic classes
]
};
Note: To avoid massive safelists, it is often better to use full class names in your logic (e.g., const colors = { red: 'text-red-500' }) so the JIT engine can find them naturally.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.