【问题标题】:Browserify + Babelify Gulp task is not terminating, when Watchify is used使用 Watchify 时,Browserify + Babelify Gulp 任务未终止
【发布时间】:2016-09-14 18:26:54
【问题描述】:

我在我正在 WebStorm 中开发的 Angular 项目中使用 babelify 为 browserify 做了一个 gulp 任务。首先,我应该说,这段代码可以完美运行

我的浏览器包如下:

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);

而我的 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'));
});

问题我完全看不懂:

我的问题是:为什么我的 gulp 任务在完成其工作后没有终止?我应该始终通过单击 WebStorm 中的方形按钮手动停止它。

更新 1

我发现如果我直接通过“browserify”捆绑包而不使用变量appBundle,问题就解决了。于是代码变成:

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'));

它有效! 但主要的困难是我在我的 watchify 任务中使用了这个 appBundle,所以我没有想要复制捆绑包。

更新 2

几个小时后,我再次发现,这就是我的 watchify 任务所关心的问题。漏洞代码是这样的:

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);
});

当我将 const b 的声明放入我的 watchify 任务中时,一切都开始正常工作了。

但最后一个问题是:为什么它开始起作用了?

【问题讨论】:

    标签: javascript angularjs gulp browserify babeljs


    【解决方案1】:

    看看这个文件我想它会帮助你

    /*
     *  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());
    }
    

    【讨论】:

    • 感谢您的回复。不幸的是,它并没有解决我的问题。我已经更新了问题。
    猜你喜欢
    • 1970-01-01
    • 2016-12-22
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    相关资源
    最近更新 更多