gimm / gulp-express

gulp plugin for express
56 stars 26 forks source link

Your code example throws an error #16

Closed simevidas closed 9 years ago

simevidas commented 9 years ago

In your README, you have this line in the code example:

gulp.watch(['{.tmp,app}/styles/**/*.css'], ['styles:css', server.notify]);

It seems that this code is invalid. The event with the "path" property is not passed to the server.notify method. I get a "TypeError: Cannot read property 'path' of null".

My full gulpfile.js is:

var gulp = require('gulp');
var server = require('gulp-express');
var stylus = require('gulp-stylus');
var minify = require('gulp-minify-css');
var autoprefixer = require('gulp-autoprefixer');

gulp.task('server', function () {   
    server.run({
        file: 'app.js',
    });

    gulp.watch(['source/styl/*.styl'], ['css', server.notify]);
});

gulp.task('css', function () {
    return gulp.src('source/styl/*.styl')
        .pipe(stylus({ compress: false, 'include css': true }))
        .pipe(autoprefixer({ cascade: false }))
        .pipe(minify())
        .pipe(gulp.dest('public/css/'));
});
simevidas commented 9 years ago

Update: As a temporary workaround, I’m performing these steps:

  1. I’m using gulp-livereload to manually .pipe(livereload()) at the end of my CSS and JS build streams. This works OK, I just have to call livereload.listen() after server.run().
  2. I’ve manually edited the port in your index.js so that it doesn’t conflict with gulp-livereload.
whisher commented 9 years ago

Hi all, same problem here :( @simevidas your workaround it seems not work

'use strict';

var gulp = require('gulp');
var server = require('gulp-express');
var concat = require('gulp-concat');
var ngAnnotate = require('gulp-ng-annotate');
var livereload = require('gulp-livereload');
require('./gulp/config');

gulp.task('scripts', function () {
    return gulp.src(config.paths.src.scripts)
        .pipe(concat(config.filenames.scripts))
        .pipe(ngAnnotate())
        .pipe(gulp.dest(config.paths.dest.build.scripts))
        .pipe(livereload());
});
gulp.task('serve', function () {
    // Start the server at the beginning of the task
    server.run({
        file: 'server.js',
        port: 45729
    });
    livereload.listen();
    gulp.watch(config.paths.src.scripts, ['scripts', server.notify]);
});
gulp.task('default', ['serve']); //

I always get

node_modules/gulp-express/index.js:80 var fileName = require('path').relative(__dirname, event.path); TypeError: Cannot read property 'path' of null

simevidas commented 9 years ago

@whisher I’m using livereload() as a replacement for server.notify. So, in your case, the watch call should invoke the "scripts" task, and nothing more:

gulp.watch(config.paths.src.scripts, ['scripts']);

Note: I have the livereload.js script manually included in my website, via:

<script src="//localhost:35729/livereload.js"></script> 

at the bottom of my HTML pages.

whisher commented 9 years ago

mmm like this it works :)

var scriptsWatcher = gulp.watch(config.paths.src.scripts, ['scripts']);
scriptsWatcher.on('change', server.notify);

You should put

app.use(require('connect-livereload')());

in your express file

gimm commented 9 years ago

Hello simevidas and whisher, I've just verified the issue, the 'event' is passed to notify correctly with the path value. Could you please provide more details here so that I can reproduce the issue you have?

whisher commented 9 years ago

Hello gimm, node v0.10.33 npm 1.4.28 gulp 3.8.10 in my example

//this work
var scriptsWatcher = gulp.watch(config.paths.src.scripts, ['scripts']);
scriptsWatcher.on('change', server.notify);
//this doesn't work
/*node_modules/gulp-express/index.js:80
var fileName = require('path').relative(__dirname, event.path);
TypeError: Cannot read property 'path' of null*/
var scriptsWatcher = gulp.watch(config.paths.src.scripts, ['scripts',server.notify]);
gimm commented 9 years ago

@whisher @simevidas Yes, you're right. The event object is not passed to the callback (server.notify) if there's multiple ones, like this:

gulp.watch(config.paths.src.scripts, ['scripts',server.notify]); // two callbacks here

According to gulp.watch, in order to make sure the callbacks get the event object, we need to:

var fileWatcher = gulp.watch('files to watch', callback1);
fileWatcher.on('change', callback2);

or

gulp.watch(['files to wach'], function (event) {
        callback1(event);
        callback2(event);
    });

I have updated the readme, thanks man!