mirror of
https://github.com/status-im/react-native.git
synced 2025-01-28 02:04:55 +00:00
181896e4d9
Summary: public `babel-plugin-react-transform` doesn't support to run on transformed code (it doesn't detect some react components). The reason why HMR was originally on the internal transform was so that it worked with non JS code (TypeScript, Coffeescript, etc), but looks like this is not very popupar in the community, maybe because the JS ecosystem is getting better everyday. So long story short, for now lets move HMR to the external transformer. This should also give us a perf boost. Reviewed By: davidaurelio Differential Revision: D2870973 fb-gh-sync-id: 68ca8d0540b88b9516b9fef10643f93fc9b530d2
76 lines
1.9 KiB
JavaScript
76 lines
1.9 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 resolvePlugins = require('./react-packager/src/JSTransformer/resolvePlugins');
|
|
|
|
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.hot) {
|
|
extraPlugins.push([
|
|
'react-transform',
|
|
{
|
|
transforms: [{
|
|
transform: 'react-transform-hmr/lib/index.js',
|
|
imports: ['React'],
|
|
locals: ['module'],
|
|
}]
|
|
},
|
|
]);
|
|
}
|
|
|
|
if (options.inlineRequires) {
|
|
extraPlugins.push(inlineRequires);
|
|
}
|
|
config.plugins = resolvePlugins(extraPlugins.concat(config.plugins));
|
|
|
|
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;
|