-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabelise.js
66 lines (52 loc) · 1.75 KB
/
babelise.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
(function() {
var elements = document.getElementsByTagName('script'),
element = elements[elements.length - 1],
url = element.src;
index = url.lastIndexOf('/'),
base = '/node_modules',
paramStart = url.indexOf('?'),
systemConfig = '',
stage = 0;
if (paramStart !== -1) {
var params = {};
url.substring(paramStart + 1).split('&').forEach(function(value) {
var components = value.split('=');
if (components.length > 1) {
params[decodeURIComponent(components[0].trim())]
= decodeURIComponent((components[1] || '').trim());
}else{
params[decodeURIComponent(components[0].trim())] = true;
}
});
if (params.base) {
base = params.base;
}
// Support for node >=4 flat node_modules file system.
if (params.noflat) {
base = url.substring(0, index) + '/node_modules';
}
stage = params.stage || 0;
}
systemConfig += 'System.babelOptions = { stage: ' + stage + ' };';
function inject(url, content) {
document.write('<script data-babelise="true"'
+ (url ? (' src="' + url + '"') : '') + '>'
+ (content || '') + '</script>');
}
inject(base + '/babel-core/browser-polyfill.js');
inject(base + '/es6-module-loader/dist/es6-module-loader.js');
inject(base + '/systemjs/dist/system.js');
inject(null, ''
+ 'System.transpiler = "babel"; '
+ systemConfig
+ 'System.config({'
+ 'paths: {'
+ 'babel: "' + base + '/babel-core/browser.js"'
+ '}'
+ '});'
// Leave no trace, like a ninja.
+ 'Array.prototype.filter.call(document.getElementsByTagName("script"), function(tag) {'
+ 'return (tag.getAttribute && tag.getAttribute("data-babelise"));'
+ '}).map(function(tag) { tag.parentNode.removeChild(tag) });'
);
})();