36
loading...
This website collects cookies to deliver better user experience
@
,:
, and /
. Therefore, PurgeCSS may not fit the exact file type or CSS framework that you're using.fontFace
, keyframes
, extractors
, css
, and more. Customization can improve the performance and efficiency of PurgeCSS.npx create-react-app purgecss-tutorial
purgecss-tutorial
directory we just created:cd purgecss-tutorial
npm i --save-dev @fullhuman/postcss-purgecss glob-all purgecss-webpack-plugin
App.js
file and paste the following code:import React from 'react';
import "./App.css";
function App() {
return <div className="App"></div>;
}
export default App;
App
and returned a div
with a classname
of App
.App.css
is left untouched, so it contains the following unused CSS code:.App {
text-align: center;
}
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
package.json
file and add the following line under scripts:"postbuild": "purgecss --css build/static/css/*.css --content build/index.html build/static/js/*.js --output build/static/css"
post
is a prefix that can be added to any npm script and will automatically run when you run the main script. In our case, postbuild
runs after the build
script is executed.postbuild
contains three options. The --css
option specifies what CSS files PurgeCSS should process. It can be an array of filenames or globs. The --content
option is similar to the --css
option, specifying what content should be analyzed by PurgeCSS. The --output
option specifies what directory you should write the purified CSS files to. By default, it places the result in the console.postbuild
does the following:build/static/css
build/static/css
config/webpack.prod.conf.js
file by adding the following code along with the rest of the imports:// import PurgeCSS webpack plugin and glob-all
const PurgecssPlugin = require('purgecss-webpack-plugin')
const glob = require('glob-all')
new ManifestPlugin(...)
in the plugins list, add the code below:new PurgecssPlugin({
paths: [paths.appHtml, ...glob.sync(`${paths.appSrc}/**/*`, { nodir: true })]
}),
build/static/css
. The output looks like the code below, containing only the used CSS:body{margin:0;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Roboto","Oxygen","Ubuntu","Cantarell","Fira Sans","Droid Sans","Helvetica Neue",sans-serif;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.App{text-align:center}@keyframes App-logo-spin{0%{transform:rotate(0deg)}to{transform:rotate(1turn)}}
safelist
or the special CSS special comment./* purgecss start ignore */
and /* purgecss end ignore */
to safelist a range of rules. To prevent our comments from being removed, we add an exclamation mark to tell PurgeCSS that it is important. See the example below:/*! purgecss start ignore */
h1 {
color: pink;
font-size: 2rem;
}
/*! purgecss end ignore */
keyframes
and font-faces
options to true
.