You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 945B

1234567891011121314151617181920212223242526272829303132333435363738
  1. const PluginError = require('plugin-error');
  2. const through = require('through2');
  3. const inlineCss = require('inline-css');
  4. module.exports = opt => through.obj(function (file, enc, cb) {
  5. const self = this;
  6. const _opt = JSON.parse(JSON.stringify(opt || {}));
  7. // 'url' option is required
  8. // set it automatically if not provided
  9. if (!_opt.url) {
  10. _opt.url = `file://${file.path}`;
  11. }
  12. if (file.isNull()) {
  13. cb(null, file);
  14. return;
  15. }
  16. if (file.isStream()) {
  17. cb(new PluginError('gulp-inline-css', 'Streaming not supported'));
  18. return;
  19. }
  20. inlineCss(String(file.contents), _opt)
  21. .then(html => {
  22. file.contents = Buffer.from(html);
  23. self.push(file);
  24. return cb();
  25. })
  26. .catch(err => {
  27. if (err) {
  28. self.emit('error', new PluginError('gulp-inline-css', err));
  29. }
  30. });
  31. });