mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 01:25:39 +00:00
8c9c7e6286
Summary: public This diff introduces an internal transforms pipeline that integrates with the external one. This has been a feature we've been looking to implement for a long time to use babel instead of `replace` with regexps on many parts of the packager. Also, to split the bundle we'll need to run one transform. Internally for Facebook we can run the system-import transform altogether withe the other ones. For OSS we offer `transformer.js` which people can use out of the box if they're writing ES6 code. For those people, `transformer.js` will also run the internal transforms`. However they might want to tune the transforms, or even write the code on another language that compiles to Javascript and use a complete different transformer. On those cases we'll need to run the external transforms first and pipe the output through the internal transforms. Note that the order it's important as the internal transforms assume the code is written in JS, though the original code could be on other scripting languages (CoffeeScript, TypeScript, etc). Reviewed By: davidaurelio Differential Revision: D2725109 fb-gh-sync-id: d764e209c78743419c4cb97068495c771372ab90
81 lines
2.2 KiB
JavaScript
81 lines
2.2 KiB
JavaScript
/**
|
|
* 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 ReactPackager = require('./react-packager');
|
|
|
|
const babelRC =
|
|
json5.parse(
|
|
fs.readFileSync(
|
|
path.resolve(__dirname, 'react-packager', '.babelrc')));
|
|
|
|
function transform(src, filename, options) {
|
|
options = options || {};
|
|
|
|
const extraPlugins = ['external-helpers-2'];
|
|
const extraConfig = {
|
|
filename,
|
|
sourceFileName: filename,
|
|
};
|
|
|
|
const config = Object.assign({}, babelRC, extraConfig);
|
|
|
|
if (options.inlineRequires) {
|
|
extraPlugins.push(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 = config.plugins.map(function(plugin) {
|
|
// 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;
|
|
});
|
|
|
|
config.plugins = config.plugins.concat(ReactPackager.getTransforms());
|
|
|
|
const result = babel.transform(src, Object.assign({}, babelRC, config));
|
|
|
|
return {
|
|
code: result.code,
|
|
filename: filename,
|
|
};
|
|
}
|
|
|
|
module.exports = function(data, callback) {
|
|
let result;
|
|
try {
|
|
result = transform(data.sourceCode, data.filename, data.options);
|
|
} catch (e) {
|
|
callback(e);
|
|
return;
|
|
}
|
|
|
|
callback(null, result);
|
|
};
|
|
|
|
// export for use in jest
|
|
module.exports.transform = transform;
|