Allow to pass regular expressions to `buildRegExps`

Summary: Allows to use `setupBabel.buildRegExps` with regular expressions, not only with strings.

Reviewed By: mjesun

Differential Revision: D5614738

fbshipit-source-id: ccad4d49f8d2006a714833989b3c6be2ce7071ab
This commit is contained in:
David Aurelio 2017-08-14 08:31:42 -07:00 committed by Facebook Github Bot
parent 41504103ce
commit 475cf1ad29
1 changed files with 17 additions and 7 deletions

View File

@ -13,9 +13,7 @@ const babelRegisterOnly = require('metro-bundler/src/babelRegisterOnly');
const escapeRegExp = require('lodash/escapeRegExp');
const path = require('path');
const BABEL_ENABLED_PATHS = [
'local-cli',
];
const BABEL_ENABLED_PATHS = ['local-cli'];
/**
* We use absolute paths for matching only the top-level folders reliably. For
@ -23,10 +21,22 @@ const BABEL_ENABLED_PATHS = [
* have the same name as one of `BABEL_ENABLED_PATHS`.
*/
function buildRegExps(basePath, dirPaths) {
return dirPaths.map(folderPath =>
return dirPaths.map(
folderPath =>
// Babel `only` option works with forward slashes in the RegExp so replace
// backslashes for Windows.
new RegExp(`^${escapeRegExp(path.resolve(basePath, folderPath).replace(/\\/g, '/'))}`)
folderPath instanceof RegExp
? new RegExp(
`^${escapeRegExp(
path.resolve(basePath, '.').replace(/\\/g, '/'),
)}/${folderPath.source}`,
folderPath.flags,
)
: new RegExp(
`^${escapeRegExp(
path.resolve(basePath, folderPath).replace(/\\/g, '/'),
)}`,
),
);
}