Move hmr config generator into its own file, burn the bridge

Reviewed By: mjesun

Differential Revision: D7788847

fbshipit-source-id: 481a457abe58b41f5e7001601c126adc457cd28c
This commit is contained in:
Peter van der Zee 2018-05-02 10:06:52 -07:00 committed by Facebook Github Bot
parent 6ddbbfbae1
commit 81b49fa89b
4 changed files with 55 additions and 65 deletions

View File

@ -1,63 +0,0 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+javascript_foundation
* @flow (won't like this)
* @format
*/
'use strict';
// This is a temporary migration bridge to switch between babel 6 and 7
const makeHMRConfig7 = makeMakeHMRConfig7();
module.exports = {
makeHMRConfig: makeHMRConfig7,
};
function makeMakeHMRConfig7() {
// from: babel-preset-react-native/configs/hmr
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var path = require('path');
var hmrTransform = 'react-transform-hmr/lib/index.js';
var transformPath = require.resolve(hmrTransform);
return function(options: mixed, filename?: string) {
var transform = filename
? './' + path.relative(path.dirname(filename), transformPath) // packager can't handle absolute paths
: hmrTransform;
// Fix the module path to use '/' on Windows.
if (path.sep === '\\') {
transform = transform.replace(/\\/g, '/');
}
return {
plugins: [
[
require('metro-babel7-plugin-react-transform'),
{
transforms: [
{
transform,
imports: ['react'],
locals: ['module'],
},
],
},
],
],
};
};
}

View File

@ -0,0 +1,53 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+javascript_foundation
* @flow
* @format
*/
'use strict';
// imported from: babel-preset-react-native/configs/hmr
const path = require('path');
const hmrTransform = 'react-transform-hmr/lib/index.js';
const transformPath = require.resolve(hmrTransform);
function makeHMRConfig(options: mixed, filename?: string) {
// We need to get a _path_ to transform relative to/from.
// Either take the filename that is passed on or use the transform as base.
let relativePath = filename
? // packager can't handle absolute paths
'./' + path.relative(path.dirname(filename), transformPath)
: hmrTransform;
// Fix the module path to use '/' on Windows.
if (path.sep === '\\') {
relativePath = relativePath.replace(/\\/g, '/');
}
return {
plugins: [
[
// This is a Babel 7 compatible fork
// of https://github.com/gaearon/babel-plugin-react-transform
require('metro-babel7-plugin-react-transform'),
{
transforms: [
{
transform: relativePath,
imports: ['react'],
locals: ['module'],
},
],
},
],
],
};
}
module.exports = makeHMRConfig;

View File

@ -13,8 +13,8 @@
const blacklist = require('./blacklist');
const debug = require('debug');
const invariant = require('fbjs/lib/invariant');
const makeHMRConfig = require('./hmrConfig');
const {makeHMRConfig} = require('./babel-bridge');
const {Logger} = require('metro-core');
const {fromRawMappings, toSegmentTuple} = require('metro-source-map');

View File

@ -16,9 +16,9 @@ const externalHelpersPlugin = require('babel-plugin-external-helpers');
const fs = require('fs');
const inlineRequiresPlugin = require('babel-preset-fbjs/plugins/inline-requires');
const json5 = require('json5');
const makeHMRConfig = require('./hmrConfig');
const path = require('path');
const {makeHMRConfig} = require('./babel-bridge');
const {transformSync} = require('@babel/core');
import type {Transformer, TransformOptions} from './JSTransformer/worker';