2023-04-28 23:21:51 +08:00
|
|
|
import { globSync } from 'glob';
|
|
|
|
import path from 'path';
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
type StyleFn = (prefix?: string) => void;
|
|
|
|
|
2023-05-08 19:48:24 +08:00
|
|
|
interface GenCssinjsOptions {
|
2023-05-08 15:25:48 +08:00
|
|
|
key: string;
|
2023-05-08 19:48:24 +08:00
|
|
|
render: (component: React.FC) => void;
|
2023-05-08 15:25:48 +08:00
|
|
|
beforeRender?: (componentName: string) => void;
|
2023-05-08 19:48:24 +08:00
|
|
|
}
|
2023-05-08 15:25:48 +08:00
|
|
|
|
2023-04-28 23:21:51 +08:00
|
|
|
export const styleFiles = globSync(
|
|
|
|
path.join(
|
|
|
|
process.cwd(),
|
2023-07-17 23:43:32 +08:00
|
|
|
'components/!(version|config-provider|icon|auto-complete|col|row|time-picker|qrcode)/style/index.?(ts|tsx)',
|
2023-04-28 23:21:51 +08:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2023-05-08 19:48:24 +08:00
|
|
|
export const generateCssinjs = ({ key, beforeRender, render }: GenCssinjsOptions) =>
|
|
|
|
Promise.all(
|
|
|
|
styleFiles.map(async (file) => {
|
|
|
|
const pathArr = file.split('/');
|
|
|
|
const styleIndex = pathArr.lastIndexOf('style');
|
|
|
|
const componentName = pathArr[styleIndex - 1];
|
|
|
|
let useStyle: StyleFn = () => {};
|
|
|
|
if (file.includes('grid')) {
|
|
|
|
const { useColStyle, useRowStyle } = await import(file);
|
|
|
|
useStyle = (prefixCls: string) => {
|
|
|
|
useRowStyle(prefixCls);
|
|
|
|
useColStyle(prefixCls);
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
useStyle = (await import(file)).default;
|
|
|
|
}
|
|
|
|
const Demo: React.FC = () => {
|
|
|
|
useStyle(`${key}-${componentName}`);
|
|
|
|
return React.createElement('div');
|
2023-04-28 23:21:51 +08:00
|
|
|
};
|
2023-05-08 19:48:24 +08:00
|
|
|
beforeRender?.(componentName);
|
|
|
|
render?.(Demo);
|
|
|
|
}),
|
|
|
|
);
|