diff --git a/packages/metro/src/babel-bridge.js b/packages/metro/src/babel-bridge.js deleted file mode 100644 index 64943240..00000000 --- a/packages/metro/src/babel-bridge.js +++ /dev/null @@ -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'], - }, - ], - }, - ], - ], - }; - }; -} diff --git a/packages/metro/src/hmrConfig.js b/packages/metro/src/hmrConfig.js new file mode 100644 index 00000000..02bfe456 --- /dev/null +++ b/packages/metro/src/hmrConfig.js @@ -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; diff --git a/packages/metro/src/legacy.js b/packages/metro/src/legacy.js index e71918fa..c2f176f7 100644 --- a/packages/metro/src/legacy.js +++ b/packages/metro/src/legacy.js @@ -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'); diff --git a/packages/metro/src/transformer.js b/packages/metro/src/transformer.js index 449cad4e..2a09f2de 100644 --- a/packages/metro/src/transformer.js +++ b/packages/metro/src/transformer.js @@ -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';