-
Notifications
You must be signed in to change notification settings - Fork 24
/
plugin.coffee
56 lines (44 loc) · 1.42 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
less = require 'less'
path = require 'path'
async = require 'async'
fs = require 'fs'
module.exports = (env, callback) ->
options = env.config.less ? {}
if env.mode is 'preview' and !options.sourceMap?
options.sourceMap =
sourceMapFileInline: true
outputSourceFiles: true
exclude = options.exclude ? []
delete options['excluded']
class LessPlugin extends env.ContentPlugin
constructor: (@filepath, @source) ->
getFilename: ->
@filepath.relative.replace /less$/, 'css'
isExcluded: ->
path.basename(@filepath.relative)[0] is '_' or @filepath.relative in exclude
getView: ->
if @isExcluded()
return 'none'
return (env, locals, contents, templates, callback) ->
opts = {}
env.utils.extend opts, options
opts.paths = [path.dirname(@filepath.full)]
opts.filename = @filepath.relative
less.render @source, opts, (error, result) ->
if error?
callback error
return
callback null, new Buffer result.css
getPluginInfo: ->
if @isExcluded()
return 'excluded'
else
super()
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()