-
Notifications
You must be signed in to change notification settings - Fork 5
/
transformer.js
84 lines (78 loc) · 2.48 KB
/
transformer.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
'use strict';
const babel = require('babel-core');
/**
* This is your `.babelrc` equivalent.
*/
const babelRC = {
presets: [require('babel-preset-react-native')],
plugins: [
// The following plugin will rewrite imports. Reimplementations of node
// libraries such as `assert`, `buffer`, etc. will be picked up
// automatically by the React Native packager. All other built-in node
// libraries get rewritten to their browserify counterpart.
[
require('babel-plugin-rewrite-require'),
{
aliases: {
constants: 'constants-browserify',
crypto: 'crypto-browserify',
//buffer: 'safe-buffer',
//dns: 'node-libs-browser/mock/dns',
//domain: 'domain-browser',
//fs: 'node-libs-browser/mock/empty',
//http: 'stream-http',
//https: 'https-browserify',
net: 'react-native-tcp',
//os: 'os-browserify/browser',
//path: 'path-browserify',
//querystring: 'querystring-es3',
stream: 'stream-browserify',
_stream_duplex: 'readable-stream/duplex',
_stream_passthrough: 'readable-stream/passthrough',
_stream_readable: 'readable-stream/readable',
_stream_transform: 'readable-stream/transform',
_stream_writable: 'readable-stream/writable',
//sys: 'util',
//timers: 'timers-browserify',
tls: 'node-libs-browser/mock/tls'
//tty: 'tty-browserify',
//vm: 'vm-browserify',
//zlib: 'browserify-zlib',
// You can add your own, much like webpack aliases:
//'corporate-lib': 'corporate-lib-react-native',
// You can also mock any libraries that you don't need to support on
// React Native, but have to be present for 3rd party code to work:
//'some-third-party-dependency': 'node-libs-browser/mock/empty',
},
throwForNonStringLiteral: true
}
]
]
};
function transform(src, filename, options) {
const babelConfig = Object.assign({}, babelRC, {
filename,
sourceFileName: filename
});
const result = babel.transform(src, babelConfig);
return {
ast: result.ast,
code: result.code,
map: result.map,
filename: filename
};
}
module.exports.transform = function(data, callback) {
let result;
try {
result = transform(
data.sourceCode || data.src,
data.filename,
data.options
);
} catch (e) {
throw e;
return;
}
return result;
};