简体   繁体   English

当使用Watchify时,Browserify + Babelify Gulp任务不会终止

[英]Browserify + Babelify Gulp task is not terminating, when Watchify is used

I've made a gulp task for browserify with babelify in my Angular project, which I'm developing in WebStorm. 我已经在我的Angular项目中使用babelify进行了浏览器的gulp任务,我正在WebStorm中开发。 First of all, I should say, that this code does work perfectly . 首先,我应该说, 这段代码确实完美无缺

My browserify bundle be like: 我的browserify包就像:

const appBundle = browserify({
    entries: config.client.src.appModule, // main angular app file 
    debug: TRUE,
    packageCache: {},
    cache: {}
}).transform(babelify, {only: './src/client'}).on('log', gutil.log);

And my gulp.task be like: 而我的gulp.task就像:

gulp.task('browserify', ['jshint'], function () {
        return appBundle.bundle()
            .on('error', function (err) {
                gutil.log('Browserify error: ' + gutil.colors.red(err.message));
            })
            .pipe(source('app.js')) // main dist file
            .pipe(gulp.dest('./dist'));
});

The issue I can't understand at all: 我根本无法理解的问题

My question was: Why is my gulp task not terminating after it is through with its job? 我的问题是: 为什么我的gulp任务在完成工作后不会终止? I should always stop it manually by clicking square button in my WebStorm. 我应该通过单击WebStorm中的方形按钮手动停止它。

UPDATE 1 更新1

I've figured out that the problem is eliminated if I pass "browserify" bundle directly, without the variable appBundle . 我已经发现,如果我直接传递“browserify”软件包而没有变量appBundle ,问题就会消除。 So the code turns to: 所以代码转向:

browserify({
    entries: config.client.src.appModule, // main angular app file 
    debug: TRUE,
    packageCache: {},
    cache: {}
}).transform(babelify, {only: './src/client'}).on('log', gutil.log) 
  .bundle().on('error', function (err) {
         gutil.log('Browserify error: ' + gutil.colors.red(err.message));
   })
   .pipe(source('app.js')) // main dist file
   .pipe(gulp.dest('./dist'));

And it works! 它的工作原理! But the main hardship is that i'm using this appBundle in my watchify task, so I don't want to duplicate the bundle. 但主要的困难是我在我的watchify任务中使用这个appBundle ,所以我不想复制这个包。

UPDATE 2 更新2

After a couple of hours I've figured out again, that's the issue was concerned with my watchify task. 几个小时后我再次想通了,这个问题与我的密切任务有关。 The hole code was like that: 孔代码是这样的:

const appBundle = browserify({
    entries: config.client.src.appModule, // main js file 
    debug: TRUE,
    packageCache: {},
    cache: {}
}).transform(babelify, {only: './src/client'});

const b = watchify(appBundle);

function rebundle(bundler) {
    return bundler
        .bundle()
        .on('error', function (err) {
            gutil.log('Browserify error: ' + gutil.colors.red(err.message));
        })
        .pipe(source('app.js'))
        .pipe(gulp.dest('./dist'));
}

gulp.task('watchify', function () {

    rebundle(b);

    b.on('update', function () {
        rebundle(b)
    });
    b.on('log', gutil.log); // output build logs to terminal
});

gulp.task('browserify', ['jshint'], function () {
    rebundle(appBundle);
});

And when I've put down declaration of the const b into my watchify task, everything everything has started to work properly. 当我将const b声明放入我的watchify任务时,一切都已开始正常工作。

But the final question is: WHY IT HAS STARTED TO WORK? 但最后一个问题是:为什么它开始工作?

see this file i think it will help you 看到这个文件,我认为它会对你有所帮助

/*
 *  Task Automation to make my life easier.
 *  Author: Jean-Pierre Sierens
 *  ===========================================================================
 */

// declarations, dependencies
// ----------------------------------------------------------------------------
var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var gutil = require('gulp-util');
var babelify = require('babelify');
var browserSync = require('browser-sync').create();

// Static server
gulp.task('serve', ['scripts'], function() {

    browserSync.init({
        server: "./"
    });
    gulp.watch(['./app/**/*.js'], ['scripts']);
    gulp.watch('./index.html').on('change', browserSync.reload);
   // gulp.watch("app/scss/*.scss", ['sass']);
  //  gulp.watch("app/*.html").on('change', browserSync.reload);
});
// External dependencies you do not want to rebundle while developing,
// but include in your application deployment
var dependencies = [
    'react',
    'react-dom'
];
// keep a count of the times a task refires
var scriptsCount = 0;

// Gulp tasks
// ----------------------------------------------------------------------------
gulp.task('scripts', function () {
    bundleApp(false);
});

gulp.task('deploy', function (){
    bundleApp(true);
});

gulp.task('watch', function () {
    gulp.watch(['./app/*.js'], ['scripts']);
});

// When running 'gulp' on the terminal this task will fire.
// It will start watching for changes in every .js file.
// If there's a change, the task 'scripts' defined above will fire.
gulp.task('default', ['serve']);

// Private Functions
// ----------------------------------------------------------------------------
function bundleApp(isProduction) {
    scriptsCount++;
    // Browserify will bundle all our js files together in to one and will let
    // us use modules in the front end.
    var appBundler = browserify({
        entries: './app/app.js',
        debug: true
    })

    // If it's not for production, a separate vendors.js file will be created
    // the first time gulp is run so that we don't have to rebundle things like
    // react everytime there's a change in the js file
    if (!isProduction && scriptsCount === 1){
        // create vendors.js for dev environment.
        browserify({
            require: dependencies,
            debug: true
        })
            .bundle()
            .on('error', gutil.log)
            .pipe(source('vendors.js'))
            .pipe(gulp.dest('./web/js/'));
    }
    if (!isProduction){
        // make the dependencies external so they dont get bundled by the
        // app bundler. Dependencies are already bundled in vendor.js for
        // development environments.
        dependencies.forEach(function(dep){
            appBundler.external(dep);
        })
    }

    appBundler
    // transform ES6 and JSX to ES5 with babelify
        .transform("babelify", {presets: ["es2015", "react"]})
        .bundle()
        .on('error',gutil.log)
        .pipe(source('bundle.js'))
        .pipe(gulp.dest('./web/js/'))
        .pipe(browserSync.stream());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM