如何生成相对于gulp-inject生成的目标文件的路径

时间:2016-11-16 18:19:06

标签: npm gulp gulp-inject

我正在使用gulp-inject向位于dist/index.htmldist/css/*.css

dist/js/*.js注入一些css和js文件
gulp.task('prep-index', function () {
  var target = gulp.src('./dist/index.html');
  var sources = gulp.src(['./dist/js/*.js', './dist/css/*.css'], {read: false}, {relative: true});

  return target.pipe(inject(sources))
    .pipe(gulp.dest('./dist/'));
});

目前它是从项目的基本URL注入文件,但我希望它从目标文件中注入文件链接,即dist/index.html

/dist/css/*.csscss/*.css

/dist/js/*.jsjs/*.js

但它的产生是这样的:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Directi Task</title>
    <!-- inject:css -->
    <link rel="stylesheet" href="/dist/css/basscss.min.css">
    <link rel="stylesheet" href="/dist/css/style.css">
    <!-- endinject -->
  </head>
  <body>
    <!-- inject:js -->
    <script src="/dist/js/jquery.min.js"></script>
    <script src="/dist/js/script.min.js"></script>
    <!-- endinject -->
  </body>
</html>

有关此的任何提示吗?

1 个答案:

答案 0 :(得分:0)

发现错误,{relative: true}应在inject()内,但不在src()

return target.pipe(inject(sources, {relative: true}))解决了这个问题。