1. Introduction
-
craco
It is aCreate React App(CRA)
tool for expansion andCRA
aReact
scaffolding tool for quickly building applications.CRA
Provides a simple project structure and configuration, allowing developers to quickly startReact
the development of a project.However
CRA
, the configuration is hidden and developers cannot customize and extend it. That'scraco
why .craco(Create React App Configuration Override)
Allows developers to override and extendCRA
the configuration to meet more complex project needs.By using
craco
, developers can modify , , , etc. without popping upCRA
the configuration of . Provides a simple way to override the default configuration of , while retaining the simplicity and ease of use of .webpack 配置
babel 配置
ESLint 配置
devServer配置
craco
CRA
CRA
Through this
craco
, developers can use customwebpack 插件
, ,babel 插件
and其他工具
to meet the specific needs of the project. For example: you can add a custom onewebpack loader
, configure a custombabel preset
oneplugin
, or modifywebpack
the output path, etc.
2. Use
- Install
$ npm i -D @craco/craco
-
The configuration file created in the root directory
craco.config.js
can be understood asVue
thrown in and out of the projectvue.config.js
.my-app ├── node_modules + ├── craco.config.js └── package.json
Other writing methods for configuration file names are supported. If multiple configuration files are found,
craco
the highest one in the above list will be used:1、craco.config.ts 2、craco.config.js 3、craco.config.cjs 4、.cracorc.ts 5、.cracorc.js 6、.cracorc
-
If there are multiple configuration files, you can also
package.json
specify the configuration file by adding fields:... { "cracoConfig": "config/craco-config-with-custom-name.js" } ...
Or
scripts
specify the configuration file in the instruction to compile:{ "scripts": { "start": "craco start --config config/craco-config-with-custom-name.js" } }
-
Modify the configuration
package.json
in the file and replace it with , for example:scripts
react-scripts
craco
"scripts": { - // "start": "react-scripts start", - // "build": "react-scripts build", - // "test": "react-scripts test", + "start": "craco start", + "build": "craco build", + "test": "craco test", "eject": "react-scripts eject" }
-
In
craco.config.js
the file, the configuration of can be modified and extended as neededCRA
. Here is the basic structure of an example configuration file:It can basically cover all required configurations for normal business. For more configurations, you can view the official documentation :
const path = require('path') const lessPlugin = require('craco-less') module.exports = { // 插件配置 plugins: [ { plugin: lessPlugin, options: { lessLoaderOptions: { lessOptions: { // modifyVars: { '@primary-color': '#1DA57A' }, javascriptEnabled: true } } } } // ... ], // 修改 babel 配置 babel: { // plugins: [ // ['@babel/plugin-proposal-decorators', { legacy: true }], // [ // 'import', // { // 'libraryName': 'antd', // 'libraryDirectory': 'es', // 'style': 'css' // 设置为 true 即是 less 这里用的是 css // } // ] // ] // ... }, // 修改 eslint 配置 eslint: { // ... }, // 修改 module 配置 module: { // rules:[// 规则,在写 style.module.scss 的时候发现引入后缀为 .scss 会报错,在这里配置一下即可 // { // test: /.scss$/, // loaders: ['style-loader', 'css-loader', 'sass-loader'] // } // ] // ... }, // 跨域配置 devServer: { proxy: { '/api': { target: 'https://xxx:8080', changeOrigin: true, pathRewrite: { '^/api': '' } } } // ... }, // webpack 配置 webpack: { // 配置别名 alias: { // 约定:使用 @ 表示 src 文件所在路径 '@': path.resolve(__dirname, 'src') }, // webpack 插件 plugins: { add: [ /* ... */], remove: [ /* ... */], }, // plugins: [ // new ConfigWebpackPlugin(), // ...whenDev(() => [new CircularDependencyPlugin()], []), // ], // 这里面是 webpack 原始配置,这里追加的配置,将与原始配置合并 configure: { resolve: { fallback: { 'path': false, 'util': false, 'url': false, 'http': false, 'https': false, 'stream': false, 'assert': false, 'querystring': false, 'zlib': false } } } // ... } }