如何使用现代的nodeJS动态选择模块? [英] How to dynamically select module with modern nodeJS?

查看:119
本文介绍了如何使用现代的nodeJS动态选择模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 node --experimental-modules test.mjs (NodeJs v11.9.0)。



实现同一个类有很多选择,我需要通过终端切换

  switch(process.argv [2] ){
case'a':
从'./MyClass-v1.mjs'导入MyClass;打破;
情况‘b’:
从’./MyClass-v2.mjs’导入MyClass;打破;
默认值:
从 ./MyClass-v3.mjs导入MyClass;
}

import MyClass上发生错误:意外标识符



PS:使用 isolated 从'./MyClass-v3.mjs导入MyClass时可以正常工作';






注意: const MyClass = require(' ./MyClass-v3.mjs')不适用于现代Javascript。 ( ReferenceError:未定义)。该文件只有一个类定义,

 导出默认类MyClass {...} 






PS:没有什么像 C_preprocessor 用于NodeJ吗?

解决方案

现代Javascript在这里可以使用旧的预编译器(具有argv访问权限)。 (ES6 +)实现正在构建中,某些语法正在使用NodeJS( node --experimental-modules )和优秀的 browsers 使用,其他语法则不能。 / p>

这是没有全部加载成本的唯一方法(请参阅@Markus解决方案),但是由于要使用非全局导入,因此需要一个难看的块定义作用域和异步操作...第一步是了解基本的impor块语法:

  import('./ MyClass- v3.mjs')。then(({{default:MyClass})=> {
//只能在此块中使用MyClass
let x = new MyClass();
控制台.log( DEBUG =,x);
});

它们,将argv选择和可选导入放在一起。



解决方案



  const classes = {
a:'./MyClass-v1.mjs '
,b:'./MyClass-v3.mjs'
,c:'./MyClass-v3.mjs'
};
import(classes [process.argv [2]] || classes.c).then(({{default:MyClass})=> {
let x = new MyClass();
console.log( DEBUG =,x);
});

PS:有些魔术(无理性)是使用({默认值:MyClass})代替(MyClass)






谢谢



致@Bergi(!),解释此解决方案(请参阅问题注释)。


I am using node --experimental-modules test.mjs (NodeJs v11.9.0).

There are many options to implement the same class, and I need to swith by terminal,

switch (process.argv[2]) {
  case 'a':
    import MyClass from './MyClass-v1.mjs'; break;
  case 'b':
    import MyClass from './MyClass-v2.mjs'; break;
  default:
    import MyClass from './MyClass-v3.mjs';
}

ERROR on import MyClass: Unexpected identifier

PS: working fine when using isolated import MyClass from './MyClass-v3.mjs';.


NOTE: const MyClass = require('./MyClass-v3.mjs') not works with modern Javascript. (ReferenceError: require is not defined). The file have only a class definition,

export default class MyClass { ... }


PS: there are nothing like C_preprocessor for NodeJs? An old pre-compiler (with some argv access) will be fine here.

解决方案

Modern Javascript (ES6+) implementations are under construction, some syntax are working with NodeJS (node --experimental-modules) and good browsers, other not.

This is the only way to do without cost of "load all" (see @Markus solution), but with the cost of non-global import, needs an ugly block to define scope and assyncronous operation... First step is to understand the basic impor block syntax:

import('./MyClass-v3.mjs').then(({default: MyClass}) => {
    // can use MyClass only here in this block
    let x = new MyClass();
    console.log("DEBUG=",x);
});

Them, putting all together, the argv selection and the optional imports.

Solution

const classes = {
  a: './MyClass-v1.mjs'
  ,b: './MyClass-v3.mjs'
  ,c: './MyClass-v3.mjs'
};
import(classes[process.argv[2]] || classes.c ).then(({default: MyClass}) => {
    let x = new MyClass();
    console.log("DEBUG=",x);
});

PS: some of the magic (no rational) is to use ({default: MyClass}) instead (MyClass).


Thanks

To @Bergi (!), for explain this solution (see question comments).

这篇关于如何使用现代的nodeJS动态选择模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