Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix dynamic requires in third-party libraries?

My desktop app is using Electron+React for the interface and Edge.js to connect Node with my C# application.

My problem is: Webpack is failing to bundle my application because the Edge.js dependency throws the following error:

Critical dependency: the request of a dependency is an expression

The problem is that Edge.js has the following dynamic require:

var compilerName = 'edge-' + language.toLowerCase();
var compiler = require(compilerName);

Most of the time, compilerName will be translated to "edge-cs", but Webpack is not able to determine this.

How can I solve this issue? People are suggesting to set the require context or ContextReplacementPlugin, but both of them are usually applied in cases where you have require('./directory/' + variable) and I don't know how to use them in my case where I have require(variable).

Note: I need a solution where I don't need to modify my third-party library code.

like image 608
user8983325 Avatar asked Nov 08 '22 13:11

user8983325


1 Answers

I don't think this is possible, Have you considered something like a massive switch below? It is suggested here

switch (name) {
  case 'a': return require('./a');
  case 'b': return require('./b');
  // etc...
}
like image 55
nilesh Avatar answered Nov 15 '22 11:11

nilesh