Aucune description

autoprefixer.js 3.1KB

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