[lomod] 一个用来加载项目内部模块的的工具
发布于 10 年前 作者 withinsea 3782 次浏览 最后一次编辑是 8 年前 来自 分享

先扔传送门:https://www.npmjs.org/package/lomod

碰到的问题与希望的解决方式

  1. 项目里有一些 utils 之类的通用模块需要从各处加载
  2. “../”,“./” 之类相对路径不够方便;常见的预先设定 proj_root,然后写从它开始的绝对路径还是不够方便
  3. 最好还是像 require 寻找 node_modules 一样,自动从多个可能的位置发现模块
  4. 但也不能直接放进 node_modules 里,因为通常这里的都被 .gitignore 掉了
  5. 所以我要个跟 node_modules 相似地位的另一个文件夹,比如说,各目录下的 lib/
  6. 只有非常必要的时候,才增加额外的功能

以上。如果你对此有同感,那么这个工具是为你写的。 以下拷贝自 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.

回到顶部