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

詳解在create-react-app使用less與antd按需加載

 更新時(shí)間:2018年12月06日 15:30:43   作者:summer_味道制造  
這篇文章主要介紹了詳解在create-react-app使用less與antd按需加載,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

使用antd按需加載

使用react-app-rewired對(duì) create-react-app 的默認(rèn)配置進(jìn)行自定義

1、yarn add react-app-rewired --dev

/* package.json */
"scripts": {
-  "start": "react-scripts start",
+  "start": "react-app-rewired start",
-  "build": "react-scripts build",
+  "build": "react-app-rewired build",
-  "test": "react-scripts test --env=jsdom",
+  "test": "react-app-rewired test --env=jsdom",
}

2.然后在項(xiàng)目根目錄創(chuàng)建一個(gè) config-overrides.js 用于修改默認(rèn)配置。

module.exports = function override(config, env) {
 // do stuff with the webpack config...
 return config;
};

使用 babel-plugin-import

babel-plugin-import 是一個(gè)用于按需加載組件代碼和樣式babel 插件(原理),現(xiàn)在我們嘗試安裝它并修改config-overrides.js 文件

1.yarn add babel-plugin-import --dev

+ const { injectBabelPlugin } = require('react-app-rewired');

 module.exports = function override(config, env) {
+  config = injectBabelPlugin(['import', { libraryName: 'antd', libraryDirectory: 'es', style: 'css' }], config);
  return config;
 };

或者也可修改

{
      test: /\.(js|jsx|mjs)$/,
      include: paths.appSrc,
      loader: require.resolve('babel-loader'),
      options: {
       plugins: [
        ['import', { libraryName: 'antd', style: true }],
       ],
       // This is a feature of `babel-loader` for webpack (not Babel itself).
       // It enables caching results in ./node_modules/.cache/babel-loader/
       // directory for faster rebuilds.
       cacheDirectory: true,
      },
     },

自定義主題

yarn add react-app-rewire-less --dev

 const { injectBabelPlugin } = require('react-app-rewired');
+ const rewireLess = require('react-app-rewire-less');

 module.exports = function override(config, env) {
-  config = injectBabelPlugin(['import', { libraryName: 'antd', style: 'css' }], config);
+  config = injectBabelPlugin(['import', { libraryName: 'antd', style: true }], config);
+  config = rewireLess.withLoaderOptions({
+   modifyVars: { "@primary-color": "#1DA57A" },
+  })(config, env);
  return config;
 };

參考鏈接:https://ant.design/docs/react/use-with-create-react-app-cn

使用less

如果已經(jīng)配置react-app-rewire-less,則無(wú)需再進(jìn)行此操作

1.npm install less-loader less --save-dev
2.修改node_modules/react_script/config下的webpack.config.dev.js 和 webpack.config-prod.js 配置文件

  • test: /.css$/ 改為 /.(css|less)$/
  • test: /.css$/ 的 use 數(shù)組配置增加 less-loader
{
 test: /\.(css|less)$/,
 use: [
  require.resolve('style-loader'),
  {
   loader: require.resolve('css-loader'),
   options: {
    importLoaders: 1,
   },
  },
  {
   loader: require.resolve('postcss-loader'),
   options: {
    // Necessary for external CSS imports to work
    // https://github.com/facebookincubator/create-react-app/issues/2677
    ident: 'postcss',
    plugins: () => [
     require('postcss-flexbugs-fixes'),
     autoprefixer({
      browsers: [
       '>1%',
       'last 4 versions',
       'Firefox ESR',
       'not ie < 9', // React doesn't support IE8 anyway
      ],
      flexbox: 'no-2009',
     }),
    ],
   },
  },
  {
   loader: require.resolve('less-loader') // compiles Less to CSS
  }
 ],
},

使用css Module

antd 和 css modules 不能混用,針對(duì)antd的css 單獨(dú)寫(xiě)一條loader的規(guī)則,不開(kāi)啟 css modules。

