Skip to content

A Vite plugin that integrates PurgeCSS with Laravel template assets. Apart from Blade, it also supports frontend frameworks such as Svelte, Vue, React and Angular. It works well with Inertia.

License

erbelion/vite-plugin-laravel-purgecss

Repository files navigation

vite-plugin-laravel-purgecss

Get package from npm Downloads Downloads views

A Vite plugin that integrates PurgeCSS with Laravel 11/10/9 template assets (currently updated to laravel/laravel@11.0.7).

It purges assets only in production mode (yarn build/npm run build).

Installation

yarn

yarn add @erbelion/vite-plugin-laravel-purgecss

npm

npm i @erbelion/vite-plugin-laravel-purgecss

Usage

Use plugin in your Vite config (vite.config.ts)

import purge from '@erbelion/vite-plugin-laravel-purgecss'

export default {
    plugins: [
        laravel(...),
        purge({
            templates: ['blade']
        })
    ]
}

See more examples

Options

Parameter Type Description
templates? string[] List of ready paths to be processed by PurgeCSS. See below.
paths? string[] List of paths to be processed by PurgeCSS.
safelist? UserDefinedSafelist Check available safelist options in PurgeCSS docs.
extractors? Extractors[] Check available extractors options in PurgeCSS docs.

Templates

Parameter Path to be processed
blade resources/views/**/*.blade.php
svelte resources/{js,views}/**/*.svelte
vue resources/{js,views}/**/*.vue
react resources/{js,views}/**/*.{tsx,ts,jsx,js,html}
angular resources/{js,views}/**/*.html

You may also provide custom paths via paths option.

Other examples

Vue via template option:

purge({
    templates: ['blade', 'vue']
})

Via custom path:

purge({
    paths: ['resources/views/**/*.blade.php']
})

Via custom paths + always keep #bruh, .nice-button and h1 styling:

purge({
    paths: [
        'resources/views/**/*.blade.php',
        'resources/{js,views}/**/*.vue'
    ],
    safelist: ['bruh', 'nice-button', 'h1']
})

Example config with fix for escaped prefixes (sm:, lg:, etc.):

purge({
    paths: [
        'resources/views/**/*.blade.php',
        'resources/{js,views}/**/*.vue'
    ],
    extractors: [
        {
            extractor: (content) => {
                return content.match(/[a-z-_:\/]+/g) || []
            },
            extensions: ['php', 'vue', 'html']
        }
    ]
})

Tutorial

https://github.com/erbelion/tutorial-vite-plugin-laravel-purgecss

See also

vite-plugin-sveltekit-purgecss

About

A Vite plugin that integrates PurgeCSS with Laravel template assets. Apart from Blade, it also supports frontend frameworks such as Svelte, Vue, React and Angular. It works well with Inertia.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published