rollup-plugin-purgecss
v9.1.0
Published
Rollup plugin for purgecss
Readme
rollup-plugin-purgecss
Rollup plugin to remove unused css.
Install
npm i rollup-plugin-purgecss -DUsage
// rollup.config.js
import purgecss from "rollup-plugin-purgecss";
export default {
input: "main.js",
output: { dir: "dist" },
plugins: [
purgecss({
content: ["index.html", "src/**/*.js"],
}),
],
};Each imported CSS file (**/*.css outside of the node_modules directories
by default) is purged and becomes a JS module whose default export is the purged CSS:
import css from "./style.css"; // the purged CSS, as a stringCSS files imported with a query, e.g. ./style.css?raw, are not purged.
The plugin works the same way with Rolldown.
In watch mode (rollup -w), the content files are watched: changing one of
them purges the CSS again, also after a build that failed because of it. A new
file matching a glob of content is only used once the CSS is purged again,
e.g. after a change to a watched file or a restart. The CSS files are also
purged again when the build reuses a Rollup cache, as the content may have
changed. The content files are only read again when they changed.
Options
The plugin accepts the PurgeCSS options
(except css, which is replaced by the imported CSS files) and:
content: relative paths and globs are resolved from the current working directory (from Vite'srootwith Vite, as are the relative globs ofskippedContentGlobs). WithoutcontentandcontentFunction, all the CSS is removed, with a warning.include/exclude: picomatch patterns or regular expressions of the files to purge,["**/*.css"]and"**/node_modules/**"by default: CSS imported from a dependency (e.g.import "bootstrap/dist/css/bootstrap.css") is not purged unlessexcludeis changed, wherever itsnode_modulesdirectory is (e.g. at the root of a workspace). Relative patterns are resolved from the current working directory.contentFunction(sourceFile): returns the content to use for the CSS file at the pathsourceFile. When it is set,contentis not used (and not needed).output: what to do with the purged CSS of all the CSS files once the bundle is generated, in the order the files are imported:- a path: the CSS is written to this file, relative to the current working
directory (to Vite's
rootwith Vite). Its directory is created when it does not exist. - a function: it is called with the CSS and an array of the CSS of each
file,
(css, styles) => {}.
With Rollup and Rolldown, when
outputis set, the imported CSS files export an empty string. When a build no longer has CSS files, e.g. in watch mode, the file is emptied and the function is called with an empty string.- a path: the CSS is written to this file, relative to the current working
directory (to Vite's
insert: whentrue, the imported CSS files insert their purged CSS in the<head>of the document, in a<style>element, when the bundle runs in a browser. They still export their CSS.sourceMap: with Rollup and Rolldown, the source map is inlined in the purged CSS exported by each CSS file, as the CSS files become JS modules. The CSS given tooutputhas no source map. With Vite, it is handed to Vite along with the CSS.
The types of the plugin import the types of rollup and postcss: if
TypeScript cannot find them, e.g. with Vite or Rolldown, or with pnpm, which
does not hoist the dependencies of purgecss, install them as dev
dependencies or enable skipLibCheck.
Vite
With Vite, the plugin hands the purged CSS back to Vite, which bundles and
minifies it as usual. It is tested with Vite 8. Add it to the top-level
plugins array of the Vite config:
// vite.config.js
import purgecss from "rollup-plugin-purgecss";
export default {
plugins: [
purgecss({
content: ["index.html", "src/**/*.{js,ts,jsx,tsx}"],
}),
],
};- The plugin must be in the
pluginsarray, withoutenforce: "post". Inbuild.rolldownOptions.pluginsorbuild.rollupOptions.plugins, or withenforce: "post", it runs after Vite has turned the CSS files into JS modules and cannot purge them: with Vite 7 and later, the build fails with an error telling to move it. It can be restricted to some environments withperEnvironmentPlugin(Vite then logs that the plugin'sconfigResolvedhook is ignored, which is expected). - Relative paths and globs of
contentandskippedContentGlobs, and theoutputpath, are resolved from Vite'sroot, like the other paths of the Vite config. - The CSS is only purged by
vite build. The dev server serves it as it is, as it would not purge it again when the content changes. - CSS modules (
*.module.css) are left untouched, with a warning: Vite renames their classes before the plugin runs, so they cannot be found in the content. Exclude them withexcludeto silence the warning, or use @fullhuman/postcss-purgecss in Vite's PostCSS config. inserthas no effect, and the imported CSS files are not turned into JS modules.outputgets a copy of the purged CSS that Vite bundles in its CSS files: Vite still emits its own CSS file, so do not link both in the page. It does not get the CSS imported with?inline, nor the CSS of the<style>elements andstyleattributes ofindex.html, which Vite leaves in the HTML. When Vite builds several environments (e.g.vite build --appor an SSR framework), it only gets the CSS of the client environments, not of the server ones (ssr), which usually import the same CSS. The URLs of the assets, e.g.url(./bg.png), are the URLs of the files Vite emits: with Vite's defaultbase,/assets/bg-[hash].png, and with a relativebase, a path relative to theoutputfile.- CSS files imported with
?inlineare purged. Those imported with another query, e.g.?rawor?url, are not. - Files processed by a preprocessor (e.g.
.scss) are only purged when they matchinclude. - CSS imported from a
node_modulesdirectory is not purged by default, seeexclude. - The style blocks of Vue and Svelte components match
includetoo. Their scoped styles use selectors that are not in the content ([data-v-…],.svelte-…): safelist them, e.g.safelist: [/data-v-/]orsafelist: [/svelte-/], or exclude the components, e.g.exclude: ["**/node_modules/**", "**/*.vue?*"]. With Vue, also follow the Vue guide to leave out the selectors of the<style>blocks from the content.
Contributing
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
Versioning
We use SemVer for versioning.
License
This project is licensed under the MIT License - see the LICENSE file for details
