2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
* 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.
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
|
|
|
* Note: This is a fork of the fb-specific transform.js
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-09-19 22:20:25 +00:00
|
|
|
const babel = require('babel-core');
|
2016-02-03 16:14:38 +00:00
|
|
|
const externalHelpersPlugin = require('babel-plugin-external-helpers');
|
2015-11-14 11:23:46 +00:00
|
|
|
const fs = require('fs');
|
2016-02-03 16:14:38 +00:00
|
|
|
const makeHMRConfig = require('babel-preset-react-native/configs/hmr');
|
|
|
|
const resolvePlugins = require('babel-preset-react-native/lib/resolvePlugins');
|
2016-06-02 18:38:01 +00:00
|
|
|
const inlineRequiresPlugin = require('babel-preset-fbjs/plugins/inline-requires');
|
2015-11-14 11:23:46 +00:00
|
|
|
const json5 = require('json5');
|
|
|
|
const path = require('path');
|
|
|
|
|
2016-02-03 16:14:38 +00:00
|
|
|
/**
|
|
|
|
* Return a memoized function that checks for the existence of a
|
|
|
|
* project level .babelrc file, and if it doesn't exist, reads the
|
|
|
|
* default RN babelrc file and uses that.
|
|
|
|
*/
|
|
|
|
const getBabelRC = (function() {
|
|
|
|
let babelRC = null;
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2016-02-03 16:14:38 +00:00
|
|
|
return function _getBabelRC(projectRoots) {
|
|
|
|
if (babelRC !== null) {
|
|
|
|
return babelRC;
|
|
|
|
}
|
|
|
|
|
|
|
|
babelRC = { plugins: [] }; // empty babelrc
|
|
|
|
|
|
|
|
// Let's look for the .babelrc in the first project root.
|
|
|
|
// In the future let's look into adding a command line option to specify
|
|
|
|
// this location.
|
|
|
|
//
|
|
|
|
// NOTE: we're not reading the project's .babelrc here. We leave it up to
|
|
|
|
// Babel to do that automatically and apply the transforms accordingly
|
|
|
|
// (which works because we pass in `filename` and `sourceFilename` to
|
|
|
|
// Babel when we transform).
|
|
|
|
let projectBabelRCPath;
|
|
|
|
if (projectRoots && projectRoots.length > 0) {
|
|
|
|
projectBabelRCPath = path.resolve(projectRoots[0], '.babelrc');
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a .babelrc file doesn't exist in the project,
|
|
|
|
// use the Babel config provided with react-native.
|
|
|
|
if (!projectBabelRCPath || !fs.existsSync(projectBabelRCPath)) {
|
|
|
|
babelRC = json5.parse(
|
|
|
|
fs.readFileSync(
|
|
|
|
path.resolve(__dirname, 'react-packager', 'rn-babelrc.json'))
|
|
|
|
);
|
|
|
|
|
|
|
|
// Require the babel-preset's listed in the default babel config
|
|
|
|
babelRC.presets = babelRC.presets.map((preset) => require('babel-preset-' + preset));
|
|
|
|
babelRC.plugins = resolvePlugins(babelRC.plugins);
|
|
|
|
}
|
|
|
|
|
|
|
|
return babelRC;
|
2016-03-08 17:50:14 +00:00
|
|
|
};
|
2016-02-03 16:14:38 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a filename and options, build a Babel
|
|
|
|
* config object with the appropriate plugins.
|
|
|
|
*/
|
|
|
|
function buildBabelConfig(filename, options) {
|
|
|
|
const babelRC = getBabelRC(options.projectRoots);
|
2015-11-14 11:23:46 +00:00
|
|
|
|
|
|
|
const extraConfig = {
|
|
|
|
filename,
|
|
|
|
sourceFileName: filename,
|
2016-09-12 16:14:45 +00:00
|
|
|
sourceMaps: options.generateSourceMaps,
|
2015-11-14 11:23:46 +00:00
|
|
|
};
|
|
|
|
|
2016-02-03 16:14:38 +00:00
|
|
|
let config = Object.assign({}, babelRC, extraConfig);
|
|
|
|
|
|
|
|
// Add extra plugins
|
|
|
|
const extraPlugins = [externalHelpersPlugin];
|
2015-08-15 22:59:37 +00:00
|
|
|
|
2016-03-08 17:50:14 +00:00
|
|
|
var inlineRequires = options.inlineRequires;
|
|
|
|
var blacklist = inlineRequires && inlineRequires.blacklist;
|
|
|
|
if (inlineRequires && !(blacklist && filename in blacklist)) {
|
2016-02-03 16:14:38 +00:00
|
|
|
extraPlugins.push(inlineRequiresPlugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
config.plugins = extraPlugins.concat(config.plugins);
|
|
|
|
|
|
|
|
if (options.hot) {
|
2016-03-08 17:50:14 +00:00
|
|
|
const hmrConfig = makeHMRConfig(options, filename);
|
2016-02-03 16:14:38 +00:00
|
|
|
config = Object.assign({}, config, hmrConfig);
|
2015-08-15 22:59:37 +00:00
|
|
|
}
|
2015-11-20 15:52:36 +00:00
|
|
|
|
2016-02-03 16:14:38 +00:00
|
|
|
return Object.assign({}, babelRC, config);
|
|
|
|
}
|
|
|
|
|
|
|
|
function transform(src, filename, options) {
|
|
|
|
options = options || {};
|
|
|
|
|
2016-05-18 19:32:20 +00:00
|
|
|
const OLD_BABEL_ENV = process.env.BABEL_ENV;
|
|
|
|
process.env.BABEL_ENV = options.dev ? 'development' : 'production';
|
|
|
|
|
|
|
|
try {
|
|
|
|
const babelConfig = buildBabelConfig(filename, options);
|
|
|
|
const result = babel.transform(src, babelConfig);
|
|
|
|
|
|
|
|
return {
|
|
|
|
ast: result.ast,
|
|
|
|
code: result.code,
|
|
|
|
map: result.map,
|
|
|
|
filename: filename,
|
|
|
|
};
|
|
|
|
} finally {
|
|
|
|
process.env.BABEL_ENV = OLD_BABEL_ENV;
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 01:43:36 +00:00
|
|
|
module.exports = function(data, callback) {
|
2015-09-19 22:20:25 +00:00
|
|
|
let result;
|
2015-02-19 01:43:36 +00:00
|
|
|
try {
|
2015-10-21 23:25:24 +00:00
|
|
|
result = transform(data.sourceCode, data.filename, data.options);
|
2015-02-19 01:43:36 +00:00
|
|
|
} catch (e) {
|
2015-05-22 19:16:15 +00:00
|
|
|
callback(e);
|
|
|
|
return;
|
2015-02-19 01:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, result);
|
|
|
|
};
|
|
|
|
|
|
|
|
// export for use in jest
|
|
|
|
module.exports.transform = transform;
|