LoginSignup
6
2

More than 3 years have passed since last update.

gulp4 + gulp-sass が出力するファイルのタイムスタンプが更新されないのを解決

Posted at

下記のような環境でsassのコンパイルをすると、出力ファイルのタイムスタンプがソースファイルと同一になってしまう問題がありました。

  • node v10.15.3
  • gulp v4.0.2
  • gulp-sass v4.0.2
# ソースファイル
-rw-r--r--   1 dada  staff  338  5 17 00:50 common.scss
# 出力ファイル
-rw-r--r--   1 dada  staff  159063  5 17 00:50 common.css

無事解決できたのでQiitaにも記録しておきます。

through2 を使う

ここにそのものズバリな答えが書かれています。
https://github.com/dlmanning/gulp-sass/issues/706

$ npm i --save-dev through2
gulpfile.js
const gulp = require('gulp');
const sass = require('gulp-sass');
const through2 = require('through2');

gulp.task('sass', () => {
  return (
    gulp
      .src('./src/sass/*.scss')
      .pipe(sass().on('error', sass.logError))
      // タイムスタンプを書き換える
      .pipe(through2.obj((chunk, enc, callback)=>{
        const date = new Date();
        chunk.stat.atime = date;
        chunk.stat.mtime = date;
        callback(null, chunk);
      }))
      .pipe(gulp.dest('./dist/css/'))
  );
});

through2 についてはこのへんも詳しいです。

gulpプラグインの基本構造(プラグイン開発者向け)
https://qiita.com/morou/items/1297d5dd379ef013d46c

6
2
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
6
2