-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
64 lines (51 loc) · 1.51 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
55
56
57
58
59
60
61
62
63
64
'use strict';
var fs = require('fs');
var path = require('path');
function parse(loader, source, context, cb) {
var imports = [];
var importPattern = /#include "([.\/\w_-]+)"/gi;
var match = importPattern.exec(source);
while (match != null) {
imports.push({
key: match[1],
target: match[0],
content: ''
});
match = importPattern.exec(source);
}
processImports(loader, source, context, imports, cb);
}
function processImports(loader, source, context, imports, cb) {
if (imports.length === 0) {
return cb(null, source);
}
var imp = imports.pop();
loader.resolve(context, imp.key, function(err, resolved) {
if (err) {
return cb(err);
}
loader.addDependency(resolved);
fs.readFile(resolved, 'utf-8', function(err, src) {
if (err) {
return cb(err);
}
parse(loader, src, path.dirname(resolved), function(err, bld) {
if (err) {
return cb(err);
}
source = source.replace(imp.target, bld);
processImports(loader, source, context, imports, cb);
});
});
});
}
module.exports = function(source) {
this.cacheable();
var cb = this.async();
parse(this, source, this.context, function(err, bld) {
if (err) {
return cb(err);
}
cb(null, 'module.exports = ' + JSON.stringify(bld));
});
};