Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gulp.spritesmith to supress "icon-" prefix in the generated css file

I am using gulp.spritesmith to automate sprite image and css generation. All works good except the generated css is haivng "icon-" prefix. I don't want to add this prefix as I have existing HTML markup and I don't want to change the markup. Following is my gulp task.

'use strict';
var gulp = require('gulp');
var spritesmith = require('gulp.spritesmith');


gulp.task('sprite', function () {    

    var spriteData =
            gulp.src('./sprite/*.*') // source path of the sprite images
            .pipe(spritesmith({
                imgName: 'sprite.png',
                cssName: 'sprite.css'
            }));

    spriteData.img.pipe(gulp.dest("./css")); // output path for the sprite
    spriteData.css.pipe(gulp.dest("./css")); // output path for the CSS
});

Following is my generated css.

.icon-icon1 {
  background-image: url(sprite.png);
  background-position: 0px 0px;
  width: 120px;
  height: 120px;
}
.icon-icon2 {
  background-image: url(sprite.png);
  background-position: -120px 0px;
  width: 120px;
  height: 120px;
}

Please help to supress "icon-".

like image 494
joy Avatar asked Nov 27 '22 10:11

joy


2 Answers

You can alternatively use the cssSelector of the library.

cssOpts: {
  cssSelector: function (sprite) {
    return '.' + sprite.name;
  }
}
like image 197
bekos Avatar answered May 27 '23 13:05

bekos


You can use gulp-replace to remove the "icon-" prefixes.

Last line of your sprite task would become

spriteData.css
  .pipe(replace(/^\.icon-/gm, '.'))
  .pipe(gulp.dest("./css"));
like image 41
psalaets Avatar answered May 27 '23 15:05

psalaets