使用 exclude 和 include 配置,修改webpack.config.dev.js 和 webpack.config-prod.js 配置文件 (盡量不要使用less-loader 來(lái)處理css文件,在與antd一起使用時(shí)可能出現(xiàn)錯(cuò)誤,單獨(dú)寫(xiě)一條規(guī)則)

    {
      test: /\.css$/,
      exclude: /node_modules|antd\.css/,
      use: [
       require.resolve('style-loader'),
       {
        loader: require.resolve('css-loader'),
        options: {
         importLoaders: 1,
         modules: true,  // 新增對(duì)css modules的支持
         localIdentName: '[name]__[local]__[hash:base64:5]',
        },
       },
       {
        loader: require.resolve('postcss-loader'),
        options: {
         // Necessary for external CSS imports to work
         // https://github.com/facebookincubator/create-react-app/issues/2677
         ident: 'postcss',
         plugins: () => [
          require('postcss-flexbugs-fixes'),
          autoprefixer({
           browsers: [
            '>1%',
            'last 4 versions',
            'Firefox ESR',
            'not ie < 9', // React doesn't support IE8 anyway
           ],
           flexbox: 'no-2009',
          }),
         ],
        },
       },
      ],
     },
     {
      test: /\.less$/,
      exclude: /node_modules|antd\.less/,
      use: [
       require.resolve('style-loader'),
       {
        loader: require.resolve('css-loader'),
        options: {
         importLoaders: 1,
         modules: true,  // 新增對(duì)css modules的支持
         localIdentName: '[name]__[local]__[hash:base64:5]',
        },
       },
       {
        loader: require.resolve('postcss-loader'),
        options: {
         // Necessary for external CSS imports to work
         // https://github.com/facebookincubator/create-react-app/issues/2677
         ident: 'postcss',
         plugins: () => [
          require('postcss-flexbugs-fixes'),
          autoprefixer({
           browsers: [
            '>1%',
            'last 4 versions',
            'Firefox ESR',
            'not ie < 9', // React doesn't support IE8 anyway
           ],
           flexbox: 'no-2009',
          }),
         ],
        },
       },
       {
        loader: require.resolve('less-loader') // compiles Less to CSS
       },
      ],
     },
     {
      test: /\.(css)$/,
      include: /node_modules|antd\.css/,
      use: [
       require.resolve('style-loader'),
       {
        loader: require.resolve('css-loader'),
        options: {
         importLoaders: 1,
        },
       },
       {
        loader: require.resolve('postcss-loader'),
        options: {
         // Necessary for external CSS imports to work
         // https://github.com/facebookincubator/create-react-app/issues/2677
         ident: 'postcss',
         plugins: () => [
          require('postcss-flexbugs-fixes'),
          autoprefixer({
           browsers: [
            '>1%',
            'last 4 versions',
            'Firefox ESR',
            'not ie < 9', // React doesn't support IE8 anyway
           ],
           flexbox: 'no-2009',
          }),
         ],
        },
       },
      ],
     },
     {
      test: /\.(less)$/,
      include: /node_modules|antd\.less/,
      use: [
       require.resolve('style-loader'),
       {
        loader: require.resolve('css-loader'),
        options: {
         importLoaders: 1,
        },
       },
       {
        loader: require.resolve('postcss-loader'),
        options: {
         // Necessary for external CSS imports to work
         // https://github.com/facebookincubator/create-react-app/issues/2677
         ident: 'postcss',
         plugins: () => [
          require('postcss-flexbugs-fixes'),
          autoprefixer({
           browsers: [
            '>1%',
            'last 4 versions',
            'Firefox ESR',
            'not ie < 9', // React doesn't support IE8 anyway
           ],
           flexbox: 'no-2009',
          }),
         ],
        },
       },
       {
        loader: require.resolve('less-loader') // compiles Less to CSS
       },
      ],
     },

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解React中Fragment的簡(jiǎn)單使用

    詳解React中Fragment的簡(jiǎn)單使用

    這篇文章主要介紹了詳解React中Fragment的簡(jiǎn)單使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)我們學(xué)習(xí)React有一定的幫助,感興趣的小伙伴們可以參考一下
    2022-10-10
  • react函數(shù)組件useState異步,數(shù)據(jù)不能及時(shí)獲取到的問(wèn)題

    react函數(shù)組件useState異步,數(shù)據(jù)不能及時(shí)獲取到的問(wèn)題

    這篇文章主要介紹了react函數(shù)組件useState異步,數(shù)據(jù)不能及時(shí)獲取到的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • React實(shí)現(xiàn)全局組件的Toast輕提示效果

    React實(shí)現(xiàn)全局組件的Toast輕提示效果

    這篇文章主要介紹了React實(shí)現(xiàn)全局組件的Toast輕提示效果,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • 詳解React中合并單元格的正確寫(xiě)法

    詳解React中合并單元格的正確寫(xiě)法

    用表格進(jìn)行頁(yè)面布局,頁(yè)面布局在各種瀏覽器的的兼容性, 本文主要介紹了詳解React中合并單元格的正確寫(xiě)法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • 詳解webpack + react + react-router 如何實(shí)現(xiàn)懶加載

    詳解webpack + react + react-router 如何實(shí)現(xiàn)懶加載

    這篇文章主要介紹了詳解webpack + react + react-router 如何實(shí)現(xiàn)懶加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-11-11
  • React ref的使用示例

    React ref的使用示例

    這篇文章主要介紹了React ref的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用React,感興趣的朋友可以了解下
    2021-04-04
  • 詳解react setState

    詳解react setState

    這篇文章主要介紹了react setState的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用react,感興趣的朋友可以了解下
    2021-04-04
  • react實(shí)現(xiàn)Modal彈窗效果

    react實(shí)現(xiàn)Modal彈窗效果

    這篇文章主要為大家詳細(xì)介紹了react實(shí)現(xiàn)Modal彈窗效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 詳解如何在React組件“外”使用父組件的Props

    詳解如何在React組件“外”使用父組件的Props

    這篇文章主要介紹了詳解如何在React組件“外”使用父組件的Props,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • react中使用heatmap.js實(shí)現(xiàn)熱力圖的繪制

    react中使用heatmap.js實(shí)現(xiàn)熱力圖的繪制

    heatmap.js?是一個(gè)用于生成熱力圖的?JavaScript?庫(kù),React?是一個(gè)流行的?JavaScript?庫(kù),用于構(gòu)建用戶界面,本小編給大家介紹了在React?應(yīng)用程序中使用heatmap.js實(shí)現(xiàn)熱力圖的繪制的示例,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12

最新評(píng)論

大理市| 堆龙德庆县| 泰安市| 玉山县| 禹州市| 姜堰市| 吉隆县| 芷江| 九江市| 兰坪| 白水县| 上栗县| 汨罗市| 炉霍县| 于田县| 绥江县| 游戏| 林甸县| 宝清县| 建湖县| 巢湖市| 阿鲁科尔沁旗| 榆中县| 丰县| 镇坪县| 阿克苏市| 永平县| 信丰县| 明溪县| 皋兰县| 原阳县| 扎鲁特旗| 海城市| 无极县| 咸宁市| 清流县| 泸水县| 尚志市| 鹤山市| 贡山| 通化县|