Skip to content

Instantly share code, notes, and snippets.



Created Dec 15, 2015
What would you like to do?
Transformer.js and babelrc for RN + Relay
"retainLines": true,
"compact": true,
"comments": false,
"plugins": [
["transform-es2015-modules-commonjs", {"strict": false, "allowTopLevelThis": true}],
"sourceMaps": false
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
* Note: This is a fork of the fb-specific transform.js
'use strict';
const babel = require('babel-core');
const fs = require('fs');
const inlineRequires = require('fbjs-scripts/babel-6/inline-requires');
const json5 = require('json5');
const path = require('path');
const babelRC =
path.resolve(__dirname, '.babelrc')));
function transform(src, filename, options) {
options = options || {};
const extraPlugins = ['external-helpers-2', require('./relay/babelRelayPlugin')];
const extraConfig = {
sourceFileName: filename,
const config = Object.assign({}, babelRC, extraConfig);
if (options.inlineRequires) {
config.plugins = extraPlugins.concat(config.plugins);
// Manually resolve all default Babel plugins. babel.transform will attempt to resolve
// all base plugins relative to the file it's compiling. This makes sure that we're
// using the plugins installed in the react-native package.
config.plugins = {
// Normalise plugin to an array.
if (!Array.isArray(plugin)) {
plugin = [plugin];
// Only resolve the plugin if it's a string reference.
if (typeof plugin[0] === 'string') {
plugin[0] = require(`babel-plugin-${plugin[0]}`);
plugin[0] = plugin[0].__esModule ? plugin[0].default : plugin[0];
return plugin;
const result = babel.transform(src, Object.assign({}, babelRC, config));
return {
code: result.code
module.exports = function(data, callback) {
let result;
try {
result = transform(data.sourceCode, data.filename, data.options);
} catch (e) {
callback(null, result);
// export for use in jest
module.exports.transform = transform;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment