UNPKG

@lobehub/ui

Version:

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

1 lines 5.43 kB
{"version":3,"file":"buildAntdStaticCss.mjs","names":["antdTheme","extractCssinjsStyle"],"sources":["../../src/static-css/buildAntdStaticCss.ts"],"sourcesContent":["import { createCache, extractStyle as extractCssinjsStyle } from '@ant-design/cssinjs';\nimport type { ThemeConfig } from 'antd';\nimport { ConfigProvider, theme as antdTheme } from 'antd';\nimport { StyleProvider } from 'antd-style';\nimport type { ReactElement } from 'react';\nimport { createElement } from 'react';\nimport { renderToString } from 'react-dom/server';\n\nimport { createLobeAntdTheme } from '@/styles/theme/antdTheme';\n\nimport { lobeUiAntdBaseline } from './baseline';\nimport { hashCss } from './hash';\nimport type { AntdProbeName } from './registry';\nimport { antdProbeNames, antdProbeRegistry, expandComponentsToProbes } from './registry';\nimport type { ScanAntdUsageOptions } from './scan';\nimport { scanAntdUsage } from './scan';\nimport { styleKeysOf } from './shared';\n\nexport interface AntdStaticCssOptions {\n /** escape hatch for render paths the registry has no probe for */\n customProbes?: [name: string, render: () => ReactElement][];\n /** extra probes on top of 'auto' scan results */\n extraIncluded?: AntdProbeName[];\n hrefTemplate?: (hash: string) => string;\n included?: 'auto' | AntdProbeName[];\n scan?: ScanAntdUsageOptions;\n /** must equal the ConfigProvider theme used at SSR — token hash decides styleKeys matching */\n theme?: ThemeConfig;\n}\n\nexport interface AntdStaticCssPayload {\n css: string;\n failedProbes: string[];\n hash: string;\n href: string;\n probes: string[];\n styleKeys: string[];\n /** antd exports found by 'auto' scan that no registry probe covers */\n unmatchedComponents: string[];\n}\n\nexport const createProbeAntdTheme = (): ThemeConfig => {\n const config = createLobeAntdTheme({ appearance: 'light' });\n return {\n ...config,\n algorithm: [antdTheme.defaultAlgorithm, config.algorithm ?? []].flat(),\n };\n};\n\nconst resolveProbes = (options: AntdStaticCssOptions) => {\n const { included = 'auto', extraIncluded = [], customProbes = [], scan } = options;\n\n const names = new Set<AntdProbeName>();\n const unmatchedComponents: string[] = [];\n\n if (included === 'auto') {\n const usage = scanAntdUsage(scan);\n if (usage.wildcard) {\n for (const name of antdProbeNames) names.add(name);\n } else {\n const expanded = expandComponentsToProbes(usage.components);\n for (const name of expanded.probes) names.add(name);\n unmatchedComponents.push(...expanded.unmatched);\n }\n if (usage.importsLobeUi) for (const name of lobeUiAntdBaseline) names.add(name);\n } else {\n for (const name of included) names.add(name);\n }\n\n for (const name of extraIncluded) names.add(name);\n\n const probes: [string, () => ReactElement][] = [...names]\n .sort()\n .map((name) => [name, antdProbeRegistry[name].render]);\n\n return { probes: [...probes, ...customProbes], unmatchedComponents };\n};\n\nexport const buildAntdStaticCss = (options: AntdStaticCssOptions = {}): AntdStaticCssPayload => {\n const { theme = createProbeAntdTheme(), hrefTemplate = (hash) => `/antd.css?v=${hash}` } =\n options;\n\n const { probes, unmatchedComponents } = resolveProbes(options);\n\n const cache = createCache();\n const failedProbes: string[] = [];\n\n for (const [name, probe] of probes) {\n try {\n renderToString(\n createElement(StyleProvider, {\n cache,\n children: createElement(ConfigProvider, { theme }, probe()),\n }),\n );\n } catch {\n failedProbes.push(name);\n }\n }\n\n const css = extractCssinjsStyle(cache, { plain: true, types: ['style'] });\n const hash = hashCss(css);\n\n return {\n css,\n failedProbes,\n hash,\n href: hrefTemplate(hash),\n probes: probes.map(([name]) => name),\n styleKeys: styleKeysOf(cache as unknown as { cache: Map<string, unknown> }).sort(),\n unmatchedComponents,\n };\n};\n"],"mappings":";;;;;;;;;;;;AAyCA,MAAa,6BAA0C;CACrD,MAAM,SAAS,oBAAoB,EAAE,YAAY,QAAQ,CAAC;CAC1D,OAAO;EACL,GAAG;EACH,WAAW,CAACA,MAAU,kBAAkB,OAAO,aAAa,CAAC,CAAC,CAAC,CAAC,KAAK;CACvE;AACF;AAEA,MAAM,iBAAiB,YAAkC;CACvD,MAAM,EAAE,WAAW,QAAQ,gBAAgB,CAAC,GAAG,eAAe,CAAC,GAAG,SAAS;CAE3E,MAAM,wBAAQ,IAAI,IA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