最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react?app?rewrited替代品craco使用示例

 更新時(shí)間:2022年11月07日 14:47:25   作者:敲代碼的彭于晏  
這篇文章主要為大家介紹了react?app?rewrited替代品craco使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

1. 不使用custom-cra的原因

custom-cra,react-app-rewiredcraco 都是用來(lái)無(wú) eject 重寫 CRA 配置

custom-cra上次更新在兩年前,有些配置跟不上新的版本,例如使用webpack5配置less會(huì)出錯(cuò), 雖說(shuō)目前有了解決方案引入新包customize-cra-less-loader,但是隨著webpack5的廣泛使用,越來(lái)越多的問題暴露了出來(lái),因此在未來(lái)版本中尋找替代方案是非常有必要的

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ú)效的后端服務(wù)。如有需要將secure設(shè)為false

changeOrigin:默認(rèn)情況下,代理時(shí)會(huì)保留主機(jī)頭的來(lái)源,可以將changeOrigin設(shè)為true覆蓋此行為

8. 最后

如果不清楚webpack5的配置,可參考我的另一篇文章webpack5詳細(xì)教程(5.68.0版本)

以上就是react app rewrited替代品craco使用示例的詳細(xì)內(nèi)容,更多關(guān)于craco替代react app rewrited的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • react-dnd實(shí)現(xiàn)任意拖動(dòng)與互換位置

    react-dnd實(shí)現(xiàn)任意拖動(dòng)與互換位置

    這篇文章主要為大家詳細(xì)介紹了react-dnd實(shí)現(xiàn)任意拖動(dòng)與互換位置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Component與PureComponent對(duì)比解析

    Component與PureComponent對(duì)比解析

    這篇文章主要為大家介紹了Component與PureComponent解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • 一文詳解React中如何實(shí)現(xiàn)組件懶加載

    一文詳解React中如何實(shí)現(xiàn)組件懶加載

    懶加載是一種優(yōu)化技術(shù),旨在延遲加載不必要的資源,直到它們真正需要時(shí)再進(jìn)行加載,那么React的懶加載是如何實(shí)現(xiàn)的呢,下面小編就來(lái)和大家詳細(xì)講講吧
    2025-03-03
  • react結(jié)合bootstrap實(shí)現(xiàn)評(píng)論功能

    react結(jié)合bootstrap實(shí)現(xiàn)評(píng)論功能

    這篇文章主要為大家詳細(xì)介紹了react結(jié)合bootstrap實(shí)現(xiàn)評(píng)論功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • React BootStrap用戶體驗(yàn)框架快速上手

    React BootStrap用戶體驗(yàn)框架快速上手

    這篇文章主要介紹了React BootStrap用戶體驗(yàn)框架快速上手的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-03-03
  • ReactJS應(yīng)用程序中設(shè)置Axios攔截器方法demo

    ReactJS應(yīng)用程序中設(shè)置Axios攔截器方法demo

    這篇文章主要為大家介紹了ReactJS應(yīng)用程序中設(shè)置Axios攔截器方法demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • react組件中過(guò)渡動(dòng)畫的問題解決

    react組件中過(guò)渡動(dòng)畫的問題解決

    這篇文章主要為大家介紹了react組件中過(guò)渡動(dòng)畫的問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • React實(shí)現(xiàn)路由鑒權(quán)的實(shí)例詳解

    React實(shí)現(xiàn)路由鑒權(quán)的實(shí)例詳解

    React應(yīng)用中的路由鑒權(quán)是確保用戶僅能訪問其授權(quán)頁(yè)面的方式,用于已登錄或具有訪問特定頁(yè)面所需的權(quán)限,這篇文章就來(lái)記錄下React實(shí)現(xiàn)路由鑒權(quán)的流程,需要的朋友可以參考下
    2024-07-07
  • react-router-dom5如何升級(jí)到6

    react-router-dom5如何升級(jí)到6

    這篇文章主要介紹了react-router-dom5如何升級(jí)到6問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • React中this丟失的四種解決方法

    React中this丟失的四種解決方法

    這篇文章主要給大家介紹了關(guān)于React中this丟失的四種解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用React具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03

最新評(píng)論

文化| 思茅市| 绍兴市| 东乡| 彭阳县| 谢通门县| 恩平市| 安吉县| 荔浦县| 游戏| 家居| 龙门县| 贵南县| 阿城市| 富锦市| 都江堰市| 昌图县| 泰兴市| 莱西市| 嘉禾县| 铜陵市| 嘉善县| 大名县| 贵州省| 泸定县| 乐昌市| 黄骅市| 金坛市| 教育| 宁德市| 龙海市| 万宁市| 南陵县| 涿鹿县| 宜昌市| 长宁区| 个旧市| 怀安县| 麦盖提县| 方城县| 临猗县|