Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex for blacklisting node module with dynamic yarn workspace path in React Native metro bundler

Similar to: How to blacklist specific node_modules of my package's dependencies in react-native's packager?

I am trying to exclude react native from metro packager using the blacklist option which needs to return a regexp.

What I need is to return something like:

/\/DYNAMIC_PROJECT_DIRECTORY\/node_modules\/react-native\/.*/,

where I can insert a variable into the DYNAMIC_PROJECT_DIRECTORY as it will change dependant on the yarn workspace path of the other module.

I just have no familiarity with regex!

Thanks

Edit: I tried hard coding in the path into that format and it still didn't work to blacklist, so if someone can point me in the right direction on what works to exclude that folder and everything in it that would be much appreciated!

like image 265
Sam Matthews Avatar asked Jan 07 '19 04:01

Sam Matthews


1 Answers

You can create a regular expression that uses dynamic variables with the RegExp constructor:

new RegExp(`\/${DYNAMIC_PROJECT_DIRECTORY}\/node_modules\/react-native\/.*`);

Note:

  • This example is using a template string to insert the DYNAMIC_PROJECT_DIRECTORY; you could just as well write:
    new RegExp('\/' + DYNAMIC_PROJECT_DIRECTORY + '\/node_modules\/react-native\/.*');
  • The leading and trailing slashes are omitted when using this method of creating a regular expression
like image 186
Patrick Hund Avatar answered Sep 28 '22 03:09

Patrick Hund