Skip to content

Commit 82157fe

Browse files
committed
antd主题修改热更新
1 parent c63a8f3 commit 82157fe

18 files changed

+1394
-26450
lines changed

.babelrc.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,4 @@
11
module.exports = {
2-
presets: [
3-
[
4-
"next/babel",
5-
{
6-
"styled-jsx": {
7-
plugins: [
8-
"styled-jsx-plugin-sass",
9-
"styled-jsx-plugin-postcss"
10-
],
11-
},
12-
},
13-
],
14-
],
15-
plugins: [
16-
["@babel/plugin-proposal-decorators", { legacy: true }],
17-
// [
18-
// "styled-jsx/babel",
19-
// {
20-
// optimizeForSpeed: true,
21-
// },
22-
// ],
23-
],
2+
presets: [["next/babel"]],
3+
plugins: [["@babel/plugin-proposal-decorators", { legacy: true }]],
244
};

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ yarn-error.log*
3232

3333
# vercel
3434
.vercel
35+
/scripts/generated-theme.less

.npmrc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
1+
# sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
22
registry=https://registry.npm.taobao.org
3-
sharp_dist_base_url=https://npm.taobao.org/mirrors/sharp-libvips/
4-
electron_mirror=https://npm.taobao.org/mirrors/electron/
5-
phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs/
6-
sentrycli_cdnurl=https://npm.taobao.org/mirrors/sentry-cli/
7-
sqlite3_binary_site=https://npm.taobao.org/mirrors/sqlite3/
8-
python_mirror=https://npm.taobao.org/mirrors/python/
3+
# sharp_dist_base_url=https://npm.taobao.org/mirrors/sharp-libvips/
4+
# electron_mirror=https://npm.taobao.org/mirrors/electron/
5+
# phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs/
6+
# sentrycli_cdnurl=https://npm.taobao.org/mirrors/sentry-cli/
7+
# sqlite3_binary_site=https://npm.taobao.org/mirrors/sqlite3/
8+
# python_mirror=https://npm.taobao.org/mirrors/python/

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ yarn dev
1313
yarn build
1414
// 部署,启动node服务, 建议通过pm2启动
1515
yarn start
16+
17+
// 打开网页http://localhost:3000/
1618
```
1719

1820
## 目录结构
@@ -38,8 +40,6 @@ yarn start
3840
## antd主题配置
3941

4042
1. 修改./theme/antd-theme-var.less文件,添加你需要修改的主题色
41-
2. 运行命令```yarn gt```,完成主题修改(此命令会自动生成一份antd.css并覆盖node_modules/antd下的css文件)
42-
3. 不支持热更新
4343

4444

4545
## 关于接口请求

next.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
1+
require('./scripts/plugins/watch-antd-theme');
22

33
const isProd = process.env.NODE_ENV === 'production'
44

5+
56
module.exports = (phase) => {
67
return {
78
// Use the CDN in production and localhost for development.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
"scripts": {
66
"dev": "next dev",
77
"build": "next build",
8-
"start": "next start",
9-
"gt": "generate-theme './theme/antd-theme-var.less' './theme/antd.css' && cp ./theme/antd.css ./node_modules/antd/dist/",
10-
"postinstall": "npm run gt"
8+
"start": "next start"
119
},
1210
"dependencies": {
1311
"antd": "^4.9.4",
@@ -28,8 +26,10 @@
2826
"@emeks/antd-custom-theme-generator": "^0.1.6",
2927
"@types/node": "^14.14.16",
3028
"@types/react": "^17.0.0",
29+
"chokidar": "^3.4.3",
3130
"eslint": "^7.16.0",
32-
"less": "^4.0.0",
31+
"less": "3.11.3",
32+
"rimraf": "^3.0.2",
3333
"sass": "^1.30.0",
3434
"typescript": "^4.1.3"
3535
}

postcss.config.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

scripts/generate-theme.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env node
2+
const { exec } = require("child_process");
3+
const path = require("path");
4+
const rimraf = require('rimraf');
5+
6+
const cwd = process.cwd();
7+
8+
const scriptVariables = {
9+
customThemeFilePath: path.join(cwd, "./theme/antd-theme-var.less"),
10+
generatedThemeFilePath: path.join(cwd, "./node_modules/antd/dist/antd.css"),
11+
antdLibraryPath: path.join(cwd, "./node_modules/antd"),
12+
theme: "default",
13+
};
14+
15+
const content = `
16+
@import url("${scriptVariables.antdLibraryPath}/lib/style/themes/${scriptVariables.theme}.less");
17+
@import url("${scriptVariables.antdLibraryPath}/dist/antd.less");
18+
@import url("${scriptVariables.customThemeFilePath}");
19+
`;
20+
21+
const generatedThemePath = './scripts/generated-theme.less';
22+
23+
module.exports = () => {
24+
exec(
25+
`echo "${content}" > ${generatedThemePath}`,
26+
{ cwd },
27+
(error1, stdout, stderr) => {
28+
if (!error1) {
29+
exec(
30+
`node_modules/less/bin/lessc --js ${generatedThemePath} ${scriptVariables.generatedThemeFilePath}`,
31+
{ cwd },
32+
(error2, stdout, stderr) => {
33+
if (!error2) {
34+
if (process.env.NODE_ENV !== 'development') {
35+
rimraf.sync(generatedThemePath);
36+
}
37+
} else {
38+
console.error('生成antd.css错误: ', error2);
39+
}
40+
}
41+
);
42+
} else {
43+
console.error(error1);
44+
}
45+
}
46+
);
47+
}
48+

scripts/plugins/watch-antd-theme.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const chokidar = require('chokidar');
2+
const path = require('path');
3+
const gt = require('../generate-theme');
4+
5+
if (process.env.NODE_ENV === 'development') {
6+
chokidar.watch(path.resolve('./theme/antd-theme-var.less'))
7+
.on('all', gt);
8+
} else {
9+
gt();
10+
}

store/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ const initServerData = (global as any)?.__NEXT_DATA__?.props?.pageProps || {};
8888

8989
const lsData = isBrowser ? (getData() || {}) : {};
9090

91-
console.log(lsData)
92-
9391
const store = initStore({
9492
...lsData,
9593
...initServerData,

theme/antd-theme-var.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
@primary-color: orange;
1+
@primary-color: orange;

0 commit comments

Comments
 (0)