1. 程式人生 > >create-react-app踩坑記

create-react-app踩坑記

tcs onf class working zip als mpi iconfont hat

前言

哇,不的不說這個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.jswebpack.config-prod.js 配置文件

在164 行:

加入 modules:true,

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

然後重新 npm start

生產環境去除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 重度 喜歡者 ,不知道你是不是和我一樣咯

create-react-app踩坑記