-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
169 lines (138 loc) · 4.02 KB
/
index.js
File metadata and controls
169 lines (138 loc) · 4.02 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* csscomb-webpack-plugin
* ---
* A Webpack plugin to let CSSComb process CSS source files.
*/
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import globby from 'globby';
import CSSComb from 'csscomb';
class CSSCombWebpackPlugin {
/**
* Constructor, get options
* @param {[Object]} options [User options]
*/
constructor (options = {}) {
this.pluginName = 'CSSCombWebpackPlugin';
this.options = options;
this.logs = [];
}
/**
* Init webpack plugin hooks
* @param {[Object]} compiler [Webpack compiler]
*/
apply (compiler) {
// If Webpack 4, then use new plugin hooks
if (compiler.hooks) {
compiler.hooks.run.tapAsync(this.pluginName, (compiler, callback) => {
this.run(compiler, callback);
});
compiler.hooks.watchRun.tapAsync(this.pluginName, (compiler, callback) => {
this.run(compiler, callback);
});
}
// Otherwise use the old way of init plugin
else {
compiler.plugin('run', (compiler, callback) => {
this.run(compiler, callback);
})
compiler.plugin('watch-run', (compiler, callback) => {
this.run(compiler, callback);
})
}
}
initCSSComb () {
if (!this.csscomb) {
this.csscomb = new CSSComb();
}
this.loadCSSCombConfig();
}
loadCSSCombConfig () {
// Get CSSComb config path
const configPath = path.resolve(this.options.configFile);
let config = null;
// If a custom config exists, then get it
if (fs.existsSync(configPath)) {
this.logs.push(`Using custom config file "${configPath}"...\n`);
config = JSON.parse(fs.readFileSync(path.resolve(this.options.configFile), 'utf8'));
}
// Otherwise get csscomb stadard config
else {
this.logs.push('Using default config file...\n');
config = CSSComb.getConfig('csscomb');
}
this.csscomb.configure(config);
}
/**
* Process files
* @param {[Object]} compiler [Webpack compiler]
* @param {Function} callback [Webpack callback function]
*/
run (compiler, callback) {
// Merge options
this.options = Object.assign({
configFile: './.csscomb',
displayErrors: true,
files: '**/*.s?(c|a)ss'
}, this.options);
let touched = false;
// Start message
this.logs.push(`\n${chalk.bgWhite.black(' CSSComb is processing files: ')}\n`);
// Initilaize CSSComb
this.initCSSComb();
// Get all files to process
globby(this.options.files).then(paths => {
this.promises = [];
// Walk trough files
paths.forEach(path => {
let thisPromies = new Promise((resolve, reject) => {
// Read file
fs.readFile(path, (err, data) => {
// Get file content
let css = data.toString();
// Let CSSComb process the content
this.csscomb.processString(css).then(processedData => {
// If the file is already processed. Then resolve without write to the file
if (css === processedData) {
resolve();
return;
}
// Write changes to the file, then log success message and resolve
fs.writeFile(path, processedData, () => {
this.logs.push(`${chalk.greenBright('[success]')} ${path}`);
touched = true;
resolve();
});
// On CSSComb process error
}, reason => {
this.logs.push(`${chalk.redBright('[failed]')} ${path}`);
touched = true;
// If we want to display CSSComb errors, then log the error
if (this.options.displayErrors) {
let error = reason.stack || '';
this.logs.push(`${chalk.underline.whiteBright('Message')}: "${error}"\n`);
}
resolve();
});
});
});
// Push promise to the premise array
this.promises.push(thisPromies);
});
//When all files is processed
Promise.all(this.promises).then(() => {
this.logs.push(`\n${chalk.bgWhite.black(' CSSComb is done processing files ')}\n`);
// Console log all messages if some file where touched
if (touched === true) {
this.logs.forEach(message => {
console.log(message);
});
}
// Webpack callback..
callback();
});
});
}
}
export default CSSCombWebpackPlugin;