forked from mikach/requirejs-babel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
es6.js
76 lines (70 loc) · 2.4 KB
/
es6.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
69
70
71
72
73
74
75
76
var fetchText, _buildMap = {};
//>>excludeStart('excludeBabel', pragmas.excludeBabel)
if (typeof window !== "undefined" && window.navigator && window.document) {
fetchText = function (url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function (evt) {
//Do not explicitly handle errors, those should be
//visible via console output in the browser.
if (xhr.readyState === 4) {
callback(xhr.responseText);
}
};
xhr.send(null);
};
} else if (typeof process !== "undefined" &&
process.versions &&
!!process.versions.node) {
//Using special require.nodeRequire, something added by r.js.
fs = require.nodeRequire('fs');
fetchText = function (path, callback) {
callback(fs.readFileSync(path, 'utf8'));
};
}
//>>excludeEnd('excludeBabel')
define([
//>>excludeStart('excludeBabel', pragmas.excludeBabel)
'babel',
//>>excludeEnd('excludeBabel')
'module'
], function(
//>>excludeStart('excludeBabel', pragmas.excludeBabel)
babel,
//>>excludeEnd('excludeBabel')
_module
) {
return {
load: function (name, req, onload, config) {
//>>excludeStart('excludeBabel', pragmas.excludeBabel)
function applyOptions(options) {
var defaults = {
modules: 'amd',
sourceMap: config.isBuild ? false :'inline',
sourceFileName: name,
stage: 0
};
for(var key in options) {
if(options.hasOwnProperty(key)) {
defaults[key] = options[key];
}
}
return defaults;
}
var url = req.toUrl(name + '.js');
fetchText(url, function (text) {
var code = babel.transform(text, applyOptions(_module.config())).code;
if (config.isBuild) {
_buildMap[name] = code;
}
onload.fromText(code);
});
//>>excludeEnd('excludeBabel')
},
write: function (pluginName, moduleName, write) {
if (moduleName in _buildMap) {
write.asModule(pluginName + '!' + moduleName, _buildMap[moduleName]);
}
}
}
});