-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
63 lines (57 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
55
56
57
58
59
60
61
62
63
var sweet = require('sweet.js');
var path = require('path');
var RSVP = require('rsvp');
var loaderUtils = require('loader-utils');
var moduleCache = {};
function resolve(loader, filepath) {
var promise = new RSVP.Promise(function(resolve, reject) {
loader.resolve(loader.context, filepath, function(err, res) {
if(err) {
reject(err);
}
else {
resolve(res);
}
});
});
return promise;
}
module.exports = function(source) {
var loader = this;
loader.async();
var config = loaderUtils.parseQuery(loader.query);
var loaderRequest = loaderUtils.getCurrentRequest(this);
var fileRequest = loaderUtils.getRemainingRequest(this);
config.modules = config.modules || [];
RSVP.all(config.modules.map(function(mod) {
if(moduleCache[mod]) {
return moduleCache[mod];
}
return resolve(loader, mod).then(function(res) {
moduleCache[mod] = sweet.loadNodeModule(process.cwd(), res);
return moduleCache[mod];
});
})).then(function(modules) {
if(config.readers) {
return RSVP.all(config.readers.map(function(mod) {
return resolve(loader, mod).then(function(res) {
sweet.setReadtable(res);
})
})).then(function() {
return modules;
});
}
return modules;
}).then(function(modules) {
var result = sweet.compile(source, {
modules: modules,
sourceMap: true,
filename: fileRequest,
readableNames: !!config.readableNames
});
loader.cacheable && loader.cacheable();
loader.callback(null, result.code, result.sourceMap);
}).catch(function(err) {
loader.callback(err);
});
};