5

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!

Sam Matthews
  • 677
  • 1
  • 12
  • 27

1 Answers1

6

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
Patrick Hund
  • 19,163
  • 11
  • 66
  • 95
  • 1
    Great thanks. I was aware of template strings but wasn't sure if one could be output as a regexp, so I guess thats where the leading and trailing slash thing comes in. Thanks! – Sam Matthews Jan 09 '19 at 15:25
  • 1
    Please note that you need to escape the `$` in your regex for line ends when using template strings – inetphantom Jan 15 '19 at 13:14