forked from nicobot/node-sass-globbing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
54 lines (47 loc) · 1.63 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
glob = require('glob')
path = require('path')
module.exports = function(url, prev, done) {
if(!glob.hasMagic(url)) {
// normal url, resolve to an import of the file
if(url.indexOf('|prev=') === 0) {
// if there is a |prev keyword, resolve file relative to that file.. so if
// you do @import |prev=~|foo.sass, it will resolve to ~/foo.sass
var prev_dir = null
var prev_re = /^\|prev=([^\|]*?)\|/
var matches = url.match(prev_re)
if(matches.length) {
prev_dir = path.dirname(matches[1])
url = url.replace(prev_re, '')
url = path.resolve(prev_dir, url)
done({file: url})
return void 0;
}
} else {
done({file: url})
return void 0;
}
}
// search for scss | sass files if no extension provided
// url is a glob, like foo/*.sass, resolve to importing all matching files, so
// if there is foo/1.sass and foo/2.sass, it will import both of those files
var cwd = path.dirname(prev)
glob(url, {cwd: cwd}, function (err, files) {
var ret = {
contents: "\n"
}
// import all the matched files, passing through the current file. this is
// because if you didn't, the path will resolve relative to foo/*.sass,
// which will fail since that is not an actual path.
files.forEach(function(file) {
var ext = path.extname(file)
var abspath = path.resolve(cwd, file)
if(/^\.s[ac]ss$/.test(ext) && prev !== abspath) {
// don't import self
var prev_kwd = '|prev=' + prev + '|'
ret.contents += '@import "' + prev_kwd + file + "\";\n"
}
})
done(ret)
return void 0;
})
};