Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
vite-plugin-unused-css-vars
Advanced tools
Vite plugin to remove unused css variables using lightningcss custom transforms.
A simple Vite plugin that removes unused variables from your bundled CSS files using lightningcss custom transforms.
:warning: Please note that this plugin only works with
vite build
command and notvite dev
.
npm i --save-dev vite-plugin-unused-css-vars lightningcss
# pnpm add -D vite-plugin-unused-css-vars lightningcss
# yarn add -D vite-plugin-unused-css-vars lightningcss
# bun add -D vite-plugin-unused-css-vars lightningcss
import { defineConfig } from 'vite'
import unusedCssVars from 'vite-plugin-unused-css-vars'
export default defineConfig({
plugins: [unusedCssVars()],
})
The plugin looks for variable declarations and references across all chunks bundled by Rollup:
/* chunk-1.css */
:root {
--used-var: #fff; /* <-- Declaration */
}
/* chunk-2.css */
.my-class {
color: var(--used-var); /* <-- Reference */
}
If a variable is never referenced, all its declarations are stripped away from any chunk that contains them:
/* chunk-3.css */
:root {
--unused-var: #fff; /* <-- Removed */
}
The plugin doesn't perform any transpilation or additional processing; think of it as a find/replace operation on the files that will be shipped to the browser.
Enable the log
option to see which variables were removed:
export default defineConfig({
plugins: [
unusedCssVars({
log: true, // Default: false
}),
],
})
Pass an array of variable names to the exclude
option to prevent them from being removed:
export default defineConfig({
plugins: [
unusedCssVars({
exclude: ['--color'],
}),
],
})
By default, this plugin assumes that your files are minified and will perform the minification again when rewriting files that contains unused variables.
In case you are not emitting minified files, you can disable this behavior by passing false
to the minify
option:
export default defineConfig({
plugins: [
unusedCssVars({
minify: false,
}),
],
})
Some frameworks may not always output CSS files but instead generate a JS file that injects the styles directly in the HTML.
An example of this is Nuxt, when using the css
option in nuxt.config.js
:
export default defineNuxtConfig({
css: ['@/assets/variables.css'],
})
In this case the plugin won't be able to remove unused variables, as it only works by reading CSS files.
For this specific case, import the file in your default.vue
layout or app.vue
component to make sure it's bundled as a CSS file:
<script setup>
import '@/assets/variables.css'
</script>
If you are referencing CSS variables directly in your HTML, the plugin won't be able to detect the references and will remove their declarations:
<div class="my-var" style="color: var(--color);"></div>
.my-var {
--color: red; /* <-- Removed */
}
In such case, you might want to exclude those variables from being removed.
MIT
FAQs
Vite plugin to remove unused css variables using lightningcss custom transforms.
The npm package vite-plugin-unused-css-vars receives a total of 0 weekly downloads. As such, vite-plugin-unused-css-vars popularity was classified as not popular.
We found that vite-plugin-unused-css-vars demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.