|
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'; |
8 | 8 |
|
9 | 9 | /*----------------------------------------------------------------------------*/
|
10 | 10 |
|
11 | 11 | export default class ModuleCache extends MapCache {
|
12 | 12 | constructor(moduleRoot) {
|
13 |
| - super() |
| 13 | + super(); |
14 | 14 |
|
15 |
| - moduleRoot = _.toString(moduleRoot) |
16 |
| - |
17 |
| - if (! moduleRoot) { |
18 |
| - return |
| 15 | + moduleRoot = _.toString(moduleRoot); |
| 16 | + if (!moduleRoot) { |
| 17 | + return; |
19 | 18 | }
|
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))); |
24 | 22 |
|
25 | 23 | // Sort paths by the “main” entry first.
|
26 | 24 | const dirPaths = _.orderBy(glob.sync(path.join(moduleRoot, '**/'), {
|
27 | 25 | '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 | + }); |
39 | 37 | }
|
40 | 38 |
|
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 | + } |
56 | 55 | }
|
57 |
| - } |
58 |
| - |
59 |
| - return dirs.join('/') |
| 56 | + return dirs.join('/'); |
| 57 | + } catch (e) {} |
| 58 | + return ''; |
60 | 59 | }
|
61 |
| -} |
| 60 | +}; |
0 commit comments