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

詳解使用create-react-app添加css modules、sasss和antd

 更新時(shí)間:2018年07月31日 10:08:10   作者:林大元  
這篇文章主要介紹了詳解使用create-react-app添加css modules、sasss和antd,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

create-react-app 是facebook的官方腳手架,對(duì)于個(gè)人開發(fā)者和中小型公司快速創(chuàng)建項(xiàng)目非常推薦。react的CSS解決方案有很多,這里我技術(shù)選型時(shí)用 css modules 和 sass ,然后配合antd使用通用組件庫(kù)。但是create-react-app原生并不支持 css modules 和 sass ,所以需要額外配置。

配置

增加css modules和sass

使用eject暴露配置

create-react-app 默認(rèn)是沒(méi)有暴露 webpack 配置的,所以需要eject一下。注意如果項(xiàng)目在git倉(cāng)庫(kù)環(huán)境下,先提交代碼到git倉(cāng)庫(kù),否則會(huì)報(bào)錯(cuò)

npm run eject

npm添加css modules和sass

npm install react-css-modules 
npm install sass-loader node-sass

這里安裝sass可能會(huì)遇到墻的問(wèn)題報(bào)錯(cuò),所以要么使用cnpm或者使用本地代理設(shè)置,因?yàn)槲矣衧s所以使用本地代理

// 開啟代理
npm config set proxy http://127.0.0.1:1080
// 安裝完sass后關(guān)閉代理
npm config delete proxy

webpack配置

重點(diǎn)來(lái)了,我們需要給 webpack 配置上 css-modules 和 sass-loader 。但是使用 css-modules 會(huì)使 node_modules 庫(kù)里的css樣式找不到,比如后面要使用到的antd,這個(gè)時(shí)候我們需要inclube來(lái)排除影響 node_modules ,使得 css-modules 不會(huì)影響到 node_modules

修改項(xiàng)目中 config 目錄下的 webpack.config.dev.js 和 webpack.config.prod.js ,說(shuō)明下這兩個(gè)文件,前一個(gè)是開發(fā)環(huán)境 npm start 使用,后一個(gè)是 npm run build 打包后使用

修改 webpack.config.dev.js :

大約在160行左右,找到 test: /\.css$/ ,中文注釋的地方就是修改和增加的地方

{
      test: [/\.css$/, /\.scss$/],// 這里增加SCSS的支持
      exclude: [/node_modules/],// 這里去排除node_modules,防止css modules影響到node_modules
      use: [
       require.resolve('style-loader'),
       {
        loader: require.resolve('css-loader'),
        options: {
         importLoaders: 1,
         modules: true, // 這里增加對(duì)css modules的支持
         localIdentName: '[name]__[local]__[hash:base64:5]' //這里增加對(duì)css modules的支持
        },
       },
       {
        loader: require.resolve('sass-loader'), // 這里增加sass的支持
       },
       {
        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',
          }),
         ],
        },
       },
      ],
     },
     // 因?yàn)樯厦媾懦薱ss_modules所以這里一定要再添加個(gè)排除src來(lái)識(shí)別css_modules
     // 其實(shí)就是復(fù)制之前沒(méi)修改前的所有,再增加一個(gè)exclude: [/src/]
     {
      test: /\.css$/, 
      exclude: [/src/], // 這里添加排除src,
      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',
          }),
         ],
        },
       },
      ],
     }

修改 webpack.config.prod.js :

和上面修改 webpack.config.dev.js 類似

{
      test: [/\.css$/, /\.scss$/], // 這里增加SCSS的支持
      exclude: [/node_modules/], // 這里去排除node_modules
      loader: ExtractTextPlugin.extract(
       Object.assign(
        {
         fallback: {
          loader: require.resolve('style-loader'),
          options: {
           hmr: false,
          },
         },
         use: [
          {
           loader: require.resolve('css-loader'),
           options: {
            importLoaders: 1,
            minimize: true,
            sourceMap: true,
            modules: true, // 這里添加css modules支持
           },
          },
          {
           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('sass-loader'), // 這里添加sass支持
          }
         ],
        },

        extractTextPluginOptions
       )
      ),
      // Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
     },
     {
      test: /\.css$/,
      exclude: [/src/], // 排除src
      loader: ExtractTextPlugin.extract(
       Object.assign(
        {
         fallback: {
          loader: require.resolve('style-loader'),
          options: {
           hmr: false,
          },
         },
         use: [
          {
           loader: require.resolve('css-loader'),
           options: {
            importLoaders: 1,
            minimize: true,
            sourceMap: true,
           },
          },
          {
           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',
             }),
            ],
           },
          }
         ],
        },

        extractTextPluginOptions
       )
      ),
      // Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
     }

