Importing Asset as URL
Importing a static asset will return the resolved public URL when it is served:imgUrl will be /src/img.png during development, and become /assets/img.2d8efhg.png in the production build.
The behavior is similar to webpack’s file-loader. The difference is that the import can be either using absolute public paths (based on project root during dev) or relative paths.
Asset Handling Features:
url()references in CSS are handled the same way.- If using the Vue plugin, asset references in Vue SFC templates are automatically converted into imports.
- Common image, media, and font filetypes are detected as assets automatically. You can extend the internal list using the
assetsIncludeoption. - Referenced assets are included as part of the build assets graph, will get hashed file names, and can be processed by plugins for optimization.
- Assets smaller in bytes than the
assetsInlineLimitoption will be inlined as base64 data URLs.
Asset Import Types
Vite supports various ways to import assets with different suffixes to control how they’re handled.- URL Import
- Inline/No-Inline
- String Import
- Worker Import
Assets that are not included in the internal list or in
assetsInclude can be explicitly imported as a URL using the ?url suffix. This is useful, for example, to import Houdini Paint Worklets.The public Directory
If you have assets that are:
- Never referenced in source code (e.g.
robots.txt) - Must retain the exact same file name (without hashing)
- …or you simply don’t want to have to import an asset first just to get its URL
public directory under your project root. Assets in this directory will be served at root path / during dev, and copied to the root of the dist directory as-is.
The directory defaults to <root>/public, but can be configured via the publicDir option.
You should always reference
public assets using root absolute path - for example, public/icon.png should be referenced in source code as /icon.png.Dynamic Asset URLs with new URL()
import.meta.url is a native ESM feature that exposes the current module’s URL. Combining it with the native URL constructor, we can obtain the full, resolved URL of a static asset using relative path from a JavaScript module:
This works natively in modern browsers - in fact, Vite doesn’t need to process this code at all during development!
build.target does not support import.meta.url.
TypeScript Support
TypeScript, by default, does not recognize static asset imports as valid modules. To fix this, includevite/client in your tsconfig.json:
tsconfig.json
d.ts declaration file:
vite-env.d.ts
*.svg a React component:
Asset Configuration Options
assetsInclude
Extend the internal list of asset types that Vite should treat as assets.
assetsInlineLimit
Control the threshold (in bytes) for inlining assets as base64 data URLs. Assets smaller than this limit will be inlined.
publicDir
Configure the directory to serve as plain static assets. Files in this directory are served at / during dev and copied to the root of outDir during build.
The
publicDir can be disabled by setting it to false. Files in the disabled public directory won’t be served or copied.