1

我开始喜欢 gulp,但我遇到了太多难以找到的神秘错误,最后我正在为我的 gulpfile.js 工作而不是做我的工作。

无论如何,我之前尝试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*/];
4

1 回答 1

1

好吧,您摘录的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。请注意,如果您要在此处传递 URL,则该任务将需要更长的时间才能完成。如果您想将一些 HTML 直接传递到任务中,则可以在此处将其指定为字符串。

于 2015-04-10T18:31:54.567 回答