1 | less = require 'less'
|
2 | sysPath = require 'path'
|
3 |
|
4 | module.exports = class LESSCompiler
|
5 | brunchPlugin: yes
|
6 | type: 'stylesheet'
|
7 | extension: 'less'
|
8 | _dependencyRegExp: /^ *@import ['"](.*)['"]/
|
9 |
|
10 | constructor: (@config) ->
|
11 | null
|
12 |
|
13 | compile: (data, path, callback) ->
|
14 | parser = new less.Parser
|
15 | paths: [@config.paths.root, (sysPath.dirname path)],
|
16 | filename: path
|
17 | parser.parse data, (error, tree) =>
|
18 | return callback error.message if error?
|
19 | result = null
|
20 | err = null
|
21 |
|
22 | try
|
23 | result = tree.toCSS()
|
24 | catch ex
|
25 | err = "#{ex.type}Error:#{ex.message}"
|
26 | if ex.filename
|
27 | err += " in '#{ex.filename}:#{ex.line}:#{ex.column}'"
|
28 | callback err, result
|
29 |
|
30 | getDependencies: (data, path, callback) =>
|
31 | parent = sysPath.dirname path
|
32 | dependencies = data
|
33 | .split('\n')
|
34 | .map (line) =>
|
35 | line.match(@_dependencyRegExp)
|
36 | .filter (match) =>
|
37 | match?.length > 0
|
38 | .map (match) =>
|
39 | match[1]
|
40 | .filter (path) =>
|
41 | !!path and path isnt 'nib'
|
42 | .map (path) =>
|
43 | if sysPath.extname(path) isnt ".#{@extension}"
|
44 | path + ".#{@extension}"
|
45 | else
|
46 | path
|
47 | .map (path) =>
|
48 | if path.charAt(0) is '/'
|
49 | sysPath.join @config.paths.root, path[1..]
|
50 | else
|
51 | sysPath.join parent, path
|
52 | process.nextTick =>
|
53 | callback null, dependencies
|