繁体   English   中英

使用Gulp为所有.html页面生成关键CSS

[英]Generate critical CSS for all .html pages with Gulp

因此,Google建议使用以下npm软件包来生成关键的CSS:

var critical = require('critical');

gulp.task('critical', function (cb) {
    critical.generate({
        base: 'app/',
        src: 'index.html',
        dest: 'css/index.css'
    });
});

它实际上是一个很棒的工具,就像一个魅力。 唯一的缺点是,它只适用于Gulp中的单个文件,而不是可以对所有匹配文件使用gulp.src('app/*.html')调用的其他任务。

因此,我决定将我的所有页面列出在一个数组中并对其进行遍历:

var pages = ['index', 'pricing'];

gulp.task('critical', function (cb) {
    for (var i = 0; i < pages.length; i++) {

        critical.generate({
            base: 'app/',
            src: pages[i] + '.html',
            dest: 'css/' + pages[i] + '.css'
        });
    }
});

事实证明,这也是一个可行的解决方案(某些node.js内存问题除外)。

现在的问题是我必须在阵列中手动列出页面。 因此,我想知道是否有一种方法可以使用类似于gulp.src('app/*.html')的功能自动生成这样的列表,例如将选定文件夹中的所有.html页面包括在内,以生成重要的CSS?

任何帮助将不胜感激!

您可以做类似的事情(后面是伪代码):

var foreach = require('gulp-foreach');

gulp.task('default', function () {

  return gulp.src('./yourPathTo/*.html')

          // treats each file in gulp.src as a separate stream
    .pipe(foreach(function (stream, file) {

         // see in next code for example of following
         var fileBase = file.path.stringOptoRemove Extension;

         critical.generate({
            base: 'app/',
            src: file.path,
            dest: 'css/' + fileBase + '.css'
        });
   }))
});

使用gulp-foreach

另外, glob-fs也会很有用,我包括下面编写的示例代码,该示例代码处理流中的单个文件,然后使用新的文件名将其写出。

var gulp = require('gulp');
var glob = require('glob-fs')({ gitignore: true });
var html2text = require('html-to-text');
var fs = require('fs');
var path = require('path');  

gulp.task('default', function () {

  glob.readdirStream('build/*.html')
    .on('data', function (file) {
      console.log(file.path);

      html2text.fromFile(file.path, (err, text) => {
        if (err) return console.error(err);

        // strip off extension and parent directories
        var filename = path.basename(file.path, '.html');

        // I assume you want a.html text to be written to 'build/txt/a.txt'
        fs.writeFileSync(path.join('build/txt', filename  + '.txt'), text, 'utf8');
      });
    });
});

我正在使用gulp.src进行此工作。 唯一的问题是,在包含许多html文件的网站上需要花费3分钟的时间才能完成,并且如果同时在计算机上执行其他操作,则很容易崩溃。 我实际上是通过尝试找到一种更有效的方法来找到这篇文章的。

gulp.task('criticalCSS', ['compile-html', 'compile-styles'], function() {
    return gulp.src(config.dest.root + '**/*.html')
    .pipe(critical({
        base: config.dest.root, // "app/" which is the root of my build folder
        inline: true, 
        css: config.dest.css + styleBundleFilename, // "app/css/mainCssFilename"
        dimensions: [{
            width: 320,
            height: 480
            },{
            width: 768,
            height: 1024
            },{
            width: 1280,
            height: 960
            }],
        ignore: ['font-face']
    }))
    .on('error', function(err) { log(err.message); })
    .pipe(gulp.dest(config.dest.root));
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM