2015-04-02 91 views
0

我开始爱喝酒,但我一直有太多难以发现的神秘错误,最后我正在为我的gulpfile.js工作做我的工作。gulp-uncss'TypeError'在与gulp一起使用时未定义 - 如果

无论如何,我试过gulp-uncss之前,gulp-useref之外,因此gulp-if之外,但我的gulpfile.js结束了太笨重,难以理解。现在它是可读的,但它不起作用。欢呼。

var p = require('gulp-load-plugins')(); 

gulp.task('html', function() { 
    var assets = p.useref.assets(); 

    gulp.src('*.html') 
     .pipe(assets) 
     .pipe(p.if('*.js', p.uglify())) 
     .pipe(p.if('*.css', p.uncss())) 
     .pipe(assets.restore()) 
     .pipe(p.useref()) 
     .pipe(gulp.dest(build_folder)) 
     .pipe(p.size()); 
}); 

生成此:

[12:47:53] /long/path/web $ gulp html 
[12:48:06] Using gulpfile /long/path/web/gulpfile.js 
[12:48:06] Starting 'html'... 
[12:48:07] 'html' errored after 507 ms 
[12:48:07] TypeError: Cannot set property 'ignoreSheets' of undefined 
    at Object.module.exports (/long/path/web/node_modules/gulp-uncss/index.js:14:26) 
    at Gulp.<anonymous> (/long/path/web/gulpfile.js:45:31) 
    at module.exports (/long/path/web/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:34:7) 
    at Gulp.Orchestrator._runTask (/long/path/web/node_modules/gulp/node_modules/orchestrator/index.js:273:3) 
    at Gulp.Orchestrator._runStep (/long/path/web/node_modules/gulp/node_modules/orchestrator/index.js:214:10) 
    at Gulp.Orchestrator.start (/long/path/web/node_modules/gulp/node_modules/orchestrator/index.js:134:8) 
    at /usr/local/lib/node_modules/gulp/bin/gulp.js:129:20 
    at process._tickCallback (node.js:355:11) 
    at Function.Module.runMain (module.js:503:11) 
    at startup (node.js:129:16) 
    at node.js:814:3 
[12:48:12] /long/path/web $ 

Uncss是我的工作流程的关键,我无法破解什么错在这里。任何线索?

编辑:这是什么在/long/path/web/node_modules/gulp-uncss/index.js,直到第14行

'use strict'; 

var uncss  = require('uncss'), 
    gutil  = require('gulp-util'), 
    assign  = require('object-assign'), 
    transform = require('stream').Transform, 

    PLUGIN_NAME = 'gulp-uncss'; 

module.exports = function(options) { 
    var stream = new transform({ objectMode: true }); 

    // Ignore stylesheets in the HTML files; only use those from the stream 
    options.ignoreSheets = [/\s*/]; 

回答

0

好了,你的gulp-uncss/index.js状态摘录,它不能处理未定义options参数 - 它是不是在你gulpfile:

.pipe(p.if('*.css', 
    p.uncss({ 
    html: [ '*.html' ] // html files to check for styles to keep 
    }) 
)) 

该文档还指出,这html是一个必需的选项属性:npmjs.com/package/gulp-uncss#html

类型:Array|String要求的值。

一个数组,它可以包含与您的gulpfile.js相关的文件数组,也可以包含URL。请注意,如果您要在此处传递网址,则该任务将需要更长时间才能完成。如果您想将某些HTML直接传递给任务,则可以在此处将其指定为字符串。

+0

它一定是最近的更新,因为我读过的例子就是这样。你能提供一个可行的解决方案吗 – 2015-04-13 23:59:41

相关问题