Skip to content

Commit

Permalink
Initial commit. Fully tested, but basic first version
Browse files Browse the repository at this point in the history
  • Loading branch information
fatso83 committed Jun 18, 2014
1 parent f955df6 commit 6ad9a7a
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 3 deletions.
14 changes: 14 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"boss": true,
"eqnull": true,
"node": true,
"laxcomma": true
}
89 changes: 89 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* grunt-penthouse
* https://github.com/carl-erik.kopseng/grunt-penthouse
*
* Copyright (c) 2014 Carl-Erik Kopseng
* Licensed under the MIT license.
*/

'use strict';

module.exports = function(grunt) {

// Autoload tasks from dependencies
require('load-grunt-tasks')(grunt);

// Project configuration.
grunt.initConfig({

jshint: {
all: [
'Gruntfile.js',
'tasks/*.js',
//'<%= nodeunit.tests %>',
],
options: {
jshintrc: '.jshintrc',
},
},

// Before generating any new files, remove any previously-created files.
clean: {
tests: ['.tmp'],
},

// Configuration to be run (and then tested).
penthouse: {
testTask: {
outfile : '.tmp/out.css',
css : 'test/www/main.css',
url : 'http://localhost:9001',
width : 1300,
height : 900
},
},

// Unit tests.
mochaTest: {
tests: ['test/*test.js']
},

connect: {
server: {
options: {
port: 9001,
base: 'test/www'
}
}
},

watch: {
dev: {
files: ['Gruntfile.js', 'package.json','tasks/*.js'],
tasks : ['jshint', 'penthouse']
},

logic : {
files: ['tasks/*.js'],
tasks : ['test']
},

test : {
files: ['test/**'],
tasks : ['test']
},

}

});

// Actually load this plugin's task(s).
grunt.loadTasks('tasks');

// Whenever the "test" task is run, first clean the ".tmp" dir, then run this
// plugin's task(s), then test the result.
grunt.registerTask('test', ['clean', 'connect', 'penthouse', 'mochaTest']);

// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test']);
};
22 changes: 22 additions & 0 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2014 Carl-Erik Kopseng

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
45 changes: 42 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
grunt-penthouse
===============
# grunt-penthouse
> A grunt plugin based on [Penthouse](npmjs.org/package/penthouse) that extracts [critical path css](http://addyosmani.com/blog/tag/critical-path-css/)
A grunt plugin that uses Penthouse to extract critical path css
[![NPM version](https://badge.fury.io/js/grunt-penthouse.svg)](http://badge.fury.io/js/grunt-penthouse)
[![Build Status](https://travis-ci.org/fatso83/grunt-penthouse.svg?branch=master)](https://travis-ci.org/fatso83/grunt-penthouse)

## Getting Started

Install the plugin

```shell
npm install grunt-penthouse --save-dev
```

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

```js
grunt.loadNpmTasks('grunt-penthouse');
```

## The "penthouse" task

### Overview
In your project's Gruntfile, add a section named `penthouse` to the data object passed into `grunt.initConfig()`.

### Usage Example

```js
grunt.initConfig({
penthouse: {
extract : {
outfile : '.tmp/out.css',
css : './dist/css/full.css',
url : 'http://localhost:9000',
width : 1300,
height : 900
},
},
});
```

## Release History
None yet
53 changes: 53 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "grunt-penthouse",
"description": "Grunt plugin for extracting critical path css",
"version": "0.1.0",
"homepage": "https://github.com/fatso83/grunt-penthouse",
"author": {
"name": "Carl-Erik Kopseng",
"email": "[email protected]",
"url": "http://oligofren.wordpress.com"
},
"repository": {
"type": "git",
"url": "git://github.com/fatso83/grunt-penthouse.git"
},
"bugs": {
"url": "https://github.com/fatso83/grunt-penthouse/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/fatso83/grunt-penthouse/blob/master/LICENSE-MIT"
}
],
"engines": {
"node": ">= 0.8.0"
},
"scripts": {
"test": "grunt test"
},
"dependencies": {
"penthouse": "^0.2.0"
},
"devDependencies": {
"chai": "^1.9.1",
"grunt": "~0.4.2",
"grunt-contrib-clean": "~0.4.0",
"grunt-contrib-connect": "^0.8.0",
"grunt-contrib-jshint": "~0.6.0",
"grunt-contrib-watch": "^0.6.1",
"grunt-mocha-test": "^0.11.0",
"load-grunt-tasks": "^0.4.0"
},
"peerDependencies": {
"grunt": "~0.4.2"
},
"keywords": [
"gruntplugin",
"penthouse",
"critical path css",
"tools",
"css"
]
}
42 changes: 42 additions & 0 deletions tasks/penthouse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* grunt-penthouse
* https://github.com/fatso83/grunt-penthouse
*
* Copyright (c) 2014 Carl-Erik Kopseng
* Licensed under the MIT license.
*/

'use strict';

var penthouse = require('penthouse'),
path = require('path'),
fs = require('fs-extra');

module.exports = function(grunt) {
var pretty = function(o) {
return JSON.stringify(o, null, " ");
};

grunt.registerMultiTask('penthouse', 'Extracts critical path CSS', function() {

// we don't check for errors in config, but leave the error reporting to penthouse
var done = this.async(),
options = this.data,
outfile = path.normalize(this.data.outfile),
dir=path.dirname(outfile);

penthouse( options, function(err, result) {
if(err) {
if(err.msg) grunt.log.errorlns(err.msg);
done(false);
return;
}

grunt.log.debug(result);

grunt.file.write(outfile, result, {});
done();
});
});
};

41 changes: 41 additions & 0 deletions test/basic-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Expected to be run after 'grunt penthouse' */
'use strict';

var read = require('fs').readFileSync
, chai = require('chai')
, should = chai.should();

chai.config.includeStack = true;

describe('Basic functionality tests', function () {

var taskConfig;

function stub() { }

before(function() {
// get the config
require(__dirname + '/../Gruntfile.js')({
loadTasks : stub,
registerTask : stub,
loadNpmTasks : stub,
initConfig : function(config) {
taskConfig = config.penthouse.testTask;
}
});
});

it('should produce a non-empty file', function() {
read(taskConfig.outfile).should.not.be.empty;
});

it('should produce a file with the same content as the penthouse module', function(done) {
var penthouse = require('penthouse');

penthouse(taskConfig, function(result) {
read(taskConfig.outfile).should.equal.result;
done();
});
});

});
4 changes: 4 additions & 0 deletions test/www/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Server of static files
var express = require('express');
var app = module.exports = express();
app.use(express.static(__dirname ));
16 changes: 16 additions & 0 deletions test/www/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="main.css"/>
</head>
<body>
<div id="box1">Box 1
<div id="box11">Box 1.1</div>
</div>
<div id="box2">Box 2
<div id="box21">Box 2.1</div>
<div id="box22">Box 2.2</div>
</div>
</body>
</html>
23 changes: 23 additions & 0 deletions test/www/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#box1 {
width : 800px;
height: 400px;
background : red;
}
#box2 {
width : 800px;
height: 400px;
background : blue;
}

#box21 {
width : 100%;
height: 100px;
background: yellow;
}

#box22 {
width : 100%;
height: 50px;
padding-top: 200px;
background: green;
}

0 comments on commit 6ad9a7a

Please sign in to comment.