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

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

Open
saileshkotha opened this issue Jun 29, 2020 · 2 comments

Comments

@saileshkotha
Copy link

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 saileshkotha changed the title Do we really need this package if we use create-react-app and react-app-rewired? (Trying to understand) Do we really need this package if we use create-react-app and react-app-rewired? (Trying to understand) (Working code below) Jun 29, 2020
@MildTomato
Copy link

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

@nad182
Copy link

nad182 commented Jul 29, 2020

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').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants