Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 30 additions & 25 deletions packages/core/src/plugins/appIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, HtmlBasicTag[]>();
const iconFormatMap = new Map<string, IconExtra>();
const iconFormatMap = new Map<string, AppIconItem & IconExtra>();

const formatIcon = (
icon: AppIconItem,
Expand All @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -151,7 +153,7 @@ export const pluginAppIcon = (): RsbuildPlugin => ({
attrs: {
rel: 'apple-touch-icon',
sizes: icon.sizes,
href: icon.requestPath,
href: icon.src,
},
});
}
Expand All @@ -164,7 +166,7 @@ export const pluginAppIcon = (): RsbuildPlugin => ({
)
.map((icon) => {
const result = {
src: icon.requestPath,
src: icon.src,
sizes: icon.sizes,
};
if (icon.mimeType) {
Expand Down Expand Up @@ -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);
},
});
Loading