What is @craco/craco?
@craco/craco is a configuration layer for Create React App (CRA) that allows you to customize the default CRA configuration without ejecting. It provides a way to override the Webpack configuration, Babel configuration, and other settings in a more flexible and maintainable way.
What are @craco/craco's main functionalities?
Webpack Configuration Override
This feature allows you to override the default Webpack configuration provided by Create React App. In this example, the CracoLessPlugin is used to customize the primary color in a LESS file.
const CracoLessPlugin = require('craco-less');
module.exports = {
plugins: [
{
plugin: CracoLessPlugin,
options: {
lessLoaderOptions: {
lessOptions: {
modifyVars: { '@primary-color': '#1DA57A' },
javascriptEnabled: true,
},
},
},
},
],
};
Babel Configuration Override
This feature allows you to override the default Babel configuration. In this example, the Babel plugin for decorators is added to the configuration.
module.exports = {
babel: {
plugins: [
['@babel/plugin-proposal-decorators', { legacy: true }],
],
},
};
ESLint Configuration Override
This feature allows you to override the default ESLint configuration. In this example, the 'no-console' rule is turned off.
module.exports = {
eslint: {
enable: true,
mode: 'extends',
configure: {
rules: {
'no-console': 'off',
},
},
},
};
Other packages similar to @craco/craco
customize-cra
customize-cra is another package that allows you to override the default Create React App configuration without ejecting. It provides a set of utilities to customize Webpack, Babel, and other configurations. Compared to @craco/craco, customize-cra is more focused on providing utility functions for common customizations, whereas @craco/craco offers a more structured plugin system.
react-app-rewired
react-app-rewired is a package that lets you override the Create React App configuration without ejecting. It allows you to modify the Webpack configuration and other settings by providing a config-overrides.js file. Compared to @craco/craco, react-app-rewired is simpler but less flexible, as it does not offer a plugin system.
craco
Create React App Configuration Override is an easy and comprehensible configuration layer for create-react-app v2.
Get all the benefits of create-react-app and customization without using 'eject' by adding a single craco.config.js
file at the root of your application and customize your eslint, babel, postcss configurations and many more.
All you have to do is create your app using create-react-app and customize the configuration with a craco.config.js
file.
Acknowledgements:
We are grateful to @timarney the creator of react-app-rewired for his original idea.
Also, please note that the configuration style of this plugin has been greatly influenced by the way Vue CLI does it.
Please Note:
By doing this you're breaking the "guarantees" that CRA provides. That is to say you now "own" the configs. No support will be provided. Proceed with caution.
Installation
Install the plugin from npm:
$ npm install @craco/craco --save-dev
Create a craco.config.js
file in the root directory:
my-app
├── node_modules
├── craco.config.js
└── package.json
Export your configuration as an object literal:
module.exports = {
...
}
or a function:
module.exports = function({ env, paths }) {
return {
...
};
}
Update the existing calls to react-scripts
in the scripts
section of your package.json
file to use the craco
CLI:
/* package.json */
"scripts": {
- "start": "react-scripts start",
+ "start": "craco start",
- "build": "react-scripts build",
+ "build": "craco build"
}
Start your app for development:
$ npm start
Or build your app:
$ npm run build
CLI Options
When you execute craco start
or craco build
a few options are available.
To change the location of the configuration file:
$ npm start craco --config config/my-cra-customized-config.js
To use a custom version of the react-scripts
packages:
$ npm start craco --react-scripts react-scripts-ts
To activate verbose logging:
$ npm start craco --verbose
Configuration Overview
When the property mode is available there are 2 possible values:
extends
: the provided configuration will extends the CRA settingsfile
: the CRA settings will be reseted and you will provide an official configuration file for the plugin (postcss, eslint) that will supersede any settings.
const { paths, when, whenDev, whenProd, ESLINT_MODES, POSTCSS_MODES } = require("craco");
module.exports = {
style: {
modules: {
localIdentName: ""
},
css: {
loaderOptions: {} || (cssLoaderOptions, { env, paths }) => { return cssLoaderOptions; }
},
sass: {
loaderOptions: {} || (sassLoaderOptions, { env, paths }) => { return sassLoaderOptions; }
},
postcss: {
mode: "extends" || "file",
plugins: [],
loaderOptions: {} || (postcssLoaderOptions, { env, paths }) => { return postcssLoaderOptions; }
}
},
eslint: {
enable: true,
mode: "extends" || "file",
formatter: "",
globals: [],
plugins: [],
extends: [],
rules: {},
loaderOptions: {} || (eslintOptions, { env, paths }) => { return eslintOptions; }
},
webpack: {
alias: {},
plugins: []
},
configureWebpack: {} || (webpackConfig, { env, paths }) => { return webpackConfig; },
devServer: {},
babel: {
presets: [],
plugins: [],
loaderOptions: {} || (babelLoaderOptions, { env, paths }) => { return babelLoaderOptions; }
},
plugins: [
{
plugin: {
overrideCracoConfig: ({ cracoConfig, pluginOptions, context: { env, paths } }) => { return cracoConfig; },
overrideWebpackConfig: ({ webpackConfig, cracoConfig, pluginOptions, context: { env, paths } }) => { return webpackConfig; },
},
options: {}
}
]
};
Develop a plugin
Acknowledgements
@timarney for having created react-app-rewired.
License
Copyright © 2018, Groupe Sharegate inc. This code is licensed under the Apache License, Version 2.0. You may obtain a copy of this license at https://github.com/sharegate/craco/blob/master/LICENSE.