MCPcopy
hub / github.com/vitejs/vite / getNodeModulesPackageRoot

Function getNodeModulesPackageRoot

packages/vite/src/node/server/sourcemap.ts:25–45  ·  packages/vite/src/node/server/sourcemap.ts::getNodeModulesPackageRoot
(
  filePath: string,
)

Source from the content-addressed store, hash-verified

23 * Returns `undefined` if the file is not inside node_modules.
24 */
25export function getNodeModulesPackageRoot(
26 filePath: string,
27): string | undefined {
28 const normalized = normalizePath(filePath)
29 const nodeModulesIndex = normalized.lastIndexOf(class="st">'/node_modules/')
30 if (nodeModulesIndex === -1) return undefined
31
32 const packageStart = nodeModulesIndex + class="st">'/node_modules/'.length
33 const rest = normalized.slice(packageStart)
34 const firstSlash = rest.indexOf(class="st">'/')
35
36 let packageName: string
37 if (rest.startsWith(class="st">'@')) {
38 class="cm">// scoped package: @scope/pkg
39 const secondSlash = rest.indexOf(class="st">'/', firstSlash + 1)
40 packageName = secondSlash === -1 ? rest : rest.slice(0, secondSlash)
41 } else {
42 packageName = firstSlash === -1 ? rest : rest.slice(0, firstSlash)
43 }
44 return normalized.slice(0, packageStart) + packageName
45}
46
47class="cm">// Virtual modules should be prefixed with a null byte to avoid a
48class="cm">// false positive class="st">"missing source" warning. We also check for certain

Callers 3

sourcemap.spec.tsFile · 0.90
injectSourcesContentFunction · 0.85

Calls 1

normalizePathFunction · 0.90

Tested by

no test coverage detected