diff --git a/packages/core/src/plugins/appIcon.ts b/packages/core/src/plugins/appIcon.ts index 3a794f1299..e565824008 100644 --- a/packages/core/src/plugins/appIcon.ts +++ b/packages/core/src/plugins/appIcon.ts @@ -12,20 +12,24 @@ import { import type { AppIconItem, HtmlBasicTag, RsbuildPlugin } from '../types'; type IconExtra = { + src: string; sizes: string; - isURL?: boolean; - absolutePath: string; - relativePath: string; - requestPath: string; mimeType?: string; -}; +} & ( + | { isURL: true } + | { + isURL: false; + absolutePath: string; + relativePath: string; + } +); export const pluginAppIcon = (): RsbuildPlugin => ({ name: 'rsbuild:app-icon', setup(api) { const htmlTagsMap = new Map(); - const iconFormatMap = new Map(); + const iconFormatMap = new Map(); const formatIcon = ( icon: AppIconItem, @@ -36,24 +40,22 @@ export const pluginAppIcon = (): RsbuildPlugin => ({ const cached = iconFormatMap.get(src); if (cached) { - return { ...cached, ...icon }; + return cached; } const sizes = `${size}x${size}`; if (isURL(src)) { - const paths = { + const formatted = { + ...icon, + src, sizes, - isURL: true, - requestPath: src, - absolutePath: src, - relativePath: src, + isURL: true as const, mimeType: lookup(src), }; - iconFormatMap.set(src, paths); - - return { ...paths, ...icon }; + iconFormatMap.set(src, formatted); + return formatted; } const absolutePath = path.isAbsolute(src) @@ -63,19 +65,19 @@ export const pluginAppIcon = (): RsbuildPlugin => ({ distDir, path.basename(absolutePath), ); - const requestPath = ensureAssetPrefix(relativePath, publicPath); - const paths = { + const formatted = { + ...icon, sizes, - requestPath, + src: ensureAssetPrefix(relativePath, publicPath), + isURL: false as const, absolutePath, relativePath, mimeType: lookup(absolutePath), }; - iconFormatMap.set(src, paths); - - return { ...paths, ...icon }; + iconFormatMap.set(src, formatted); + return formatted; }; api.processAssets( @@ -151,7 +153,7 @@ export const pluginAppIcon = (): RsbuildPlugin => ({ attrs: { rel: 'apple-touch-icon', sizes: icon.sizes, - href: icon.requestPath, + href: icon.src, }, }); } @@ -164,7 +166,7 @@ export const pluginAppIcon = (): RsbuildPlugin => ({ ) .map((icon) => { const result = { - src: icon.requestPath, + src: icon.src, sizes: icon.sizes, }; if (icon.mimeType) { @@ -206,9 +208,12 @@ export const pluginAppIcon = (): RsbuildPlugin => ({ return { headTags, bodyTags }; }); - api.onCloseDevServer(() => { + const clean = () => { htmlTagsMap.clear(); iconFormatMap.clear(); - }); + }; + + api.onCloseDevServer(clean); + api.onCloseBuild(clean); }, });