如何在 gulp-inject 上配置 dist 文件夹的相对文件路径?

how configure relative file path to dist folder on gulp-inject?

使用 gulp-inject.

将资源注入 index.html 时出现问题

我有以下应用样式流、供应商 js 代码和应用 js 代码:

// Vendor JS code
var vendorStream = gulp.src(mainBowerFiles({
      paths: {
        bowerDirectory: config.bowerDir
      },
      checkExistence: true
  }))
  .pipe(concat('vendors.js'))
  .pipe(gulp.dest(config.bowerLibDist))
  .pipe(size({
      title:"Vendor JS code"
  }));

// JS App Code
var jsStream = gulp.src(config.jsSrcPath)
  .pipe(concat('app.js'))
  .pipe(uglify())
  .pipe(gulp.dest(config.jsDistFolder))
  .pipe(size({
      title:"App js code"
  }));

// SASS App Code
var cssStream = gulp.src(config.sassSrcPath)
    .pipe(sass({
      outputStyle: 'compressed'
    })
    .on('error', sass.logError))
    .pipe(autoprefixer())
    .pipe(gulpIf(config.production, cssnano()))
    .on("error", notify.onError(function (error) {
      return "Error: " + error.message;
    })).pipe(gulp.dest(config.cssDistFolder))
    .pipe(size({
      title:"Styles"
    }));

我想做的是 index.ml 通过以下任务注入这些资源并将它们放在 dist 目录中:

gulp.task('inject', function() {
   return gulp.src('index.html', {cwd: './app'})
        .pipe(inject(es.merge(vendorStream, jsStream, cssStream)))
        .pipe(htmlmin({collapseWhitespace: true}))
        .pipe(gulp.dest(config.distFolder));
});

这工作正常,但问题是路径还包括 dist 目录。这是发布基目录:

// Starts a server using the production build
gulp.task('server', ['build'], function () {
  connect.server({
    root: './dist',
    hostname: '0.0.0.0',
    port: 90
  });
});

我如何配置 gulp-inject 而不是

<script src="/dist/js/app.js"></script>

成为

<script src="js/app.js"></script>

谢谢:)

其中一种方法是使用 transform function

gulp.task('inject', function() {
  return gulp.src('index.html', {cwd: './app'})
             .pipe(inject(
                 es.merge(vendorStream, jsStream, cssStream),
                 { 
                     transform: function( filepath, file, index, length, targetFile ) {
                       return inject.transform.call(inject.transform, filepath.replace(/\/dist\//, ""), file, index, length, targetFile);
                     }
                 }
             ))
             .pipe(htmlmin({collapseWhitespace: true}))
             .pipe(gulp.dest(config.distFolder));
});

如果您只想更改文件名,也可以保留初始转换功能。