美文网首页
React 最新 create-react-app 配置Less

React 最新 create-react-app 配置Less

作者: Yokiijay | 来源:发表于2019-03-19 10:44 被阅读0次

    1. create-react-app 新建项目

    $ create-react-app add-less && cd add-less
    

    2. yarn eject 暴露配置文件

    $ yarn eject
    
    $ react-scripts eject
    
    NOTE: Create React App 2 supports TypeScript, Sass, CSS Modules and more without ejecting: https://reactjs.org/blog/2018/10/01/create-react-app-v2.html
    
    ? Are you sure you want to eject? This action is permanent. (y/N) 
    
    

    y 确认

    3. 修改 cofig/webpack.config.js

    找到

    const cssRegex = /\.css$/;
    const cssModuleRegex = /\.module\.css$/;
    

    修改为

    const cssRegex = /\.(css|less)$/;
    const cssModuleRegex = /\.module\.(css|less)$/;
    

    找到

    {
                  test: cssRegex,
                  exclude: cssModuleRegex,
                  use: getStyleLoaders({
                    importLoaders: 1,
                    sourceMap: isEnvProduction && shouldUseSourceMap,
                  }),
                  // 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,
                },
    

    修改为

    {
                  test: cssRegex,
                  exclude: cssModuleRegex,
                  use: getStyleLoaders({
                    importLoaders: 2,
                    sourceMap: isEnvProduction && shouldUseSourceMap,
                  },
                    '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,
                },
    

    4. Done就这么简单

    相关文章

      网友评论

          本文标题:React 最新 create-react-app 配置Less

          本文链接:https://www.haomeiwen.com/subject/skawmqtx.html