hawtio / hawtio-ui

Hawtio UI widgets for hawtio 2.x see http://ui.hawt.io/
Apache License 2.0
7 stars 6 forks source link

Revisit 'embed-images' task in gulpfile.js #51

Closed tadayosi closed 6 years ago

tadayosi commented 7 years ago

The embed-images task is currently incomplete and not used. If we can make sure the icon example page still works then we can remove this task.

See also: https://github.com/hawtio/hawtio-ui/pull/50#issuecomment-274255905

https://github.com/hawtio/hawtio-ui/blob/v3.0.5/gulpfile.js#L237-L278

gulp.task('embed-images', ['concat'], function() {

  var replacements = [];

  var files = glob.sync('img/**/*.{png,svg,gif,jpg}');
  //console.log("files: ", files);
  function escapeRegExp(str) {
    return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
  }

  function getDataURI(filename) {
    var relative = path.relative('.', filename);
    var ext = path.extname(filename);
    var mime = 'image/jpg';
    switch (ext) {
      case '.png':
        mime = 'image/png';
      break;
      case '.svg':
        mime = 'image/svg+xml';
      break;
      case '.gif':
        mime='image/gif';
      break;
    }
    var buf = fs.readFileSync(filename);
    return 'data:' + mime + ';base64,' + buf.toString('base64');
  }

  files.forEach(function(file) {
    replacements.push({
      match: new RegExp(escapeRegExp(file), 'g'),
      replacement: getDataURI(file)
    });
  });

  gulp.src(config.dist + config.js)
  .pipe(plugins.replaceTask({
    patterns: replacements
  }))
  .pipe(gulp.dest(config.dist));
});