Skip to content

Commit

Permalink
patch: use single generator instance, remove render bottleneck
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonwocky committed May 15, 2022
1 parent c1f997f commit 98eebd7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
11 changes: 7 additions & 4 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ export { default as presetWind } from "https://cdn.skypack.dev/@unocss/preset-wi
export { default as presetTypography } from "https://cdn.skypack.dev/@unocss/[email protected]";
export { default as presetIcons } from "https://cdn.skypack.dev/@unocss/[email protected]";

const _iconifyCache = new Map();
export const iconifyCollections = async (...sets: string[]) => {
const reqs = [], sourceUrl = "https://esm.sh/@iconify/[email protected]/json";
for (const set of sets) {
reqs.push(
fetch(`${sourceUrl}/${set}.json`)
.then((res) => res.json()).then((json) => [set, json]),
);
if (!_iconifyCache.has(set)) {
const req = fetch(`${sourceUrl}/${set}.json`)
.then((res) => res.json()).then((json) => [set, json]);
_iconifyCache.set(set, req);
}
reqs.push(_iconifyCache.get(set));
}
return { collections: Object.fromEntries(await Promise.all(reqs)) };
};
31 changes: 15 additions & 16 deletions render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,25 +180,24 @@ const h = (
});

// configuration
let unoTheme: unknown = undefined;
const setTheme = (theme: unknown) => {
unoTheme = theme;
return unoTheme;
};
const uno = createGenerator(),
setTheme = async (theme?: unknown) => {
uno.setConfig({
presets: [
presetWind({ dark: "class", variablePrefix: "uno-" }),
presetIcons(await iconifyCollections("twemoji", "ph")),
],
theme: theme ?? undefined,
});
};
await setTheme();

// renderers
const renderStylesheet = async (className: string) => {
const uno = createGenerator({
presets: [
presetWind({ dark: "class", variablePrefix: "uno-" }),
presetIcons(await iconifyCollections("twemoji", "ph")),
],
theme: unoTheme,
}),
{ css } = await uno.generate(
className, //
{ id: undefined, scope: undefined, minify: true },
);
const { css } = await uno.generate(
className, //
{ id: undefined, scope: undefined, minify: true },
);
return css;
},
renderIsland = async ($: JSX.Element | JSX.Element[]) => {
Expand Down

0 comments on commit 98eebd7

Please sign in to comment.