This repository has been archived by the owner on Sep 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
postcss.js
206 lines (166 loc) · 6.31 KB
/
postcss.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict';
var path = require('path');
var postcss = require('postcss');
var diff = require('diff');
var chalk = require('chalk');
module.exports = function(grunt) {
var options;
var processor;
/**
* Returns an input map contents if a custom map path was specified
* @param {string} from Input CSS path
* @returns {?string}
*/
function getPrevMap(from) {
if (typeof options.map.prev === 'string') {
var mapPath = options.map.prev + path.basename(from) + '.map';
if (grunt.file.exists(mapPath)) {
return grunt.file.read(mapPath);
}
}
}
/**
* @param {string} to Output CSS path
* @returns {string}
*/
function getSourcemapPath(to) {
return path.join(options.map.annotation, path.basename(to)) + '.map';
}
/**
* @param {string} to Output CSS path
* @returns {boolean|string}
*/
function getAnnotation(to) {
var annotation = true;
if (typeof options.map.annotation === 'boolean') {
annotation = options.map.annotation;
}
if (typeof options.map.annotation === 'string') {
annotation = path.relative(path.dirname(to), getSourcemapPath(to)).replace(/\\/g, '/');
}
return annotation;
}
/**
* @param {string} input Input CSS contents
* @param {string} from Input CSS path
* @param {string} to Output CSS path
* @returns {{css: string, map: ?string}}
*/
function process(input, from, to) {
return processor.process(input, {
map: (typeof options.map === 'boolean') ? options.map : {
prev: getPrevMap(from),
inline: (typeof options.map.inline === 'boolean') ? options.map.inline : true,
annotation: getAnnotation(to),
sourcesContent: (typeof options.map.sourcesContent === 'boolean') ? options.map.sourcesContent : true
},
from: from,
to: to,
parser: options.parser,
stringifier: options.stringifier,
syntax: options.syntax
});
}
/**
* @param {string} msg Log message
*/
function log(msg) {
grunt.verbose.writeln(msg);
}
grunt.registerMultiTask('postcss', 'Process CSS files.', function() {
options = this.options({
processors: [],
map: false,
diff: false,
safe: false,
failOnError: false,
writeDest: true
});
var tally = {
sheets: 0,
maps: 0,
diffs: 0,
issues: 0
};
if (typeof options.processors === 'function') {
processor = postcss(options.processors.call());
} else {
processor = postcss(options.processors);
}
var done = this.async();
var tasks = [];
this.files.forEach(function(f) {
var src = f.src.filter(function(filepath) {
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file ' + chalk.cyan(filepath) + ' not found.');
return false;
}
return true;
});
if (src.length === 0) {
grunt.log.error('No source files were found.');
return done();
}
Array.prototype.push.apply(tasks, src.map(function(filepath) {
var dest = f.dest || filepath;
var input = grunt.file.read(filepath);
return process(input, filepath, dest).then(function(result) {
var warnings = result.warnings();
tally.issues += warnings.length;
warnings.forEach(function(msg) {
grunt.log.error(msg.toString());
});
if (options.writeDest) {
grunt.file.write(dest, result.css);
log('File ' + chalk.cyan(dest) + ' created.');
}
tally.sheets += 1;
if (result.map) {
var mapDest = dest + '.map';
if (typeof options.map.annotation === 'string') {
mapDest = getSourcemapPath(dest);
}
grunt.file.write(mapDest, result.map.toString());
log('File ' + chalk.cyan(dest + '.map') + ' created (source map).');
tally.maps += 1;
}
if (options.diff) {
var diffPath = (typeof options.diff === 'string') ? options.diff : dest + '.diff';
grunt.file.write(diffPath, diff.createPatch(dest, input, result.css));
log('File ' + chalk.cyan(diffPath) + ' created (diff).');
tally.diffs += 1;
}
});
}));
});
Promise.all(tasks).then(function() {
if (tally.sheets) {
if (options.writeDest) {
grunt.log.ok(tally.sheets + ' processed ' + grunt.util.pluralize(tally.sheets, 'stylesheet/stylesheets') + ' created.');
} else {
grunt.log.ok(tally.sheets + ' ' + grunt.util.pluralize(tally.sheets, 'stylesheet/stylesheets') + ' processed, no files written.');
}
}
if (tally.maps) {
grunt.log.ok(tally.maps + ' ' + grunt.util.pluralize(tally.maps, 'sourcemap/sourcemaps') + ' created.');
}
if (tally.diffs) {
grunt.log.ok(tally.diffs + ' ' + grunt.util.pluralize(tally.diffs, 'diff/diffs') + ' created.');
}
if (tally.issues) {
grunt.log.error(tally.issues + ' ' + grunt.util.pluralize(tally.issues, 'issue/issues') + ' found.');
if (options.failOnError) {
return done(false);
}
}
done();
}).catch(function(error) {
if (error.name === 'CssSyntaxError') {
grunt.fatal(error.message + error.showSourceCode());
} else {
grunt.fatal(error);
}
done(error);
});
});
};