Gulp fontgen失败,因为“TypeError:无法读取属性'替换'未定义”

时间:2016-07-13 09:38:18

标签: npm gulp

我从一个gulp-fontgen项目开始,但是当我使用gulp命令运行它时出现错误:

TypeError: Cannot read property 'replace' of undefined

我的gulpfile:

var gulp = require('gulp');
var fontgen = require('./node_modules/gulp-fontgen');

gulp.task('fontgen', function() {
  return gulp.src("./assets/*.{ttf,otf}")
    .pipe(fontgen({
      dest: "./dest/"
    }));
});

gulp.task('default', ['fontgen']);

我的package.json:

{
  "name": "garagord-webfont",
  "version": "1.0.0",
  "description": "",
  "main": "gulpfile.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-fontgen": "^0.2.4"
  }
}

整个终端输出:

$ gulp
[11:26:24] Using gulpfile /Volumes/B/Documentos/tipografia-garagord/Gulp/gulpfile.js
[11:26:24] Starting 'fontgen'...
/Volumes/B/Documentos/tipografia-garagord/Gulp/node_modules/fontfacegen/lib/configure.js:19
    _.config_file  = _.source.replace(_.extension, '') + '.json';
                             ^

TypeError: Cannot read property 'replace' of undefined
    at module.exports (/Volumes/B/Documentos/tipografia-garagord/Gulp/node_modules/fontfacegen/lib/configure.js:19:30)
    at module.exports (/Volumes/B/Documentos/tipografia-garagord/Gulp/node_modules/fontfacegen/lib/fontfacegen.js:16:18)
    at DestroyableTransform._transform (/Volumes/B/Documentos/tipografia-garagord/Gulp/node_modules/gulp-fontgen/lib/gulp-fontgen.js:47:31)
    at DestroyableTransform.Transform._read (/Volumes/B/Documentos/tipografia-garagord/Gulp/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:159:10)
    at DestroyableTransform.Transform._write (/Volumes/B/Documentos/tipografia-garagord/Gulp/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:147:83)
    at doWrite (/Volumes/B/Documentos/tipografia-garagord/Gulp/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:313:64)
    at writeOrBuffer (/Volumes/B/Documentos/tipografia-garagord/Gulp/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:302:5)
    at DestroyableTransform.Writable.write (/Volumes/B/Documentos/tipografia-garagord/Gulp/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:241:11)
    at write (/Volumes/B/Documentos/tipografia-garagord/Gulp/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:623:24)
    at flow (/Volumes/B/Documentos/tipografia-garagord/Gulp/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:632:7)

1 个答案:

答案 0 :(得分:3)

gulp-fontgen插件严重受损。只需看看source code

return _through2.default.obj(function (file, enc, callback) {
  // options.source = file.path;
  (0, _fontfacegen2.default)(options);

  undefined.push(file);
  return callback();
});

上面注释掉的行是造成错误的原因。 source选项未传递给fontfacegen。即使您取消注释该行,也会出现一个不可能有效的undefined.push(file);(教训:不要吸毒,小孩,并且总是写单元测试)。

我的建议:不要使用gulp-fontgen。该插件实际上只是fontfacegen的一个薄包装器。您可以直接将fontfacegenmap-stream结合使用来轻松替换它:

var gulp = require('gulp');
var fontfacegen = require('fontfacegen');
var map = require('map-stream');

gulp.task('fontgen', function() {
  return gulp.src("./assets/*.{ttf,otf}")
    .pipe(map(function(file, cb) {
      fontfacegen({
        source: file.path,
        dest: './dest/'
      });
      cb(null, file);
    }));
});