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

Function buildHtmlPlugin

packages/vite/src/node/plugins/html.ts:413–1083  ·  view source on GitHub ↗
(config: ResolvedConfig)

Source from the content-addressed store, hash-verified

411 * Compiles index.html into an entry js module
412 */
413export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
414 const [preHooks, normalHooks, postHooks] = resolveHtmlTransforms(
415 config.plugins,
416 )
417 preHooks.unshift(injectCspNonceMetaTagHook(config))
418 preHooks.unshift(preImportMapHook(config))
419 preHooks.push(htmlEnvHook(config))
420 postHooks.push(injectNonceAttributeTagHook(config))
421 postHooks.push(postImportMapHook(config))
422 const processedHtml = perEnvironmentState(() => new Map<string, string>())
423
424 const isExcludedUrl = (url: string) =>
425 url[0] === '#' || isExternalUrl(url) || isDataUrl(url)
426
427 // Same reason with `htmlInlineProxyPlugin`
428 isAsyncScriptMap.set(config, new Map())
429
430 return {
431 name: 'vite:build-html',
432
433 applyToEnvironment(environment) {
434 return environment.config.isBundled
435 },
436
437 transform: {
438 filter: { id: /\.html$/ },
439 async handler(html, id) {
440 id = normalizePath(id)
441 const relativeUrlPath = normalizePath(path.relative(config.root, id))
442 const publicPath = `/${relativeUrlPath}`
443 const publicBase = getBaseInHTML(relativeUrlPath, config)
444
445 const publicToRelative = (filename: string) => publicBase + filename
446 const toOutputPublicFilePath = (url: string) =>
447 toOutputFilePathInHtml(
448 url.slice(1),
449 'public',
450 relativeUrlPath,
451 'html',
452 config,
453 publicToRelative,
454 )
455 // Determines true start position for the node, either the < character
456 // position, or the newline at the end of the previous line's node.
457 const nodeStartWithLeadingWhitespace = (
458 node: DefaultTreeAdapterMap['node'],
459 ) => {
460 const startOffset = node.sourceCodeLocation!.startOffset
461 if (startOffset === 0) return 0
462
463 // Gets the offset for the start of the line including the
464 // newline trailing the previous node
465 const lineStartOffset =
466 startOffset - node.sourceCodeLocation!.startCol
467
468 // <previous-line-node></previous-line-node>
469 // <target-node></target-node>
470 //

Callers 1

resolvePluginsFunction · 0.90

Calls 8

perEnvironmentStateFunction · 0.90
resolveHtmlTransformsFunction · 0.85
preImportMapHookFunction · 0.85
htmlEnvHookFunction · 0.85
postImportMapHookFunction · 0.85
setMethod · 0.45

Tested by

no test coverage detected