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

Function normalizeResolvedIdToUrl

packages/vite/src/node/plugins/importAnalysis.ts:108–145  ·  view source on GitHub ↗
(
  environment: DevEnvironment,
  url: string,
  resolved: PartialResolvedId,
)

Source from the content-addressed store, hash-verified

106}
107
108function normalizeResolvedIdToUrl(
109 environment: DevEnvironment,
110 url: string,
111 resolved: PartialResolvedId,
112): string {
113 const root = environment.config.root
114 const depsOptimizer = environment.depsOptimizer
115
116 // normalize all imports into resolved URLs
117 // e.g. `import 'foo'` -> `import '/@fs/.../node_modules/foo/index.js'`
118 if (resolved.id.startsWith(withTrailingSlash(root))) {
119 // in root: infer short absolute path from root
120 url = resolved.id.slice(root.length)
121 } else if (
122 depsOptimizer?.isOptimizedDepFile(resolved.id) ||
123 // vite-plugin-react isn't following the leading \0 virtual module convention.
124 // This is a temporary hack to avoid expensive fs checks for React apps.
125 // We'll remove this as soon we're able to fix the react plugins.
126 (resolved.id !== '/@react-refresh' &&
127 path.isAbsolute(resolved.id) &&
128 fs.existsSync(cleanUrl(resolved.id)))
129 ) {
130 // an optimized deps may not yet exists in the filesystem, or
131 // a regular file exists but is out of root: rewrite to absolute /@fs/ paths
132 url = path.posix.join(FS_PREFIX, resolved.id)
133 } else {
134 url = resolved.id
135 }
136
137 // if the resolved id is not a valid browser import specifier,
138 // prefix it to make it valid. We will strip this before feeding it
139 // back into the transform pipeline
140 if (url[0] !== '.' && url[0] !== '/') {
141 url = wrapId(resolved.id)
142 }
143
144 return url
145}
146
147function extractImportedBindings(
148 id: string,

Callers 1

normalizeUrlFunction · 0.85

Calls 3

withTrailingSlashFunction · 0.90
cleanUrlFunction · 0.90
wrapIdFunction · 0.90

Tested by

no test coverage detected