Skip to content

Instantly share code, notes, and snippets.

@Golodhros
Created October 11, 2015 02:36
Show Gist options
  • Save Golodhros/467901440093a9904541 to your computer and use it in GitHub Desktop.
Save Golodhros/467901440093a9904541 to your computer and use it in GitHub Desktop.
gulp.task('critical', function() {
var request = require('request');
var path = require( 'path' );
var criticalcss = require("criticalcss");
var fs = require('fs');
var tmpDir = require('os').tmpdir();
var cssUrl = 'http://PROJECT.dev/app/themes/sage/dist/styles/main-3c29b398.css';
var cssPath = path.join( tmpDir, 'style.css' );
var includePath = path.join( 'dist/styles/', 'critical.css' );
request(cssUrl).pipe(fs.createWriteStream(cssPath)).on('close', function() {
criticalcss.getRules(cssPath, function(err, output) {
if (err) {
throw new Error(err);
} else {
criticalcss.findCritical("http://umamibierzo.dev/", { rules: JSON.parse(output) }, function(err, output) {
if (err) {
throw new Error(err);
} else {
fs.writeFile(includePath, output, function(err) {
if(err) {
return console.log(err);
}
console.log("Critical written to include!");
});
}
});
}
});
});
});
@Golodhros
Copy link
Author

It would be awesome if we could figure out the cache token of the css file.
Maybe adding it to the CSS processing pipeline, but my Gulp is not enough...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment