From 469d0bc81c1b2ea7e425f0af3b77eb036410130d Mon Sep 17 00:00:00 2001 From: MadCcc <1075746765@qq.com> Date: Mon, 4 Jul 2022 22:08:24 +0800 Subject: [PATCH] chore: compile without lib (#36362) * chore: compile es only * chore: update package.json * chore: code clean * chore: code clean * chore: update workflow * chore: update workflow * chore: update less src * chore: replace lib with es * chore: recover less * Revert "chore: replace lib with es" This reverts commit 41b8bf193873ca2d578fff6e6833c739d7f09f1d. * chore: update dist less * test: fix --- .antd-tools.config.js | 12 ++--- .github/workflows/test.yml | 23 +-------- .gitignore | 1 + package.json | 14 +++--- scripts/collect-token-statistic.js | 76 ++++++++++++------------------ tests/dekko/lib.test.js | 65 ------------------------- tsconfig.node.json | 7 +++ 7 files changed, 52 insertions(+), 146 deletions(-) delete mode 100644 tests/dekko/lib.test.js create mode 100644 tsconfig.node.json diff --git a/.antd-tools.config.js b/.antd-tools.config.js index 7a5bec3673..79f9ec6dd8 100644 --- a/.antd-tools.config.js +++ b/.antd-tools.config.js @@ -14,11 +14,11 @@ module.exports = { // We need compile additional content for antd user function finalizeCompile() { - if (fs.existsSync(path.join(__dirname, './lib'))) { + if (fs.existsSync(path.join(__dirname, './es'))) { // Build a entry less file to dist/antd.less const componentsPath = path.join(process.cwd(), 'components'); let componentsLessContent = ''; - // Build components in one file: lib/style/components.less + // Build components in one file: es/style/components.less fs.readdir(componentsPath, (err, files) => { files.forEach(file => { if (fs.existsSync(path.join(componentsPath, file, 'style', 'index.less'))) { @@ -30,7 +30,7 @@ function finalizeCompile() { } }); fs.writeFileSync( - path.join(process.cwd(), 'lib', 'style', 'components.less'), + path.join(process.cwd(), 'es', 'style', 'components.less'), componentsLessContent, ); }); @@ -42,7 +42,7 @@ function buildThemeFile(theme, vars) { if (theme !== 'default') { fs.writeFileSync( path.join(process.cwd(), 'dist', `antd.${theme}.less`), - `@import "../lib/style/${theme}.less";\n@import "../lib/style/components.less";`, + `@import "../es/style/${theme}.less";\n@import "../es/style/components.less";`, ); // eslint-disable-next-line no-console console.log(`Built a entry less file to dist/antd.${theme}.less`); @@ -78,7 +78,7 @@ function finalizeDist() { // Build less entry file: dist/antd.less fs.writeFileSync( path.join(process.cwd(), 'dist', 'antd.less'), - '@import "../lib/style/default.less";\n@import "../lib/style/components.less";', + '@import "../es/style/default.less";\n@import "../es/style/components.less";', ); // eslint-disable-next-line no-console fs.writeFileSync( @@ -96,7 +96,7 @@ function finalizeDist() { ` function getThemeVariables(options = {}) { let themeVar = { - 'hack': \`true;@import "\${require.resolve('antd/lib/style/color/colorPalette.less')}";\`, + 'hack': \`true;@import "\${require.resolve('antd/es/style/color/colorPalette.less')}";\`, ...defaultTheme }; if(options.dark) { diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 03412ce279..7d481a0a56 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -163,12 +163,6 @@ jobs: path: node_modules key: node_modules-${{ hashFiles('**/package-temp-dir/package-lock.json') }} - - name: restore cache from lib - uses: actions/cache@v3 - with: - path: lib - key: lib-${{ github.sha }} - - name: restore cache from es uses: actions/cache@v3 with: @@ -198,12 +192,6 @@ jobs: path: node_modules key: node_modules-${{ hashFiles('**/package-temp-dir/package-lock.json') }} - - name: restore cache from lib - uses: actions/cache@v3 - with: - path: lib - key: lib-${{ github.sha }} - - name: restore cache from es uses: actions/cache@v3 with: @@ -324,12 +312,6 @@ jobs: path: node_modules key: node_modules-${{ hashFiles('**/package-temp-dir/package-lock.json') }} - - name: cache lib - uses: actions/cache@v3 - with: - path: lib - key: lib-${{ github.sha }} - - name: cache es uses: actions/cache@v3 with: @@ -338,9 +320,6 @@ jobs: - name: compile run: npm run compile - - - name: check - run: node ./tests/dekko/lib.test.js needs: setup compiled-module-test: @@ -349,7 +328,7 @@ jobs: strategy: matrix: react: ['16', '17', '18'] - module: [lib, es] + module: [es] shard: ['1/2', '2/2'] env: REACT: ${{ matrix.react }} diff --git a/.gitignore b/.gitignore index 5be4d3cb0b..16ffbbdab3 100644 --- a/.gitignore +++ b/.gitignore @@ -60,6 +60,7 @@ site/theme/template/Resources/**/*.jsx site/theme/template/NotFound.jsx scripts/previewEditor/index.html components/version/version.tsx +components/version/token.tsx # Image snapshot diff __diff_output__/ diff --git a/package.json b/package.json index 84ee5d3617..8f8ed95596 100644 --- a/package.json +++ b/package.json @@ -32,19 +32,17 @@ }, "files": [ "dist", - "lib", "es" ], "sideEffects": [ "dist/*", "es/**/style/*", - "lib/**/style/*", "*.less" ], - "main": "lib/index.js", + "main": "es/index.js", "module": "es/index.js", "unpkg": "dist/antd.min.js", - "typings": "lib/index.d.ts", + "typings": "es/index.d.ts", "scripts": { "prepare": "husky install", "api-collection": "antd-tools run api-collection", @@ -55,12 +53,13 @@ "check-ts-demo": "node ./scripts/check-ts-demo", "clean": "antd-tools run clean && rm -rf es lib coverage dist report.html", "clean-lockfiles": "rm -rf package-lock.json yarn.lock", + "collect-token-statistic": "ts-node --project tsconfig.node.json scripts/collect-token-statistic.js", "prestart": "npm run version", "precompile": "npm run version", "pretest": "npm run version", "predist": "npm run version", "presite": "npm run version", - "compile": "npm run clean && antd-tools run compile && node scripts/collect-token-statistic.js", + "compile": "npm run clean && antd-tools run compile-experimental", "changelog": "node ./scripts/print-changelog", "predeploy": "antd-tools run clean && npm run site && cp CNAME _site && npm run site:test", "deploy": "bisheng gh-pages --push-only --dotfiles", @@ -81,7 +80,7 @@ "pre-publish": "npm run test-all -- --skip-build", "prettier": "prettier -c --write **/*", "pretty-quick": "pretty-quick", - "pub": "npm run version && antd-tools run pub", + "pub": "npm run version && antd-tools run pub-experimental", "prepublishOnly": "node scripts/collect-token-statistic.js && antd-tools run guard", "postpublish": "node ./scripts/post-script.js", "site:theme": "npm run site:theme-dark && npm run site:theme-compact", @@ -100,7 +99,7 @@ "site:test": "jest --config .jest.site.js --cache=false --force-exit", "test-image": "npm run dist && docker-compose run tests", "argos": "node ./scripts/argos-upload.js", - "version": "node ./scripts/generate-version", + "version": "node ./scripts/generate-version && npm run collect-token-statistic", "install-react-16": "npm i --no-save --legacy-peer-deps react@16 react-dom@16 enzyme-adapter-react-16", "install-react-17": "npm i --no-save --legacy-peer-deps react@17 react-dom@17", "install-react-18": "npm i --no-save --legacy-peer-deps react@18 react-dom@18 @testing-library/react@13" @@ -294,6 +293,7 @@ "stylelint-declaration-block-no-ignored-properties": "^2.1.0", "stylelint-order": "^5.0.0", "theme-switcher": "^1.0.2", + "ts-node": "^10.8.2", "typescript": "~4.7.2", "webpack-bundle-analyzer": "^4.1.0", "xhr-mock": "^2.4.1", diff --git a/scripts/collect-token-statistic.js b/scripts/collect-token-statistic.js index 5d24f47437..2015c4d987 100644 --- a/scripts/collect-token-statistic.js +++ b/scripts/collect-token-statistic.js @@ -1,62 +1,46 @@ -/* eslint-disable import/no-unresolved,no-console */ +/* eslint-disable import/no-unresolved,no-console,global-require,import/no-dynamic-require */ const chalk = require('chalk'); const React = require('react'); const ReactDOMServer = require('react-dom/server'); const fs = require('fs-extra'); -const antd = require('../lib'); -const { statistic } = require('../lib/theme/util/statistic'); -const useMessageStyle = require('../lib/message/style/index').default; -const useNotificationStyle = require('../lib/notification/style/index').default; +const glob = require('glob'); +const path = require('path'); +const { statistic } = require('../components/theme/util/statistic'); console.log(chalk.green(`🔥 Collecting token statistics...`)); -// Automatic rendering -Object.entries(antd).forEach(([key, component]) => { - if ( - /[A-Z]/.test(key.charAt(0)) && - key !== 'Form' && - key !== 'Dropdown' && - key !== 'Grid' && - key !== 'ConfigProvider' - ) { - ReactDOMServer.renderToString(React.createElement(component)); - } -}); const EmptyElement = React.createElement('div'); -// Dropdown -ReactDOMServer.renderToString( - React.createElement(antd.Dropdown, { overlay: EmptyElement }, EmptyElement), +const styleFiles = glob.sync( + path.join( + process.cwd(), + 'components/!(version|config-provider|icon|locale-provider|auto-complete|col|row|page-header|comment|time-picker|)/style/index.tsx', + ), ); - -// Form -ReactDOMServer.renderToString(React.createElement(antd.Form, undefined, EmptyElement)); - -// message -const Message = () => { - useMessageStyle('message'); - return EmptyElement; -}; -ReactDOMServer.renderToString(React.createElement(Message)); - -// Notification -const Notification = () => { - useNotificationStyle('notification'); - return EmptyElement; -}; -ReactDOMServer.renderToString(React.createElement(Notification)); +styleFiles.forEach(file => { + console.log(file); + let useStyle = () => {}; + if (file.includes('grid')) { + const { useColStyle, useRowStyle } = require(file); + useStyle = () => { + useRowStyle(); + useColStyle(); + }; + } else { + useStyle = require(file).default; + } + const Component = () => { + useStyle('file'); + return EmptyElement; + }; + ReactDOMServer.renderToString(React.createElement(Component)); +}); (async () => { - const libPath = `${process.cwd()}/lib/theme/util/statistic.js`; - const libContent = await fs.readFile(libPath, 'utf8'); - const newLibContent = `${libContent}\nexports._statistic_build_ = ${JSON.stringify(statistic)}`; - await fs.writeFile(libPath, newLibContent, 'utf8'); - - const esPath = `${process.cwd()}/es/theme/util/statistic.js`; - const esContent = await fs.readFile(esPath, 'utf8'); - const newEsContent = `${esContent}\n_statistic_build_ = ${JSON.stringify(statistic)}`; - await fs.writeFile(esPath, newEsContent, 'utf8'); + const tokenPath = `${process.cwd()}/components/version/token.tsx`; + const content = `export default ${JSON.stringify(statistic, null, 2)}`; + await fs.writeFile(tokenPath, content, 'utf8'); console.log(chalk.green(`✅ Collecting token statistics done.`)); })(); diff --git a/tests/dekko/lib.test.js b/tests/dekko/lib.test.js deleted file mode 100644 index e39939f3f4..0000000000 --- a/tests/dekko/lib.test.js +++ /dev/null @@ -1,65 +0,0 @@ -const $ = require('dekko'); -const chalk = require('chalk'); -const path = require('path'); - -function getFileName(filePath) { - return filePath.slice(filePath.lastIndexOf(path.sep) + 1); -} - -$('lib').isDirectory().hasFile('index.js').hasFile('index.d.ts'); - -$('lib/style').isDirectory().hasFile('index.css').hasFile('default.css'); - -$('lib/*') - .filter( - filename => - !filename.endsWith('index.js') && - !filename.endsWith('index.d.ts') && - !filename.endsWith('.map'), - ) - .isDirectory() - .filter( - filename => - !filename.endsWith('style') && - !filename.endsWith('_util') && - !filename.endsWith('theme') && - !filename.endsWith('locale'), - ) - .hasFile('index.js') - .hasFile('index.d.ts') - .hasDirectory('style'); - -$('lib/*/style').hasFile('css.js').hasFile('index.js'); - -// locale -const filterLocaleFile = filePath => { - const fileName = getFileName(filePath); - return ( - !fileName.endsWith('index.js') && - !fileName.endsWith('.d.ts') && - !fileName.endsWith('.map') && - !fileName.endsWith('style') && - !fileName.includes('-') && - !fileName.endsWith('LocaleReceiver.js') && - !fileName.endsWith('context.js') - ); -}; -const localeFiles = $('lib/locale/*').filter(filterLocaleFile); -const localeProviderFiles = $('lib/locale-provider/*').filter(filterLocaleFile); - -function compare(originFiles, targetFiles, targetPath) { - originFiles.assert( - `not exist in '${targetPath}'. Please use 'scripts/generateLegacyLocale.js' to refresh locale files.`, - filePath => { - const fileName = getFileName(filePath); - - return targetFiles.filenames.some(targetFilePath => getFileName(targetFilePath) === fileName); - }, - ); -} - -compare(localeFiles, localeProviderFiles, '/locale-provider'); -compare(localeProviderFiles, localeFiles, '/locale'); - -// eslint-disable-next-line no-console -console.log(chalk.green('✨ `lib` directory is valid.')); diff --git a/tsconfig.node.json b/tsconfig.node.json new file mode 100644 index 0000000000..d58b1bdd7f --- /dev/null +++ b/tsconfig.node.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "CommonJS", + "types": ["node"] + } +}