A
A
amorphine2016-09-02 15:48:39
JavaScript
amorphine, 2016-09-02 15:48:39

How to issue a task in gulp to generate critical CSS at given URLs using the critical plugin?

Uses the npm critical module to generate critical CSS. However, html files for it are downloaded manually:

gulp.task("criticalCss", function() {
    var critical = require('critical'),
        name = "ProductProductCommon";
    critical.generate({
        base: 'uncss_ignore/',
        src: object + '.html',
        dest: 'css/critical/mobile' + object + '.css',
        minify: true,
        dimensions: [{
            width: 360,
            height: 640
        }],
    });
});

There are many types of pages, automation is clearly not enough.
With what you can automate the process, so that, for example, from an array:
[{name: "Product", url: "site.com/product/2345.html"},
{name: "Catalog", url: "site.com/catalog.html"}]

The script downloaded the necessary pages and returned critical? What additional modules can be used and how can this be arranged?
UPD. While there are no other opinions, I found out that the critical module supports loading by url:
module.exports = function(gulp, plugins) {
    return function () {
        var critical = require('critical'),
            object = "ProductProductCommon",
            settings = require("./settings.js");

        critical.generate({
            src: settings.base_url,
            dest: './' + object + '.css',
            minify: true,
            dimensions: [{
                width: 360,
                height: 640
            }],
        });
    };
}

Here base-url is just one url. How to do it beautifully and asynchronously with iteration over an array with urls instead of html files?
UPD 2. At the moment I came to such a decision
module.exports = function(gulp, plugins) {
    return function () {
        var critical = require('critical'),
            settings = require("./settings.js"),
            urls = [
                {name: "home", url:""},
                {name: "product", url:"palatki/34174.html"},
                {name: "catalog", url:"palatki.html"},
                {name: "o_nas", url:"o_nas.html"},
                {name: "dostavka", url:"dostavka.html"},
                {name: "oplata", url:"oplata.html"},
                {name: "basket", url:"basket.html"},
                {name: "contacts", url:"contacts.html"}
            ];

        urls.forEach(function(entry){
            critical.generate({
                src: settings.base_url + entry.url,
                dest: settings.dest_folder + entry.name + '.css',
                minify: true,
                dimensions: settings.dimensions
            });
        });
    };
}

I am not an expert on gulp and especially on the node, but I have rarely seen iteration in custom solutions. If there are recommendations, I will be glad.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question