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

Function devHtmlHook

packages/vite/src/node/server/middlewares/indexHtml.ts:204–449  ·  view source on GitHub ↗
(
  html,
  { path: htmlPath, filename, server, originalUrl },
)

Source from the content-addressed store, hash-verified

202 return processedUrl
203}
204const devHtmlHook: IndexHtmlTransformHook = async (
205 html,
206 { path: htmlPath, filename, server, originalUrl },
207) => {
208 const { config, watcher } = server!
209 const base = config.base || '/'
210 const decodedBase = config.decodedBase || '/'
211
212 let proxyModulePath: string
213 let proxyModuleUrl: string
214
215 const trailingSlash = htmlPath.endsWith('/')
216 if (!trailingSlash && fs.existsSync(filename)) {
217 // If htmlPath is a /@fs/ URL (e.g. vitest-browser always uses this form
218 // for testerHtmlPath), normalise to an absolute FS path so proxyCacheUrl
219 // is always root-relative.
220 proxyModulePath = htmlPath.startsWith(FS_PREFIX) ? filename : htmlPath
221 proxyModuleUrl = htmlPath
222 } else {
223 // There are users of vite.transformIndexHtml calling it with url '/'
224 // for SSR integrations #7993, filename is root for this case
225 // A user may also use a valid name for a virtual html file
226 // Mark the path as virtual in both cases so sourcemaps aren't processed
227 // and ids are properly handled
228 const validPath = `${htmlPath}${trailingSlash ? 'index.html' : ''}`
229 proxyModulePath = `\0${validPath}`
230 proxyModuleUrl = wrapId(proxyModulePath)
231 }
232 proxyModuleUrl = joinUrlSegments(decodedBase, proxyModuleUrl)
233
234 const s = new MagicString(html)
235 let inlineModuleIndex = -1
236 // The key to the proxyHtml cache is decoded, as it will be compared
237 // against decoded URLs by the HTML plugins.
238 const proxyCacheUrl = decodeURI(
239 cleanUrl(proxyModulePath).replace(normalizePath(config.root), ''),
240 )
241 const styleUrl: AssetNode[] = []
242 const inlineStyles: InlineStyleAttribute[] = []
243 const inlineModulePaths: string[] = []
244
245 const addInlineModule = (
246 node: DefaultTreeAdapterMap['element'],
247 ext: 'js',
248 ) => {
249 inlineModuleIndex++
250
251 const contentNode = node.childNodes[0] as DefaultTreeAdapterMap['textNode']
252
253 const code = contentNode.value
254
255 let map: SourceMapInput | undefined
256 if (proxyModulePath[0] !== '\0') {
257 map = new MagicString(html)
258 .snip(
259 contentNode.sourceCodeLocation!.startOffset,
260 contentNode.sourceCodeLocation!.endOffset,
261 )

Callers

nothing calls this directly

Calls 15

wrapIdFunction · 0.90
joinUrlSegmentsFunction · 0.90
cleanUrlFunction · 0.90
normalizePathFunction · 0.90
traverseHtmlFunction · 0.90
nodeIsElementFunction · 0.90
getScriptInfoFunction · 0.90
removeViteIgnoreAttrFunction · 0.90
overwriteAttrValueFunction · 0.90
getNodeAssetAttributesFunction · 0.90

Tested by

no test coverage detected