Nodejs lomod:一个用来加载项目内部模块的工具
Nodejs lomod:一个用来加载项目内部模块的工具
先扔传送门:https://www.npmjs.org/package/lomod
碰到的问题与希望的解决方式
- 项目里有一些 utils 之类的通用模块需要从各处加载
- “../”,“./” 之类相对路径不够方便;常见的预先设定 proj_root,然后写从它开始的绝对路径还是不够方便
- 最好还是像 require 寻找 node_modules 一样,自动从多个可能的位置发现模块
- 但也不能直接放进 node_modules 里,因为通常这里的都被 .gitignore 掉了
- 所以我要个跟 node_modules 相似地位的另一个文件夹,比如说,各目录下的 lib/
- 只有非常必要的时候,才增加额外的功能
以上。如果你对此有同感,那么这个工具是为你写的。 以下拷贝自 README.md:
lomod
Loading modules from not only ‘node_modules’ but also ‘lib’ folders, which usually contains project’s local modules.
Install
npm install lomod
Usage
project/
+ common/
| + lib/
| + common-util.js
+ app/
+ lib/
| + app-util.js
+ submod/
| + lib/
| | + libdir/
| | | + dir-util.js
| | + sub-util.js
| + test.js
+ package.json
app/submod/test.js
var lomod = require('lomod');
// use as a replacement of original require
var fs = lomod('fs');
var sutil = lomod('sub-util');
var dutil = lomod('libdir/dir-util');
var autil = lomod('app-util');
var cutil = lomod('common-util');
app/package.json
{
"name": "proj-app",
"version": "1.0.0",
"localDependencies": [
"../common"
]
}
Looking-up Modules
Any module identifier passed to lomod() will be tried on original require first.
If this failed, and the module identifier does not begin with ‘/’, ‘…/’, or ‘./’, then lomod starts at the current directory, adds /lib, and attempts to load the module from that location.
If it is not found there, then it moves to the parent directory, and so on, until the root of the tree is reached, or a package.json with property localDependencies was found (check the next chapter).
For example, if the file at ‘/home/ry/projects/foo.js’ called lomod(‘bar.js’), then lomod would look in the following locations, in this order:
try to require('bar.js')
/home/ry/projects/lib/bar.js
/home/ry/lib/bar.js
/home/lib/bar.js
/lib/bar.js
This is almost same with the original require, just consider it as you got another group of module directories named ‘lib’.
Local Dependencies
A “localDependencies” property with string array value in package.json stop the recusive moving to parent directory. Instead of it, lomod start looking up from each path in this array.
For example, if the file at ‘/home/ry/projects/foo.js’ called lomod(‘bar.js’), and the file at ‘/home/ry/package.json’ contains localDependencies assigned <code>[’/share’, ‘/usr/share’]</code>, then lomod would look in the following locations, in this order:
try to require('bar.js')
/home/ry/projects/lib/bar.js
/home/ry/lib/bar.js (stop moving to parent, go to localDependencies)
/share/lib/bar.js
/lib/bar.js
/usr/share/lib/bar.js
/usr/lib/bar.js
/lib/bar.js (has been scaned, ignore)
Lomod ignored ‘/home/lib/bar.js’ in this example. You can simply prevent it by append ‘…’ to the localDependencies array.
Support Other File Formats
Any format supported by original require will be inherited to lomod.
project/
+ node_modules/
| + res.yaml
+ lib/
| + lores.yaml
+ test.js
test.js
require('require-yaml');
var res = require('res');
var lomod = require('lomod'),
lores = lomod('lores');
some format modules by olalonde
These modules support extra formats by adding handlers to require.extensions which is a deprecated feature in node’s module system. Since the module system is locked, this feature will probably never go away but may have subtle bugs. Use on your own risk.
- better-require (a multiple formats bundle)
- require-yaml
- require-csv
- require-xml
- require-ini
Nodejs lomod:一个用来加载项目内部模块的工具
先扔传送门:
碰到的问题与希望的解决方式
- 项目里有一些
utils
之类的通用模块需要从各处加载。 - “…/”,“./” 之类的相对路径不够方便;常见的预先设定
proj_root
,然后写从它开始的绝对路径还是不够方便。 - 最好还是像
require
寻找node_modules
一样,自动从多个可能的位置发现模块。 - 但也不能直接放进
node_modules
里,因为通常这里的都被.gitignore
掉了。 - 所以我要个跟
node_modules
相似地位的另一个文件夹,比如说,各目录下的lib/
。 - 只有非常必要的时候,才增加额外的功能。
以上。如果你对此有同感,那么这个工具是为你写的。
以下拷贝自 README.md:
lomod
Loading modules from not only ‘node_modules’ but also ‘lib’ folders, which usually contains project’s local modules.
Install
npm install lomod
Usage
假设项目结构如下:
project/
+ common/
| + lib/
| + common-util.js
+ app/
+ lib/
| + app-util.js
+ submod/
| + lib/
| | + libdir/
| | | + dir-util.js
| | + sub-util.js
| + test.js
+ package.json
app/submod/test.js
var lomod = require('lomod');
// 使用 lomod 替换原始的 require
var fs = lomod('fs');
var sutil = lomod('sub-util');
var dutil = lomod('libdir/dir-util');
var autil = lomod('app-util');
var cutil = lomod('common-util');
app/package.json
{
"name": "proj-app",
"version": "1.0.0",
"localDependencies": [
"../common"
]
}
Looking-up Modules
任何传递给 lomod()
的模块标识符首先会尝试使用原始的 require
加载。
如果失败,并且模块标识符不以 /
、../
或 ./
开头,则 lomod
从当前目录开始,添加 /lib
,并尝试从该位置加载模块。
如果没有找到,就移动到父目录,以此类推,直到到达树的根部,或者找到具有 localDependencies
属性的 package.json
文件(参见下一节)。
例如,如果文件 /home/ry/projects/foo.js
调用 lomod('bar.js')
,则 lomod
会按以下顺序查找:
try to require('bar.js')
/home/ry/projects/lib/bar.js
/home/ry/lib/bar.js
/home/lib/bar.js
/lib/bar.js
这几乎与原始的 require
相同,只是你可以将其视为多一组名为 lib
的模块目录。
Local Dependencies
package.json
中包含字符串数组值的 localDependencies
属性会停止递归地移动到父目录。相反,lomod
会从这个数组中的每个路径开始查找。
例如,如果文件 /home/ry/projects/foo.js
调用 lomod('bar.js')
,并且文件 /home/ry/package.json
包含 localDependencies
赋值 [ '/share', '/usr/share' ]
,则 lomod
会按以下顺序查找:
try to require('bar.js')
/home/ry/projects/lib/bar.js
/home/ry/lib/bar.js (停止移动到父目录,转到 localDependencies)
/share/lib/bar.js
/lib/bar.js
/usr/share/lib/bar.js
/usr/lib/bar.js
/lib/bar.js (已经扫描过,忽略)
Support Other File Formats
任何由原始 require
支持的格式也会被 lomod
继承。
project/
+ node_modules/
| + res.yaml
+ lib/
| + lores.yaml
+ test.js
test.js
require('require-yaml');
var res = require('res');
var lomod = require('lomod'),
lores = lomod('lores');
一些格式模块由 olalonde 提供
这些模块通过向 require.extensions
添加处理程序来支持额外的格式,这是 node
模块系统的弃用功能。由于模块系统是锁定的,此功能可能永远不会消失,但可能会有微妙的错误。请自行承担风险使用。