关于前端:createreactapp-v4-的lessantd配置

44次阅读

共计 2330 个字符,预计需要花费 6 分钟才能阅读完成。

1、less 反对

  • yarn eject
  • yarn add less@^2.7.3 less-loader@^7.3.0
  • 在 webpack.config.js 中找到 sassRegex
const cssRegex = /\.css$/;
const cssModuleRegex = /\.module\.css$/;
const sassRegex = /\.(scss|sass)$/;
const sassModuleRegex = /\.module\.(scss|sass)$/;
const lessRegex = /\.less$/;
const lessModuleRegex = /\.module\.less$/;
  • 找到配置

    {
                test: sassRegex,
                exclude: sassModuleRegex,
                use: getStyleLoaders(
                  {
                    importLoaders: 3,
                    sourceMap: isEnvProduction
                      ? shouldUseSourceMap
                      : isEnvDevelopment,
                  },
                  'sass-loader'
                ),
                // Don't consider CSS imports dead code even if the
                // containing package claims to have no side effects.
                // Remove this when webpack adds a warning or an error for this.
                // See https://github.com/webpack/webpack/issues/6571
                sideEffects: true,
              },
              // Adds support for CSS Modules, but using SASS
              // using the extension .module.scss or .module.sass
              {
                test: sassModuleRegex,
                use: getStyleLoaders(
                  {
                    importLoaders: 3,
                    sourceMap: isEnvProduction
                      ? shouldUseSourceMap
                      : isEnvDevelopment,
                    modules: {getLocalIdent: getCSSModuleLocalIdent,},
                  },
                  'sass-loader'
                ),
              },
              {
                test: lessRegex,
                exclude: lessModuleRegex,
                use: getStyleLoaders(
                  {
                    importLoaders: 3,
                    sourceMap: isEnvProduction
                      ? shouldUseSourceMap
                      : isEnvDevelopment,
                  },
                  'less-loader'
                ),
                // Don't consider CSS imports dead code even if the
                // containing package claims to have no side effects.
                // Remove this when webpack adds a warning or an error for this.
                // See https://github.com/webpack/webpack/issues/6571
                sideEffects: true,
              },
              // Adds support for CSS Modules, but using less
              // using the extension .module.less
              {
                test: lessModuleRegex,
                use: getStyleLoaders(
                  {
                    importLoaders: 3,
                    sourceMap: isEnvProduction
                      ? shouldUseSourceMap
                      : isEnvDevelopment,
                    modules: {getLocalIdent: getCSSModuleLocalIdent,},
                  },
                  'less-loader'
                ),
              },

    2、antd 装置

  • yarn add antd@^3.4.2
  • yarn add babel-plugin-import@^1.13.3
  • 配置动静加载:package.json 中
"babel": {
    "presets": ["react-app"],
    "plugins": [
      [
        "import",
        {
          "libraryName": "antd",
          "style": true
        }
      ]
    ]
  },

3、antd 主题配置:找到 preProcessor 在前面退出

 if (preProcessor) {
      loaders.push(
        {loader: require.resolve('resolve-url-loader'),
          options: {
            sourceMap: isEnvProduction ? shouldUseSourceMap : isEnvDevelopment,
            root: paths.appSrc,
          },
        },
        {loader: require.resolve(preProcessor),
          options: {sourceMap: true,},
        }
      );
    }
    // 在此退出
    if (preProcessor && preProcessor === 'less-loader') {
      loaders.push(
        {loader: require.resolve('resolve-url-loader'),
          options: {
            sourceMap: isEnvProduction ? shouldUseSourceMap : isEnvDevelopment,
            root: paths.appSrc,
          }
        },
        {loader: require.resolve(preProcessor),
          options: {
            sourceMap: true,
            lessOptions:{ 
              modifyVars: {'@primary-color': '#ff4757',}}
          }
        }
      );
    }

    return loaders;
  };

正文完
 0