MCPcopy Create free account
hub / github.com/stenciljs/core / resolveModule

Method resolveModule

src/sys/node/node-resolve-module.ts:10–56  ·  view source on GitHub ↗
(fromDir: string, moduleId: string, opts?: d.ResolveModuleOptions)

Source from the content-addressed store, hash-verified

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('/');

Callers 2

ensureMethod · 0.80
getModulePathMethod · 0.80

Calls 9

resolveModuleManuallyMethod · 0.95
resolveTypesModuleMethod · 0.95
normalizePathFunction · 0.85
resolveMethod · 0.80
joinMethod · 0.80
parseMethod · 0.80
dirnameMethod · 0.80
getMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected