forked from jnordberg/wintersmith-less
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.coffee
42 lines (35 loc) · 1.16 KB
/
plugin.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
less = require 'less'
path = require 'path'
async = require 'async'
fs = require 'fs'
module.exports = (env, callback) ->
class LessPlugin extends env.ContentPlugin
constructor: (@filepath, @source) ->
getFilename: ->
@filepath.relative.replace /less$/, 'css'
getView: ->
return (env, locals, contents, templates, callback) ->
options = env.config.less or {}
options.filename = @filepath.relative
options.paths = [path.dirname(@filepath.full)]
# less throws errors all over the place...
async.waterfall [
(callback) =>
try
less.render(@source, options, callback)
catch error
callback error
(output, callback) =>
try
callback null, new Buffer(output.css)
catch error
callback error
], callback
LessPlugin.fromFile = (filepath, callback) ->
fs.readFile filepath.full, (error, buffer) ->
if error
callback error
else
callback null, new LessPlugin filepath, buffer.toString()
env.registerContentPlugin 'styles', '**/[^\_]*.less', LessPlugin
callback()