-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.js
123 lines (74 loc) · 3.12 KB
/
task.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
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
const autoprefixer = require('autoprefixer');
const postcss = require('postcss');
const fs = require('fs-extra');
const path = require('path');
const _ = require('lodash');
const glob = require('glob');
module.exports = (logger, dirname, config) => {
return () => {
return new Promise((resolve, reject) => {
// Validate config
if ( ! config.dir || ! config.outDir ) return reject(new Error('Autoprefixer plugin misconfiguration! dir and outDir must be present.'));
if ( ! config.overrideBrowserslist && ! config.browserslist ) return reject(new Error('Autoprefixer plugin misconfiguration! Either browserslist or overrideBrowserslist must be present.'));
// Empty outDir if necessary
if ( config.cleanOutDir ) {
fs.emptyDirSync(path.join(dirname, config.outDir));
}
const finalOptions = _.cloneDeep(config);
delete finalOptions.dir;
delete finalOptions.outDir;
delete finalOptions.cleanOutDir;
delete finalOptions.browserslist;
// Load browserslist if necessary
if ( config.browserslist ) {
try {
const data = fs.readFileSync(path.join(dirname, config.browserslist), { encoding: 'utf8' }).replace(/\r\n/, '\n').split('\n');
finalOptions.overrideBrowserslist = [];
for ( const line of data ) {
if ( line.trim().substr(0, 1) === '#' ) continue;
if ( ! line.trim() ) continue;
finalOptions.overrideBrowserslist.push(line.trim());
}
}
catch (error) {
return reject(new Error(`Could not read browserslist at "${path.join(dirname, config.browserslist)}"!`));
}
}
const processor = postcss([autoprefixer(finalOptions)]);
// Search `config.dir` for `.css` files
glob('**/*.css', { cwd: path.join(dirname, config.dir) }, (error, files) => {
if ( error ) return reject(error);
const promises = [];
logger(`Prefixing ${files.length} files...`);
for ( const file of files ) {
promises.push(new Promise((resolve, reject) => {
// Read file
fs.readFile(path.join(dirname, config.dir, file), { encoding: 'utf8' }, (error, data) => {
if ( error ) return reject(error);
// Prefix CSS
processor.process(data, { from: path.join(dirname, config.dir, file), to: path.join(dirname, config.outDir, file) })
.then(result => {
// Log warnings
result.warnings().forEach(warn => {
logger(`Autoprefixer warning: ${warn.toString()}`);
});
// Write to file
fs.outputFile(path.join(dirname, config.outDir, file), result.css, error => {
if ( error ) return reject(error);
resolve();
});
})
.catch(reject);
});
}));
}
Promise.all(promises)
.then(() => {
logger(`All ${files.length} files were prefixed.`);
resolve();
})
.catch(error);
});
});
};
};