Skip to content

Commit

Permalink
Move buildRegExps from react-native to metro-babel-register
Browse files Browse the repository at this point in the history
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
  • Loading branch information
davidaurelio authored and facebook-github-bot committed Apr 23, 2018
1 parent 05b75b9 commit f80000b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
"inquirer": "^3.0.6",
"lodash": "^4.17.5",
"metro": "^0.34.0",
"metro-babel-register": "^0.34.1",
"metro-babel-register": "^0.34.2",
"metro-core": "^0.34.0",
"mime": "^1.3.4",
"minimist": "^1.2.0",
Expand Down
41 changes: 2 additions & 39 deletions setupBabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,16 @@
'use strict';

const babelRegisterOnly = require('metro-babel-register');
const escapeRegExp = require('lodash/escapeRegExp');
const path = require('path');

const BABEL_ENABLED_PATHS = ['local-cli', 'metro'];

/**
* 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);
}
const BABEL_ENABLED_PATHS = ['local-cli'];

/**
* Centralized place to register all the directories that need a Babel
* transformation before being fed to Node.js. Notably, this is necessary to
* support Flow type annotations.
*/
function setupBabel() {
babelRegisterOnly(getOnlyList());
babelRegisterOnly(babelRegisterOnly.buildRegExps(__dirname, BABEL_ENABLED_PATHS));
}

setupBabel.buildRegExps = buildRegExps;
setupBabel.getOnlyList = getOnlyList;
module.exports = setupBabel;

0 comments on commit f80000b

Please sign in to comment.