|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var path = require('path'); |
| 4 | +var postcss = require('postcss'); |
| 5 | +var diff = require('diff'); |
| 6 | +var chalk = require('chalk'); |
| 7 | + |
| 8 | +module.exports = function(grunt) { |
| 9 | + |
| 10 | + var options; |
| 11 | + var processor = postcss(); |
| 12 | + |
| 13 | + /** |
| 14 | + * Returns an input map contents if a custom map path was specified |
| 15 | + * @param {string} from Input CSS path |
| 16 | + * @returns {?string} |
| 17 | + */ |
| 18 | + function getPrevMap(from) { |
| 19 | + if (typeof options.map.prev === 'string') { |
| 20 | + var mapPath = options.map.prev + path.basename(from) + '.map'; |
| 21 | + |
| 22 | + if (grunt.file.exists(mapPath)) { |
| 23 | + return grunt.file.read(mapPath); |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @param {string} input Input CSS contents |
| 30 | + * @param {string} from Input CSS path |
| 31 | + * @param {string} to Output CSS path |
| 32 | + * @returns {{css: string, map: ?string}} |
| 33 | + */ |
| 34 | + function process(input, from, to) { |
| 35 | + return processor.process(input, { |
| 36 | + map: (typeof options.map === 'boolean') ? options.map : { |
| 37 | + prev: getPrevMap(from), |
| 38 | + inline: options.map.inline, |
| 39 | + annotation: options.map.annotation, |
| 40 | + sourcesContent: options.map.sourcesContent |
| 41 | + }, |
| 42 | + from: from, |
| 43 | + to: to |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param {string} msg Log message |
| 49 | + */ |
| 50 | + function log(msg) { |
| 51 | + if (!options.silent) { |
| 52 | + grunt.log.writeln(msg); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + grunt.registerMultiTask('postcss', 'Process CSS files.', function() { |
| 57 | + options = this.options({ |
| 58 | + diff: false, |
| 59 | + map: false, |
| 60 | + processors: [], |
| 61 | + silent: false |
| 62 | + }); |
| 63 | + |
| 64 | + options.processors.forEach(processor.use.bind(processor)); |
| 65 | + |
| 66 | + this.files.forEach(function(f) { |
| 67 | + if (!f.src.length) { |
| 68 | + return grunt.fail.warn('No source files were found.'); |
| 69 | + } |
| 70 | + |
| 71 | + f.src |
| 72 | + .forEach(function(filepath) { |
| 73 | + var dest = f.dest || filepath; |
| 74 | + var input = grunt.file.read(filepath); |
| 75 | + var output = process(input, filepath, dest); |
| 76 | + |
| 77 | + grunt.file.write(dest, output.css); |
| 78 | + log('File ' + chalk.cyan(dest) + ' created.'); |
| 79 | + |
| 80 | + if (output.map) { |
| 81 | + grunt.file.write(dest + '.map', output.map.toString()); |
| 82 | + log('File ' + chalk.cyan(dest + '.map') + ' created (source map).'); |
| 83 | + } |
| 84 | + |
| 85 | + if (options.diff) { |
| 86 | + var diffPath = (typeof options.diff === 'string') ? options.diff : dest + '.diff'; |
| 87 | + |
| 88 | + grunt.file.write(diffPath, diff.createPatch(dest, input, output.css)); |
| 89 | + log('File ' + chalk.cyan(diffPath) + ' created (diff).'); |
| 90 | + } |
| 91 | + }); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}; |
0 commit comments