Skip to content
This repository was archived by the owner on Oct 10, 2023. It is now read-only.

Commit 46c3d9e

Browse files
SaiCharanMahadevanSai
andauthored
feat: add the ability to customize Antd theme variables (#5)
* feat: add babel-import-plugin + less loaders to support dynamic imports of antd comps * misc: update package to pelago-react-scripts * refactor: add comments * feat: add plugin to switch momentjs with dayjs for antd comps * misc: rebase antd changes on latest CRA tag v3.4.1 * misc: update the CRA and react-scripts readme * misc: update parent readme * feat: add the ability to customize Antd theme variables * feat: add babel-import-plugin + less loaders to support dynamic imports of antd comps * misc: rebase antd changes on latest CRA tag v3.4.1 * misc: update the CRA and react-scripts readme * feat: add the ability to customize Antd theme variables Co-authored-by: Sai <[email protected]>
1 parent 9ee5b17 commit 46c3d9e

File tree

3 files changed

+912
-59
lines changed

3 files changed

+912
-59
lines changed

packages/react-scripts/config/webpack.config.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin');
3333
const ForkTsCheckerWebpackPlugin = require('react-dev-utils/ForkTsCheckerWebpackPlugin');
3434
const typescriptFormatter = require('react-dev-utils/typescriptFormatter');
3535
const AntdDayjsWebpackPlugin = require('antd-dayjs-webpack-plugin');
36+
const lessToJs = require('less-vars-to-js');
3637
// @remove-on-eject-begin
3738
const getCacheIdentifier = require('react-dev-utils/getCacheIdentifier');
3839
// @remove-on-eject-end
@@ -62,7 +63,10 @@ const lessRegex = /\.less$/;
6263
const lessModuleRegex = /\.module\.less$/;
6364
const sassRegex = /\.(scss|sass)$/;
6465
const sassModuleRegex = /\.module\.(scss|sass)$/;
65-
66+
// Read the antd variable overrides from respective app's `ant-default-vars.less`
67+
const themeVariables = lessToJs(
68+
fs.readFileSync(path.join(process.cwd(), 'src/ant-default-vars.less'), 'utf8')
69+
);
6670
// This is the production and development configuration.
6771
// It is focused on developer experience, fast rebuilds, and a minimal bundle.
6872
module.exports = function(webpackEnv) {
@@ -81,7 +85,11 @@ module.exports = function(webpackEnv) {
8185
const env = getClientEnvironment(paths.publicUrlOrPath.slice(0, -1));
8286

8387
// common function to get style loaders
84-
const getStyleLoaders = (cssOptions, preProcessor) => {
88+
89+
// Each loader's options prop seem to be typed and so throws an error even if we pass a
90+
// null / undefined prop as part of `cssOptions`. Hence passing a separate `additionalCssOptions` param
91+
// to expose the ability to add custom css options.
92+
const getStyleLoaders = (cssOptions, preProcessor, customCssOptions) => {
8593
const loaders = [
8694
isEnvDevelopment && require.resolve('style-loader'),
8795
isEnvProduction && {
@@ -134,6 +142,7 @@ module.exports = function(webpackEnv) {
134142
loader: require.resolve(preProcessor),
135143
options: {
136144
sourceMap: true,
145+
...customCssOptions,
137146
},
138147
}
139148
);
@@ -557,7 +566,12 @@ module.exports = function(webpackEnv) {
557566
importLoaders: 3,
558567
sourceMap: isEnvProduction && shouldUseSourceMap,
559568
},
560-
'less-loader'
569+
'less-loader',
570+
{
571+
// variable overrides for antd default less variables
572+
// https://github.com/ant-design/ant-design/blob/master/components/style/themes/default.less
573+
modifyVars: themeVariables,
574+
}
561575
),
562576
// Don't consider CSS imports dead code even if the
563577
// containing package claims to have no side effects.

0 commit comments

Comments
 (0)