Added ignore option

This commit is contained in:
Ben Zörb 2015-06-09 07:12:09 +02:00
parent e3d6717c8e
commit e9616df148
3 changed files with 450 additions and 0 deletions

View File

@ -57,6 +57,19 @@ module.exports = function(html, styles, options) {
var noscript = $('<noscript>\n</noscript>');
var o = options || {};
if (_.isString(o.ignore)) {
o.ignore = [o.ignore];
}
if (o.ignore) {
links = links.filter(function() {
var href = $(this).attr('href');
return _.findIndex(o.ignore, function(arg) {
return _.isRegExp(arg) && arg.test(href) || arg === href;
}) === -1;
});
}
// minify if minify option is set
if (o.minify) {
styles = new CleanCSS().minify(styles).styles;
@ -71,6 +84,8 @@ module.exports = function(html, styles, options) {
return $(this).attr('href');
}).toArray();
// extract styles from stylesheets if extract option is set
if (o.extract) {
if (!o.basePath) {

View File

@ -0,0 +1,407 @@
<!doctype html>
<html class="no-js">
<head>
<meta charset="utf-8">
<title>critical css test</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<!-- build:css styles/main.css -->
<style type="text/css">
body {
padding-top: 20px;
padding-bottom: 20px;
}
.header {
padding-left: 15px;
padding-right: 15px;
}
.header {
border-bottom: 1px solid #e5e5e5;
}
.header h3 {
margin-top: 0;
margin-bottom: 0;
line-height: 40px;
padding-bottom: 19px;
}
.jumbotron {
text-align: center;
border-bottom: 1px solid #e5e5e5;
}
.jumbotron .btn {
font-size: 21px;
padding: 14px 24px;
}
@media screen and (min-width: 768px) {
.container {
max-width: 730px;
}
.header {
padding-left: 0;
padding-right: 0;
}
.header {
margin-bottom: 30px;
}
.jumbotron {
border-bottom: 0;
}
}
html {
font-family: sans-serif;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
body {
margin: 0;
}
a {
background: transparent;
}
h1 {
margin: .67em 0;
font-size: 2em;
}
@media print {
* {
color: #000 !important;
text-shadow: none !important;
background: transparent !important;
box-shadow: none !important;
}
a {
text-decoration: underline;
}
a[href]:after {
content: " (" attr(href) ")";
}
a[href^="#"]:after {
content: "";
}
p,
h3 {
orphans: 3;
widows: 3;
}
h3 {
page-break-after: avoid;
}
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
*:before,
*:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html {
font-size: 62.5%;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.42857143;
color: #333;
background-color: #fff;
}
a {
color: #428bca;
text-decoration: none;
}
h1,
h3 {
font-family: inherit;
font-weight: 500;
line-height: 1.1;
color: inherit;
}
h1,
h3 {
margin-top: 20px;
margin-bottom: 10px;
}
h1 {
font-size: 36px;
}
h3 {
font-size: 24px;
}
p {
margin: 0 0 10px;
}
.lead {
margin-bottom: 20px;
font-size: 16px;
font-weight: 200;
line-height: 1.4;
}
@media (min-width: 768px) {
.lead {
font-size: 21px;
}
}
.text-muted {
color: #999;
}
ul {
margin-top: 0;
margin-bottom: 10px;
}
.container {
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
}
@media (min-width: 768px) {
.container {
width: 750px;
}
}
@media (min-width: 992px) {
.container {
width: 970px;
}
}
@media (min-width: 1200px) {
.container {
width: 1170px;
}
}
.btn {
display: inline-block;
padding: 6px 12px;
margin-bottom: 0;
font-size: 14px;
font-weight: normal;
line-height: 1.42857143;
text-align: center;
white-space: nowrap;
vertical-align: middle;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-image: none;
border: 1px solid transparent;
border-radius: 4px;
}
.btn-success {
color: #fff;
background-color: #5cb85c;
border-color: #4cae4c;
}
.btn-lg {
padding: 10px 16px;
font-size: 18px;
line-height: 1.33;
border-radius: 6px;
}
.nav {
padding-left: 0;
margin-bottom: 0;
list-style: none;
}
.nav > li {
position: relative;
display: block;
}
.nav > li > a {
position: relative;
display: block;
padding: 10px 15px;
}
.nav-pills > li {
float: left;
}
.nav-pills > li > a {
border-radius: 4px;
}
.nav-pills > li + li {
margin-left: 2px;
}
.nav-pills > li.active > a {
color: #fff;
background-color: #428bca;
}
.jumbotron {
padding: 30px;
margin-bottom: 30px;
color: inherit;
background-color: #eee;
}
.jumbotron h1 {
color: inherit;
}
.jumbotron p {
margin-bottom: 15px;
font-size: 21px;
font-weight: 200;
}
.container .jumbotron {
border-radius: 6px;
}
@media screen and (min-width: 768px) {
.jumbotron {
padding-top: 48px;
padding-bottom: 48px;
}
.container .jumbotron {
padding-right: 60px;
padding-left: 60px;
}
.jumbotron h1 {
font-size: 63px;
}
}
.container:before,
.container:after,
.nav:before,
.nav:after {
display: table;
content: " ";
}
.container:after,
.nav:after {
clear: both;
}
.pull-right {
float: right !important;
}
</style>
<script>
(function (u) {
function loadCSS(e, t, n) {
"use strict";
function r() {
for (var t, i = 0; i < d.length; i++)d[i].href && d[i].href.indexOf(e) > -1 && (t = !0);
t ? o.media = n || "all" : setTimeout(r)
}
var o = window.document.createElement("link"), i = t || window.document.getElementsByTagName("script")[0], d = window.document.styleSheets;
return o.rel = "stylesheet", o.href = e, o.media = "only x", i.parentNode.insertBefore(o, i), r(), o
}
for (var i in u) {
loadCSS(u[i]);
}
}(['css/main.css', 'http://fonts.googleapis.com/css?family=Open+Sans']));
</script>
<noscript>
<link rel="stylesheet" href="css/main.css">
<link href="http://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet" type="text/css">
</noscript>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
<!-- endbuild -->
</head>
<body>
<!--[if lt IE 10]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade
your browser</a> to improve your experience.</p>
<![endif]-->
<div class="container">
<div class="header">
<ul class="nav nav-pills pull-right">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
<h3 class="text-muted">critical css test</h3>
</div>
<div class="jumbotron">
<h1>'Allo, 'Allo!</h1>
<p class="lead">Always a pleasure scaffolding your apps.</p>
<p><a class="btn btn-lg btn-success" href="#">Splendid!</a></p>
</div>
<div class="row marketing">
<div class="col-lg-6">
<h4>HTML5 Boilerplate</h4>
<p>HTML5 Boilerplate is a professional front-end template for building fast, robust, and adaptable web apps or
sites.</p>
<h4>Bootstrap</h4>
<p>Sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development.</p>
</div>
</div>
<div class="footer">
<p>♥ from the Yeoman team</p>
</div>
</div>
</body>
</html>

View File

@ -103,4 +103,32 @@ describe('inline-critical', function() {
expect(strip(out.toString('utf-8'))).to.be.equal(strip(html));
done();
});
it('should respect ignore option with string', function(done) {
function strip2(string) {
return string.replace(/\s+/gm,'');
}
var html = read('test/fixtures/external.html');
var expected = read('test/expected/external-ignore-expected.html');
var css = read('test/fixtures/critical.css');
var out = inlineCritical(html, css, {ignore: ['bower_components/bootstrap/dist/css/bootstrap.css']});
expect(strip2(out.toString('utf-8'))).to.be.equal(strip2(expected));
done();
});
it('should respect ignore option with RegExp', function(done) {
function strip2(string) {
return string.replace(/\s+/gm,'');
}
var html = read('test/fixtures/external.html');
var expected = read('test/expected/external-ignore-expected.html');
var css = read('test/fixtures/critical.css');
var out = inlineCritical(html, css, {ignore: [/bootstrap/]});
expect(strip2(out.toString('utf-8'))).to.be.equal(strip2(expected));
done();
});
});