gulp browserify不会退出

时间:2017-05-25 02:47:38

标签: gulp browserify

以下是我为gulp和browserify继承的代码

'use strict';

var gulp = require('gulp'),
    browserify = require('browserify'),
    glob = require('glob'),
    path = require('path'),
    source = require('vinyl-source-stream'),
    notifier = require('node-notifier'),
    _ = require('underscore'),
    watchify = require('watchify'),
    jshint = require('gulp-jshint'),
    chalk = require('chalk'),
    del = require('del'),
    webserver = require('gulp-webserver'),
    cleanCSS = require('gulp-clean-css'),
    babelify = require("babelify"),
    jasmine = require('gulp-jasmine');

var browserifyables = './web-app/**/*-browserify.js';

function logError(error){
    /* jshint validthis: true */
    notifier.notify({
        title: 'Browserify compilation error',
        message: error.toString()
    });
    console.error(chalk.red(error.toString()));
    this.emit('end');
}

function bundleShare(b, config) {
    b.bundle()
        .on('error', logError)
        .pipe(source(config.destFilename))
        .pipe(gulp.dest(config.destDir));
}

function browserifyShare(config, watch) {
    var browserifyConfig = {
        cache: {},
        packageCache: {},
        fullPaths: true,
        insertGlobals: true
    };

    if(process.env.NODE_ENV !== 'production'){
        browserifyConfig.debug = true;
    }

    var b = browserify(browserifyConfig);

    b.transform('browserify-css', {});

    b.transform(babelify, {presets: ["es2015"]});

    // Need to fix dollar sign getting effed in angular when it gets minified before we can enable
    if(process.env.NODE_ENV === 'production'){
        b.transform('uglifyify', {global: true});
    }

    if(watch) {
        // if watch is enable, wrap this bundle inside watchify
        b = watchify(b);
        b.on('update', function(ids) {
            ids.forEach(function(id){
                console.log(chalk.green(id + ' finished compiling'));
            });

            bundleShare(b, config);
        });

        b.on('log', function(message){
            console.log(chalk.magenta(message));
        });
    }
    // source to watch
    b.add(config.sourceFile);
    bundleShare(b, config);
}

gulp.task('minify-css', function() {
    var minifyConfig = {compatibility: 'ie8'};

    if(process.env.NODE_ENV !== 'production'){
        minifyConfig.debug = true;
    }

    return gulp.src('./web-app/css/*/*.css')
        .pipe(cleanCSS(minifyConfig))
        .pipe(gulp.dest('./web-app/css'))
        ;
});

gulp.task('browserify', ['minify-css'], function(){
    glob(browserifyables, {}, function(err, files){
        _.each(files, function(file){
            browserifyShare({
                sourceFile: file,
                destDir: path.dirname(file),
                destFilename: path.basename(file).replace('-browserify', '')
            });
        });
    });
});

我使用

运行gulp任务
NODE_ENV='production' gulp browserify

gulp任务已完成但未终止。以下是输出

[22:40:57] Using gulpfile ~/some/dir/Gulpfile.js
[22:40:57] Starting 'minify-css'...
[22:40:58] Finished 'minify-css' after 565 ms
[22:40:58] Starting 'browserify'...
[22:40:58] Finished 'browserify' after 2.12 ms

这大大减缓了我对詹金斯的构建。

但是如果我注释掉这一行:

b.add(config.sourceFile);

任务退出但是它不起作用(原因很明显)。

所以不确定我在这里缺少什么。我需要找出阻止任务退出的原因。

0 个答案:

没有答案