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

Function toOutputFilePathInHtml

packages/plugin-legacy/src/index.ts:43–79  ·  view source on GitHub ↗
(
  filename: string,
  type: 'asset' | 'public',
  hostId: string,
  hostType: 'js' | 'css' | 'html',
  config: ResolvedConfig,
  toRelative: (filename: string, importer: string) => string,
)

Source from the content-addressed store, hash-verified

41// Duplicated from build.ts in Vite Core, at least while the feature is experimental
42// We should later expose this helper for other plugins to use
43function toOutputFilePathInHtml(
44 filename: string,
45 type: 'asset' | 'public',
46 hostId: string,
47 hostType: 'js' | 'css' | 'html',
48 config: ResolvedConfig,
49 toRelative: (filename: string, importer: string) => string,
50): string {
51 const { renderBuiltUrl } = config.experimental
52 let relative = config.base === '' || config.base === './'
53 if (renderBuiltUrl) {
54 const result = renderBuiltUrl(filename, {
55 hostId,
56 hostType,
57 type,
58 ssr: !!config.build.ssr,
59 })
60 if (typeof result === 'object') {
61 if (result.runtime) {
62 throw new Error(
63 `{ runtime: "${result.runtime}" } is not supported for assets in ${hostType} files: ${filename}`,
64 )
65 }
66 if (typeof result.relative === 'boolean') {
67 relative = result.relative
68 }
69 } else if (result) {
70 return result
71 }
72 }
73 if (relative && !config.build.ssr) {
74 return toRelative(filename, hostId)
75 } else {
76 // @ts-expect-error `decodedBase` is internal
77 return joinUrlSegments(config.decodedBase, filename)
78 }
79}
80function getBaseInHTML(urlRelativePath: string, config: ResolvedConfig) {
81 // Prefer explicit URL if defined for linking to assets and public files from HTML,
82 // even when base relative is specified

Callers 3

toAssetPathFromHtmlFunction · 0.85
toOutputPublicFilePathFunction · 0.85
toOutputFilePathFunction · 0.85

Calls 3

toRelativeFunction · 0.70
joinUrlSegmentsFunction · 0.70
renderBuiltUrlFunction · 0.50

Tested by

no test coverage detected