-
Notifications
You must be signed in to change notification settings - Fork 9
/
es6.js
94 lines (81 loc) · 2.98 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Author: Lee Yao <yaoli111144@gmail.com>
* Version: 0.5.0
* License: MIT
*/
define(['babel', 'module'], function(babel, _module) {
'use strict';
var fetchText, buildMap = {};
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'));
};
}
return {
version: '0.5.0',
load: function(name, req, onload, config) {
var babelOptions;
try {
// Deep clone babel config (#7)
babelOptions = JSON.parse(JSON.stringify(config.babel));
// clone callbacks from babel config manually
var callbackNames = ['getModuleId', 'resolveModuleSource', 'shouldPrintComment', 'wrapPluginVisitorMethod'];
callbackNames.forEach(function(key) {
babelOptions[key] = config.babel[key];
})
} catch (err) {
babelOptions = {}
onload.error(err);
}
var pluginOptions = config.es6 || {},
fileExtension = pluginOptions.fileExtension || '.js',
sourceFileName = name + fileExtension,
url = req.toUrl(sourceFileName);
// Do not load if it is an empty: url
if (url.indexOf('empty:') === 0) {
onload();
return;
}
var defaults = {
sourceMaps: config.isBuild ? false : 'inline',
sourceFileName: sourceFileName
};
for (var key in defaults) {
babelOptions[key] = defaults[key];
}
fetchText(url, function(text) {
try {
var code = babel.transform(text, babelOptions).code;
} catch (err) {
onload.error(err);
}
if (config.isBuild) {
buildMap[name] = code;
}
onload.fromText(code);
});
},
write: function(pluginName, moduleName, write) {
if (moduleName in buildMap) {
write.asModule(pluginName + '!' + moduleName, buildMap[moduleName]);
}
}
}
});