-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (63 loc) · 1.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const mix = require('laravel-mix');
const path = require('path');
class CleanCss {
/**
* All dependencies that should be installed by Mix.
*
* @return {Array}
*/
dependencies() {
return ['clean-css-loader'];
}
/**
* Register the component.
*
* @param {Object} options CleanCSS configuration object
* @param {String|Array} files (optional) Files to apply clean-css to
* @return {void}
*/
register(options, files = null) {
// Convert single given file to array
if (typeof files === 'string') files = [files];
// Make every file path absolute
if (Array.isArray(files)) {
Object.keys(files).forEach(function (key) {
if (!path.isAbsolute(files[key])) {
files[key] = path.resolve(files[key]);
}
});
}
// Create properties
this.options = options;
this.files = files;
}
/**
* Insert the clean-css-loader into the generated webpack configuration.
*
* @param {Object} webpackConfig
* @return {void}
*/
webpackConfig(webpackConfig) {
let options = this.options;
let files = this.files;
// Where to insert the clean-css-loader
const insertBefore = "postcss-loader";
// Go through all rules
webpackConfig.module.rules.forEach(function (rule) {
// Skip rules without loaders or files that are not in the file list
if (typeof rule.use === "undefined" || (files && !files.includes(rule.test))) return;
// Go through all loaders of a rule
rule.use.forEach(function (loader) {
// Search for postcss-loader
if (typeof loader === "object" && loader.loader === insertBefore) {
// Insert clean-css right before
rule.use.splice(rule.use.indexOf(insertBefore) - 1, 0, {
loader: "clean-css-loader",
options: options
});
}
});
});
}
}
mix.extend('cleanCss', new CleanCss());