Skip to content

Commit

Permalink
Merge pull request #10 from mdarnall/2.1-wip
Browse files Browse the repository at this point in the history
version 2.1.0
  • Loading branch information
mdarnall committed Mar 13, 2016
2 parents 0eed21a + 5d28e33 commit d87f0cc
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 39 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ $ npm install hapi-sass --save
```javascript
var Hapi = require('hapi');
var HapiSass = require('../index')
var Inert = require('inert');

var server = new Hapi.Server();
server.connection({ port: 1337 });
Expand All @@ -30,13 +31,14 @@ var options = {
routePath: '/css/{file}.css',
includePaths: ['./example/vendor/sass'],
outputStyle: 'nested',
sourceComments: true
sourceComments: true,
srcExtension: 'scss'
};

server.register({
server.register([Inert, {
register: HapiSass,
options: options
}
}]
, function (err) {
if (err) throw err;
server.start(function () {
Expand All @@ -46,11 +48,14 @@ server.register({
);
```

See the `example/` folder for more.

### Options:

* `debug`: used to print statements to the console. Defaults to `false`
* `force`: forces re-compilation for every request. Defaults to `false`
* `src`: the directory to find the requested `.sass` file. Defaults to `./lib/sass`
* `srcExtension`: the extension of the requested `.sass` file. Defaults to `scss`
* `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`
Expand Down
24 changes: 24 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "hapi-sass-example",
"version": "0.1.0",
"description": "A Hapi.js plugin for compiling and serving Sass stylesheets",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"author": {
"name": "Matt Darnall",
"email": "[email protected]",
"url": "http://mdarnall.com"
},
"engines": {
"node": ">=4.0.0"
},
"license": "MIT",
"peerDependencies": {
},
"dependencies": {
"hapi": "10.0.0",
"inert": "3.2.0"
}
}
16 changes: 16 additions & 0 deletions example/sass/style2.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@import base

nav
ul
margin: 0
padding: 0
list-style: none


li
display: inline-block

a
display: block
padding: 6px 12px
text-decoration: none
18 changes: 10 additions & 8 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,32 @@
'use strict';

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

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

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

server.register({
server.register([Inert, {
register: HapiSass,
options: options
}
}]
, function (err) {
if (err) throw err;
server.start(function () {
server.log("Hapi server started @ " + server.info.uri);
});
}
);
);
63 changes: 38 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict'

var Boom = require('boom'),
sass = require('node-sass'),
Hoek = require('hoek'),
fs = require('fs'),
dirname = require('path').dirname,
mkdirp = require('mkdirp'),
join = require('path').join;
sass = require('node-sass'),
Hoek = require('hoek'),
fs = require('fs'),
dirname = require('path').dirname,
mkdirp = require('mkdirp'),
join = require('path').join;

var internals = {

Expand All @@ -16,7 +18,8 @@ var internals = {
dest: './public/css',
routePath: '/css/{file}.css',
outputStyle: 'compressed',
sourceComments: false
sourceComments: 'none',
srcExtension: 'scss'
},

error: function (reply, err) {
Expand All @@ -28,8 +31,10 @@ var internals = {
}
},

log: function log(key, val) {
console.error(' hapi-sass: \033[90m%s :\033[0m \033[36m%s\033[0m', key, val);
log: function () {
var args = Array.prototype.slice.call(arguments);
args[0] = '[hapi-sass] ' + args[0];
console.log.apply(console, args)
}
};

Expand All @@ -56,21 +61,22 @@ exports.register = function (server, options, next) {
path: settings.routePath,
handler: function (request, reply) {

// todo: sass file extension configurable? (.sass/.scss)
var cssPath = join(dest, request.params.file + '.css'),
sassPath = join(src, request.params.file + '.scss'),
sassDir = dirname(sassPath);
sassPath = join(src, request.params.file + '.' + settings.srcExtension),
sassDir = dirname(sassPath);

if (debug) {
internals.log('source ', sassPath);
internals.log('dest ', cssPath);
internals.log('sassDir ', sassDir);
internals.log("Processing Request with values: %j", {
"sassPath": sassPath,
"dest": dest,
"sassDir": sassDir
})
}

var compile = function () {

if (debug) {
internals.log('read', sassPath);
internals.log('Compiling Sass at %s', sassPath);
}

sass.render({
Expand All @@ -79,24 +85,29 @@ exports.register = function (server, options, next) {
imagePath: settings.imagePath,
outputStyle: settings.outputStyle,
sourceComments: settings.sourceComments
}, function(err, result){
}, function (err, result) {

if(err){
if (err) {
if (debug) {
internals.log('error', err.formatted);
let message = err.formatted ? err.formatted : err.message;
internals.log('Compilation failed: %s', message);
}
return internals.error(reply, err);
}

if (debug) {
internals.log('render', 'compilation ok');
internals.log('Compilation ok');
}

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

if(err && debug){
internals.log("Error writing file - %s", err.message);
}
reply(result.css).type('text/css');
});
});
Expand All @@ -119,7 +130,7 @@ exports.register = function (server, options, next) {
if (err.code == 'ENOENT') {
// css has not been compiled
if (debug) {
internals.log('not found, compiling', cssPath);
internals.log('Compiled file not found, compiling %s', cssPath);
}
compile();

Expand All @@ -129,27 +140,29 @@ exports.register = function (server, options, next) {
}
else { // compiled version exists, check mtimes

if (sassStats.mtime > cssStats.mtime) { // the sass version is newer
if (sassStats.mtime.getTime() > cssStats.mtime.getTime()) { // the sass version is newer
if (debug) {
internals.log('minified', cssPath);
internals.log('Sass file is newer, compiling %s', cssPath);
}
compile();
}
else {
// serve
if (debug) {
internals.log('Compiled file found and up to date. Serving');
}
reply.file(cssPath);
}

}
});
});

}
});

next();
};

exports.register.attributes = {
dependencies: 'inert',
pkg: require('./package.json')
};
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"name": "hapi-sass",
"version": "2.0.0",
"version": "2.1.0",
"description": "A Hapi.js plugin for compiling and serving Sass stylesheets",
"main": "index.js",
"scripts": {
"test": "mocha --ui bdd",
"start": "node example/server.js"
"test": "mocha --ui bdd"
},
"author": {
"name": "Matt Darnall",
Expand Down

0 comments on commit d87f0cc

Please sign in to comment.