简体   繁体   English

在Meteor node.js中动态加载模块

[英]dynamically load modules in Meteor node.js

I'm trying to load multiple modules on the fly via chokidar (watchdog) using Meteor 1.6 beta, however after doing extensive research on the matter I just can't seem to get it to work. 我正在尝试使用Meteor 1.6 beta通过chokidar(看门狗)动态加载多个模块,但是,在对此问题进行了广泛研究之后,我似乎无法正常工作。

From what I gather require by design will not take in anything other than static strings, ie 根据我的设计需求,除了静态字符串(即静态字符串)

require("test/string/here")

Since if I try: 因为如果我尝试:

var path = "test/string/here"
require(path)

I just get Error: Cannot find module, even though the strings are identical. 我只是得到错误:即使字符串相同,也找不到模块。

Now the thing is I'm uncertain how to go on about this, am I really forced to either use import or static strings when using meteor or is there some workaround this? 现在的事情是我不确定如何继续进行下去,我真的被迫在使用流星时要么使用导入字符串,要么使用静态字符串,或者是否有解决方法?

watchdog(cmddir, (dir) => {
match = "." + regex_cmd.exec(dir);

match = dir;

loader.emit("loadcommand", match)


});

loader.on('loadcommand', (file) => {
require(file);
});

There is something intrinsically weird in what you describe. 您所描述的内容本质上有些奇怪。

chokidar is used to watch actual files and folders. chokidar用于观看实际文件和文件夹。

But Meteor compiles and bundles your code, resulting in an app folder after build that is totally different from your project structure. 但是Meteor会编译并捆绑您的代码,从而在构建后生成一个与您的项目结构完全不同的应用程序文件夹。

Although Meteor now supports dynamic imports, the mechanism is internal to Meteor and does not rely on your actual project files, but on Meteor built ones. 尽管Meteor现在支持动态导入,但是该机制是Meteor内部的,它不依赖于您实际的项目文件,而是依赖于Meteor构建的文件。

If you want to dynamically require files like in Node, including with dynamically generated module path, you should avoid import and require statements, which are automatically replaced by Meteor built-in import mechanism. 如果要动态require Node中的文件(包括动态生成的模块路径),则应避免使用importrequire语句,这些语句会被Meteor内置的导入机制自动替换。 Instead you would have to make up your own loading function, taking care of the fact that your app built folder is different from your project folder. 取而代之的是,您必须组成自己的加载功能,同时要注意应用程序构建的文件夹与项目文件夹不同。

That may work for example if your server is watching files and/or folders in a static location, different from where your app will be running. 例如,如果您的服务器正在静态位置监视文件和/或文件夹,则可能会起作用,这与应用程序的运行位置不同。

In the end, I feel this is a sort of XY problem: you have not described your objective in the first place, and the above issue is trying to solve a weird solution that does not seem to fit how Meteor works, hence which may not be the most appropriate solution for your implicit objective. 最后,我觉得这是一个XY问题:您最初没有描述您的目标,而以上问题试图解决一个奇怪的解决方案,该解决方案似乎不适合Meteor的工作原理,因此可能不适合是您隐含目标的最合适解决方案。

@Sashko does a great job of explaining Meteor's dynamic imports here . @Sashko 在这里很好地解释了Meteor的动态导入。 There are also docs 也有文档

A dynamic import is a function that returns a promise instead of just importing statically at build time. 动态导入是一种返回承诺函数 ,而不仅仅是在构建时静态地导入。 Example: 例:

import('./component').then((MyComponent) => {
  render(MyComponent);
});

The promise runs once the module has been loaded. 一旦模块加载完成,promise将运行。 If you try to load the module repeatedly then it only gets loaded once and is immediately available on subsequent requests. 如果您尝试重复加载模块,那么它只会被加载一次,并且在随后的请求中立即可用。

afaict you can use a variable for the string to import. 您可以使用变量将字符串导入。

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

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