File size: 3,311 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
import path from 'path';
import React from 'react';
import {
createCache,
extractStyle,
legacyNotSelectorLinter,
logicalPropertiesLinter,
NaNLinter,
parentSelectorLinter,
StyleProvider,
} from '@ant-design/cssinjs';
import chalk from 'chalk';
import { parse } from 'css-tree';
import type { SyntaxParseError } from 'css-tree';
import { validate } from 'csstree-validator';
import fs from 'fs-extra';
import isCI from 'is-ci';
import ReactDOMServer from 'react-dom/server';
import { ConfigProvider } from '../components';
import { generateCssinjs } from './generate-cssinjs';
const tmpDir = path.join(`${__filename}.tmp`);
fs.emptyDirSync(tmpDir);
console.log(chalk.green(`🔥 Checking CSS-in-JS...`));
let errorCount = 0;
const originError = console.error;
console.error = (msg: any) => {
if (msg.includes('Warning: [Ant Design CSS-in-JS]')) {
errorCount += 1;
console.log(chalk.red(`❌ `), msg.slice(msg.indexOf('Error in')).replace(/\s+/g, ' '));
} else {
originError(msg);
}
};
// https://github.com/csstree/validator/blob/7df8ca/lib/validate.js#L187
function cssValidate(css: string, filename: string) {
const errors: SyntaxParseError[] = [];
const ast = parse(css, {
filename,
positions: true,
onParseError(error) {
errors.push(error);
},
});
return errors.concat(validate(ast));
}
async function checkCSSVar() {
await generateCssinjs({
key: 'check',
render(Component: any) {
ReactDOMServer.renderToString(
<StyleProvider linters={[NaNLinter]}>
<ConfigProvider theme={{ cssVar: true, hashed: false }}>
<Component />
</ConfigProvider>
</StyleProvider>,
);
},
});
}
async function checkCSSContent() {
const errors = new Map();
await generateCssinjs({
key: 'css-validate',
render(Component: any, filePath: string) {
const cache = createCache();
ReactDOMServer.renderToString(
<StyleProvider cache={cache}>
<Component />
</StyleProvider>,
);
const css = extractStyle(cache, { types: 'style', plain: true });
let showPath = filePath;
if (!isCI) {
const [, name] = filePath.split(path.sep);
const writeLocalPath = path.join(tmpDir, `${name}.css`);
showPath = path.relative(process.cwd(), writeLocalPath);
fs.writeFileSync(writeLocalPath, `/* ${filePath} */\n${css}`);
}
errors.set(filePath, cssValidate(css, showPath));
},
});
for (const [filePath, error] of errors) {
if (error.length > 0) {
errorCount += error.length;
console.log(chalk.red(`❌ ${filePath} has ${error.length} errors:`));
console.log(error);
}
}
}
(async () => {
await generateCssinjs({
key: 'check',
render(Component: any) {
ReactDOMServer.renderToString(
<StyleProvider
linters={[logicalPropertiesLinter, legacyNotSelectorLinter, parentSelectorLinter]}
>
<Component />
</StyleProvider>,
);
},
});
await checkCSSVar();
await checkCSSContent();
if (errorCount > 0) {
console.log(chalk.red(`❌ CSS-in-JS check failed with ${errorCount} errors.`));
process.exit(1);
} else {
console.log(chalk.green(`✅ CSS-in-JS check passed.`));
}
})();
|