更新时间:2022-06-06 10:19:03
经过一番搜索,结果发现动态导入可能有点痛苦.
After a bit of searching, in turns out dynamic imports can be a bit of a pain.
这是我想出的解决方案,我已经在 node 中尝试过了.
This is the solution I came up with, I've tried it in node.
const MODULE_NAME = <CONDITION> ? require(MODULE_A) : require(MODULE_B);
或者,我想你可以做这样的事情;
Alternatively, I guess you could do something like this;
const MODULE_TO_IMPORT = 'MODULE_IMPORT_STRING';
const MODULE_NAME = import(MODULE_TO_IMPORT).then(({someFunc}) => someFunc());
但问题是这些需要一个以任何方式导入的模块.
But the problem is that these require a module to be imported either way.