Move `buildRegExps` from `react-native` to `metro-babel-register`

Summary: Moves the `buildRegExps` function from `react-native` to `metro-babel-register`. This way, it is easier to reuse, and we can remove FB-specific logic from React Native.

Reviewed By: jeanlauliac, mjesun

Differential Revision: D7727483

fbshipit-source-id: 0f7773ff044033c465f0712c523a0aef61bf8444
This commit is contained in:
David Aurelio 2018-04-23 13:37:19 -07:00 committed by Facebook Github Bot
parent 2a029e05e8
commit 6b7bffe653
3 changed files with 36 additions and 6 deletions

View File

@ -8,6 +8,8 @@
*/ */
'use strict'; 'use strict';
const escapeRegExp = require('escape-string-regexp');
const path = require('path');
require('./node-polyfills'); require('./node-polyfills');
var _only = []; var _only = [];
@ -34,14 +36,41 @@ function registerOnly(onlyList) {
function config(onlyList) { function config(onlyList) {
_only = _only.concat(onlyList); _only = _only.concat(onlyList);
return { return {
presets: [], babelrc: false,
plugins: PLUGINS, ignore: null,
only: _only, only: _only,
plugins: PLUGINS,
presets: [],
retainLines: true, retainLines: true,
sourceMaps: 'inline', sourceMaps: 'inline',
babelrc: false,
}; };
} }
/**
* We use absolute paths for matching only the top-level folders reliably. For
* example, we would not want to match some deeply nested forder that happens to
* have the same name as one of `BABEL_ENABLED_PATHS`.
*/
function buildRegExps(basePath, dirPaths) {
return dirPaths.map(
folderPath =>
// Babel `only` option works with forward slashes in the RegExp so replace
// backslashes for Windows.
folderPath instanceof RegExp
? new RegExp(
`^${escapeRegExp(
path.resolve(basePath, '.').replace(/\\/g, '/'),
)}/${folderPath.source}`,
folderPath.flags,
)
: new RegExp(
`^${escapeRegExp(
path.resolve(basePath, folderPath).replace(/\\/g, '/'),
)}`,
),
);
}
module.exports = registerOnly; module.exports = registerOnly;
module.exports.config = config; module.exports.config = config;
module.exports.buildRegExps = buildRegExps;

View File

@ -1,5 +1,5 @@
{ {
"version": "0.34.1", "version": "0.34.2",
"name": "metro-babel-register", "name": "metro-babel-register",
"description": "🚇 babel/register configuration for Metro.", "description": "🚇 babel/register configuration for Metro.",
"main": "babel-register.js", "main": "babel-register.js",
@ -14,6 +14,7 @@
"@babel/plugin-transform-flow-strip-types": "7.0.0-beta.40", "@babel/plugin-transform-flow-strip-types": "7.0.0-beta.40",
"@babel/plugin-transform-modules-commonjs": "7.0.0-beta.40", "@babel/plugin-transform-modules-commonjs": "7.0.0-beta.40",
"@babel/register": "7.0.0-beta.40", "@babel/register": "7.0.0-beta.40",
"core-js": "^2.2.2" "core-js": "^2.2.2",
"escape-string-regexp": "^1.0.5"
} }
} }

View File

@ -78,7 +78,7 @@
"merge-stream": "^1.0.1", "merge-stream": "^1.0.1",
"metro-babel7-plugin-react-transform": "0.34.0", "metro-babel7-plugin-react-transform": "0.34.0",
"metro-babylon7": "0.34.0", "metro-babylon7": "0.34.0",
"metro-babel-register": "0.34.1", "metro-babel-register": "0.34.2",
"metro-cache": "0.34.0", "metro-cache": "0.34.0",
"metro-core": "0.34.0", "metro-core": "0.34.0",
"metro-minify-uglify": "0.34.0", "metro-minify-uglify": "0.34.0",