-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.js
68 lines (60 loc) · 1.89 KB
/
import.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
//
// Native modules.
//
var fs = require('fs')
, path = require('path');
/**
* Expose the importer and allow configuration of the import/require statement.
* The regular expression defaults to /@import\s["']?([^'"]+)["']?/gm
*
* @param {RegExp} regexp regular expression to check against imports.
* @return {Function} import functionality
* @api public
*/
module.exports = function importer(regexp) {
regexp = regexp || /@import\s["']?([^'"]+)["']?/gm;
/**
* Parse potential import statements from supplied content. It should
* also parse the import statements recursively
*
* @param {String} location location of the file that needs to be parsed
* @param {Array} paths collection to append to
* @param {Array} current reference to currently found files
* @returns {Array} absolute paths
* @api public
*/
return function imports(location, paths, current) {
paths = paths || [];
current = current || [];
if (!fs.existsSync(location)) return paths;
//
// Get the file, unparsed so we can minimize the overhead of parsing it
//
var content = fs.readFileSync(location, 'utf8')
, directory = path.dirname(location)
, ext = path.extname(location);
//
// Parse out require statements for the files.
//
content.replace(regexp, function detect(x, match) {
//
// If no file extension, assume the extension equal to the includer extension.
//
if (!path.extname(path.basename(match))) match += ext;
match = path.join(directory, match);
if (!~paths.indexOf(match)) {
paths.push(match);
current.push(match);
}
});
//
// Iterate over all the paths to see if required files also contains files
// that we need to watch.
//
current.forEach(function recursive(location) {
paths = imports(location, paths, []);
});
return paths;
};
};