Slbox
Slbox

Reputation: 13138

Exclude only a top level directory in Metro Bundler using blacklist regex? (blacklistRE)

I believe this to be primarily a question about regex, but I may be wrong.


Let's say I wanted to exclude the folder in my project root called art.

My output regex in the CLI looks like:

blacklistRE: /(all|the|other|matchers|art\.*)$/

But this leads to an error:

Unable to resolve "art/core/color" from "..\..\node_modules\react-native\Libraries\ART\ReactNativeART.js"

How can I exclude the top level art directory without affecting all child directories?

From the bundler's docs

blacklistRE Type: RegExp

A RegEx defining which paths to ignore. https://facebook.github.io/metro/docs/configuration/#blacklistre

The code of the function I'm calling: It's short and self-contained in this file, if anyone better than me with regex might have any insight. https://github.com/facebook/metro/blob/8c53c38932c3e396109014ac707287fbdf2500d3/packages/metro-config/src/defaults/blacklist.js#L35-L44

My full code looks like:

const blacklist = require('metro-config/src/defaults/blacklist');

const regexStrings = [
  //  DEFAULTS
  '.*\\android\\ReactAndroid\\.*',
  '.*\\versioned-react-native\\.*',
  'node_modules[\\\\]react[\\\\]dist[\\\\].*',
  'node_modules[\\\\].cache[\\\\].*',
  'packages[\\\\]electron[\\\\].*',
  'node_modules[\\\\]electron.*',
  'website\\node_modules\\.*',
  'heapCapture\\bundle.js',
  '.*\\__tests__\\.*',
  '.*\\.git\\.*',
  // CUSTOM
  'art\\.*',
]

const constructBlacklistRE = () => {
  const formedRegexes = regexStrings.map(piece => new RegExp(piece));
  console.warn(formedRegexes);
  return blacklist([...formedRegexes]);
};

const config = {
  blacklistRE: constructBlacklistRE(),
}

Upvotes: 3

Views: 2062

Answers (1)

oNaiPs
oNaiPs

Reputation: 679

This should work:

const blacklist = require('metro-config/src/defaults/blacklist');
const path = require('path');

const ignoreTopLevelFolders = [
   'art'
   // add more top level folders here
].map(f => new RegExp(`${ path.resolve(f) }/.*`));

module.exports = { resolver: { blacklistRE: blacklist(ignoreTopLevelFolders) } };

Upvotes: 1

Related Questions