NodeJS 和 Javascript (requirejs) 依赖注入
我目前正在使用 requirejs 来管理模块 js/css 依赖项。 我想发现让节点通过集中配置文件执行此操作的可能性。 因此,不要
define([
'jquery'
'lib/somelib'
'views/someview']
在每个模块内手动执行类似的操作。
我会让节点注入依赖项,即
require('moduleA').setDeps('jquery','lib/somelib','views/someview')
无论如何,我对任何研究节点依赖项注入的项目都很感兴趣。
谢谢
I am currently using requirejs to manage module js/css dependencies.
I'd like to discover the possibilities of having node do this via a centralized config file.
So instead of manually doing something like
define([
'jquery'
'lib/somelib'
'views/someview']
within each module.
I'd have node inject the dependencies ie
require('moduleA').setDeps('jquery','lib/somelib','views/someview')
Anyway, I'm interested in any projects looking at dependency injection for node.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我提出了一个依赖注入的解决方案。它称为 injectr,它使用节点的
vm
库并替换require< 的默认功能/code> 包含文件时。
因此,在您的测试中,请使用
injectr('libToTest' { 'libToMock' : myMock });
,而不是require('libToTest')
。我想让界面尽可能简单,无需更改正在测试的代码。我认为效果很好。值得注意的是,注入器文件是相对于工作目录的,不像 require 是相对于当前文件的,但这并不重要,因为它只在测试中使用。
I've come up with a solution for dependency injection. It's called injectr, and it uses node's
vm
library and replaces the default functionality ofrequire
when including a file.So in your tests, instead of
require('libToTest')
, useinjectr('libToTest' { 'libToMock' : myMock });
. I wanted to make the interface as straightforward as possible, with no need to alter the code being tested. I think it works quite well.It's just worth noting that injectr files are relative to the working directory, unlike require which is relative to the current file, but that shouldn't matter because it's only used in tests.
我之前曾考虑过提供替代的
require
来在 Node.js 中提供某种形式的依赖注入。模块代码
例如,假设您在 code.js 中有以下语句:
fs = 要求('fs');
如果您使用
node code.js
运行此代码,那么它将打印出 text.txt 的内容。注入器代码
但是,假设您有一个想要抽象出文件系统的测试模块。
您的测试文件 test.js 可能如下所示:
如果您现在运行
node test.js
,它将打印出“测试内容” ,即使它包含 code.js。I've previously toyed with the idea of providing an alternate
require
to make a form of dependency injection available in Node.js.Module code
For example, suppose you have following statements in code.js:
fs = require('fs');
If you run this code with
node code.js
, then it will print out the contents of text.txt.Injector code
However, suppose you have a test module that wants to abstract away the file system.
Your test file test.js could then look like this:
If you now run
node test.js
, it will print out "test contents", even though it includes code.js.我还编写了一个模块来完成此任务,它称为 rewire。只需使用
npm install rewire
然后:我受到 Nathan MacInnes 的注入器的启发,但使用一种不同的方法。我不使用 vm 来评估测试模块,事实上我使用节点自己的 require。这样,您的模块的行为与使用 require() 完全相同(除了您的修改)。还完全支持调试。
I've also written a module to accomplish this, it's called rewire. Just use
npm install rewire
and then:I've been inspired by Nathan MacInnes's injectr but used a different approach. I don't use
vm
to eval the test-module, in fact I use node's own require. This way your module behaves exactly like usingrequire()
(except your modifications). Also debugging is fully supported.