简体   繁体   English

使用Gulp和Browserify创建多个捆绑包时避免代码重复

[英]Avoiding code repetition when creating multiple bundles with Gulp and Browserify

Given these dependencies: 鉴于这些依赖性:

var browserify = require('gulp-browserify');
var buffer = require('vinyl-buffer');
var maps = require('gulp-sourcemaps');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');

How can I remove the code repetition in these two tasks: 如何在以下两个任务中删除代码重复:

gulp.watch('source/js/site/*.js', ['build-site-js']);
gulp.watch('source/js/admin/*.js', ['build-admin-js']);

gulp.task('build-site-js', function () {
    return gulp.src('source/js/site/site-client.js') // DIFFERENT
        .pipe(browserify({ debug: true }))
        .pipe(buffer())
        .pipe(rename('bundle.js')) // DIFFERENT
        .pipe(gulp.dest('public/js/'))
        .pipe(maps.init({ loadMaps: true }))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(maps.write('./'))
        .pipe(gulp.dest('public/js/'))
        .pipe(livereload());
});

gulp.task('build-admin-js', function () {
    return gulp.src('source/js/admin/admin-client.js') // DIFFERENT
        .pipe(browserify({ debug: true }))
        .pipe(buffer())
        .pipe(rename('admin.js')) // DIFFERENT
        .pipe(gulp.dest('public/js/'))
        .pipe(maps.init({ loadMaps: true }))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(maps.write('./'))
        .pipe(gulp.dest('public/js/'))
        .pipe(livereload());
});

Notice that I'm generating two Browserify bundles using an identical process. 注意,我正在使用相同的过程生成两个Browserify捆绑包。 The only differences between these two tasks are the names for the source and destination files. 这两个任务之间的唯一区别是源文件和目标文件的名称。 (I've marked the differences with // DIFFERENT comments.) (我已经用// DIFFERENT注释标记了差异。)

Ideally, I'd like to have a single gulp.watch('source/js/**/*.js', ['build-js']); 理想情况下,我想要一个gulp.watch('source/js/**/*.js', ['build-js']); , and then inside the task callback, determine in which directory the change occurred, and set the source and destination file names dynamically. ,然后在任务回调中确定更改发生在哪个目录中,并动态设置源文件和目标文件名。

Just write a function that takes the different args: 只需编写一个带有不同args的函数:

function doGulp(src, res) {
   return gulp.src(src) 
    .pipe(browserify({ debug: true }))
    .pipe(buffer())
    .pipe(rename(res)) 
    .pipe(gulp.dest('public/js/'))
    .pipe(maps.init({ loadMaps: true }))
    .pipe(rename({ suffix: '.min' }))
    .pipe(uglify())
    .pipe(maps.write('./'))
    .pipe(gulp.dest('public/js/'))
    .pipe(livereload());
}

and call it in the tasks: 并在任务中调用它:

gulp.task('build-admin-js', function () {
  return doGulp('source/js/admin/admin-client.js','admin.js');
});
gulp.task('build-site-js', function () {
   return doGulp('source/js/site/site-client.js','bundle.js');
});

So that your gulpfile becomes: 这样您的gulpfile变为:

var browserify = require('gulp-browserify');
var buffer = require('vinyl-buffer');
var maps = require('gulp-sourcemaps');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');

function doGulp(src, res) {
   return gulp.src(src) 
    .pipe(browserify({ debug: true }))
    .pipe(buffer())
    .pipe(rename(res)) 
    .pipe(gulp.dest('public/js/'))
    .pipe(maps.init({ loadMaps: true }))
    .pipe(rename({ suffix: '.min' }))
    .pipe(uglify())
    .pipe(maps.write('./'))
    .pipe(gulp.dest('public/js/'))
    .pipe(livereload());
}

gulp.watch('source/js/site/*.js', ['build-site-js']);
gulp.watch('source/js/admin/*.js', ['build-admin-js']);
gulp.task('build-admin-js', function () {
  return doGulp('source/js/admin/admin-client.js','admin.js');
});
gulp.task('build-site-js', function () {
   return doGulp('source/js/site/site-client.js','bundle.js');
});

This is the accepted answer. 这是公认的答案。 (Stack Overflow doesn't allow me to accept my own answer until tomorrow. I am not going to set up a reminder on my phone just for this, so in case I forget, consider this the accepted answer.) (Stack Overflow直到明天才允许我接受自己的答案。我不会为此而在手机上设置提醒,因此如果我忘记了,请考虑将其视为接受的答案。)


This setup works for me: 此设置对我有用:

gulp.watch('source/js/**/*.js', function (options) {
    var dir = options.path.match(/\\js\\(\w+)\\/)[1]; // Windows specific [1]

    gulp.src('source/js/' + dir + '/' + dir + '-client.js')
        .pipe(browserify({ debug: true }))
        .pipe(buffer())
        .pipe(gulp.dest('public/js/'))
        .pipe(maps.init({ loadMaps: true }))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(maps.write('./'))
        .pipe(gulp.dest('public/js/'))
        .pipe(livereload());
});

// [1] Use path.sep for a portable solution: nodejs.org/api/path.html#path_path_sep

I'm using the object argument of the gulp.watch callback to determine the parent directory of the changed file - either 'site' or 'admin' in my case. 我正在使用gulp.watch回调的object参数来确定已更改文件的父目录-在我的情况下为'site''admin' Based on that, I dynamically generate the gulp.src input. 基于此,我动态生成了gulp.src输入。

To make things simpler, I've also removed the renaming part, so the bundles “keep” the name of the file that is the bundle's entry point. 为简化起见,我还删除了重命名部分,因此捆绑软件“保留”了捆绑软件的入口点文件的名称。

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

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