Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate React-app-rewired with Customize-CRA

I want to override the Webpack config by react-app-rewired. But I using Ant design for my project, so I must use Customize-CRA to import Babel plugin, etc. How to using React-app-rewired and Customize-CRA together.

The config-overrides.js for React-app-rewired as below:

module.exports = function override(config, env) {
  config.module.rules = config.module.rules.map(rule => {
        if (rule.oneOf instanceof Array) {
            return {
                ...rule,
                oneOf: [
                    {
                        test: /\.(svg|png|jpg|jpeg|gif|bmp|tiff)$/i,
                        use: [
                            {
                                loader: 'file-loader',
                                options: {
                                    name: '[path][name]-[hash:8].[ext]'
                                }
                            }
                        ]
                    },
                    ...rule.oneOf
                ]
            };
        }

        return rule;
    });

  return config;
}

The config-overrides.js for Customize-CRA as below:

const {override, fixBabelImports, addLessLoader, addDecoratorsLegacy, disableEsLint} = require('customize-cra');

module.exports = override(
  addDecoratorsLegacy(),
  disableEsLint(),
  fixBabelImports('import', {
    libraryName: 'antd',
    libraryDirectory: 'es',
    style: true,
  }),
  addLessLoader({
    javascriptEnabled: true,
    modifyVars: {'@primary-color': '#a50052'},
  }),
);

Thank you.

like image 457
Kary Lyoko Avatar asked Apr 01 '19 05:04

Kary Lyoko


People also ask

What is customize CRA?

customize-cra takes advantage of react-app-rewired 's config-overrides. js file. By importing customize-cra functions and exporting a few function calls wrapped in our override function, you can easily modify the underlying config objects ( webpack , webpack-dev-server , babel , etc.) that make up create-react-app .

How do you update Webpack config in Create-react-app?

Because If you want to add some loaders or update some configurations we need to update the webpack config of create-react-app . How to do it ? Go to node_modules/react-scripts/config/webpack. config.


1 Answers

I think I ran into the same problem you did. The customize-cra override takes any number of override functions as arguments. Each function is given config as its first argument. Think of it as a compose function. Take your existing override export, put it into a function you define, I called myOverrides or something, then export customize-cra's override with your override function as one of the arguments.

before:

module.exports = function override(config, env){
  // do stuff to config
  return config
}

after:

function myOverrides(config) {
  // do stuff to config
  return config
}

module.exports = override(
  myOverrides,
  addDecoratorsLegacy(),
  disableEsLint(),
  fixBabelImports('import', {
    libraryName: 'antd',
    libraryDirectory: 'es',
    style: true,
  }),
  addLessLoader({
    javascriptEnabled: true,
    modifyVars: {'@primary-color': '#a50052'},
  }),
);
like image 98
Trevor Hewitt Avatar answered Nov 15 '22 23:11

Trevor Hewitt