tschaub / gulp-newer

Pass through newer source files only
https://npmjs.org/package/gulp-newer
226 stars 24 forks source link

Different behaviour when using watch? #55

Open MarcelRobitaille opened 7 years ago

MarcelRobitaille commented 7 years ago

I am having an issue where everything works fine when calling the task directly (gulp css) but when the task gets triggered by gulp.watch, it recompiles no matter what. Even if I just write the file and it doesn't change. Just writing the file doesn't update the timestamp so running the task from the command line doesn't trigger the recompile.

As you can see in this screenshot, writing global.scss at 1:27 triggers the task but running the task from the command line after that does not. Is there any difference behind the scenes when a task is triggered by gulp.watch? image

Relevant file included into gulp:

const path = require('path')

const sassGraph = require('sass-graph')
const gutil = require('gulp-util')
const $ = require('gulp-load-plugins')()

const styles = [ 'global', 'dashboard', 'schedule', 'devices' ]
const tasks = []
const buildTasks = []

module.exports = gulp => {
  styles.map(style => {

    const file = `./source/css/${style}.scss`
    const files = Object.keys(sassGraph.parseFile(file)
      .filter(file => !file.startsWith(path.join(__dirname, '../node_modules/')))

    const task = `css-${style}`
    tasks.push(task)

    gulp.task(task, () => {
      return gulp.src(file)
        .pipe($.newer({ dest: `./public/css/${style}.css`, extra: files }))
        .pipe($.tap(() => gutil.log(`Recompiling '${gutil.colors.cyan(style)}'...`)))
        .pipe($.sourcemaps.init())
        .pipe($.sassGlob())
        .pipe($.sass().on('error', $.sass.logError))
        .pipe($.autoprefixer())
        .pipe($.sourcemaps.write('maps/'))
        .pipe(gulp.dest('public/css'))
        .pipe($.if(
          file => path.extname(file.path) === '.css',
          require('browser-sync').stream()
        ))
    })

    gulp.task(`watch-css-${style}`, () => gulp.watch(files, [ task ]))

    const buildTask = `build-css-${style}`
    buildTasks.push(buildTask)

    gulp.task(buildTask, () => {
      return gulp.src(`./source/css/${style}.scss`)
        .pipe($.sassGlob())
        .pipe($.sass())
        .pipe($.cssnano())
        .pipe($.autoprefixer())
        .pipe($.rev())
        .pipe($.dbust())
        .pipe(gulp.dest('build/css/'))
        .pipe($.gzip())
        .pipe(gulp.dest('build/css/'))
    })
  })

  gulp.task('css', tasks)
  gulp.task('build-css', buildTasks)
}