developer tip

Gulp.js를 사용하여 스트림을 여러 대상에 저장하는 방법은 무엇입니까?

copycodes 2020. 11. 25. 08:05
반응형

Gulp.js를 사용하여 스트림을 여러 대상에 저장하는 방법은 무엇입니까?


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가 스트림 인 경우 여러 대상에 대한 파이핑은 현재 지원되지 않습니다. 이 문제에 대한 해결 방법은 무엇입니까?


현재 file.contents를 스트림으로 사용할 때 각 대상에 대해 두 개의 스트림을 사용해야합니다. 이것은 아마도 향후 수정 될 것입니다.

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

편집 :이 버그는 이제 꿀꺽 꿀꺽에서 수정되었습니다. 원본 게시물의 코드가 제대로 작동합니다.


나는 비슷한 문제에 직면했고 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
});

이 방법이 더 쉽다고 생각합니다. Justo에는 두 개의 대상이 있지만 minify 플러그인 전에 일반 파일에 대한 경로 하나를 넣고 minify 플러그인을 넣으면 축소 파일을 원하는 경로를 따릅니다.

예를 들면 :

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

});

For the case of broadcasting updates to multiple destinations, looping the gulp.dest command over an array of destinations works well.

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

I've had a lot of the same problem with Gulp, for various tasks piping to multiple destinations seems difficult or potentially impossible. Also, setting up multiple streams for one task seems inefficient but I guess this is the solution for now.

For my current project I needed multiple bundles to be associated with various pages. Modifying the Gulp Starter

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

browserify/watchify task:

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

I used a forEach loop inside of the glob module callback:

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

});

참고URL : https://stackoverflow.com/questions/21951497/how-to-save-a-stream-into-multiple-destinations-with-gulp-js

반응형