【问题标题】:How to save a stream into multiple destinations with Gulp.js?如何使用 Gulp.js 将流保存到多个目的地?
【发布时间】:2019-02-06 16:32:37
【问题描述】:
const gulp = require('gulp');
const $ = require('gulp-load-plugins')();
const source = require('vinyl-source-stream');
const browserify = require('browserify');

gulp.task('build', () =>
  browserify('./src/app.js').bundle()
    .pipe(source('app.js'))
    .pipe(gulp.dest('./build'))       // OK. app.js is saved.
    .pipe($.rename('app.min.js'))
    .pipe($.streamify($.uglify())
    .pipe(gulp.dest('./build'))       // Fail. app.min.js is not saved.
);

当前不支持当 file.contents 是流时通过管道传输到多个目标。有什么办法可以解决这个问题?

【问题讨论】:

    标签: javascript node.js browserify gulp


    【解决方案1】:

    目前,当使用 file.contents 作为流时,您必须为每个 dest 使用两个流。这可能会在未来得到解决。

    var gulp       = require('gulp');
    var rename     = require('gulp-rename');
    var streamify  = require('gulp-streamify');
    var uglify     = require('gulp-uglify');
    var source     = require('vinyl-source-stream');
    var browserify = require('browserify');
    var es         = require('event-stream');
    
    gulp.task('scripts', function () {
        var normal = browserify('./src/index.js').bundle()
            .pipe(source('bundle.js'))
            .pipe(gulp.dest('./dist'));
    
        var min = browserify('./src/index.js').bundle()
            .pipe(rename('bundle.min.js'))
            .pipe(streamify(uglify())
            .pipe(gulp.dest('./dist'));
    
        return es.concat(normal, min);
    });
    

    编辑:此错误现已在 gulp 中修复。您原始帖子中的代码应该可以正常工作。

    【讨论】:

    • 我们不能保存 bundle() 的输出,var bundle = browserify('./src/index.js').bundle();,然后将其提供给 2 个不同的“管道”吗?像 bundle.pipe(source('bundle.js')).pipe(gulp.dest('./dist'));bundle.pipe(rename('bundle.min.js')).pipe(streamify(uglify()) .pipe(gulp.dest('./dist')); 这样 bundle 运行一次,我们使用输出两次。
    • 从哪个版本的 gulp 中修复了这个错误?
    • @AlirezaMirian +1 想知道它修复了哪个版本
    【解决方案2】:

    我遇到了类似的问题,希望在 lint、uglify 和 minify 任务之后将 gulp 源复制到多个位置。我最终解决了这个问题,

    gulp.task('script', function() {
      return gulp.src(jsFilesSrc)
        // lint command
        // uglify and minify commands
        .pipe(concat('all.min.js'))
        .pipe(gulp.dest('build/js')) // <- Destination to one location
        .pipe(gulp.dest('../../target/build/js')) // <- Destination to another location
    });
    

    【讨论】:

      【解决方案3】:

      对于向多个目标广播更新的情况,在目标数组上循环 gulp.dest 命令效果很好。

      var gulp = require('gulp');
      
      var source = './**/*';
      
      var destinations = [
          '../foo/dest1',
          '../bar/dest2'
      ];
      
      gulp.task('watch', function() {
          gulp.watch(source, ['sync']);
      });
      
      gulp.task('sync', function (cb) {
          var pipeLine = gulp.src(source);
      
          destinations.forEach(function (d) {
              pipeLine = pipeLine.pipe(gulp.dest(d));
          });
      
          return pipeLine;
      });
      

      【讨论】:

        【解决方案4】:

        我认为这种方式更容易。 Justo 你有两个目的地,但是在缩小插件之前你把一个路径放到普通文件中,然后你把缩小插件放在你想要一个缩小文件的路径之后。

        例如:

        gulp.task('styles', function() {
        
            return gulp.src('scss/main.scss')
            .pipe(sass())
            .pipe(gulp.dest('css')) // Dev normal CSS
            .pipe(minifycss())
            .pipe(gulp.dest('public_html/css')); // Live Minify CSS
        
        });
        

        【讨论】:

          【解决方案5】:

          我在使用 Gulp 时遇到过很多相同的问题,因为管道传输到多个目的地的各种任务似乎很困难,或者可能是不可能的。此外,为一项任务设置多个流似乎效率低下,但我想这是目前的解决方案。

          对于我当前的项目,我需要将多个捆绑包与不同的页面相关联。修改 Gulp Starter

          https://github.com/greypants/gulp-starter

          浏览/监视任务:

          https://github.com/dtothefp/gulp-assemble-browserify/blob/master/gulp/tasks/browserify.js

          我在 glob 模块回调中使用了一个 forEach 循环:

          gulp.task('browserify', function() {
          
            var bundleMethod = global.isWatching ? watchify : browserify;
          
            var bundle = function(filePath, index) {
              var splitPath = filePath.split('/');
              var bundler = bundleMethod({
                // Specify the entry point of your app
                entries: [filePath],
                // Add file extentions to make optional in your requires
                extensions: ['.coffee', '.hbs', '.html'],
                // Enable source maps!
                debug: true
              });
          
              if( index === 0 ) {
                // Log when bundling starts
                bundleLogger.start();
              }
          
              bundler
                .transform(partialify)
                //.transform(stringify(['.html']))
                .bundle()
                // Report compile errors
                .on('error', handleErrors)
                // Use vinyl-source-stream to make the
                // stream gulp compatible. Specifiy the
                // desired output filename here.
                .pipe(source( splitPath[splitPath.length - 1] ))
                // Specify the output destination
                .pipe(gulp.dest('./build/js/pages'));
          
              if( index === (files.length - 1) ) {
                // Log when bundling completes!
                bundler.on('end', bundleLogger.end);
              }
          
              if(global.isWatching) {
                // Rebundle with watchify on changes.
                bundler.on('update', function(changedFiles) {
                  // Passes an array of changed file paths
                  changedFiles.forEach(function(filePath, index) {
                    bundle(filePath, index);
                  });
                });
              }
            }
          
            // Use globbing to create multiple bundles
            var files = glob('src/js/pages/*.js', function(err, files) {
              files.forEach(function(file, index) {
                bundle(process.cwd() + '/' + file, index);
              })
            });
          
          });
          

          【讨论】:

            猜你喜欢
            • 2014-09-02
            • 1970-01-01
            • 2013-01-06
            • 2019-07-20
            • 1970-01-01
            • 2016-04-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多