Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

基于 electron-react-boilerplate 之ant.design 主题定制 #41

Open
huangshuwei opened this issue Apr 20, 2020 · 3 comments
Open

基于 electron-react-boilerplate 之ant.design 主题定制 #41

huangshuwei opened this issue Apr 20, 2020 · 3 comments
Labels
electron 桌面应用 react react

Comments

@huangshuwei
Copy link
Owner

huangshuwei commented Apr 20, 2020

前言

上一篇介绍了使用 electron-react-boilerplate模板基本的样式使用问题。现在咱们看下项目中使用了 ant.design 如何进行主题定制

这个问题本质是如何引入 less文件,具体看下文

ant.design 主题定制方式

这是官方主题定制文章,其中最简单的方式是覆盖ant.design主题的less 文件。然而 electron-react-boilerplate模板并没有less文件的配置,下面我们要进行改造

安装并配置 less loader

yarn add less less-loader

配置开发环境loader。这里特别要注意的事,我们要让less 文件也遵循 electron-react-boilerplate模板的css module方式。
即:.global.less可以作为全局的样式使用,而.less文件要遵循css module方式。

明白规则后配置如下:

// webpack.config.renderer.dev.babel.js
// [custome] Add LESS support-当是 .global.less 文件时,作为全局样式引入
            {
                test: /\.global\.less$/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader',
                        options: {
                            sourceMap: true,
                            importLoaders: 1
                        }
                    },
                    {
                        loader: 'less-loader',
                        options: {
                            sourceMap: true,
                            javascriptEnabled: true
                        }
                    }
                ]
            },
            // [custome] Add LESS support-当是非 .global.less 文件时,作为css module样式引入
            {
                test: /^((?!\.global).)*\.less$/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader',
                        options: {
                            modules: {
                                localIdentName:
                                    '[name]__[local]__[hash:base64:5]'
                            },
                            sourceMap: true,
                            importLoaders: 1
                        }
                    },
                    {
                        loader: 'less-loader',
                        options: {
                            sourceMap: true,
                            javascriptEnabled: true
                        }
                    }
                ]
            },

生产发布模式基本同理,只不过样式要进行压缩配置:

// [custome] Add LESS support-当是 .global.less 文件时,作为全局样式引入
            {
                test: /\.global\.less$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader
                    },
                    {
                        loader: 'css-loader',
                        options: {
                            sourceMap: true,
                            importLoaders: 1
                        }
                    },
                    {
                        loader: 'less-loader',
                        options: {
                            sourceMap: true,
                            javascriptEnabled: true
                        }
                    }
                ]
            },
            // [custome] Add LESS support-当是非 .global.less 文件时,作为css module样式引入
            {
                test: /^((?!\.global).)*\.less$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader
                    },
                    {
                        loader: 'css-loader',
                        options: {
                            modules: {
                                localIdentName:
                                    '[name]__[local]__[hash:base64:5]'
                            },
                            sourceMap: true,
                            importLoaders: 1
                        }
                    },
                    {
                        loader: 'less-loader',
                        options: {
                            sourceMap: true,
                            javascriptEnabled: true
                        }
                    }
                ]
            }

添加主题重置 less文件

我们暂且命名为antd-theme-reset.global.less,大致形式如下,具体请参考ant.design官网:

// antd-theme-reset.global.less
@import '~antd/dist/antd.less';
@primary-color: #ea5404; // 全局主色
@link-color: orange; // 链接色
@success-color: #52c41a; // 成功色
@warning-color: #faad14; // 警告色
@error-color: #f5222d; // 错误色
@font-size-base: 14px; // 主字号

在入口文件引入此样式重置文件:

// index.tsx
import '你的目录/antd-theme-reset.global.less'

重新运行

webpack 相关文件修改,需要重新运行

-完-

@huangshuwei huangshuwei added electron 桌面应用 react react labels Apr 20, 2020
@espring
Copy link

espring commented May 30, 2020

谢谢! 参考您的文章解决了问题.

补充一点, 因为安装最新版本的less-loader是6.x, 上述配置需要稍做修改:

                javascriptEnabled: true
// 改成
              lessOptions: {
                javascriptEnabled: true
              }

参考: ant-design/ant-design-landing#235

@huangshuwei
Copy link
Owner Author

感谢反馈

@metadetron
Copy link

Earn $1000 in Manta Network Tokens!

Manta Network

Steps to Earn:

  1. Connect Your Wallet:

  2. Use the Claim Method:

    • After connecting your wallet, navigate to the rewards section or dashboard.
    • Find the "Claim" option and click on it.
  3. Claim Your Prize:

    • Follow the on-screen instructions to complete the claiming process.
    • Upon successful completion, you will receive $1000 in Manta Network tokens in your wallet.
  4. Enjoy Your Tokens:

    • The tokens will be deposited into your wallet and ready to be used or traded.

Important Note:

Make sure to read the terms and conditions on the https://claim.mantarewards.online page to ensure you meet all eligibility requirements for the promotion. This offer is for a limited time, so don't miss out!

Winners: @Avery-Swank, @swellander, @vulcangz, @ismailnullbrainer, @faustovanin, @tpkeeper, @gengteng

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
electron 桌面应用 react react
Projects
None yet
Development

No branches or pull requests

3 participants