Gulp-inject says “Nothing to inject into index.html”

家住魔仙堡 提交于 2019-12-10 13:24:32

问题


I'm trying to inject some files in my index, all of them concatenated and minified into a .tmp folder, as follows:

gulp.task('prep-js',['clean'], function(){
  var jspath = './src/page/**/*.js';
  var treatJs = gulp.src(jspath)
         .pipe(plugins.concat('scripts.js'))
         .pipe(plugins.uglify())
         .pipe(gulp.dest('.tmp/page/js'))
});

But when I run the injection task, it says "Nothing to inject into index.html". Here's the code:

gulp.task('inject-deps', ['prep-css', 'prep-js'], function(){

  //select main bower files
  var bowerDep = gulp.src(plugins.mainBowerFiles(), {read: false});

  //inject files
  return  gulp.src('./src/page/index.html')
          .pipe(plugins.inject(bowerDep, {relative: true, name:'bower'}))
          .pipe(plugins.inject(gulp.src('.tmp/page/js/*.js'), {name:'frontjs'}))
          .pipe(plugins.inject(gulp.src('.tmp/page/css/*.css'), {name:'frontcss'}))
          .pipe(gulp.dest('.tmp/page'));
});

Interesting thing, the first pipe injecting the main bower files works perfectly, but it doesn't happen to the following two.

Also, just for information, 'plugins' is a variable which is requiring my plugins.

Any idea about this issue?


回答1:


You need to return the stream in your prep-js task:

gulp.task('prep-js',['clean'], function(){
  var jspath = './src/page/**/*.js';
  return gulp.src(jspath)
         .pipe(plugins.concat('scripts.js'))
         .pipe(plugins.uglify())
         .pipe(gulp.dest('.tmp/page/js'))
});

Otherwise inject-deps will not wait for prep-js to finish before it runs, meaning the concatenated and uglified JS files will not be in .tmp/page/js yet.

Relevant portion of the Gulp documentation:

Note: By default, tasks run with maximum concurrency -- e.g. it launches all the tasks at once and waits for nothing. If you want to create a series where tasks run in a particular order, you need to do two things:

  • give it a hint to tell it when the task is done,
  • and give it a hint that a task depends on completion of another.


来源:https://stackoverflow.com/questions/35364857/gulp-inject-says-nothing-to-inject-into-index-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!