Last active
April 7, 2023 10:02
-
-
Save paulund/d56f556a37d83ab20feb4e258f4017b8 to your computer and use it in GitHub Desktop.
Using CSS Purge With Laravel Mix. Tutorial on how to use the following can be found https://paulund.co.uk/reduce-css-file-size-with-css-purge reduced a production ready CSS file from 179kb to 7.9kb.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mix.webpackConfig({ | |
plugins: [ | |
new purgeCss({ | |
paths: glob.sync([ | |
path.join(__dirname, 'resources/views/**/*.blade.php'), | |
path.join(__dirname, 'resources/assets/js/**/*.vue') | |
]), | |
extractors: [ | |
{ | |
extractor: class { | |
static extract(content) { | |
return content.match(/[A-z0-9-:\/]+/g) | |
} | |
}, | |
extensions: ['html', 'js', 'php', 'vue'] | |
} | |
] | |
}) | |
] | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let purgeCss = require('purgecss-webpack-plugin') | |
let glob = require('glob-all') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install purgecss-webpack-plugin --save-dev | |
npm install glob-all --save-dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let mix = require('laravel-mix'); | |
let purgeCss = require('purgecss-webpack-plugin') | |
let glob = require('glob-all') | |
var tailwindcss = require('tailwindcss'); | |
/* | |
|-------------------------------------------------------------------------- | |
| Mix Asset Management | |
|-------------------------------------------------------------------------- | |
| | |
| Mix provides a clean, fluent API for defining some Webpack build steps | |
| for your Laravel application. By default, we are compiling the Sass | |
| file for the application as well as bundling up all the JS files. | |
| | |
*/ | |
mix.js('resources/assets/js/app.js', 'public/js'); | |
mix.sass('resources/assets/sass/app.scss', 'public/css') | |
.options({ | |
processCssUrls: false, | |
postCss: [ require('postcss-import'), tailwindcss('tailwind.js') ], | |
}).version(); | |
if (mix.inProduction()) { | |
mix.webpackConfig({ | |
plugins: [ | |
new purgeCss({ | |
paths: glob.sync([ | |
path.join(__dirname, 'resources/views/**/*.blade.php'), | |
path.join(__dirname, 'resources/assets/js/**/*.vue') | |
]), | |
extractors: [ | |
{ | |
extractor: class { | |
static extract(content) { | |
return content.match(/[A-z0-9-:\/]+/g) | |
} | |
}, | |
extensions: ['html', 'js', 'php', 'vue'] | |
} | |
] | |
}) | |
] | |
}) | |
} |
Great! I was going nuts trying to get this to work. Very helpful.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful Gist, thanks!