LoginSignup
2
3

More than 5 years have passed since last update.

gulp.spritesmithとpostcss

Last updated at Posted at 2016-04-05

postcss用のspritesmithテンプレートを作成する。

css.template.handlerbars

{{#sprites}}
@define-placeholder {{name}} {
  background-position: {{px.offset_x}} {{px.offset_y}};
  size: {{px.width}} {{px.height}};
}
{{/sprites}}

gulpタスク

gulp.task('sprite', () => {
  const spriteData = gulp.src('/src/img/sprite/*.png')
    .pipe(spritesmith({
      imgName: 'sprite.png',
      cssName: '_sprite.css',
      algorithm: 'binary-tree',
      cssTemplate: '/src/css.template.handlerbars'
    }));

  const imgStream = spriteData.img
    .pipe(buffer())
    .pipe(imagemin())
    .pipe(gulp.dest('/dist/img/sprite/'));

  const cssStream = spriteData.css
    .pipe(gulp.dest('/src/css/'));

  return merge(imgStream, cssStream);
});

postcssファイル

_sprite.css

@define-placeholder icon-40-book {
  background-position: -44px 0px;
  size: 44px 44px;
}
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3