// If has inline query, unconditionally inline the asset if (inlineRE.test(id)) { const file = publicFile || cleanUrl(id) const content = await fsp.readFile(file) returnassetToDataURL(environment, file, content) }
// If is svg and it's inlined in build, also inline it in dev to match // the behaviour in build due to quote handling differences. if (svgExtRE.test(id)) { const file = publicFile || cleanUrl(id) const content = await fsp.readFile(file) if (shouldInline(environment, file, id, content, undefined, undefined)) { returnassetToDataURL(environment, file, content) } }
letrtn: string if (publicFile) { // in public dir during dev, keep the url as-is rtn = id } elseif (id.startsWith(withTrailingSlash(config.root))) { // in project root, infer short public path rtn = '/' + path.posix.relative(config.root, id) } else { // outside of project root, use absolute fs path // (this is special handled by the serve static middleware rtn = path.posix.join(FS_PREFIX, id) } if (skipBase) { return rtn } const base = joinUrlSegments(config.server.origin ?? '', config.decodedBase) returnjoinUrlSegments(base, removeLeadingSlash(rtn)) }
// resolve const id = module?.id ?? resolved?.id ?? url
module ??= environment.moduleGraph.getModuleById(id) if (module) { // if a different url maps to an existing loaded id, make sure we relate this url to the id await environment.moduleGraph._ensureEntryFromUrl(url, undefined, resolved) // try use cache from id const cached = awaitgetCachedTransformResult( environment, url, module, timestamp, ) if (cached) return cached }
// If has inline query, unconditionally inline the asset if (inlineRE.test(id)) { const file = publicFile || cleanUrl(id) const content = await fsp.readFile(file) returnassetToDataURL(environment, file, content) }
// If is svg and it's inlined in build, also inline it in dev to match // the behaviour in build due to quote handling differences. if (svgExtRE.test(id)) { const file = publicFile || cleanUrl(id) const content = await fsp.readFile(file) if (shouldInline(environment, file, id, content, undefined, undefined)) { returnassetToDataURL(environment, file, content) } }
// If has inline query, unconditionally inline the asset if (inlineRE.test(id)) { const file = publicFile || cleanUrl(id) const content = await fsp.readFile(file) returnassetToDataURL(environment, file, content) }
// If is svg and it's inlined in build, also inline it in dev to match // the behaviour in build due to quote handling differences. if (svgExtRE.test(id)) { const file = publicFile || cleanUrl(id) const content = await fsp.readFile(file) if (shouldInline(environment, file, id, content, undefined, undefined)) { returnassetToDataURL(environment, file, content) } }
letrtn: string if (publicFile) { // in public dir during dev, keep the url as-is rtn = id } elseif (id.startsWith(withTrailingSlash(config.root))) { // in project root, infer short public path rtn = '/' + path.posix.relative(config.root, id) } else { // outside of project root, use absolute fs path // (this is special handled by the serve static middleware rtn = path.posix.join(FS_PREFIX, id) } if (skipBase) { return rtn } const base = joinUrlSegments(config.server.origin ?? '', config.decodedBase) returnjoinUrlSegments(base, removeLeadingSlash(rtn)) }
functionshouldInline( environment: Environment, file: string, id: string, content: Buffer, /** Should be passed only in build */ buildPluginContext: PluginContext | undefined, forceInline: boolean | undefined, ): boolean { if (noInlineRE.test(id)) returnfalse if (inlineRE.test(id)) returntrue // Do build only checks if passed the plugin context during build if (buildPluginContext) { if (environment.config.build.lib) returntrue if (buildPluginContext.getModuleInfo(id)?.isEntry) returnfalse } if (forceInline !== undefined) return forceInline if (file.endsWith('.html')) returnfalse // Don't inline SVG with fragments, as they are meant to be reused if (file.endsWith('.svg') && id.includes('#')) returnfalse letlimit: number const { assetsInlineLimit } = environment.config.build if (typeof assetsInlineLimit === 'function') { const userShouldInline = assetsInlineLimit(file, content) if (userShouldInline != null) return userShouldInline limit = DEFAULT_ASSETS_INLINE_LIMIT } else { limit = Number(assetsInlineLimit) } return content.length < limit && !isGitLfsPlaceholder(content) }