简体   繁体   English

JavaScript中的条件导入

[英]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. 因此,项目B会将配置设置为节点,从而导致库有条件地加载本机节点。 Project A could override that config to force the Library to load the native electron config. 项目A可能会覆盖该配置,以强制库加载本机电子配置。

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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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