MCPcopy
hub / github.com/vitest-dev/vitest / parseModule

Function parseModule

packages/mocker/src/node/parsers.ts:76–121  ·  view source on GitHub ↗
(name: string)

Source from the content-addressed store, hash-verified

74 }
75
76 function parseModule(name: string): string[] {
77 if (isBuiltin(name)) {
78 if (cachedFileExports.has(name)) {
79 const cachedExports = cachedFileExports.get(name)!
80 return cachedExports
81 }
82
83 const builtinModule = getBuiltinModule(name)
84 const builtinExports = Object.keys(builtinModule)
85 cachedFileExports.set(name, builtinExports)
86 return builtinExports
87 }
88
89 const resolvedModuleUrl = format === 'module'
90 ? import.meta.resolve(name, pathToFileURL(filename).toString())
91 : getModuleRequire().resolve(name)
92
93 const resolvedModulePath = format === 'commonjs'
94 ? resolvedModuleUrl
95 : fileURLToPath(resolvedModuleUrl)
96
97 if (cachedFileExports.has(resolvedModulePath)) {
98 return cachedFileExports.get(resolvedModulePath)!
99 }
100
101 const fileContent = readFileSync(resolvedModulePath, 'utf-8')
102 const ext = extname(resolvedModulePath)
103 const code = transformCode(fileContent, resolvedModulePath)
104 if (code == null) {
105 cachedFileExports.set(resolvedModulePath, [])
106 return []
107 }
108
109 const resolvedModuleFormat = resolveModuleFormat(resolvedModulePath, code)
110 if (ext === '.json') {
111 return ['default']
112 }
113 else {
114 // can't do wasm, for example
115 console.warn(`Cannot process '${resolvedModuleFormat}' imported from ${filename} because of unknown file extension: ${ext}.`)
116 }
117 if (resolvedModuleFormat) {
118 return collectModuleExports(resolvedModulePath, code, resolvedModuleFormat, exports)
119 }
120 return []
121 }
122
123 return Array.from(new Set(exports))
124}

Callers 4

parseConfigModuleMethod · 0.85
parseConfigModuleMethod · 0.85
tryParseModuleFunction · 0.85

Calls 14

isBuiltinFunction · 0.85
getModuleRequireFunction · 0.85
readFileSyncFunction · 0.85
resolveModuleFormatFunction · 0.85
collectModuleExportsFunction · 0.85
keysMethod · 0.80
warnMethod · 0.80
getBuiltinModuleFunction · 0.70
transformCodeFunction · 0.70
hasMethod · 0.45
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected