【问题标题】:Output a minified version and non minified version with Gulp使用 Gulp 输出缩小版和非缩小版
【发布时间】:2015-12-06 14:24:56
【问题描述】:

我在 Gulpfile.js 中有这个捆绑器,它运行良好:

var compile = (watch) => {
  var bundler = watchify(browserify('./src/index.js', { debug: true }).transform(babel));

  var rebundle = () => {
    bundler.bundle()
      .on('error', function(err) { console.error(err); this.emit('end'); })
      .pipe(source('lib.js'))
      .pipe(buffer())
      .pipe(sourcemaps.init({ loadMaps: true }))
      .pipe(sourcemaps.write('./'))
      .pipe(gulp.dest('./dist/'));
  };

  if (watch) {
    bundler.on('update', function() {
      console.log('-> bundling...');
      rebundle();
    });
  }

  rebundle();
};

这给了我:arli.js 和 arli.js.map

但是当试图像这样丑化它时:

var compile = (watch) => {
  var bundler = watchify(browserify('./src/index.js', { debug: true }).transform(babel));

  var rebundle = () => {
    bundler.bundle()
      .on('error', function(err) { console.error(err); this.emit('end'); })
      .pipe(source(lib.js))
      .pipe(buffer())
      .pipe(sourcemaps.init({ loadMaps: true }))
      .pipe(sourcemaps.write('./'))
      .pipe(gulp.dest('./dist/'));

    return gulp.src('./dist/lib.js')
      .pipe(rename('lib.min.js'))
      .pipe(sourcemaps.init())
      .pipe(uglify({
        preserveComments: 'license',
      }))
      .pipe(sourcemaps.write('./'))
      .pipe(gulp.dest('./dist/'));
  };

  if (watch) {
    bundler.on('update', function() {
      console.log('-> bundling...');
      rebundle();
    });
  }

  rebundle();
};

这给了我相同的两个文件,但如果我重复这个任务,它也会给我 lib.min.js 和 lib.min.js.map,因为第一次 lib.js 不存在。

我尝试过运行序列,但它也是一样的。

【问题讨论】:

    标签: javascript node.js gulp browserify babeljs


    【解决方案1】:

    您可以尝试使用end 回调吗?

    var compile = (watch, done) => {
      var bundler = watchify(browserify('./src/index.js', { debug: true }).transform(babel));
    
      var rebundle = () => {
        bundler.bundle()
          .on('error', function(err) { console.error(err); this.emit('end'); })
          .pipe(source(lib.js))
          .pipe(buffer())
          .pipe(sourcemaps.init({ loadMaps: true }))
          .pipe(sourcemaps.write('./'))
          .pipe(gulp.dest('./dist/')).on('end', function () {
            gulp.src('./dist/lib.js')
              .pipe(rename('lib.min.js'))
              .pipe(sourcemaps.init())
              .pipe(uglify({
                preserveComments: 'license',
              }))
              .pipe(sourcemaps.write('./'))
              .pipe(gulp.dest('./dist/')).on('end', function() {
                done();
              });
          });
      };
    
      if (watch) {
        bundler.on('update', function() {
          console.log('-> bundling...');
          rebundle();
        });
      }
    
      rebundle();
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      相关资源
      最近更新 更多