Permalink
Browse files

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
  • Loading branch information...
davidaurelio authored and facebook-github-bot committed Aug 14, 2017
1 parent 4150410 commit 475cf1ad295697ccc10c39da1ee98da1a695f73a
Showing with 17 additions and 7 deletions.
  1. +17 −7 setupBabel.js
View
@@ -13,20 +13,30 @@ 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
* 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.
new RegExp(`^${escapeRegExp(path.resolve(basePath, folderPath).replace(/\\/g, '/'))}`)
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, '/'),
)}`,
),
);
}

0 comments on commit 475cf1a

Please sign in to comment.