UNPKG

@lobehub/ui

Version:

Lobe UI is an open-source UI component library for building AIGC web apps

71 lines (70 loc) 2.56 kB
import { createLobeAntdTheme } from "../styles/theme/antdTheme.mjs"; import { lobeUiAntdBaseline } from "./baseline.mjs"; import { hashCss } from "./hash.mjs"; import { antdProbeNames, antdProbeRegistry, expandComponentsToProbes } from "./registry.mjs"; import { scanAntdUsage } from "./scan.mjs"; import { styleKeysOf } from "./shared.mjs"; import { createElement } from "react"; import { ConfigProvider, theme } from "antd"; import { StyleProvider } from "antd-style"; import { createCache, extractStyle } from "@ant-design/cssinjs"; import { renderToString } from "react-dom/server"; //#region src/static-css/buildAntdStaticCss.ts const createProbeAntdTheme = () => { const config = createLobeAntdTheme({ appearance: "light" }); return { ...config, algorithm: [theme.defaultAlgorithm, config.algorithm ?? []].flat() }; }; const resolveProbes = (options) => { const { included = "auto", extraIncluded = [], customProbes = [], scan } = options; const names = /* @__PURE__ */ new Set(); const unmatchedComponents = []; if (included === "auto") { const usage = scanAntdUsage(scan); if (usage.wildcard) for (const name of antdProbeNames) names.add(name); else { const expanded = expandComponentsToProbes(usage.components); for (const name of expanded.probes) names.add(name); unmatchedComponents.push(...expanded.unmatched); } if (usage.importsLobeUi) for (const name of lobeUiAntdBaseline) names.add(name); } else for (const name of included) names.add(name); for (const name of extraIncluded) names.add(name); return { probes: [...[...names].sort().map((name) => [name, antdProbeRegistry[name].render]), ...customProbes], unmatchedComponents }; }; const buildAntdStaticCss = (options = {}) => { const { theme = createProbeAntdTheme(), hrefTemplate = (hash) => `/antd.css?v=${hash}` } = options; const { probes, unmatchedComponents } = resolveProbes(options); const cache = createCache(); const failedProbes = []; for (const [name, probe] of probes) try { renderToString(createElement(StyleProvider, { cache, children: createElement(ConfigProvider, { theme }, probe()) })); } catch { failedProbes.push(name); } const css = extractStyle(cache, { plain: true, types: ["style"] }); const hash = hashCss(css); return { css, failedProbes, hash, href: hrefTemplate(hash), probes: probes.map(([name]) => name), styleKeys: styleKeysOf(cache).sort(), unmatchedComponents }; }; //#endregion export { buildAntdStaticCss, createProbeAntdTheme }; //# sourceMappingURL=buildAntdStaticCss.mjs.map