Wednesday, 17 May 2017

Express server within Gulp run-sequence

I have a gulp file which is intended to compile my static files and then start my server. For this, as Gulp 4.0 has not been released yet, I use run-sequence, and it usually works fine. However, when trying to use it to build my static files and then run my server, it doesn't work. Instead, it only runs the 'build' task and exit. Here is the code:

var gulp = require('gulp');

var concat = require('gulp-concat');
var cleanCSS = require('gulp-clean-css');
var elm = require('gulp-elm');
var minify = require('gulp-minify');
var nodemon = require('gulp-nodemon');
var rename = require('gulp-rename');
var runSequence = require('run-sequence');
var sass = require('gulp-sass');

// Elm related

gulp.task('elm-init', elm.init);

gulp.task('elm', ['elm-init'], function() {
    return gulp.src('src/elm/**/*.elm')
        .pipe(elm())
        .pipe(gulp.dest('public/dist/'));
});

gulp.task('elm-bundle', ['elm-init'], function() {
    return gulp.src('src/elm/**/*.elm')
        .pipe(elm.bundle('bundle.js'))
        .on('error', function(error) {
            console.log(error.toString());
            this.emit('end');
        })
        .pipe(rename('app.js'))
        .pipe(gulp.dest('src/js/'));
});

// JS related

gulp.task('js', function() {
    gulp.src(['src/js/app.js', 'src/js/main.js', 'src/js/**/*.js'])
        .pipe(concat('script.js'))
        .pipe(minify({
            noSource: true
        }))
        .pipe(rename('script.js'))
        .pipe(gulp.dest('public/dist/'));
});

// CSS related

gulp.task('sass', function() {
    return gulp.src('src/sass/**/*.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(gulp.dest('src/css'));
});

gulp.task('css', ['sass'], function() {
    return gulp.src(['src/css/**/reboot.css', 'src/css/**/basscss.css', 'src/css/**/font-awesome.css', 'src/css/**/*.css'])
        .pipe(concat('style.css'))
        .pipe(cleanCSS({
            level: {
                2: {
                    mergeAdjacentRules: true, // controls adjacent rules merging; defaults to true
                    mergeIntoShorthands: true, // controls merging properties into shorthands; defaults to true
                    mergeMedia: true, // controls `@media` merging; defaults to true
                    mergeNonAdjacentRules: true, // controls non-adjacent rule merging; defaults to true
                    mergeSemantically: false, // controls semantic merging; defaults to false
                    overrideProperties: true, // controls property overriding based on understandability; defaults to true
                    removeEmpty: true, // controls removing empty rules and nested blocks; defaults to `true`
                    reduceNonAdjacentRules: true, // controls non-adjacent rule reducing; defaults to true
                    removeDuplicateFontRules: true, // controls duplicate `@font-face` removing; defaults to true
                    removeDuplicateMediaBlocks: true, // controls duplicate `@media` removing; defaults to true
                    removeDuplicateRules: true, // controls duplicate rules removing; defaults to true
                    removeUnusedAtRules: false, // controls unused at rule removing; defaults to false (available since 4.1.0)
                    restructureRules: false, // controls rule restructuring; defaults to false
                    skipProperties: [] // controls which properties won't be optimized, defaults to `[]` which means all will be optimized (since 4.1.0)
                }
            }
        }))
        .pipe(gulp.dest('public/dist/'));
});

gulp.task('build-css', function(callback) {
    runSequence('sass', 'css'); // works fine
});

gulp.task('build-js', function(callback) {
    runSequence('elm-bundle', 'js'); // works fine
});

gulp.task('build', ['build-js', 'build-css']);

gulp.task('server', function() {
    nodemon({ script: 'app.js' });
});

gulp.task('debug-server', function () {
    runSequence('build', 'server'); // doesn't works!!
});

Trying to run it returns:

[mateusfccp@pintobook Monogra-Editor]$ gulp debug-server
[19:38:17] Using gulpfile /srv/http/Monogra-Editor/gulpfile.js
[19:38:17] Starting 'debug-server'...
[19:38:17] Starting 'build-js'...
[19:38:17] Starting 'elm-init'...
[19:38:17] Starting 'build-css'...
[19:38:17] Starting 'sass'...
[19:38:17] Finished 'debug-server' after 18 ms
[19:38:17] Finished 'sass' after 51 ms
[19:38:17] Starting 'sass'...
[19:38:17] Finished 'sass' after 9.33 ms
[19:38:17] Starting 'css'...
[19:38:17] Finished 'elm-init' after 331 ms
[19:38:17] Starting 'elm-bundle'...
[19:38:17] Finished 'css' after 270 ms
[19:38:18] Finished 'elm-bundle' after 178 ms
[19:38:18] Starting 'js'...
[19:38:18] Finished 'js' after 3.67 ms
[mateusfccp@pintobook Monogra-Editor]$ 

Usually, it should show a message "Serving running at [port]", and also doesn't exit the process. It actually happens if I remove runSequence and run the tasks the default way. What exactly is happening here? Thanks.



via Mateus Felipe

No comments:

Post a Comment