简体   繁体   中英

Conditional import in javascript

I have a native library built for either node or electron. The project importing this library uses the node implementation (for testing). A further project requires the electron version. Is there a way I can setup a configuration for a conditional import that is trumped by the parent project?

Project A (electron)
  |- Project B (node)
      |- Library (native node/electron)

So Project B would set the config to node, causing the Library to conditionally load the node native. Project A could override that config to force the Library to load the native electron config.

How would I setup something like this?

You can see if you're running in electron like this:

if(process.versions['electron']) {
    console.log('we are running in electron');
}

or

if(window && window.process && window.process.type) {
    console.log('we are running in electron');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM