chore: share component render in scripts (#41603)

* chore: share component render in scripts

* chore: code clean
This commit is contained in:
MadCcc 2023-04-03 11:59:25 +08:00 committed by GitHub
parent bf9eff66d7
commit fd53ce6258
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 77 deletions

View File

@ -75,7 +75,7 @@
"lint:demo": "eslint components/*/demo/*.md", "lint:demo": "eslint components/*/demo/*.md",
"lint:deps": "antd-tools run deps-lint", "lint:deps": "antd-tools run deps-lint",
"lint:md": "remark . -f -q", "lint:md": "remark . -f -q",
"lint:style": "ts-node --project tsconfig.node.json scripts/check-cssinjs.js", "lint:style": "ts-node --project tsconfig.node.json scripts/check-cssinjs",
"lint:script": "eslint . --ext .js,.jsx,.ts,.tsx --cache", "lint:script": "eslint . --ext .js,.jsx,.ts,.tsx --cache",
"pre-publish": "npm run test-all -- --skip-build", "pre-publish": "npm run test-all -- --skip-build",
"prettier": "prettier -c --write **/* --cache", "prettier": "prettier -c --write **/* --cache",

View File

@ -1,15 +1,14 @@
/* eslint-disable import/no-unresolved,no-console,global-require,import/no-dynamic-require */ /* eslint-disable import/no-unresolved,no-console,global-require,import/no-dynamic-require */
const chalk = require('chalk'); const chalk = require('chalk');
const React = require('react');
const ReactDOMServer = require('react-dom/server');
const glob = require('glob');
const path = require('path');
const { const {
StyleProvider, StyleProvider,
logicalPropertiesLinter, logicalPropertiesLinter,
legacyNotSelectorLinter, legacyNotSelectorLinter,
parentSelectorLinter, parentSelectorLinter,
} = require('@ant-design/cssinjs'); } = require('@ant-design/cssinjs');
const React = require('react');
const ReactDOMServer = require('react-dom/server');
const { generateCssinjs } = require('./generate-cssinjs');
console.log(chalk.green(`🔥 Checking CSS-in-JS...`)); console.log(chalk.green(`🔥 Checking CSS-in-JS...`));
@ -24,30 +23,9 @@ console.error = (msg) => {
} }
}; };
const EmptyElement = React.createElement('div'); generateCssinjs({
key: 'check',
const styleFiles = glob.globSync( render: (Component) => {
path.join(
process.cwd(),
'components/!(version|config-provider|icon|auto-complete|col|row|time-picker)/style/index.?(ts|tsx)',
),
);
styleFiles.forEach((file) => {
let useStyle = () => {};
if (file.includes('grid')) {
const { useColStyle, useRowStyle } = require(file);
useStyle = () => {
useRowStyle();
useColStyle();
};
} else {
useStyle = require(file).default;
}
const Component = () => {
useStyle('check');
return EmptyElement;
};
ReactDOMServer.renderToString( ReactDOMServer.renderToString(
React.createElement( React.createElement(
StyleProvider, StyleProvider,
@ -55,6 +33,7 @@ styleFiles.forEach((file) => {
React.createElement(Component), React.createElement(Component),
), ),
); );
},
}); });
if (errorCount > 0) { if (errorCount > 0) {

View File

@ -1,52 +1,28 @@
/* eslint-disable import/no-unresolved,no-console,global-require,import/no-dynamic-require */ /* eslint-disable import/no-unresolved,no-console,global-require,import/no-dynamic-require */
const chalk = require('chalk'); const chalk = require('chalk');
const React = require('react'); const React = require('react');
const ReactDOMServer = require('react-dom/server'); const ReactDOMServer = require('react-dom/server');
const fs = require('fs-extra'); const fs = require('fs-extra');
const glob = require('glob');
const path = require('path');
const ProgressBar = require('progress'); const ProgressBar = require('progress');
const { statistic } = require('../components/theme/util/statistic'); const { statistic } = require('../components/theme/util/statistic');
const { DesignTokenContext } = require('../components/theme/internal'); const { DesignTokenContext } = require('../components/theme/internal');
const seedToken = require('../components/theme/themes/seed'); const seedToken = require('../components/theme/themes/seed');
const { generateCssinjs, filenames } = require('./generate-cssinjs');
console.log(chalk.green(`🔥 Collecting token statistics...`)); console.log(chalk.green(`🔥 Collecting token statistics...`));
const EmptyElement = React.createElement('div');
const styleFiles = glob.globSync(
path.join(
process.cwd(),
'components/!(version|config-provider|icon|auto-complete|col|row|time-picker)/style/index.?(ts|tsx)',
),
);
const bar = new ProgressBar('🚀 Collecting by component: [:bar] :component (:current/:total)', { const bar = new ProgressBar('🚀 Collecting by component: [:bar] :component (:current/:total)', {
complete: '=', complete: '=',
incomplete: ' ', incomplete: ' ',
total: styleFiles.length, total: filenames.length,
}); });
styleFiles.forEach((file) => { generateCssinjs({
const pathArr = file.split('/'); key: 'file',
const styleIndex = pathArr.lastIndexOf('style'); beforeRender: (componentName) => {
const componentName = pathArr[styleIndex - 1];
bar.tick(1, { component: componentName }); bar.tick(1, { component: componentName });
let useStyle = () => {}; },
if (file.includes('grid')) { render: (Component) => {
const { useColStyle, useRowStyle } = require(file);
useStyle = () => {
useRowStyle();
useColStyle();
};
} else {
useStyle = require(file).default;
}
const Component = () => {
useStyle('file');
return EmptyElement;
};
ReactDOMServer.renderToString(React.createElement(Component)); ReactDOMServer.renderToString(React.createElement(Component));
// Render wireframe // Render wireframe
ReactDOMServer.renderToString( ReactDOMServer.renderToString(
@ -56,6 +32,7 @@ styleFiles.forEach((file) => {
React.createElement(Component), React.createElement(Component),
), ),
); );
},
}); });
(() => { (() => {

View File

@ -0,0 +1,42 @@
const React = require('react');
const glob = require('glob');
const path = require('path');
const styleFiles = glob.globSync(
path.join(
process.cwd(),
'components/!(version|config-provider|icon|auto-complete|col|row|time-picker)/style/index.?(ts|tsx)',
),
);
module.exports = {
generateCssinjs({ key, beforeRender, render }) {
const EmptyElement = React.createElement('div');
styleFiles.forEach((file) => {
let useStyle = () => {};
if (file.includes('grid')) {
// eslint-disable-next-line global-require,import/no-dynamic-require
const { useColStyle, useRowStyle } = require(file);
useStyle = () => {
useRowStyle();
useColStyle();
};
} else {
// eslint-disable-next-line global-require,import/no-dynamic-require
useStyle = require(file).default;
}
const Component = () => {
useStyle(key);
return EmptyElement;
};
const pathArr = file.split('/');
const styleIndex = pathArr.lastIndexOf('style');
const componentName = pathArr[styleIndex - 1];
beforeRender?.(componentName);
render(Component);
});
},
filenames: styleFiles,
};