renderAssetUrlInJS() — vite Function Reference
Architecture documentation for the renderAssetUrlInJS() function in asset.ts from the vite codebase.
Entity Profile
Dependency Diagram
graph TD edf78a01_8215_eb46_3896_3db717c46747["renderAssetUrlInJS()"] e71b94ef_3010_e358_13d8_f3b3acb0a268["asset.ts"] edf78a01_8215_eb46_3896_3db717c46747 -->|defined in| e71b94ef_3010_e358_13d8_f3b3acb0a268 2b04bf20_6455_3250_24f8_b60c71116be3["assetPlugin()"] 2b04bf20_6455_3250_24f8_b60c71116be3 -->|calls| edf78a01_8215_eb46_3896_3db717c46747 cd131d16_e223_ab79_1b7c_8ea449ae51a2["cssPostPlugin()"] cd131d16_e223_ab79_1b7c_8ea449ae51a2 -->|calls| edf78a01_8215_eb46_3896_3db717c46747 c25246ea_7a11_06af_dc93_7717f85216db["createToImportMetaURLBasedRelativeRuntime()"] edf78a01_8215_eb46_3896_3db717c46747 -->|calls| c25246ea_7a11_06af_dc93_7717f85216db 10b9dea8_362c_1af2_93be_afa4dd9aed9e["cleanUrl()"] edf78a01_8215_eb46_3896_3db717c46747 -->|calls| 10b9dea8_362c_1af2_93be_afa4dd9aed9e 0b9e0d55_d22c_da82_ca1d_a2e7d0af1d66["toOutputFilePathInJS()"] edf78a01_8215_eb46_3896_3db717c46747 -->|calls| 0b9e0d55_d22c_da82_ca1d_a2e7d0af1d66 41d0f7a0_ed36_9f0f_d0d6_f403e4f50763["encodeURIPath()"] edf78a01_8215_eb46_3896_3db717c46747 -->|calls| 41d0f7a0_ed36_9f0f_d0d6_f403e4f50763 51afdf58_3045_64b1_cf5b_929b1091e877["get()"] edf78a01_8215_eb46_3896_3db717c46747 -->|calls| 51afdf58_3045_64b1_cf5b_929b1091e877 b1f5b07b_f692_69cd_1795_627055928bb7["getTopLevelConfig()"] edf78a01_8215_eb46_3896_3db717c46747 -->|calls| b1f5b07b_f692_69cd_1795_627055928bb7 style edf78a01_8215_eb46_3896_3db717c46747 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/vite/src/node/plugins/asset.ts lines 76–147
export function renderAssetUrlInJS(
pluginContext: PluginContext,
chunk: RenderedChunk,
opts: NormalizedOutputOptions,
code: string,
): MagicString | undefined {
const { environment } = pluginContext
const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(
opts.format,
environment.config.isWorker,
)
let match: RegExpExecArray | null
let s: MagicString | undefined
// Urls added with JS using e.g.
// imgElement.src = "__VITE_ASSET__5aA0Ddc0__" are using quotes
// Urls added in CSS that is imported in JS end up like
// var inlined = ".inlined{color:green;background:url(__VITE_ASSET__5aA0Ddc0__)}\n";
// In both cases, the wrapping should already be fine
assetUrlRE.lastIndex = 0
while ((match = assetUrlRE.exec(code))) {
s ||= new MagicString(code)
const [full, referenceId, postfix = ''] = match
const file = pluginContext.getFileName(referenceId)
chunk.viteMetadata!.importedAssets.add(cleanUrl(file))
const filename = file + postfix
const replacement = toOutputFilePathInJS(
environment,
filename,
'asset',
chunk.fileName,
'js',
toRelativeRuntime,
)
const replacementString =
typeof replacement === 'string'
? JSON.stringify(encodeURIPath(replacement)).slice(1, -1)
: `"+${replacement.runtime}+"`
s.update(match.index, match.index + full.length, replacementString)
}
// Replace __VITE_PUBLIC_ASSET__5aA0Ddc0__ with absolute paths
const publicAssetUrlMap = publicAssetUrlCache.get(
environment.getTopLevelConfig(),
)!
publicAssetUrlRE.lastIndex = 0
while ((match = publicAssetUrlRE.exec(code))) {
s ||= new MagicString(code)
const [full, hash] = match
const publicUrl = publicAssetUrlMap.get(hash)!.slice(1)
const replacement = toOutputFilePathInJS(
environment,
publicUrl,
'public',
chunk.fileName,
'js',
toRelativeRuntime,
)
const replacementString =
typeof replacement === 'string'
? JSON.stringify(encodeURIPath(replacement)).slice(1, -1)
: `"+${replacement.runtime}+"`
s.update(match.index, match.index + full.length, replacementString)
}
return s
}
Domain
Subdomains
Defined In
Calls
Called By
Source
Frequently Asked Questions
What does renderAssetUrlInJS() do?
renderAssetUrlInJS() is a function in the vite codebase, defined in packages/vite/src/node/plugins/asset.ts.
Where is renderAssetUrlInJS() defined?
renderAssetUrlInJS() is defined in packages/vite/src/node/plugins/asset.ts at line 76.
What does renderAssetUrlInJS() call?
renderAssetUrlInJS() calls 6 function(s): cleanUrl, createToImportMetaURLBasedRelativeRuntime, encodeURIPath, get, getTopLevelConfig, toOutputFilePathInJS.
What calls renderAssetUrlInJS()?
renderAssetUrlInJS() is called by 2 function(s): assetPlugin, cssPostPlugin.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free