Description
PostCSS plugins rely on ctx.file to access file-specific context (like path and metadata) during CSS processing. In Vite's current build process, this property is undefined, creating a compatibility gap with Webpack and Rollup.
This affects plugins that need to:
- Access the current CSS file's path
- Perform path-based calculations (e.g., for relative imports)
- Generate file-specific metadata or cache keys
- Apply conditional logic based on file location or name
The issue forces developers to avoid dependent plugins, implement fragile workarounds, or face migration hurdles when moving from Webpack/Rollup to Vite.
Benefits & Use Cases
With ctx.file implemented:
- Path resolution: Plugins can accurately resolve relative paths for assets or imports.
- File-specific logic: Plugins can apply transformations conditionally based on file attributes or directories.
- Seamless migration: Plugins working in Webpack/Rollup will function identically in Vite.
- Better debugging: Error messages can include the problematic file's path.
- Enhanced capabilities: Supports advanced plugins for source maps, caching, or file-specific outputs.
This completes Vite's PostCSS integration, supporting the full PostCSS plugin ecosystem.
Suggested solution
Vite should properly implement the PostCSS API by:
- Providing the
from option: Pass the absolute path of the CSS file to postcss.process().
- Populating
ctx.file: Ensure the PostCSS context includes a valid file property with the source file's absolute path and metadata.
- Ensuring consistency: Match the behavior of Webpack (
postcss-loader) and Rollup's PostCSS integration.
- Supporting all modes: Work consistently in both development server and production builds.
The implementation should be transparent—no user configuration changes required—enabling existing plugins that use ctx.file to work automatically.
Alternative
No response
Additional context
- References: Webpack's
postcss-loader and Rollup's PostCSS plugin demonstrate correct implementation.
- PostCSS API: The
from option is documented as the method to provide file context to plugins.
- Ecosystem impact: Popular plugins (e.g., autoprefixer, postcss-import) depend on
ctx.file. Full compatibility benefits the Vite ecosystem.
- Migration: Fixing this eliminates a key barrier for projects transitioning from Webpack/Rollup.
Similar issues exists but closed and locked
Validations
Description
PostCSS plugins rely on ctx.file to access file-specific context (like path and metadata) during CSS processing. In Vite's current build process, this property is undefined, creating a compatibility gap with Webpack and Rollup.
This affects plugins that need to:
The issue forces developers to avoid dependent plugins, implement fragile workarounds, or face migration hurdles when moving from Webpack/Rollup to Vite.
Benefits & Use Cases
With
ctx.fileimplemented:This completes Vite's PostCSS integration, supporting the full PostCSS plugin ecosystem.
Suggested solution
Vite should properly implement the PostCSS API by:
fromoption: Pass the absolute path of the CSS file topostcss.process().ctx.file: Ensure the PostCSS context includes a validfileproperty with the source file's absolute path and metadata.postcss-loader) and Rollup's PostCSS integration.The implementation should be transparent—no user configuration changes required—enabling existing plugins that use
ctx.fileto work automatically.Alternative
No response
Additional context
postcss-loaderand Rollup's PostCSS plugin demonstrate correct implementation.fromoption is documented as the method to provide file context to plugins.ctx.file. Full compatibility benefits the Vite ecosystem.Similar issues exists but closed and locked
Validations