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 коммит произвёл Facebook Github Bot
Родитель 05b75b9ebf
Коммит f80000b9e7
2 изменённых файлов: 3 добавлений и 40 удалений

Просмотреть файл

@ -167,7 +167,7 @@
"inquirer": "^3.0.6", "inquirer": "^3.0.6",
"lodash": "^4.17.5", "lodash": "^4.17.5",
"metro": "^0.34.0", "metro": "^0.34.0",
"metro-babel-register": "^0.34.1", "metro-babel-register": "^0.34.2",
"metro-core": "^0.34.0", "metro-core": "^0.34.0",
"mime": "^1.3.4", "mime": "^1.3.4",
"minimist": "^1.2.0", "minimist": "^1.2.0",

Просмотреть файл

@ -8,43 +8,8 @@
'use strict'; 'use strict';
const babelRegisterOnly = require('metro-babel-register'); const babelRegisterOnly = require('metro-babel-register');
const escapeRegExp = require('lodash/escapeRegExp');
const path = require('path');
const BABEL_ENABLED_PATHS = ['local-cli', 'metro']; const BABEL_ENABLED_PATHS = ['local-cli'];
/**
* 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 =>
folderPath === 'metro'
// metro uses flow (for example) which needs to be stripped out w/babel.
// it'll resolve to .../metro/src/index.js we want root
? path.resolve(require.resolve('metro'), '..')
// 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, '/')
)}`
)
);
}
function getOnlyList() {
return buildRegExps(__dirname, BABEL_ENABLED_PATHS);
}
/** /**
* Centralized place to register all the directories that need a Babel * Centralized place to register all the directories that need a Babel
@ -52,9 +17,7 @@ function getOnlyList() {
* support Flow type annotations. * support Flow type annotations.
*/ */
function setupBabel() { function setupBabel() {
babelRegisterOnly(getOnlyList()); babelRegisterOnly(babelRegisterOnly.buildRegExps(__dirname, BABEL_ENABLED_PATHS));
} }
setupBabel.buildRegExps = buildRegExps;
setupBabel.getOnlyList = getOnlyList;
module.exports = setupBabel; module.exports = setupBabel;