NodeJS 和 Javascript (requirejs) 依赖注入

发布于 2024-12-19 12:21:59 字数 341 浏览 2 评论 0原文

我目前正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰火雁神 2024-12-26 12:22:00

我提出了一个依赖注入的解决方案。它称为 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 of require when including a file.

So in your tests, instead of require('libToTest'), use injectr('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.

倾城花音 2024-12-26 12:22:00

我之前曾考虑过提供替代的 require 来在 Node.js 中提供某种形式的依赖注入。

模块代码

例如,假设您在 code.js 中有以下语句:
fs = 要求('fs');

console.log(fs.readFileSync('text.txt', 'utf-8'));

如果您使用 node code.js 运行此代码,那么它将打印出 text.txt 的内容。

注入器代码

但是,假设您有一个想要抽象出文件系统的测试模块。
您的测试文件 test.js 可能如下所示:

var origRequire = global.require;
global.require = dependencyLookup;
require('./code.js');

function dependencyLookup (file) {
  switch (file) {
    case 'fs': return { readFileSync: function () { return "test contents"; } };
    default: return origRequire(file);
  }
}

如果您现在运行 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');

console.log(fs.readFileSync('text.txt', 'utf-8'));

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:

var origRequire = global.require;
global.require = dependencyLookup;
require('./code.js');

function dependencyLookup (file) {
  switch (file) {
    case 'fs': return { readFileSync: function () { return "test contents"; } };
    default: return origRequire(file);
  }
}

If you now run node test.js, it will print out "test contents", even though it includes code.js.

物价感观 2024-12-26 12:22:00

我还编写了一个模块来完成此任务,它称为 rewire。只需使用 npm install rewire 然后:

var rewire = require("rewire"),
    myModule = rewire("./path/to/myModule.js"); // exactly like require()

// Your module will now export a special setter and getter for private variables.
myModule.__set__("myPrivateVar", 123);
myModule.__get__("myPrivateVar"); // = 123


// This allows you to mock almost everything within the module e.g. the fs-module.
// Just pass the variable name as first parameter and your mock as second.
myModule.__set__("fs", {
    readFile: function (path, encoding, cb) {
        cb(null, "Success!");
    }
});
myModule.readSomethingFromFileSystem(function (err, data) {
    console.log(data); // = Success!
});

我受到 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:

var rewire = require("rewire"),
    myModule = rewire("./path/to/myModule.js"); // exactly like require()

// Your module will now export a special setter and getter for private variables.
myModule.__set__("myPrivateVar", 123);
myModule.__get__("myPrivateVar"); // = 123


// This allows you to mock almost everything within the module e.g. the fs-module.
// Just pass the variable name as first parameter and your mock as second.
myModule.__set__("fs", {
    readFile: function (path, encoding, cb) {
        cb(null, "Success!");
    }
});
myModule.readSomethingFromFileSystem(function (err, data) {
    console.log(data); // = Success!
});

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 using require() (except your modifications). Also debugging is fully supported.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文