Skip to content

Commit a6bf932

Browse files
author
Marc Eickhoff
committed
Initial
0 parents  commit a6bf932

File tree

6 files changed

+8406
-0
lines changed

6 files changed

+8406
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.idea
3+
.DS_Store

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
MIT License
3+
4+
Copyright (c) 2019 Marc Eickhoff <mail@marceickhoff.com>
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Laravel Mix clean-css
2+
[![Latest Version on NPM](https://img.shields.io/npm/v/laravel-mix-clean-css.svg?style=flat-square)](https://npmjs.com/package/laravel-mix-clean-css)
3+
[![npm](https://img.shields.io/npm/dt/laravel-mix-clean-css.svg?style=flat-square)](https://www.npmjs.com/package/laravel-mix-clean-css)
4+
[![Software License](https://img.shields.io/npm/l/laravel-mix-clean-css.svg?style=flat-square)](LICENSE)
5+
6+
This extension adds support for [clean-css](https://github.com/jakubpawlowicz/clean-css) to [Laravel Mix](https://github.com/JeffreyWay/laravel-mix) by using the [clean-css-loader](https://github.com/retyui/clean-css-loader).
7+
8+
## Installation
9+
10+
```
11+
npm i -D laravel-mix-clean-css
12+
```
13+
14+
## Usage
15+
16+
Require the extension inside your ``webpack.mix.js`` and add clean-css configurations like this:
17+
18+
```javascript
19+
const mix = require('laravel-mix');
20+
21+
require('laravel-mix-clean-css');
22+
23+
mix
24+
.sass('src/app.scss', 'dist')
25+
.sass('src/app.sass', 'dist')
26+
.less('src/app.less', 'dist')
27+
.stylus('src/app.styl', 'dist')
28+
29+
// Run clean-css on all stylesheets
30+
.cleanCss({
31+
level: 2,
32+
format: mix.inProduction() ? false : 'beautify' // Beautify only in dev mode
33+
})
34+
35+
// Run clean-css only on one specific stylesheet
36+
.cleanCss({
37+
// ...
38+
}, 'src/app.scss')
39+
40+
// Run clean-css only on multiple specific stylesheets
41+
.cleanCss({
42+
// ...
43+
}, [
44+
'src/app.scss',
45+
'src/app.sass',
46+
])
47+
```
48+
49+
For more information about clean-css configurations please refer to their [documentation](https://github.com/jakubpawlowicz/clean-css/blob/master/README.md).

index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const mix = require('laravel-mix');
2+
const path = require('path');
3+
const escapeStringRegexp = require('escape-string-regexp');
4+
5+
class CleanCss {
6+
7+
/**
8+
* All dependencies that should be installed by Mix.
9+
*
10+
* @return {Array}
11+
*/
12+
dependencies() {
13+
return ['clean-css-loader', 'escape-string-regexp'];
14+
}
15+
16+
/**
17+
* Register the component.
18+
*
19+
* @param {Object} options CleanCSS configuration object
20+
* @param {String|Array} filter One or more source stylesheet files
21+
* @return {void}
22+
*/
23+
register(options, filter) {
24+
this.options = options;
25+
this.filter = filter;
26+
}
27+
28+
/**
29+
* Rules to be merged with the master webpack loaders.
30+
*
31+
* @return {Object}
32+
*/
33+
webpackRules() {
34+
// Create regular expression for file test
35+
let test = /\.(s[ac]|le)ss|styl(us)?$/; // All source stylesheets by default
36+
if (typeof this.filter !== 'undefined') { // File filter is given
37+
if (typeof this.filter === 'string') { // Only one file path given
38+
this.filter = [this.filter]; // Cast to array
39+
}
40+
if (Array.isArray(this.filter)) {
41+
let filters = [];
42+
this.filter.forEach((filter) => { // Iterate over given file filters
43+
if (!path.isAbsolute(filter)) {
44+
filter = path.resolve(filter);
45+
}
46+
filters.push(escapeStringRegexp(filter));
47+
});
48+
test = new RegExp(filters.join('|')); // Create regular expression from string
49+
}
50+
else {
51+
console.log(`laravel-mix-clean-css: Filter must be array or string, was ${typeof this.filter}, skipping`);
52+
}
53+
}
54+
55+
// Return clean-css rule
56+
return {
57+
test: test,
58+
use: [{
59+
loader: "clean-css-loader",
60+
options: this.options
61+
}]
62+
}
63+
}
64+
}
65+
66+
mix.extend('cleanCss', new CleanCss());

0 commit comments

Comments
 (0)