安裝antd和配置

主要是安裝配置 antd 和 babel-plugin-import ,這樣可以使得 antd 按需加載樣式

npm添加antd和babel-plugin-import

npm install antd
npm install babel-plugin-import

配置babel

在項(xiàng)目根目錄下增加 .babelrc 文件,然后配置如下

{
 "presets": [
  "react-app"
 ],
 "plugins": [
  "transform-runtime",
  [
   "import",
   {
    "libraryName": "antd",
    "style": "css"
   }
  ]
 ]
}

大功告成

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

相關(guān)文章

  • 在React項(xiàng)目中實(shí)現(xiàn)一個(gè)簡(jiǎn)單的錨點(diǎn)目錄定位

    在React項(xiàng)目中實(shí)現(xiàn)一個(gè)簡(jiǎn)單的錨點(diǎn)目錄定位

    錨點(diǎn)目錄定位功能在長(zhǎng)頁(yè)面和文檔類網(wǎng)站中非常常見,它可以讓用戶快速定位到頁(yè)面中的某個(gè)章節(jié),本文講給大家介紹一下React項(xiàng)目中如何實(shí)現(xiàn)一個(gè)簡(jiǎn)單的錨點(diǎn)目錄定位,文中有詳細(xì)的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2023-09-09
  • React中實(shí)現(xiàn)插槽效果的方案詳解

    React中實(shí)現(xiàn)插槽效果的方案詳解

    在React中是沒(méi)有插槽的概念的, 或者說(shuō)在React中是不需要插槽的, 因?yàn)镽eact對(duì)于這種需要插槽的情況非常靈活,本文給大家分享兩種方案實(shí)現(xiàn),分別是children實(shí)現(xiàn)插槽和props實(shí)現(xiàn)插槽,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-09-09
  • 使用react-native-doc-viewer實(shí)現(xiàn)文檔預(yù)覽

    使用react-native-doc-viewer實(shí)現(xiàn)文檔預(yù)覽

    這篇文章主要介紹了使用react-native-doc-viewer實(shí)現(xiàn)文檔預(yù)覽,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • React中上傳圖片到七牛的示例代碼

    React中上傳圖片到七牛的示例代碼

    本篇文章主要介紹了React中上傳圖片到七牛的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • 詳解React?ISR如何實(shí)現(xiàn)Demo

    詳解React?ISR如何實(shí)現(xiàn)Demo

    這篇文章主要為大家介紹了React?ISR如何實(shí)現(xiàn)Demo詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • react中的雙向綁定你真的了解嗎

    react中的雙向綁定你真的了解嗎

    這篇文章主要為大家詳細(xì)介紹了react中的雙向綁定,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • 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
  • 解決React報(bào)錯(cuò)Property does not exist on type 'JSX.IntrinsicElements'

    解決React報(bào)錯(cuò)Property does not exist on 

    這篇文章主要為大家介紹了React報(bào)錯(cuò)Property does not exist on type 'JSX.IntrinsicElements'解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • React根據(jù)當(dāng)前頁(yè)面路由進(jìn)行自動(dòng)高亮示例代碼

    React根據(jù)當(dāng)前頁(yè)面路由進(jìn)行自動(dòng)高亮示例代碼

    要根據(jù)當(dāng)前頁(yè)面路由自動(dòng)高亮頂部菜單項(xiàng),可以使用 React Router 的 useLocation 鉤子來(lái)獲取當(dāng)前路徑,并根據(jù)路徑動(dòng)態(tài)設(shè)置菜單項(xiàng)的高亮效果,本文給大家介紹了一個(gè)完整的示例,展示如何根據(jù)當(dāng)前頁(yè)面路由自動(dòng)高亮頂部菜單項(xiàng),需要的朋友可以參考下
    2024-07-07
  • React前端解鏈表數(shù)據(jù)結(jié)構(gòu)示例詳解

    React前端解鏈表數(shù)據(jù)結(jié)構(gòu)示例詳解

    這篇文章主要為大家介紹了React前端解鏈表數(shù)據(jù)結(jié)構(gòu)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10

最新評(píng)論

石河子市| 临安市| 太保市| 台南市| 灌南县| 高唐县| 蛟河市| 威海市| 青田县| 锡林郭勒盟| 宣化县| 铅山县| 政和县| 错那县| 灵寿县| 福清市| 大荔县| 扎赉特旗| 唐海县| 连云港市| 聂拉木县| 和田县| 淮安市| 焦作市| 永修县| 临澧县| 清镇市| 乳山市| 桃江县| 北安市| 芒康县| 同心县| 库车县| 刚察县| 乌苏市| 通山县| 铜鼓县| 布尔津县| 荣昌县| 蒲城县| 武汉市|