-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
49 lines (36 loc) · 1.22 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
var loaderUtils = require('loader-utils');
var path = require('path');
var extend = require('extend');
var es6mod = require('es6-module-transpiler').Compiler;
var es6tr = require('es6-transpiler');
function es6ModuleTranspiler(source, options, filename) {
var ext = path.extname(filename);
if (ext.slice(1) === 'coffee') {
options.coffee = true;
}
var moduleName = path.join(path.dirname(filename), path.basename(filename, ext)).replace(/[\\]/g, '/');
var compiler = new es6mod(source, moduleName, options);
return compiler.toCJS();
}
function es6Transpiler(source, options) {
options.src = source;
var defaults = {
environments: ['node', 'browser'],
disallowVars: false,
disallowDuplicated: true,
disallowUnknownReferences: false,
includePolyfills: false
};
var result = es6tr.run(extend({}, defaults, options));
if (result.errors.length) {
return source;
}
return result.src;
}
module.exports = function(source) {
this.cacheable && this.cacheable();
var options = loaderUtils.parseQuery(this.query);
var result = es6ModuleTranspiler(source, options, options.filename || this.resourcePath);
result = es6Transpiler(result, options);
this.callback(null, result);
};