Skip to content

Commit b294efd

Browse files
author
Marc Eickhoff
committed
Removed file filters for now and fixed problem with Webpack loader order
1 parent 67ee6f3 commit b294efd

File tree

4 files changed

+163
-123
lines changed

4 files changed

+163
-123
lines changed

README.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,6 @@ mix
3131
level: 2,
3232
format: mix.inProduction() ? false : 'beautify' // Beautify only in dev mode
3333
})
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-
])
4734
```
4835

4936
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: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
const mix = require('laravel-mix');
2-
const path = require('path');
3-
const escapeStringRegexp = require('escape-string-regexp');
42

53
class CleanCss {
64

@@ -17,49 +15,44 @@ class CleanCss {
1715
* Register the component.
1816
*
1917
* @param {Object} options CleanCSS configuration object
20-
* @param {String|Array} filter One or more source stylesheet files
2118
* @return {void}
2219
*/
23-
register(options, filter) {
20+
register(options) {
2421
this.options = options;
25-
this.filter = filter;
2622
}
2723

2824
/**
29-
* Rules to be merged with the master webpack loaders.
25+
* Insert the clean-css-loader into the generated webpack configuration.
3026
*
31-
* @return {Object}
27+
* @param {Object} webpackConfig
28+
* @return {void}
3229
*/
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-
}
30+
webpackConfig(webpackConfig) {
31+
let options = this.options;
32+
33+
// Where to insert the clean-css-loader
34+
const insertBefore = "postcss-loader";
35+
36+
// Go through all rules
37+
webpackConfig.module.rules.forEach(function (rule) {
38+
39+
// Skip rules without loaders
40+
if (typeof rule.loaders === "undefined") return;
41+
42+
// Go through all loaders of a rule
43+
rule.loaders.forEach(function (loader) {
44+
45+
// Search for postcss-loader
46+
if (typeof loader === "object" && loader.loader === insertBefore) {
5447

55-
// Return clean-css rule
56-
return {
57-
test: test,
58-
use: [{
59-
loader: "clean-css-loader",
60-
options: this.options
61-
}]
62-
}
48+
// Insert clean-css right before
49+
rule.loaders.splice(rule.loaders.indexOf(insertBefore) - 1, 0, {
50+
loader: "clean-css-loader",
51+
options: options
52+
});
53+
}
54+
});
55+
});
6356
}
6457
}
6558

0 commit comments

Comments
 (0)