antd-scss-theme-plugin icon indicating copy to clipboard operation
antd-scss-theme-plugin copied to clipboard

Do we really need this package if we use create-react-app and react-app-rewired? (Trying to understand) (Working code below)

Open saileshkotha opened this issue 4 years ago • 2 comments

My current project is currently in Ant.d, Less, Create-react-app and uses react-app-rewired, customize-cra for custom webpack configurations.

File: ./src/antdCustom.scss
---------------------------
$btn-primary-bg: #616161;
$color: black;

Current config-override.js looks like this, and it works perfectly. Please tell me why I need this package?

File: ./config-overrides.js
-------------------------
const {adjustWorkbox, fixBabelImports, addLessLoader, override, adjustStyleLoaders, addWebpackPlugin } = require('customize-cra');
const fs = require('fs');
const _ = require('lodash');
const  scssToJson = require('scss-to-json');

const scssThemeVariables = scssToJson('./src/antdCustom.scss');
const lessThemeVariables = _.mapKeys(scssThemeVariables, (value, key) => key.replace(/^\$/, '@'));

console.log(lessThemeVariables)

module.exports = override(
  fixBabelImports('import', {
    libraryName: 'antd',
    libraryDirectory: 'es',
    style: true,
  }),
  addLessLoader({
    lessOptions:{
      javascriptEnabled: true,
      modifyVars: lessThemeVariables
    }
  })
);

Still trying to understand why I need this package? Please, help!

saileshkotha avatar Jun 29 '20 00:06 saileshkotha

I'll take a look at this later. This might be exactly what I need :-)

MildTomato avatar Jul 15 '20 09:07 MildTomato

It's not going to work if you're trying to change a property with a variable that hasn't been defined in your antdCustom.scss file. For example, if you didn't define @highlight-color in your file and you're trying to set '@label-required-color': '@highlight-color'; (even though it's defined in 'antd/dist/antd.less'), it's going to throw an error.

The only way to to deal with the issue (and I've tried around 7-8 different approaches, including scss-to-json, less-to-json, this plugin and so on), is to write your styles in js file like so:

theme.js

module.exports = {
  '@primary-color': '#252930',
  '@dark-gray': '#a09fa0',
  '@light-gray': '#f2f1f1'
}

And then in config-overrides.js:

const theme = require('../common/assets/styles/theme')
const {
  ..., // your configs
  addLessLoader,
} = require('customize-cra')

module.exports = override(
  ..., // your configs
  addLessLoader({
    lessOptions: {
      javascriptEnabled: true,
      modifyVars: theme,
    },
  }),

Unfortunately, with this approach (and others I've tried), you can forget about hot reloading when you change theme variables. Other than that, everything works as expected - your variables override the default ones, and the derivative variable are calculated based on your vars (meaning, you don't need to provide values for them, i.e, no need for '@primary-5': '#fff').

nad182 avatar Jul 29 '20 01:07 nad182