-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathjsx.js
99 lines (77 loc) · 2.59 KB
/
jsx.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
95
96
97
98
99
define(function () {
'use strict';
var isNode = typeof process !== "undefined" &&
process.versions &&
!!process.versions.node;
var buildMap = {};
function getSourceMapConfig(config) {
var fallback = config.isBuild ? false : 'inline';
if (config && config.babel && 'sourceMaps' in config.babel) {
return config.babel.sourceMaps;
} else {
return fallback;
}
}
function babelSync(name, parentRequire, onLoadNative, config) {
var babel = require.nodeRequire('babel');
var path = parentRequire.toUrl(ensureJSXFileExtension(name, config));
try {
var result = babel.transformFileSync(path, {
sourceFileName: config.baseUrl + name,
sourceMaps: getSourceMapConfig(config),
filename: config.baseUrl + name
});
var compiled = result.code;
if (process.env.running_under_istanbul) {
var istanbul = require.nodeRequire('istanbul');
var coverageVariable = Object.keys(global).filter(function (key) { return key.indexOf('$$cov_') === 0 })[0];
var instrumenter = new istanbul.Instrumenter({
coverageVariable: coverageVariable,
noCompact: true,
embedSource: true
});
compiled = instrumenter.instrumentSync(compiled, path);
}
buildMap[name] = compiled;
onLoadNative.fromText(compiled);
} catch (err) {
onLoadNative.error(err);
}
}
function babelAsync(name, parentRequire, onLoadNative, config) {
name = ensureJSXFileExtension(name, config);
parentRequire(['babel', 'text'], function (babel, text) {
text.load(name, parentRequire, function (content) {
try {
var result = babel.transform(content, {
sourceFileName: config.baseUrl + name,
sourceMaps: getSourceMapConfig(config),
filename: config.baseUrl + name
});
onLoadNative.fromText(result.code);
} catch (err) {
onLoadNative.error(err);
}
});
});
}
function ensureJSXFileExtension(name, config) {
var fileExtension = config && config.jsx && config.jsx.fileExtension || '.jsx';
if (name.indexOf(fileExtension) === -1) {
name = name + fileExtension;
}
return name;
}
var jsx = {
load: isNode ? babelSync : babelAsync,
write: function (pluginName, name, write) {
if (typeof buildMap[name] === 'string') {
var text = buildMap[name];
write.asModule(pluginName + "!" + name, text);
} else {
throw new Error('Module not found in build map: ' + name);
}
}
};
return jsx;
});