简体   繁体   中英

gulp-inject not working with event-stream

gulp-inject does not working with event-stream.

var gulp = require('gulp');
var mainBowerFiles = require('main-bower-files');
var inject = require('gulp-inject');
var es = require('event-stream');

var config = {
    sassDir: './resources/assets/sass',
    jsPath: './resources/app',
    fontDir: './resources/fonts',
    imageDir: './resources/images',
    bowerDir: './bower_components'
};

gulp.task('index', function () {
    return gulp.src('./resources/index.html')
        .pipe(inject(gulp.src(mainBowerFiles('**/*.js'), {read: false}), {name: 'bower'}))
      .pipe(inject(es.merge(gulp.src(config.jsPath + '/**/*.js', {read: false}))))
      .pipe(gulp.dest('./public'));
});

The result coming like this.

<!-- inject:js -->
<script src="/resources/app/core.js"></script>
<script src="/resources/app/first-folder/01.js"></script>
<script src="/resources/app/second-folder/02.js"></script>
<!-- endinject -->

Unfortunately the mainBowerFiles('**/*.js') do not merge with inject(es.merge(gulp.src(config.jsPath + '/**/*.js', {read: false})))

在此处输入图片说明

Your problem is that you're invoking inject() twice with two different sources (the main bower files and the files in resources/app ), but you want both sources to be injected into the same section.

What you need to do is call inject() once with the already merged sources:

gulp.task('index', function () {
  return gulp.src('./resources/index.html')
    .pipe(inject(es.merge(
      gulp.src(mainBowerFiles('**/*.js'), {read: false}),
      gulp.src(config.jsPath + '/**/*.js', {read: false})
    )))
    .pipe(gulp.dest('./public'));
});

EDIT: If you want to keep the order of the files you can use streamqueue instead of es.merge() :

var streamqueue = require('streamqueue');

gulp.task('index', function () {
  return gulp.src('./resources/index.html')
    .pipe(inject(streamqueue({ objectMode: true },
      gulp.src(mainBowerFiles('**/*.js'), {read: false}),
      gulp.src(config.jsPath + '/**/*.js', {read: false})
    )))
    .pipe(gulp.dest('./public'));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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