Browse Source

refactoring: use Promise.all (#42219)

* chore: use Promise.all

* del type

* fix
pull/42225/head
lijianan 2 years ago
committed by GitHub
parent
commit
a4669ace4c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 55
      scripts/generate-cssinjs.ts

55
scripts/generate-cssinjs.ts

@ -1,15 +1,14 @@
import { globSync } from 'glob';
import path from 'path';
import type { FC } from 'react';
import React from 'react';
type StyleFn = (prefix?: string) => void;
type GenCssinjs = (options: {
interface GenCssinjsOptions {
key: string;
render: (component: FC) => void;
render: (component: React.FC) => void;
beforeRender?: (componentName: string) => void;
}) => Promise<void>;
}
export const styleFiles = globSync(
path.join(
@ -18,29 +17,27 @@ export const styleFiles = globSync(
),
);
export const generateCssinjs: GenCssinjs = async ({ key, beforeRender, render }) => {
// eslint-disable-next-line no-restricted-syntax
for (const file of styleFiles) {
const pathArr = file.split('/');
const styleIndex = pathArr.lastIndexOf('style');
const componentName = pathArr[styleIndex - 1];
let useStyle: StyleFn = () => {};
if (file.includes('grid')) {
// eslint-disable-next-line no-await-in-loop
const { useColStyle, useRowStyle } = await import(file);
useStyle = (prefixCls: string) => {
useRowStyle(prefixCls);
useColStyle(prefixCls);
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');
};
} else {
// eslint-disable-next-line no-await-in-loop
useStyle = (await import(file)).default;
}
const Demo: FC = () => {
useStyle(`${key}-${componentName}`);
return React.createElement('div');
};
beforeRender?.(componentName);
render?.(Demo);
}
};
beforeRender?.(componentName);
render?.(Demo);
}),
);

Loading…
Cancel
Save