2023-09-10 11:38:32 +08:00
|
|
|
import url from 'node:url';
|
2023-04-28 23:21:51 +08:00
|
|
|
import path from 'path';
|
2023-07-28 16:17:43 +08:00
|
|
|
import { globSync } from 'glob';
|
2023-04-28 23:21:51 +08:00
|
|
|
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(
|
2023-09-10 11:38:32 +08:00
|
|
|
path
|
|
|
|
.join(
|
|
|
|
process.cwd(),
|
|
|
|
'components/!(version|config-provider|icon|auto-complete|col|row|time-picker|qrcode)/style/index.?(ts|tsx)',
|
|
|
|
)
|
|
|
|
.split(path.sep)
|
|
|
|
.join('/'),
|
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) => {
|
2023-09-10 11:38:32 +08:00
|
|
|
const absPath = url.pathToFileURL(file).href;
|
2023-05-08 19:48:24 +08:00
|
|
|
const pathArr = file.split('/');
|
|
|
|
const styleIndex = pathArr.lastIndexOf('style');
|
|
|
|
const componentName = pathArr[styleIndex - 1];
|
|
|
|
let useStyle: StyleFn = () => {};
|
|
|
|
if (file.includes('grid')) {
|
2023-09-10 11:38:32 +08:00
|
|
|
const { useColStyle, useRowStyle } = await import(absPath);
|
2023-05-08 19:48:24 +08:00
|
|
|
useStyle = (prefixCls: string) => {
|
|
|
|
useRowStyle(prefixCls);
|
|
|
|
useColStyle(prefixCls);
|
|
|
|
};
|
|
|
|
} else {
|
2023-09-10 11:38:32 +08:00
|
|
|
useStyle = (await import(absPath)).default;
|
2023-05-08 19:48:24 +08:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}),
|
|
|
|
);
|