├── example ├── resources │ ├── views │ │ └── welcome.blade.php │ └── css │ │ └── app.css ├── webpack.mix.js └── package.json ├── .gitignore ├── __test__ ├── __snapshots__ │ └── integration.test.js.snap └── integration.test.js ├── .github ├── ISSUE_TEMPLATE │ └── config.yml └── workflows │ └── run-tests.yml ├── .editorconfig ├── index.js ├── LICENSE.md ├── package.json ├── CHANGELOG.md └── README.md /example/resources/views/welcome.blade.php: -------------------------------------------------------------------------------- 1 |
2 |
3 | Welcome! 4 |
5 |
6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | yarn-error.log 4 | /yarn.lock 5 | example/public 6 | example/mix-manifest.json 7 | example/yarn.lock 8 | -------------------------------------------------------------------------------- /example/webpack.mix.js: -------------------------------------------------------------------------------- 1 | const mix = require('laravel-mix'); 2 | require('laravel-mix-purgecss'); 3 | 4 | mix.postCss('resources/css/app.css', 'public/css').purgeCss(); 5 | -------------------------------------------------------------------------------- /__test__/__snapshots__/integration.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`it purges css 1`] = ` 4 | ".bg-blue{background-color:#00f}.is-active{background-color:green}.p-2\\\\.5{padding:2.5rem} 5 | " 6 | `; 7 | -------------------------------------------------------------------------------- /example/resources/css/app.css: -------------------------------------------------------------------------------- 1 | .bg-blue { 2 | background-color: blue; 3 | } 4 | 5 | .bg-red { 6 | background-color: red; 7 | } 8 | 9 | .is-active { 10 | background-color: green; 11 | } 12 | 13 | .p-2\.5 { 14 | padding: 2.5rem; 15 | } 16 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: true 2 | contact_links: 3 | - name: Feature Request 4 | url: https://github.com/spatie/laravel-mix-purgecss/discussions/new?category=ideas 5 | about: Share ideas for new features 6 | - name: Ask a Question 7 | url: https://github.com/spatie/laravel-mix-purgecss/discussions/new?category=q-a 8 | about: Ask the community for help 9 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [{package.json}] 15 | indent_size = 2 16 | 17 | [*.md] 18 | trim_trailing_whitespace = false 19 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "scripts": { 4 | "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --config=node_modules/laravel-mix/setup/webpack.config.js" 5 | }, 6 | "dependencies": { 7 | "cross-env": "^7.0.0", 8 | "laravel-mix": "^6.0.0", 9 | "laravel-mix-purgecss": "file:..", 10 | "postcss": "^8.1.2" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: run-tests 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v2 10 | - name: Use Node.js 11 | uses: actions/setup-node@v1 12 | with: 13 | node-version: "12.x" 14 | - run: yarn build-example 15 | - run: yarn 16 | - run: yarn test 17 | -------------------------------------------------------------------------------- /__test__/integration.test.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const path = require('path'); 3 | const { promisify } = require('util'); 4 | const readFile = promisify(fs.readFile); 5 | 6 | test('it purges css', async () => { 7 | const contents = await readFile( 8 | path.resolve(__dirname, '../example/public/css/app.css'), 9 | { 10 | encoding: 'utf8' 11 | } 12 | ); 13 | 14 | expect(contents).toMatchSnapshot(); 15 | }); 16 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const mix = require("laravel-mix"); 2 | 3 | mix.extend( 4 | "purgeCss", 5 | new (class { 6 | name() { 7 | return ["purgeCss"]; 8 | } 9 | 10 | register(config = {}) { 11 | const { enabled, ...purgeCssConfig } = config; 12 | 13 | this.enabled = enabled !== undefined ? enabled : mix.inProduction(); 14 | 15 | this.config = purgeCssConfig; 16 | } 17 | 18 | boot() { 19 | if (!this.enabled) { 20 | return; 21 | } 22 | 23 | mix.options({ 24 | postCss: [ 25 | ...global.Config.postCss, 26 | require("postcss-purgecss-laravel")(this.config), 27 | ], 28 | }); 29 | } 30 | })() 31 | ); 32 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Spatie bvba 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel-mix-purgecss", 3 | "version": "6.0.0", 4 | "description": "PurgeCSS wrapper for Laravel Mix", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/spatie/laravel-mix-purgecss.git" 9 | }, 10 | "keywords": [ 11 | "spatie" 12 | ], 13 | "author": "Sebastian De Deyne", 14 | "contributors": [ 15 | "CJMAXiK" 16 | ], 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/spatie/laravel-mix-purgecss/issues" 20 | }, 21 | "homepage": "https://github.com/spatie/laravel-mix-purgecss", 22 | "scripts": { 23 | "format": "prettier --write \"**/*.js\"", 24 | "test": "jest", 25 | "build-example": "./build-example.sh" 26 | }, 27 | "dependencies": { 28 | "postcss-purgecss-laravel": "^2.0.0" 29 | }, 30 | "devDependencies": { 31 | "jest": "^25.4.0", 32 | "prettier": "^2.0.5" 33 | }, 34 | "peerDependencies": { 35 | "laravel-mix": "^6.0.0" 36 | }, 37 | "engines": { 38 | "node": ">=12.0.0" 39 | }, 40 | "jest": { 41 | "testURL": "http://localhost" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-mix-purgecss` will be documented in this file 4 | 5 | ## 4.2.0 - 2019-09-30 6 | - Laravel Mix 5.0 7 | 8 | ## 4.1.0 - 2019-01-18 9 | - Add `extractorPattern` config option 10 | 11 | ## 4.0.0 - 2018-12-20 12 | - Laravel Mix 4.0 13 | 14 | ## 3.0.0 - 2018-10-01 15 | - Upgrade Purgecss to v1.3.0 16 | 17 | ## 2.2.3 - 2018-08-02 18 | - Fix match regex 19 | 20 | ## 2.2.2 - 2018-07-31 21 | - Fix match regex 22 | 23 | ## 2.2.1 - 2018-07-30 24 | - Ignore folders in globs 25 | 26 | ## 2.2.0 - 2018-05-25 27 | - Add `folders` option 28 | 29 | ## 2.1.3 - 2018-05-24 30 | - Upgrade Purgecss package version 31 | 32 | ## 2.1.2 - 2018-05-18 33 | - Upgrade Purgecss package version 34 | 35 | ## 2.1.0 - 2018-05-02 36 | - Add a set of commonly used `whitelistPatterns` by default 37 | 38 | ## 2.0.0 - 2018-03-18 39 | - Reworked to Laravel Mix 2.1's component-based approach 40 | 41 | ## 1.0.5 - 2018-01-30 42 | - Fix `mix.purgeCss()` return statement 43 | 44 | ## 1.0.4 - 2018-01-27 45 | - Make `mix.purgeCss()` chainable (again, got lost in a previous commit) 46 | 47 | ## 1.0.3 - 2018-01-25 48 | - Add compatibility with Node.js ^6.0 49 | 50 | ## 1.0.2 - 2018-01-24 51 | - Fixed an issue with custom `extensions` options 52 | 53 | ## 1.0.1 - 2018-01-24 54 | - Return `mix` after calling `purgeCss` to chain config 55 | 56 | ## 1.0.0 - 2018-01-24 57 | - Initial release 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [](https://supportukrainenow.org) 3 | 4 | # PurgeCSS wrapper for Laravel Mix 5 | 6 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 7 | [![Latest Version on NPM](https://img.shields.io/npm/v/laravel-mix-purgecss.svg?style=flat-square)](https://npmjs.com/package/laravel-mix-purgecss) 8 | [![npm](https://img.shields.io/npm/dt/laravel-mix-purgecss.svg?style=flat-square)](https://www.npmjs.com/package/laravel-mix-purgecss) 9 | 10 | This package adds a `purgeCss` option to Laravel Mix, which installs PurgeCSS for you with a set of sensible defaults for Laravel applications. 11 | 12 | ```js 13 | const mix = require('laravel-mix'); 14 | require('laravel-mix-purgecss'); 15 | 16 | // ... 17 | 18 | mix.js('resources/js/app.js', 'public/js') 19 | .sass('resources/sass/app.scss', 'public/css') 20 | .purgeCss(); 21 | ``` 22 | 23 | ## Support us 24 | 25 | [](https://spatie.be/github-ad-click/laravel-mix-purgecss) 26 | 27 | We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). 28 | 29 | We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). 30 | 31 | ## Installation 32 | 33 | Before you get started, make sure you're using `laravel-mix` version 5.0.0 or higher. 34 | 35 | You can install the package with yarn or npm: 36 | 37 | ```bash 38 | yarn add laravel-mix-purgecss --dev 39 | ``` 40 | 41 | ```bash 42 | npm install laravel-mix-purgecss --save-dev 43 | ``` 44 | 45 | Then install the extension by requiring the module in your Mix configuration. 46 | 47 | ```js 48 | const mix = require('laravel-mix'); 49 | require('laravel-mix-purgecss'); 50 | 51 | // ... 52 | ``` 53 | 54 | PurgeCSS can be enabled by calling `.purgeCss()` in your Mix chain. 55 | 56 | ```js 57 | mix.js('resources/js/app.js', 'public/js') 58 | .sass('resources/sass/app.scss', 'public/css') 59 | .purgeCss(); 60 | ``` 61 | 62 | By default, PurgeCSS only works when building assets for production. You can override this behaviour by specifying the `enabled` option. 63 | 64 | ```js 65 | mix.js('resources/js/app.js', 'public/js') 66 | .sass('resources/sass/app.scss', 'public/css') 67 | .purgeCss({ 68 | enabled: true, 69 | }); 70 | ``` 71 | 72 | ### Important notice for `mix.postCss` or `postcss.config.js` users 73 | 74 | When you use `mix.postCss()` or a seperate `postcss.config.js` file, Mix _overrides_ all other PostCSS plugins, including the PurgeCSS instance added by this plugin. 75 | 76 | To work around this issue, either: 77 | 78 | 1) Include your PostCSS plugins with `mix.options()` 79 | 80 | ```diff 81 | const mix = require('laravel-mix'); 82 | require('laravel-mix-purgecss'); 83 | 84 | mix.js('resources/js/app.js', 'public/js') 85 | - .postCss('resources/sass/app.css', 'public/css', [ 86 | - require('tailwindcss')(), 87 | - ]) 88 | + .postCss('resources/sass/app.css', 'public/css') 89 | + .options({ 90 | + postCss: [require('tailwindcss')] 91 | + }) 92 | .purgeCss(); 93 | ``` 94 | 95 | 2) Don't use this package, and use [`postcss-purgecss-laravel`](https://github.com/spatie/postcss-purgecss-laravel) instead 96 | 97 | ```diff 98 | const mix = require('laravel-mix'); 99 | - require('laravel-mix-purgecss'); 100 | 101 | mix.js('resources/js/app.js', 'public/js') 102 | .postCss('resources/sass/app.css', 'public/css', [ 103 | require('tailwindcss')(), 104 | + require('postcss-purgecss-laravel')({ /* ... */ }), 105 | ]) 106 | .purgeCss(); 107 | ``` 108 | 109 | ## PurgeCSS customization 110 | 111 | Custom options can be passed when calling PurgeCSS if necessary. Visit PurgeCSS' [docs](https://purgecss.com/configuration.html#options) to learn more about the available options. 112 | 113 | ```js 114 | mix.js('resources/js/app.js', 'public/js') 115 | .sass('resources/sass/app.scss', 'public/css') 116 | .purgeCss({ 117 | content: [path.join(__dirname, 'vendor/spatie/menu/**/*.php')], 118 | safelist: { deep: [/hljs/] }, 119 | }); 120 | ``` 121 | 122 | Passing options will _override_ the package defaults. If you want to _extend_ the package defaults, wrap them in an `extend` object. 123 | 124 | ```js 125 | mix.js('resources/js/app.js', 'public/js') 126 | .sass('resources/sass/app.scss', 'public/css') 127 | .purgeCss({ 128 | extend: { 129 | content: [path.join(__dirname, 'vendor/spatie/menu/**/*.php')], 130 | safelist: { deep: [/hljs/] }, 131 | }, 132 | }); 133 | ``` 134 | 135 | This package uses [`postcss-purgecss-laravel`](https://github.com/spatie/postcss-purgecss-laravel) under the hood, which has the following defaults: 136 | 137 | ```js 138 | const defaultConfig = { 139 | content: [ 140 | "app/**/*.php", 141 | "resources/**/*.html", 142 | "resources/**/*.js", 143 | "resources/**/*.jsx", 144 | "resources/**/*.ts", 145 | "resources/**/*.tsx", 146 | "resources/**/*.php", 147 | "resources/**/*.vue", 148 | "resources/**/*.twig", 149 | ], 150 | defaultExtractor: (content) => content.match(/[\w-/.:]+(?