Skip to content

Commit

Permalink
chore: compile without lib (ant-design#36362)
Browse files Browse the repository at this point in the history
* 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 41b8bf1.

* chore: update dist less

* test: fix
  • Loading branch information
MadCcc authored Jul 4, 2022
1 parent bd2b80f commit 469d0bc
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 146 deletions.
12 changes: 6 additions & 6 deletions .antd-tools.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))) {
Expand All @@ -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,
);
});
Expand All @@ -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`);
Expand Down Expand Up @@ -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(
Expand All @@ -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) {
Expand Down
23 changes: 1 addition & 22 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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 }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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__/
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
76 changes: 30 additions & 46 deletions scripts/collect-token-statistic.js
Original file line number Diff line number Diff line change
@@ -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.`));
})();
65 changes: 0 additions & 65 deletions tests/dekko/lib.test.js

This file was deleted.

7 changes: 7 additions & 0 deletions tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"types": ["node"]
}
}

0 comments on commit 469d0bc

Please sign in to comment.