- Published on
Fixing Tailwind and Vite Warnings: 'Didn't Resolve at Build Time'
- Authors
- Name
- Ripal & Zalak
Fixing Tailwind and Vite Warnings: "Didn't Resolve at Build Time"
If you are using TailwindCSS with Vite and seeing warnings like this during the build process:
/img/logo.png referenced in D:/git/my-org/my-repo/src/app.css
didn't resolve at build time, it will remain unchanged to be resolved at runtime
/fonts/Inter-Thin.woff2?v=3.19 referenced in D:/git/my-org/my-repo/src/inter.css
didn't resolve at build time, it will remain unchanged to be resolved at runtime
Don’t worry—you’re not alone! This guide explains what these warnings mean and provides practical solutions to resolve them.
What Do These Warnings Mean?
These warnings indicate that some paths or assets (like images, fonts, or files referenced in your CSS) were not processed at build time. Instead, Vite is leaving them to resolve dynamically at runtime.
Although the build output usually works fine, these warnings suggest that there could be potential issues down the line. It’s always best to treat warnings as "errors from the future."
Common Scenarios That Trigger the Warning
- Background images in CSS: Using
class="bg-[url('/favicon.png')]
or similar. - Custom font imports: Fonts imported using
@font-face
with relative paths. - Arbitrary file paths in Tailwind: Referencing files with non-standard paths.
Solutions to Resolve the Warnings
1. Use Absolute Paths for Assets
Make sure to use root-relative paths for assets like images or fonts. For example:
.selector {
background-image: url('/src/assets/images/logo.png');
}
Instead of:
.selector {
background-image: url('./src/assets/images/logo.png');
}
2. Leverage Vite’s Public Directory
Vite provides a public
directory where you can store static assets like images or fonts. Files in this directory are served at the root level of your project.
- Move your assets to the
public
folder.public/ └── logo.png
- Reference them using root-relative paths:
.selector { background-image: url('/logo.png'); }
3. Configure Aliases in Vite
You can define aliases in vite.config.ts
to simplify path resolution for static files:
import { resolve } from 'path'
import { defineConfig } from 'vite'
export default defineConfig({
resolve: {
alias: {
$assets: resolve('./src/assets'),
$fonts: resolve('./static/fonts'),
},
},
})
Then, use these aliases in your CSS or JS:
@font-face {
src: url('$fonts/Inter-Thin.woff2') format('woff2');
}
.selector {
background-image: url('$assets/logo.png');
}
4. Ensure Compatibility for Both Dev and Build Modes
Sometimes, paths behave differently in dev
and build
modes. To handle this, use the base
configuration in vite.config.ts
to ensure consistent path resolution:
export default defineConfig({
base: '/',
})
public
Directive for Background Images
5. Use Tailwind’s For background images, Tailwind CSS recommends using public paths:
<div class="bg-[url('/logo.png')]"></div>
FAQs
Q: Why does this warning appear only during build?
During the build process, Vite attempts to resolve asset paths. If it cannot resolve them at build time, it logs this warning. This is typically because of relative paths or improper configuration.
Q: Is it okay to ignore this warning?
While the project may function correctly, ignoring these warnings could lead to issues in production, especially with dynamic assets.
Q: Do I need to change anything for TailwindCSS plugins?
Yes, if plugins like Typography reference files (e.g., fonts), ensure they follow the same practices as above.