node html 模块,CommonJS 模块

总结

要获取调用 require() 时将加载的确切文件名,则使用 require.resolve() 函数。

综上所述,这里是 require() 的伪代码高级算法:

require(X) from module at path Y

1. If X is a core module,

a. return the core module

b. STOP

2. If X begins with '/'

a. set Y to be the filesystem root

3. If X begins with './' or '/' or '../'

a. LOAD_AS_FILE(Y + X)

b. LOAD_AS_DIRECTORY(Y + X)

c. THROW "not found"

4. If X begins with '#'

a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))

5. LOAD_PACKAGE_SELF(X, dirname(Y))

6. LOAD_NODE_MODULES(X, dirname(Y))

7. THROW "not found"

LOAD_AS_FILE(X)

1. If X is a file, load X as its file extension format. STOP

2. If X.js is a file, load X.js as JavaScript text. STOP

3. If X.json is a file, parse X.json to a JavaScript Object. STOP

4. If X.node is a file, load X.node as binary addon. STOP

LOAD_INDEX(X)

1. If X/index.js is a file, load X/index.js as JavaScript text. STOP

2. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP

3. If X/index.node is a file, load X/index.node as binary addon. STOP

LOAD_AS_DIRECTORY(X)

1. If X/package.json is a file,

a. Parse X/package.json, and look for "main" field.

b. If "main" is a falsy value, GOTO 2.

c. let M = X + (json main field)

d. LOAD_AS_FILE(M)

e. LOAD_INDEX(M)

f. LOAD_INDEX(X) DEPRECATED

g. THROW "not found"

2. LOAD_INDEX(X)

LOAD_NODE_MODULES(X, START)

1. let DIRS = NODE_MODULES_PATHS(START)

2. for each DIR in DIRS:

a. LOAD_PACKAGE_EXPORTS(X, DIR)

b. LOAD_AS_FILE(DIR/X)

c. LOAD_AS_DIRECTORY(DIR/X)

NODE_MODULES_PATHS(START)

1. let PARTS = path split(START)

2. let I = count of PARTS - 1

3. let DIRS = [GLOBAL_FOLDERS]

4. while I >= 0,

a. if PARTS[I] = "node_modules" CONTINUE

b. DIR = path join(PARTS[0 .. I] + "node_modules")

c. DIRS = DIRS + DIR

d. let I = I - 1

5. return DIRS

LOAD_PACKAGE_IMPORTS(X, DIR)

1. Find the closest package scope SCOPE to DIR.

2. If no scope was found, return.

3. If the SCOPE/package.json "imports" is null or undefined, return.

4. let MATCH = PACKAGE_IMPORTS_RESOLVE(X, pathToFileURL(SCOPE),

["node", "require"]) defined in the ESM resolver.

5. RESOLVE_ESM_MATCH(MATCH).

LOAD_PACKAGE_EXPORTS(X, DIR)

1. Try to interpret X as a combination of NAME and SUBPATH where the name

may have a @scope/ prefix and the subpath begins with a slash (`/`).

2. If X does not match this pattern or DIR/NAME/package.json is not a file,

return.

3. Parse DIR/NAME/package.json, and look for "exports" field.

4. If "exports" is null or undefined, return.

5. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME), "." + SUBPATH,

`package.json` "exports", ["node", "require"]) defined in the ESM resolver.

6. RESOLVE_ESM_MATCH(MATCH)

LOAD_PACKAGE_SELF(X, DIR)

1. Find the closest package scope SCOPE to DIR.

2. If no scope was found, return.

3. If the SCOPE/package.json "exports" is null or undefined, return.

4. If the SCOPE/package.json "name" is not the first segment of X, return.

5. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(SCOPE),

"." + X.slice("name".length), `package.json` "exports", ["node", "require"])

defined in the ESM resolver.

6. RESOLVE_ESM_MATCH(MATCH)

RESOLVE_ESM_MATCH(MATCH)

1. let { RESOLVED, EXACT } = MATCH

2. let RESOLVED_PATH = fileURLToPath(RESOLVED)

3. If EXACT is true,

a. If the file at RESOLVED_PATH exists, load RESOLVED_PATH as its extension

format. STOP

4. Otherwise, if EXACT is false,

a. LOAD_AS_FILE(RESOLVED_PATH)

b. LOAD_AS_DIRECTORY(RESOLVED_PATH)

5. THROW "not found"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值