-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathless.js
45 lines (34 loc) · 1.06 KB
/
less.js
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
var path = require('path')
, events = require('events')
, resrc = require('resrcify/custom').resrc
, regexp = /url\([\"\'](.*?)[\"\']\)/g
, res
, assetsConfig
var ctor = module.exports = function (opts, cb) {
assetsConfig = opts.assets
if (assetsConfig) {
// Add default error handler
assetsConfig.onError = assetsConfig.onError || onError
}
if(opts.bower) {
less = require('bower-less/less')
} else {
less = require('npm-less/less')
}
less(path.resolve(process.cwd(), opts.entry), {preprocess: preprocess, paths: opts.paths}, function (err, output) {
if (err) return process.nextTick(function () { cb(err) })
process.nextTick(function() { cb(null, output.toCSS(opts)) })
})
}
ctor.emitter = new events.EventEmitter()
function preprocess (file, src) {
ctor.emitter.emit('file', file)
if (!assetsConfig) return src
while ((res = regexp.exec(src)) !== null) {
src = src.replace(res[1], resrc(res[1], file, assetsConfig))
}
return src
}
function onError(err) {
console.error('asset not copied:', err.path)
}