2015-04-02 42 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

相關問題