forked from petehunt/sweetjs-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (36 loc) · 1.14 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
var async = require('async');
var loaderUtils = require('loader-utils');
var sweet = require('sweet.js');
module.exports = function(source) {
this.async();
var config = loaderUtils.parseQuery(this.query);
var currentFile = loaderUtils.getRemainingRequest(this)
async.map(
config.modules || [],
function(module, callback) {
this.resolve(this.context, module, function(err, result) {
if (err) {
callback(err);
return;
}
try {
callback(null, sweet.loadNodeModule(process.cwd(), result));
} catch (e) {
var err = "Sweet.js compile error: " + e.description + " in " + currentFile + "(" + e.lineNumber + ":" + e.column + ")"
console.log(err)
this.callback(null, "throw new Error('" + err + "')", "")
}
}.bind(this));
}.bind(this),
function(err, results) {
if (err) {
this.callback(err);
return;
}
config.modules = results;
var result = sweet.compile(source, config);
this.cacheable && this.cacheable();
this.callback(null, result.code, result.sourceMap);
}.bind(this)
);
};