如何在 .pipe (cheerio (function ($) {?

How to use cheerio.load in .pipe (cheerio (function ($) {?

我找到了一个我想使用的 HTML 压缩 gulp 插件。我遇到了编码问题。

这是别人插件里写的:

gulp.task ('indexHtml', function () {
    return gulp.src ('index.html')
        .pipe (cheerio (function ($) {
            $ ('script'). remove ();
            $ ('link'). remove ();
            $ ('body'). append ('<script src = "skin / zhuce / MergeMin / app.full.min.js"> </ script>');
            $ ('head'). append ('<link rel = "stylesheet" href = "skin / zhuce / MergeMin / app.full.min.css">');
        })))
        .pipe (gulp.dest ('test /'));
});

但是输出的是统一的Unicode。导致部分字符无法在浏览器中只读解释。

我搜索了一下,找到了别人指出的解决方案:

Use cheerio.load (html, {decodeEntities: false});

传入参数

但我不知道如何在 Node.js 中进行。

我在上面的代码中遇到了这个嵌套:

.pipe (cheerio (function ($) {

不知道怎么改:cheerio.load (html, {decodeEntities: false});.

假设您使用的是gulp-cheerio,您需要更改:

.pipe (cheerio (function ($) {
        $ ('script'). remove ();
        $ ('link'). remove ();
        $ ('body'). append ('<script src = "skin / zhuce / MergeMin / app.full.min.js"> </ script>');
        $ ('head'). append ('<link rel = "stylesheet" href = "skin / zhuce / MergeMin / app.full.min.css">');
    })))

收件人:

.pipe (cheerio ({
    run: (function ($) {
        $ ('script'). remove ();
        $ ('link'). remove ();
        $ ('body'). append ('<script src = "skin / zhuce / MergeMin / app.full.min.js"> </ script>');
        $ ('head'). append ('<link rel = "stylesheet" href = "skin / zhuce / MergeMin / app.full.min.css">');
    }))),
    parserOptions: {
    decodeEntities: false,
    //other options here
  }
})