Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bezoerb committed Aug 3, 2014
0 parents commit bb96310
Show file tree
Hide file tree
Showing 14 changed files with 1,109 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[package.json]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.swp
.DS_Store
node_modules/
npm-debug.log
3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"laxcomma": true
}
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "0.10"
51 changes: 51 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Generated on 2014-08-03 using generator-nodejs 2.0.1
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
complexity: {
generic: {
src: ['app/**/*.js'],
options: {
errorsOnly: false,
cyclometric: 6, // default is 3
halstead: 16, // default is 8
maintainability: 100 // default is 100
}
}
},
jshint: {
all: [
'Gruntfile.js',
'app/**/*.js',
'test/**/*.js'
],
options: {
jshintrc: '.jshintrc'
}
},
mochacli: {
all: ['test/**/*.js'],
options: {
reporter: 'spec',
ui: 'bdd'
}
},
watch: {
js: {
files: ['**/*.js', '!node_modules/**/*.js'],
tasks: ['default'],
options: {
nospawn: true
}
}
}
});

grunt.loadNpmTasks('grunt-complexity');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-mocha-cli');
grunt.registerTask('test', ['complexity', 'jshint', 'mochacli', 'watch']);
grunt.registerTask('ci', ['complexity', 'jshint', 'mochacli']);
grunt.registerTask('default', ['test']);
};
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) 2014, Ben Zörb
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Ben Zörb nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY BEN ZÖRB ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BEN ZÖRB BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# inline-critical

Inline critical-path css and load the existing stylesheets asynchronously

[![build status](https://secure.travis-ci.org/bezoerb/inline-critical.png)](http://travis-ci.org/bezoerb/inline-critical)

## Installation

This module is installed via npm:

``` bash
$ npm install inline-critical
```

## Example Usage

``` js
var inlineCritical = require('inline-critical');
```
53 changes: 53 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Module to inline styles while loading the existing stylesheets async
*
* @author Ben Zörb @bezoerb https://github.com/bezoerb
* @copyright Copyright (c) 2014 Ben Zörb
*
* Licensed under the MIT license.
* http://bezoerb.mit-license.org/
* All rights reserved.
*/
'use strict';
var cheerio = require('cheerio');
var CleanCSS = require('clean-css');

module.exports = function(html, styles, minify) {

var $ = cheerio.load(String(html));
var links = $('link[rel="stylesheet"]');
var noscript = $('<noscript>\n</noscript>');

// minify if minify option is set
if (minify) {
styles = new CleanCSS().minify(styles);
}

// insert inline styles right before first <link rel="stylesheet" />
links.eq(0).before('<style type="text/css">\n' + styles + '\n</style>\n');
// insert noscript block right after stylesheets
links.eq(0).first().after(noscript);

// wrap links to stylesheets in noscript block so that they will evaluated when js is turned off
var hrefs = links.map(function(idx, el) {
el = $(el);
noscript.append(el);
noscript.append('\n');
return el.attr('href');
}).toArray();

// build js block to load blocking stylesheets
$('body').append('<script>\n' +
'(function(d,u){' +
'for (var i in u) {' +
'var l=d.createElement(\'link\');' +
'l.type=\'text/css\';' +
'l.rel=\'stylesheet\';' +
'l.href=u[i];' +
' d.getElementsByTagName(\'head\')[0].appendChild(l);' +
'}' +
'}(document,[\'' + hrefs.join('\',\'') + '\']));\n' +
'</script>\n');

return new Buffer($.html());
};
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "inline-critical",
"version": "0.0.1",
"description": "Inline critical-path css and load the existing stylesheets asynchronously",
"main": "index.js",
"scripts": {
"test": "node_modules/.bin/grunt ci"
},
"repository": {
"type": "git",
"url": "https://github.com/bezoerb/inline-critical"
},
"keywords": [
"css critical-path"
],
"author": "Ben Zörb <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/bezoerb/inline-critical/issues"
},
"dependencies": {
"cheerio": "^0.17.0",
"clean-css": "^2.2.12"
},
"devDependencies": {
"chai": "~1.8.1",
"grunt-contrib-jshint": "~0.6.4",
"grunt-contrib-watch": "~0.5.3",
"grunt": "~0.4.1",
"grunt-mocha-cli": "~1.1.0",
"grunt-complexity": "~0.1.3",
"grunt-cli": "~0.1.9"
}
}
Loading

0 comments on commit bb96310

Please sign in to comment.