gulp-inject不注入任何东西

时间:2019-09-03 02:45:49

标签: gulp gulp-inject

不得不将gulpfile重写为gulp 4,但是我面临着gulp-inject不注入任何内容的问题。

var gulp = require('gulp');
var svgstore = require('gulp-svgstore');
var svgmin = require('gulp-svgmin');
var rename = require('gulp-rename');
var cheerio = require('gulp-cheerio');
var inject = require('gulp-inject');
var path = require('path');

gulp.task('svgstore', function () {
  var svgs = gulp
    .src('static/svg/icons/src/*.svg')
    .pipe(svgmin(function (file) {
      var prefix = path.basename(file.relative, path.extname(file.relative));
      return {
        plugins: [{
          convertPathData: false
        }, {
          cleanupIDs: {
            prefix: prefix + '-',
            minify: true
          }
        }]
      }
    }))
    .pipe(cheerio(function ($, file) {
      $('[fill]').attr('fill', 'currentColor');
    }))
    .pipe(svgstore({ inlineSvg: true }))
    .pipe(gulp.dest('dest'));

  function fileContents(filePath, file) {
    return file.contents.toString();
  }

  return gulp
    .src('layouts/partials/src/inline-svg.html')
    .pipe(inject(svgs, { transform: fileContents }))
    .pipe(gulp.dest('layouts/partials'));
});

// Watch asset folder for changes
gulp.task("watch", function () {
  var watcher = gulp.watch("static/svg/icons/src/**/*")
  watcher.on('all', gulp.series('svgstore'))
})

// Set watch as default task
gulp.task("default", gulp.series("watch"))

我不知道为什么。我尝试记录fileContents,但只显示[Function: fileContents]

0 个答案:

没有答案