index.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. var gutil = require('gulp-util');
  3. var through = require('through2');
  4. var autoprefixer = require('autoprefixer-core');
  5. var applySourceMap = require('vinyl-sourcemaps-apply');
  6. var objectAssign = require('object-assign');
  7. module.exports = function (opts) {
  8. opts = opts || {};
  9. return through.obj(function (file, enc, cb) {
  10. if (file.isNull()) {
  11. cb(null, file);
  12. return;
  13. }
  14. if (file.isStream()) {
  15. cb(new gutil.PluginError('gulp-autoprefixer', 'Streaming not supported'));
  16. return;
  17. }
  18. var res;
  19. var fileOpts = objectAssign({}, opts);
  20. try {
  21. res = autoprefixer(fileOpts).process(file.contents.toString(), {
  22. map: file.sourceMap ? {annotation: false} : false,
  23. from: file.relative,
  24. to: file.relative
  25. });
  26. file.contents = new Buffer(res.css);
  27. if (res.map && file.sourceMap) {
  28. applySourceMap(file, res.map.toString());
  29. }
  30. this.push(file);
  31. } catch (err) {
  32. this.emit('error', new gutil.PluginError('gulp-autoprefixer', err, {fileName: file.path}));
  33. }
  34. cb();
  35. });
  36. };