This repository was archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreact-require.js
More file actions
73 lines (64 loc) · 1.91 KB
/
react-require.js
File metadata and controls
73 lines (64 loc) · 1.91 KB
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
/**
* React with RequireJS
* @author Rodrigo Cesar de Freitas Dias
* @license MIT
* @see https://github.com/rodrigocfd/react-require
*/
'use strict';
(function() {
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.
var fs = require.nodeRequire('fs');
fetchText = function(path, callback) {
callback(fs.readFileSync(path, 'utf8'));
};
}
define(['babel'], function(Babel) {
return {
load: function(name, req, onload, config) {
var url = req.toUrl(name + '.js');
// Default values for Babel presets and plugins.
var babelPresets = (window.babelConfig && window.babelConfig.presets) ||
['es2015', 'react'];
var babelPlugins = (window.babelConfig && window.babelConfig.plugins) ||
['transform-class-properties', 'transform-decorators-legacy'];
fetchText(url, function(text) {
var code = Babel.transform(text, {
presets: babelPresets,
plugins: babelPlugins,
filename: 'embedded',
sourceMaps: 'inline'
}).code;
if (config.isBuild) {
_buildMap[name] = code;
}
onload.fromText(code);
});
},
write: function(pluginName, moduleName, write) {
if (moduleName in _buildMap) {
write.asModule(pluginName + '!' + moduleName,
_buildMap[moduleName]);
}
}
}
});
})();