(fromDir: string, moduleId: string, opts?: d.ResolveModuleOptions)
| 8 | private resolveModuleCache = new Map<string, string>(); |
| 9 | |
| 10 | resolveModule(fromDir: string, moduleId: string, opts?: d.ResolveModuleOptions) { |
| 11 | const cacheKey = `${fromDir}:${moduleId}`; |
| 12 | |
| 13 | const cachedPath = this.resolveModuleCache.get(cacheKey); |
| 14 | if (cachedPath) { |
| 15 | return cachedPath; |
| 16 | } |
| 17 | |
| 18 | if (opts && opts.manuallyResolve) { |
| 19 | return this.resolveModuleManually(fromDir, moduleId, cacheKey); |
| 20 | } |
| 21 | |
| 22 | if (moduleId.startsWith('@types/')) { |
| 23 | return this.resolveTypesModule(fromDir, moduleId, cacheKey); |
| 24 | } |
| 25 | |
| 26 | const Module = require('module'); |
| 27 | |
| 28 | fromDir = path.resolve(fromDir); |
| 29 | const fromFile = path.join(fromDir, 'noop.js'); |
| 30 | |
| 31 | let dir = normalizePath( |
| 32 | Module._resolveFilename(moduleId, { |
| 33 | id: fromFile, |
| 34 | filename: fromFile, |
| 35 | paths: Module._nodeModulePaths(fromDir), |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | const root = normalizePath(path.parse(fromDir).root); |
| 40 | let packageJsonFilePath: string; |
| 41 | |
| 42 | while (dir !== root) { |
| 43 | dir = normalizePath(path.dirname(dir)); |
| 44 | packageJsonFilePath = path.join(dir, 'package.json'); |
| 45 | |
| 46 | if (!fs.existsSync(packageJsonFilePath)) { |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | this.resolveModuleCache.set(cacheKey, packageJsonFilePath); |
| 51 | |
| 52 | return packageJsonFilePath; |
| 53 | } |
| 54 | |
| 55 | throw new Error(`error loading "${moduleId}" from "${fromDir}"`); |
| 56 | } |
| 57 | |
| 58 | resolveTypesModule(fromDir: string, moduleId: string, cacheKey: string) { |
| 59 | const moduleSplt = moduleId.split('/'); |
no test coverage detected