create-react-app踩坑記


前言

 哇,不的不說這個react 這個腳手架create-react-app腳確實有很多問題,哈哈,下面來看看吧有哪些坑:

 

引用sass或者less    

    記得16年還是幾年是不支持sass,和less的,貌似現在支持了,我配置sass 也遇到很多問題,還是不能正確使用:

   在這個之前:你需要運行 

      npm run eject 

   create-react-app生成的項目文,看不到webpack相關的配置文件,需要先暴露出來:

   然后運行:

 npm install sass-loader node-sass --save-dev

   

      修改webpack配置

       修改 webpack.config.dev.jswebpack.config-prod.js 配置文件

   大概在158行吧:

  

/\.css$/ 改為/\.(css|sass)$/, 

  完整的代碼:

  {
            test: /\.(css|sass)$/,
            use: [
              require.resolve('style-loader'),
              {
                loader: require.resolve('css-loader'),
                options: {
                  importLoaders: 1,
                  modules: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',
                    }),
                  ],
                },
              },
              {
                loader: require.resolve('sass-loader') // compiles sass to CSS
              },
            ],
          },

在198行 添加如下配置:

       {
            // Exclude `js` files to keep "css" loader working as it injects
            // its runtime that would otherwise processed through "file" loader.
            // Also exclude `html` and `json` extensions so they get processed
            // by webpacks internal loaders.
            exclude: [/\.(js|jsx|mjs)$/, /\.html$/, /\.json$/, /\.scss$/],
            loader: require.resolve('file-loader'),
            options: {
              name: 'static/media/[name].[hash:8].[ext]',
            },
          },

        {
            test: /\.scss$/,
            loaders: ['style-loader', 'css-loader', 'sass-loader']
          }

 

添加.sass文件

  我是把安裝的antd ui庫 導入

  當然你需要 npm i antd --save

  我把那個common.scss 文件下引入下面2個 一個是antd 和阿里圖庫,

@import "~antd/dist/antd.sass";
@icon-url: '~antd/dist/iconfont/iconfont';// 把 iconfont 地址改到本地

另外:!!! 還需要將原先在主css文件中添加的@import '~antd/dist/antd.css';語句移除。

 哈哈這樣就好了

 

CSS模塊加載

  react css 都是一塊加載的,如果你那個文件的css 的ID或者class 類一樣了,這樣就會被覆蓋了,還有就是會一起加載,

  這個對於我  3個字:不能忍

  當然有辦法咯:

    在修改 webpack.config.dev.js 和 webpack.config-prod.js 配置文件

   在164 行:

     加入   modules:true,

{
                loader: require.resolve('css-loader'),
                options: {
                  importLoaders: 1,
                  modules:true,
                },
              },

 然后重新 npm start

antd按需加載

   因為antd 樣式很多,那總不能一次性都加載吧,這樣性能很能不好,當然我這里推薦使用 babel-plugin-import 

   npm i  babel-plugin-import --save-dev

   當然我 的方法是 在你的react 需要執行eject命令,這個命令是不可逆的

  需要在暴露的config  文件下 2個文件  

webpack.config.dev.jswebpack.config.prod.js 

添加以下代碼:

  在webpack.config.dev.js 的147行左右

plugins: [
            ['import', [{ libraryName: "antd", style: 'css' }]],
         ],

在webpack.config.prod.js 的153行左右

 

然后重新 run start

 

react 跨越問題

 這個問題我前面也寫了相關的博客 詳情可以往里面看 -》  https://www.cnblogs.com/yf-html/p/9251895.html

生產環境去除sourcemap

修改webpack.config.prod.js

// devtool: shouldUseSourceMap ? 'source-map' : false,
  devtool: false,

 

添加插件 webpack-bundle-analyzer

npm i  webpack-bundle-analyzer --save-dev

修改 webpack.config.prod.js

const BundleAnalyzerPlugin = require(
  'webpack-bundle-analyzer').BundleAnalyzerPlugin

plugins:[
    ....,
    new BundleAnalyzerPlugin(),
]

.項目打包生成.gz文件

npn i --save-dev compression-webpack-plugin

修改webpack.config.prod.js

const CompressionPlugin = require("compression-webpack-plugin");

plugins: [
    ...
    new CompressionPlugin({
      asset: "[path].gz[query]",
      algorithm: "gzip",
      test: /\.js$|\.css$|\.html$/,
      threshold: 10240,
      minRatio: 0.8
    }),
]

 

總結

好啦,目前的坑 我遇到的,后面如果還有我會繼續更博的, 講道理 還是挺喜歡的react ,

   畢竟 我是react 重度 喜歡者 ,不知道你是不是和我一樣咯


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM