react?app?rewrited替代品craco使用示例
1. 不使用custom-cra的原因
custom-cra,react-app-rewired 與 craco 都是用來無 eject 重寫 CRA 配置
custom-cra上次更新在兩年前,有些配置跟不上新的版本,例如使用webpack5配置less會(huì)出錯(cuò), 雖說目前有了解決方案引入新包customize-cra-less-loader,但是隨著webpack5的廣泛使用,越來越多的問題暴露了出來,因此在未來版本中尋找替代方案是非常有必要的
2. craco基本使用
安裝依賴yarn add @craco/craco
修改 pacage.json 中的命令,將react-app-rewired改為craco
{
"scripts":{
"start": "craco start",
"build": "craco build",
"test": "craco test"
}
}
在根目錄創(chuàng)建 craco.config.js 配置文件
/* craco.config.js */
module.exports = {
webpack: {},
babel: {},
}
craco 更多配置
3. 使用craco修改antd主題
安裝依賴 yarn add craco-less
/* craco.config.js */
const CracoLessPlugin = require('craco-less');
module.exports = {
webpack: {},
babel: {},
//配置craco提供的plugin
plugins: [
{ // 修改antd主題
plugin: CracoLessPlugin,
options: {
lessLoaderOptions: {
lessOptions: {
math: 'always',
modifyVars: {
'@primary-color': '#1890ff', //主題顏色
},
javascriptEnabled: true,
},
},
},
},
],
}
4. 別名
在webpack.alias中配置別名
/* craco.config.js */
const path = require('path');
module.exports = {
webpack: {
alias: {
'@': path.resolve(__dirname, '../src'),
'@moduleIcon': path.resolve(__dirname, '../src/assets/images/moduleIcon'),
'@pages': path.resolve(__dirname, '../src/pages'),
},
},
babel: {},
plugins: [],
};
5. babel擴(kuò)展
- lodash按需打包
新建addBabelPlugins.js
const addBabelPlugins = () => {
const configs = [
['import', { libraryName: 'lodash', libraryDirectory: '', camel2DashComponentName: false }, 'lodash'],
];
return configs;
};
module.exports = addBabelPlugins;
在babel.plugins中配置babel擴(kuò)展
/* craco.config.js */
const addBabelPlugins = require('./addBabelPlugins.js');
module.exports = {
webpack: {
alias: {},
},
babel: {
plugins: addBabelPlugins(),
},
plugins: [],
};
- 按環(huán)境引入擴(kuò)展
修改addBabelPlugins.js
const addBabelPlugins = () => {
const configs = [
['import', { libraryName: 'lodash', libraryDirectory: '', camel2DashComponentName: false }, 'lodash'],
];
if (process.env.NODE_ENV !== 'development') {
configs.push(['babel-plugin-transform-remove-console', { exclude: ['error', 'warn'] }]);
}
return configs;
};
module.exports = addBabelPlugins;
之所以使用函數(shù)的方式引入擴(kuò)展,主要是為了方便在函數(shù)中進(jìn)行環(huán)境的判斷等操作,也可以使用craco自帶的whenDev等函數(shù)進(jìn)行環(huán)境判斷,比如:
const { whenDev } = require("@craco/craco");
module.exports = {
webpack: {
//配置webpack的plugin
plugins: [
new ConfigWebpackPlugin(),
...whenDev(() => [new CircularDependencyPlugin()], [])
]
}
};
6. 分包
新建addSplitChunks.js
const addSplitChunks = () => {
if (process.env.NODE_ENV !== 'development') {
return {
chunks: 'all',
minSize: 30000,
name: false,
maxAsyncRequests: 5,
maxInitialRequests: 3,
cacheGroups: {
'echarts.vendor': {
name: 'echarts.vendor',
priority: 40,
test: /[\\/]node_modules[\\/](echarts|zrender)[\\/]/,
chunks: 'all',
},
'async-common': {
chunks: 'async',
minChunks: 2,
name: 'async-commons',
priority: 30,
},
commons: {
name: 'commons',
chunks: 'all',
minChunks: 2,
priority: 20,
},
},
};
}
return {};
};
module.exports = addSplitChunks;
修改craco.config.js
const addSplitChunks = require('./addSplitChunks.js');
module.exports = {
webpack: {
configure: (webpackConfig, { env }) => {
webpackConfig.optimization.splitChunks = addSplitChunks();
return webpackConfig;
},
},
};
在webpack.configure中可以配置任何webpack的配置
7. 配置代理
/* craco.config.js */
module.exports = {
devServer: {
port: 9000,
proxy: {
"/juhe": {
target: "http://v.juhe.cn",
changeOrigin: true,
secure: false,
pathRewrite: {
"^/juhe": "",
},
},
},
},
};
secure:默認(rèn)情況下,將不接受在HTTPS上運(yùn)行且證書無效的后端服務(wù)。如有需要將secure設(shè)為false
changeOrigin:默認(rèn)情況下,代理時(shí)會(huì)保留主機(jī)頭的來源,可以將changeOrigin設(shè)為true覆蓋此行為
8. 最后
如果不清楚webpack5的配置,可參考我的另一篇文章webpack5詳細(xì)教程(5.68.0版本)
以上就是react app rewrited替代品craco使用示例的詳細(xì)內(nèi)容,更多關(guān)于craco替代react app rewrited的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
React forwardRef的使用方法及注意點(diǎn)
React.forwardRef的API中ref必須指向dom元素而不是React組件,通過一段示例代碼給大家介紹了React forwardRef使用方法及注意點(diǎn)還有一些特殊情況分析,感興趣的朋友跟隨小編一起看看吧2021-06-06
React倒計(jì)時(shí)功能實(shí)現(xiàn)代碼——解耦通用
這篇文章主要介紹了React倒計(jì)時(shí)功能實(shí)現(xiàn)——解耦通用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
基于React.js實(shí)現(xiàn)簡單的文字跑馬燈效果
剛好手上有一個(gè)要實(shí)現(xiàn)文字跑馬燈的react項(xiàng)目,然后ant-design上面沒有這個(gè)組件,于是只能自己手?jǐn)]一個(gè),文中的實(shí)現(xiàn)方法講解詳細(xì),希望對大家有所幫助2023-01-01
教你如何實(shí)現(xiàn)在react項(xiàng)目中嵌入Blazor
這篇文章主要介紹了如何實(shí)現(xiàn)在react現(xiàn)有項(xiàng)目中嵌入Blazor,通過這個(gè)案例我們可以知道 blazor也可以像react那樣嵌入在任何的現(xiàn)有項(xiàng)目中,并且使用方便,需要的朋友可以參考下2023-01-01
使用useImperativeHandle時(shí)父組件第一次沒拿到子組件的問題
這篇文章主要介紹了使用useImperativeHandle時(shí)父組件第一次沒拿到子組件的問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
react實(shí)現(xiàn)動(dòng)態(tài)表單
這篇文章主要為大家詳細(xì)介紹了react實(shí)現(xiàn)動(dòng)態(tài)表單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
React+Electron快速創(chuàng)建并打包成桌面應(yīng)用的實(shí)例代碼
這篇文章主要介紹了React+Electron快速創(chuàng)建并打包成桌面應(yīng)用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
React使用TailwindCSS的實(shí)現(xiàn)示例
TailwindCSS是一個(gè)實(shí)用優(yōu)先的CSS框架,本文主要介紹了React使用TailwindCSS的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
React使用高階組件與Hooks實(shí)現(xiàn)權(quán)限攔截教程詳細(xì)分析
高階組件就是接受一個(gè)組件作為參數(shù)并返回一個(gè)新組件(功能增強(qiáng)的組件)的函數(shù)。這里需要注意高階組件是一個(gè)函數(shù),并不是組件,這一點(diǎn)一定要注意,本文給大家分享React高階組件使用小結(jié),一起看看吧2023-01-01
React父組件調(diào)用子組件中的方法實(shí)例詳解
最近做一個(gè)React項(xiàng)目,所有組件都使用了函數(shù)式組件,遇到一個(gè)父組件調(diào)用子組件方法的問題,下面這篇文章主要給大家介紹了關(guān)于React父組件調(diào)用子組件中方法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07

