/
PostCssPluginsFactory.js
91 lines (76 loc) · 2.08 KB
/
PostCssPluginsFactory.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
let postcssrc = require('postcss-load-config');
class PostCssPluginsFactory {
/**
* Create a new instance.
* @param {Object} preprocessor
* @param {Config} Config
*/
constructor(preprocessor, Config) {
this.preprocessor = preprocessor;
this.Config = Config;
this.plugins = [];
}
/**
* Load all relevant PostCSS plugins.
*/
load() {
this.loadConfigFile()
.loadGlobalPlugins()
.loadLocalPlugins()
.loadAutoprefixer()
.loadCssNano();
return this.plugins;
}
/**
* Load the user's postcss.config.js file, if any.
*/
loadConfigFile() {
try {
this.plugins = [...this.plugins, ...postcssrc.sync().plugins];
} catch (e) {
// No postcss.config.js file exists.
}
return this;
}
/**
* Load any global postcss plugins that have been passed to Mix.
*/
loadGlobalPlugins() {
if (this.Config.postCss && this.Config.postCss.length) {
this.plugins = [...this.plugins, ...Config.postCss];
}
return this;
}
/**
* Load any postcss plugins that were passed to the Mix command.
*/
loadLocalPlugins() {
if (this.preprocessor.postCssPlugins && this.preprocessor.postCssPlugins.length) {
this.plugins = [...this.plugins, ...this.preprocessor.postCssPlugins];
}
return this;
}
/**
* Add autoprefixer to the plugins list.
*/
loadAutoprefixer() {
if (this.Config.autoprefixer) {
this.plugins.push(require('autoprefixer')(this.Config.autoprefixer));
}
return this;
}
/**
* Add CSSNano to the plugins list.
*/
loadCssNano() {
if (this.Config.production && this.Config.cssNano !== false) {
this.plugins.push(
require('cssnano')({
preset: ['default', this.Config.cssNano]
})
);
}
return this;
}
}
module.exports = PostCssPluginsFactory;