Skip to content

Commit c4a67f2

Browse files
committed
Add __resolvePath method for module path string normalization
New method __resolvePath implements path normalization to simplify requiring modules in NodeJS manner (require('moduleName') without '.js')
1 parent a77e322 commit c4a67f2

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

fs/polyfill.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,21 @@
22
let console = {log: print, error: print};
33
let module = {exports: null, _cache: {}};
44

5+
function __resolvePath(p) {
6+
let sf = '.js';
7+
let f = p.length - 3;
8+
let e = p.indexOf(sf, f < 0 ? 0 : f);
9+
if (e === -1) {
10+
return p + sf;
11+
}
12+
13+
return p;
14+
}
15+
516
function require(path) {
17+
// Add .js if not set
18+
path = __resolvePath(path);
19+
620
// Prevent duplicate load, return from cache
721
let c = module._cache[path];
822
if (c !== undefined) {

0 commit comments

Comments
 (0)