Skip to content
This repository was archived by the owner on Sep 16, 2023. It is now read-only.

Commit 1911ac7

Browse files
committed
Revert "Quit swallowing errors while trying to find modules (#185)"
This reverts commit 421468b.
1 parent 8e6b2eb commit 1911ac7

File tree

1 file changed

+45
-46
lines changed

1 file changed

+45
-46
lines changed

src/ModuleCache.js

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,60 @@
1-
import _ from 'lodash'
2-
import fs from 'fs'
3-
import glob from 'glob'
4-
import MapCache from './MapCache'
5-
import Module from 'module'
6-
import { normalizePath } from './util'
7-
import path from 'path'
1+
import _ from 'lodash';
2+
import fs from 'fs';
3+
import glob from 'glob';
4+
import MapCache from './MapCache';
5+
import Module from 'module';
6+
import { normalizePath } from './util';
7+
import path from 'path';
88

99
/*----------------------------------------------------------------------------*/
1010

1111
export default class ModuleCache extends MapCache {
1212
constructor(moduleRoot) {
13-
super()
13+
super();
1414

15-
moduleRoot = _.toString(moduleRoot)
16-
17-
if (! moduleRoot) {
18-
return
15+
moduleRoot = _.toString(moduleRoot);
16+
if (!moduleRoot) {
17+
return;
1918
}
20-
21-
const pkgPath = path.join(moduleRoot, 'package.json')
22-
const pkgMain = fs.existsSync(pkgPath) && require(pkgPath).main || 'index.js'
23-
const mainPath = normalizePath(path.dirname(path.resolve(moduleRoot, pkgMain)))
19+
const pkgPath = path.join(moduleRoot, 'package.json');
20+
const pkgMain = fs.existsSync(pkgPath) && require(pkgPath).main || 'index.js';
21+
const mainPath = normalizePath(path.dirname(path.resolve(moduleRoot, pkgMain)));
2422

2523
// Sort paths by the “main” entry first.
2624
const dirPaths = _.orderBy(glob.sync(path.join(moduleRoot, '**/'), {
2725
'ignore': path.join(moduleRoot, 'node_modules/**/')
28-
}), dirPath => _.startsWith(dirPath, mainPath), ['desc'])
29-
30-
_.each(dirPaths, (dirPath) => {
31-
const base = path.relative(moduleRoot, dirPath)
32-
const filePaths = glob.sync(path.join(dirPath, '*.js'))
33-
const pairs = _.map(filePaths, (filePath) => {
34-
const name = path.basename(filePath, '.js')
35-
return [name.toLowerCase(), name]
36-
})
37-
this.set(base, new MapCache(pairs))
38-
})
26+
}), dirPath => _.startsWith(dirPath, mainPath), ['desc']);
27+
28+
_.each(dirPaths, dirPath => {
29+
const base = path.relative(moduleRoot, dirPath);
30+
const filePaths = glob.sync(path.join(dirPath, '*.js'));
31+
const pairs = _.map(filePaths, filePath => {
32+
const name = path.basename(filePath, '.js');
33+
return [name.toLowerCase(), name];
34+
});
35+
this.set(base, new MapCache(pairs));
36+
});
3937
}
4038

41-
static resolve(id, from = process.cwd()) {
42-
const dirs = path.dirname(Module._resolveFilename(id, _.assign(new Module, {
43-
'paths': Module._nodeModulePaths(from)
44-
}))).split(path.sep)
45-
46-
let { length } = dirs
47-
48-
while (length--) {
49-
const dirSub = dirs.slice(0, length + 1)
50-
const dirPath = dirSub.join('/')
51-
const pkgPath = path.join(dirPath, 'package.json')
52-
53-
if ((length && dirs[length - 1] === 'node_modules') ||
54-
(fs.existsSync(pkgPath) && require(pkgPath).name === id)) {
55-
return dirPath
39+
static resolve(id, from=process.cwd()) {
40+
try {
41+
const dirs = path.dirname(Module._resolveFilename(id, _.assign(new Module, {
42+
'paths': Module._nodeModulePaths(from)
43+
}))).split(path.sep);
44+
45+
let { length } = dirs;
46+
while (length--) {
47+
const dirSub = dirs.slice(0, length + 1);
48+
const dirPath = dirSub.join('/');
49+
const pkgPath = path.join(dirPath, 'package.json');
50+
51+
if ((length && dirs[length - 1] == 'node_modules') ||
52+
(fs.existsSync(pkgPath) && require(pkgPath).name == id)) {
53+
return dirPath;
54+
}
5655
}
57-
}
58-
59-
return dirs.join('/')
56+
return dirs.join('/');
57+
} catch (e) {}
58+
return '';
6059
}
61-
}
60+
};

0 commit comments

Comments
 (0)