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';
|
|
|
|
import React from 'react';
|
2023-11-17 13:46:41 +08:00
|
|
|
import { globSync } from 'glob';
|
2023-04-28 23:21:51 +08:00
|
|
|
|
|
|
|
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-11-13 11:56:13 +08:00
|
|
|
ignore?: string[];
|
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-11-13 11:56:13 +08:00
|
|
|
export const generateCssinjs = ({ key, beforeRender, render, ignore }: GenCssinjsOptions) =>
|
2023-05-08 19:48:24 +08:00
|
|
|
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];
|
2023-11-17 13:46:41 +08:00
|
|
|
if (ignore?.includes(componentName)) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-08 19:48:24 +08:00
|
|
|
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);
|
|
|
|
}),
|
|
|
|
);
|