Skip to content

Commit

Permalink
Merge pull request #6 from mdarnall/2.0-wip
Browse files Browse the repository at this point in the history
2.0 feature work
  • Loading branch information
mdarnall committed Sep 16, 2015
2 parents 00ff3bf + ec3dc0d commit 86b21ba
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 63 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ build
.grunt

node_modules
example/css
50 changes: 27 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,34 @@ $ npm install hapi-sass --save
```

```javascript
var server = new Hapi.Server(config.host, config.port, config.server)

server.pack.register({
plugin: require('hapi-sass'),
options: {
debug: true,
force: true,
src: './lib/sass',
outputStyle: 'compressed',
sourceComments: 'normal',
dest: './public/css',
routePath: '/css/{file}.css'
var Hapi = require('hapi');
var HapiSass = require('../index')

var server = new Hapi.Server();
server.connection({ port: 1337 });

var options = {
src: './example/sass',
dest: './example/css',
force: true,
debug: true,
routePath: '/css/{file}.css',
includePaths: ['./example/vendor/sass'],
outputStyle: 'nested',
sourceComments: true
};

server.register({
register: HapiSass,
options: options
}
}, function(err){
if(err){
console.log(err)
return
, function (err) {
if (err) throw err;
server.start(function () {
server.log("Hapi server started @ " + server.info.uri);
});
}

server.route(routes)
server.start()
console.log('Running on port: ' + config.port);
})
);
```

### Options:
Expand All @@ -49,7 +54,6 @@ server.pack.register({
* `dest`: the destination directory to write compiled `.css` files. Defaults to `./public/css`
* `routePath`: the route to register with hapijs. Defaults to `/css/{file}.css`. The `{file}` portion of the string is currently significant. It's used as a request parameter.
* `outputStyle`: [parameter for node-sass](https://github.com/sass/node-sass#outputstyle). Defaults to `compressed`
* `sourceComments`: [parameter for node-sass](https://github.com/sass/node-sass#sourcecomments). Defaults to `none`.
* `imagePath`: [parameter for node-sass](https://github.com/sass/node-sass#imagepath). Defaults to `undefined`.
* `sourceComments`: [parameter for node-sass](https://github.com/sass/node-sass#sourcecomments). Defaults to `false`.
* `includePaths`: [parameter for node-sass](https://github.com/sass/node-sass#includepaths). Defaults to `[]`.

17 changes: 17 additions & 0 deletions example/sass/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@import "base";

nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}

li { display: inline-block; }

a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}
38 changes: 38 additions & 0 deletions example/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* hapi-sass
* https://github.com/mdarnall/hapi-sass
*
* Copyright (c) 2014 Matt Darnall
* Licensed under the MIT license.
*/

'use strict';

var Hapi = require('hapi');
var HapiSass = require('../index')

var server = new Hapi.Server();
server.connection({ port: 1337 });

var options = {
src: './example/sass',
dest: './example/css',
force: true,
debug: true,
routePath: '/css/{file}.css',
includePaths: ['./example/vendor/sass'],
outputStyle: 'nested',
sourceComments: true
};

server.register({
register: HapiSass,
options: options
}
, function (err) {
if (err) throw err;
server.start(function () {
server.log("Hapi server started @ " + server.info.uri);
});
}
);
7 changes: 7 additions & 0 deletions example/vendor/sass/_base.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$font-stack: Helvetica, sans-serif;
$primary-color: #333;

body {
font: 100% $font-stack;
color: $primary-color;
}
46 changes: 23 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Error = require('hapi').error,
var Boom = require('boom'),
sass = require('node-sass'),
Hoek = require('hoek'),
fs = require('fs'),
Expand All @@ -16,15 +16,15 @@ var internals = {
dest: './public/css',
routePath: '/css/{file}.css',
outputStyle: 'compressed',
sourceComments: 'none'
sourceComments: false
},

error: function (reply, err) {
if (err.code == 'ENOENT') {
return reply(Error.notFound());
return reply(Boom.notFound());
}
else {
return reply(Error.internal(err));
return reply(Boom.internal(err));
}
},

Expand All @@ -33,7 +33,7 @@ var internals = {
}
};

exports.register = function (plugin, options, next) {
exports.register = function (server, options, next) {

var settings = Hoek.applyToDefaults(internals.defaults, options);
// Force compilation
Expand All @@ -45,13 +45,13 @@ exports.register = function (plugin, options, next) {
// Source dir required
var src = settings.src;
if (!src) {
next(new Error('hapi-sass requires "src" directory'));
next(new Boom('hapi-sass requires "src" directory'));
}

// Default dest dir to source
var dest = settings.dest ? settings.dest : src;

plugin.route({
server.route({
method: 'GET',
path: settings.routePath,
handler: function (request, reply) {
Expand All @@ -78,26 +78,26 @@ exports.register = function (plugin, options, next) {
includePaths: [sassDir].concat(settings.includePaths || []),
imagePath: settings.imagePath,
outputStyle: settings.outputStyle,
sourceComments: settings.sourceComments,
error: function (err) {
sourceComments: settings.sourceComments
}, function(err, result){

if(err){
return internals.error(reply, err);
},
success: function (css) {
if (debug) {
internals.log('render', 'compilation ok');
}
}

mkdirp(dirname(cssPath), 0x1c0, function (err) {
if (err) {
return reply(err);
}
fs.writeFile(cssPath, css, 'utf8', function (err) {
reply(css).type('text/css');
});
});
if (debug) {
internals.log('render', 'compilation ok');
}
});

mkdirp(dirname(cssPath), 0x1c0, function (err) {
if (err) {
return reply(err);
}
fs.writeFile(cssPath, result.css, 'utf8', function (err) {
reply(result.css).type('text/css');
});
});
});
};

if (force) {
Expand Down
19 changes: 13 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "hapi-sass",
"version": "1.0.0",
"version": "2.0.0",
"description": "A Hapi.js plugin for compiling and serving Sass stylesheets",
"main": "index.js",
"scripts": {
"test": "mocha"
"test": "mocha --ui bdd",
"start": "node example/server.js"
},
"author": {
"name": "Matt Darnall",
Expand All @@ -24,16 +25,22 @@
"sass",
"node-sass"
],
"engines": {
"node": ">=4.0.0"
},
"license": "MIT",
"peerDependencies": {
"hapi": ">=10.0.0"
},
"dependencies": {
"hapi": "^6.0.2",
"node-sass": "^0.9.3",
"boom": "^2.8.0",
"hoek": "^2.3.0",
"mkdirp": "^0.3.5",
"hoek": "^2.3.0"
"node-sass": "^3.3.2"
},
"devDependencies": {
"mocha": "^1.17.1",
"chai": "^1.9.0",
"chai": "^3.2.0",
"sinon": "^1.9.0"
}
}
24 changes: 13 additions & 11 deletions test/indexTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ var HapiSass = require('../index');
var expect = require('chai').expect;
var Hapi = require('hapi');

describe('Hapi-Sass', function(){
describe('Hapi-Sass', function () {

describe('register', function(){
describe('register', function () {

it('creates a route for the plugin', function(done){
it('creates a route for the plugin', function (done) {
var server = new Hapi.Server();
server.pack.register({
plugin: HapiSass,
options: {}
}, function (err) {
var routes = server.table();
expect(routes[0].path).to.equal('/css/{file}.css');
done();
}
server.connection();
server.register({ register: HapiSass, options: {}}, function (err) {
expect(err).to.not.exist;
var tables = server.table();
expect(tables.length).to.be.greaterThan(0);
var routes = tables[0].table;
expect(routes.length).to.be.greaterThan(0);
expect(routes[0].path).to.equal('/css/{file}.css');
done();
}
);
});
});
Expand Down

0 comments on commit 86b21ba

Please sign in to comment.