简体   繁体   English

抢先运行不同的任务,然后保存到不同的输出

[英]Gulp running different tasks before saving to different output

I'm trying to map the result of flattened JavaScript files to two different locations, but I want to do some extra processing on them first. 我正在尝试将展平的JavaScript文件的结果映射到两个不同的位置,但是我想先对它们进行一些额外的处理。 The problem I have is those extra bits are completely different for each. 我的问题是每个额外的位完全不同。

Here is what I've tried. 这是我尝试过的。 I want to include a source map when saving the output to app/scripts , but I want to not include the source map, but uglify them when saving it to dist/scripts . 在将输出保存到app/scripts ,我想包括一个源映射,但是在将其保存到dist/scripts时,我不希望包括源图,而是对其进行丑化。

gulp.task('scripts', () => {
  return gulp.src('app/scripts.babel/**/*.js')
    .pipe($.babel({
      presets: ['es2015']
    }))
    .pipe($.flatten())
    .pipe($.if('*.js', $.sourcemaps.init()))
    .pipe($.if('*.js', $.sourcemaps.write('.')))
    .pipe(gulp.dest('app/scripts'))
    .pipe($.uglify())
    .pipe(gulp.dest('dist/scripts'));
})

This snippet doesn't do what I want because the result of creating a source map is passed on to the rest of the flow (Besides that, it is actually crashing saying Unhandled stream error in pipe ). 此代码片段无法满足我的需求,因为创建源映射的结果将传递到其余的流中(除此之外,它实际上崩溃了,并指出Unhandled stream error in pipe )。 I want to do this using only the same task. 我只想使用相同的任务执行此操作。 How do I do that? 我怎么做?

The way you're using gulp-sourcemaps is all wrong. 您使用gulp-sourcemaps方式都是错误的。 Transformations that should be recorded in source maps must happen between sourcemaps.init() and sourcemaps.write() . 应该记录在源映射中的转换必须在sourcemaps.init()sourcemaps.write() Additionally gulp-flatten does not support sourcemaps . 另外, gulp gulp-flatten支持sourcemaps Luckily for you you can achieve the same with using gulp-rename . 幸运的是,您可以使用gulp-rename实现相同的目的。

The reason you're getting that error is because you're trying to uglify() your source map files. 出现该错误的原因是因为您尝试对源地图文件进行uglify() Since those obviously don't contain valid JS uglify 's parser throws up. 因为那些显然不包含有效的JS,所以uglify的解析器抛出了。

The way to solve all this is to simply store your stream in a variable and then .pipe() it to two different destinations. 解决所有这些问题的方法是将流简单地存储在一个变量中,然后.pipe().pipe()到两个不同的目的地。 However since the underlying vinyl files are shared between the two resulting streams you need to first clone them using gulp-clone . 但是,由于底层的vinyl文件在两个结果流之间共享,因此您需要首先使用gulp-clone克隆它们。 Otherwise the same files will end up in both destinations. 否则,相同的文件将最终出现在两个目标位置。

Finally you use merge-stream to merge the two resulting streams so you can return them from your task. 最后,您使用merge-stream合并两个结果流,以便可以从任务中return它们。

var merge = require('merge-stream');

gulp.task('scripts', () => {
  var js = gulp.src('app/scripts.babel/**/*.js')
    .pipe($.sourcemaps.init())
    .pipe($.babel({ presets: ['es2015'] }))
    .pipe($.rename({dirname:''}))

  return merge(
    js.pipe($.clone())
      .pipe($.sourcemaps.write('.'))
      .pipe(gulp.dest('app/scripts')),
    js.pipe($.clone())
      .pipe($.uglify())
      .pipe(gulp.dest('dist/scripts')));
});

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

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