代码之家  ›  专栏  ›  技术社区  ›  Franva

如何使用Webpack 3配置Ant设计

  •  2
  • Franva  · 技术社区  · 6 年前

    修复和解决方案:

    感谢长话短说的@BoyWithSilver,以下是我为解决问题所做的:

    1. 补充。“减少到扩展部分”
    2. 删除了网页包中不必要的代码。配置。json例如删除了提取文本网页包插件
    3. 从3.0.1降级到2.7.0,修复了大量错误:索引中未定义的长度。antd中的文件更少。

    更新3

    这是我的包裹。json

    {
      "name": "mobx-reactjsx",
      "version": "1.0.0",
      "description": dfsdfsdfsd",
      "scripts": {
        "start": "webpack-dev-server --config webpack.config.js --progress --inline",
        "lint": "eslint src"
      },
      "keywords": [
        "react",
        "reactjs",
        "boilerplate",
        "mobx",
        "starter-kit",
        "firebase",
        "re-base"
      ],
      "author": "Winston Fan",
      "license": "MIT",
      "homepage": "https://horizontalvision.azurewebsites.net/",
      "devDependencies": {
        "autoprefixer": "^8.0.0",
        "babel-core": "^6.9.1",
        "babel-loader": "^7.1.2",
        "babel-plugin-import": "^1.6.4",
        "babel-plugin-transform-decorators-legacy": "^1.3.4",
        "babel-preset-es2015": "^6.9.0",
        "babel-preset-react": "^6.5.0",
        "babel-preset-stage-1": "^6.5.0",
        "css-loader": "^0.28.9",
        "extract-text-webpack-plugin": "^3.0.2",
        "less": "^3.0.1",
        "less-loader": "^4.0.5",
        "less-vars-to-js": "^1.2.1",
        "postcss-loader": "^2.1.0",
        "style-loader": "^0.20.2",
        "webpack": "^3.5.5",
        "webpack-dev-server": "^2.7.1"
      },
      "dependencies": {
        "antd": "^3.2.1",
        "firebase": "^4.9.1",
        "mobx": "^3.5.1",
        "mobx-react": "^4.4.1",
        "mobx-react-devtools": "^4.2.15",
        "re-base": "^3.2.2",
        "react": "^16.2.0",
        "react-dom": "^16.2.0"
      }
    }
    

    更新2:

    1. 删除上下文选项后: //上下文:路径。解析(\uu dirname,'dist'), 在网页中。配置。js公司

    2. 补充

      javascriptEnabled:true 在网页包的较少加载程序下。配置。json

    我收到了这些错误。

    中出错/node\u modules/antd/lib/button/style/index。更少的模块构建 失败:

    /*stylelint禁用 声明前面有空格,没有重复的选择器*/^无法读取 未定义的属性“length” 在F:\temp\playway\mobx reactjsx\node\u modules\antd\lib\style\color\bezierEasing中。较少的 (第1行,第1列)@/node\u modules/antd/lib/button/style/index。js公司 5:0-23 @ ./src/索引。js@多 (网页包)-开发服务器/客户端? http://localhost:8080 ./src/索引。js公司

    中出错/node\u modules/antd/lib/style/index。更少模块生成失败:

    /*stylelint禁用 声明前面有空格,没有重复的选择器*/^无法读取 未定义的属性“length” 在F:\temp\playway\mobx reactjsx\node\u modules\antd\lib\style\color\bezierEasing中。较少的 (第1行,第1列)@/node\u modules/antd/lib/button/style/index。js公司 3:0-33 @ ./src/索引。js@多 (网页包)-开发服务器/客户端? http://localhost:8080 ./src/索引。js公司

    中出错/node\u modules/antd/lib/style/index。更少模块生成失败: ModuleBuildError:模块生成失败:

    /*stylelint禁用 声明前面有空格,没有重复的选择器*/^无法读取 未定义的属性“length” 在F:\temp\playway\mobx reactjsx\node\u modules\antd\lib\style\color\bezierEasing中。较少的 (第1行第1列) 在RunLoader上(F:\temp\playway\mobx reactjsx\node\u modules\webpack\lib\NormalModule.js:195:19) 位于F:\temp\playway\mobx reactjsx\node\u modules\loader runner\lib\loader runner。js:364:11 位于F:\temp\playway\mobx reactjsx\node\u modules\loader runner\lib\loader runner。js:230:18 在上下文中。回调(F:\temp\playway\mobx reactjsx\node\u modules\loader runner\lib\LoaderRunner.js:111:13) 在 at过程_tickCallback(内部/进程/下一步\u tick.js:169:7)@/node\u modules/antd/lib/style/index。减去@ ./node\u modules/antd/lib/button/style/index。js@/src/索引。js公司@ 多(Web包)-开发服务器/客户端? http://localhost:8080 ./src/索引。js公司

    中出错/node\u modules/antd/lib/button/style/index。更少的模块构建 失败:ModuleBuildError:模块生成失败:

    /*stylelint禁用 声明前面有空格,没有重复的选择器*/^无法读取 未定义的属性“length” 在F:\temp\playway\mobx reactjsx\node\u modules\antd\lib\style\color\bezierEasing中。较少的 (第1行第1列) 在RunLoader上(F:\temp\playway\mobx reactjsx\node\u modules\webpack\lib\NormalModule.js:195:19) 位于F:\temp\playway\mobx reactjsx\node\u modules\loader runner\lib\loader runner。js:364:11 位于F:\temp\playway\mobx reactjsx\node\u modules\loader runner\lib\loader runner。js:230:18 在上下文中。回调(F:\temp\playway\mobx reactjsx\node\u modules\loader runner\lib\LoaderRunner.js:111:13) 在 at过程_tickCallback(内部/进程/下一步\u tick.js:169:7)@/node\u modules/antd/lib/button/style/index。减去@ ./node\u modules/antd/lib/button/style/index。js@/src/索引。js公司@ 多(Web包)-开发服务器/客户端? http://localhost:8080 ./src/索引。js公司 儿童提取文本网页包插件 node\u模块/提取文本网页包插件/dist node\u模块/css加载程序/索引。js!node\u模块/less loader/dist/cjs。js??参考——1-2!node\u modules/antd/lib/style/index。减去: [0] ./node\u模块/css加载器/node\u模块/less loader/dist/cjs。js?{“modifyVars”:{“@primary color”:“#193D71”},“root”:“F://temp//playway//mobx reactjsx”,“javascriptEnabled”:true}/node\u modules/antd/lib/style/index。较少的 302字节{0}[生成][失败][1错误]

    ERROR in ./node_modules/css-loader!./node_modules/less-loader/dist/cjs.js?{"modifyVars":{"@primary-color":"#193D71"},"root":"F://temp//Playground//mobx-reactjsx","javascriptEnabled":true}!./node_modules/antd/lib/style/index.less
    Module build failed:
    
    /* stylelint-disable declaration-bang-space-before,no-duplicate-selectors */
    ^
    Cannot read property 'length' of undefined
          in F:\temp\Playground\mobx-reactjsx\node_modules\antd\lib\style\color\bezierEasing.less
    

    (第1行,第1列)子提取文本网页包插件 node\u模块/提取文本网页包插件/dist node\u模块/css加载程序/索引。js!node\u模块/less loader/dist/cjs。js??参考——1-2!node\u modules/antd/lib/button/style/index。减去: [0] ./node\u模块/css加载器/node\u模块/less loader/dist/cjs。js?{“modifyVars”:{“@primary color”:“#193D71”},“root”:“F://temp//playway//mobx reactjsx”,“javascriptEnabled”:true}/node\u modules/antd/lib/button/style/index。较少的 302字节{0}[生成][失败][1错误]

    ERROR in ./node_modules/css-loader!./node_modules/less-loader/dist/cjs.js?{"modifyVars":{"@primary-color":"#193D71"},"root":"F://temp//Playground//mobx-reactjsx","javascriptEnabled":true}!./node_modules/antd/lib/button/style/index.less
    Module build failed:
    
    /* stylelint-disable declaration-bang-space-before,no-duplicate-selectors */
    ^
    Cannot read property 'length' of undefined
          in F:\temp\Playground\mobx-reactjsx\node_modules\antd\lib\style\color\bezierEasing.less
    

    (第1行,第1列)网页:未能编译。


    更新1:

    这是我的网页。配置。json

    var path = require('path');
    var webpack = require('webpack');
    const fs  = require('fs');
    
    const ExtractTextPlugin = require("extract-text-webpack-plugin");
    const lessToJs = require('less-vars-to-js');
    const themeVariables = lessToJs(fs.readFileSync(path.join(__dirname, './scripts/ant-theme-vars.less'), 'utf8'));
    
    module.exports = {
      watch: true,
      context: path.resolve(__dirname, './src'),
      devtool: 'source-map',
      entry: [
        './src/index.js'
      ],
      output: {
        path: path.resolve(__dirname, './dist'),
        filename: '[name].bundle.js'
      },
      plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new ExtractTextPlugin('style.css')
      ],
      resolve: {
        extensions: ['.js', '.jsx']
      },
      module: {
        rules: [{
          test: /\.jsx?$/,
          use: ['babel-loader'],
          include: path.join(__dirname, 'src')
        },
          {
            test: /\.less$/,
            use: ExtractTextPlugin.extract([
              {loader: "css-loader"},
              {loader: "less-loader",
                options: {
                  modifyVars: themeVariables,
                  root: path.resolve(__dirname, './')
                }
              }
            ])
          },
          {
            test: /\.css$/,
            use: ExtractTextPlugin.extract({
              use: "css-loader"
            })
          },]
      }
    };
    

    现在,错误消息变为:

    多(Web包)错误-开发服务器/客户端? http://localhost:8080 ./src/索引。未找到js模块:错误:无法解析“”/src/索引。js' 在“/用户/温斯顿/tmp/游乐场/mobx reactjsx/src”中@多 (网页包)-开发服务器/客户端? http://localhost:8080 ./src/索引。js公司

    (项目结构) project structure

    我有索引。项目文件夹下的js/src


    我正在尝试将Ant设计与较少的支持以及此处的按需导入功能集成在一起: https://ant.design/docs/react/use-with-create-react-app 在高级指南部分。

    在本节中,它使用react app rewired,但我的项目使用webpack 3(我的项目基于此 https://github.com/mobxjs/mobx-react-boilerplate ),因此无法在我的情况下使用rewired命令。

    所以我找到了另一篇文章: https://medium.com/@GeoffMiller/how-to-customize-ant-design-with-react-webpack-the-missing-guide-c6430f2db10f

    这是我的代码:

    包裹json

    "start": "webpack-dev-server --hot --open",
    

    网页包。配置。json

    const fs  = require('fs');
    
    const lessToJs = require('less-vars-to-js');
    const themeVariables = lessToJs(fs.readFileSync(path.join(__dirname, './scripts/ant-theme-vars.less'), 'utf8'));
    ....
    module:{
    .....
    rules:[
    .....
    test: /\.less$/,
    
    use: [
    
           {loader: "style-loader"},
    
           {loader: "css-loader"},
    
           {loader: "less-loader",
    
    options: {
    
    modifyVars: themeVariables
    
             }
    
           }
    
         ]
    ]
    }
    

    指数js公司

    import {Button} from 'antd';
    
    <Button type="primary">Hi!</Button>
    

    .巴别塔LRC

    {
    
    "presets": [
    
    "react",
    
    "es2015",
    
    "stage-1"
    
     ],
    
    "plugins": [
    
       ["import", {"libraryName": "antd", "style": true} ],
    
    "transform-decorators-legacy"
    
     ]
    
    }
    

    以下是来自控制台的错误消息: enter image description here

    5 回复  |  直到 6 年前
        1
  •  15
  •   alycecristines    3 年前

    对我来说,我只能用 import 'antd/dist/antd.min.css 而不是 import 'antd/dist/antd.css

    https://github.com/ant-design/ant-design/issues/33327#issuecomment-997355323

        2
  •  1
  •   Agney Sidharth yadav    6 年前

    错误似乎是由于无法解决 .less 文件夹。将此添加到 webpack.config.js 应帮助解决这些问题:

    resolve: {
      extensions: ['.js', '.jsx', '.less']
    },
    

    您已将上下文指定为 src 结构中的文件夹。这意味着条目必须更改为 ./index.js 或者只是 ./index 如果你不是故意的 context 键,移除它也可以完成此工作。

        3
  •  1
  •   Sivadass N    6 年前

    Webpack配置(Webpack.config.js):

    const path = require('path');
    const webpack = require('webpack');
    const ExtractTextPlugin = require("extract-text-webpack-plugin");
    const fs  = require('fs');
    const lessToJs = require('less-vars-to-js');
    const themeVariables = lessToJs(fs.readFileSync(path.join(__dirname, 'ant-theme-vars.less'), 'utf8'));
    module.exports = {
      watch: true,
      context: path.resolve(__dirname, './src'),
      entry: {
        app: './index.js',
      },  
      output: {
        path: path.resolve(__dirname, './dist'),
        filename: '[name].bundle.js',
      },
      devtool: 'source-map',
      resolve: {
        alias: {
          moment: 'moment/src/moment'
        },
      },
      module: {
        rules: [
          {
            test: /.jsx?$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            query: {
              presets: ['env','react','stage-2']
            }
          },
          {
            loader: 'babel-loader',
            exclude: /node_modules/,
            test: /\.js$/,
            options: {
              presets: ['env','react','stage-2'],
              plugins: [
                ['import', { libraryName: "antd", style: true }]
              ]
            },
          },
          {
          test: /\.scss?$/,
            loader: ExtractTextPlugin.extract({
              fallback: 'style-loader',
              loader: 'css-loader!sass-loader'
            })
          },
          {
            test: /\.less$/,
            use: ExtractTextPlugin.extract([
              {loader: "css-loader"},
              {loader: "less-loader",
                options: {
                  modifyVars: themeVariables,
                  root: path.resolve(__dirname, './')
                }
              }
            ])
          },
          {
            test: /\.(woff|woff2|eot|ttf|otf|svg|png|jpg,pdf)$/,
            loader: "file-loader"
          },
          {
            test: /\.css$/,
            use: ExtractTextPlugin.extract({
              fallback: "style-loader",
              use: "css-loader"
            })
          },
        ]
      },
      plugins: [
        new ExtractTextPlugin('style.css')
      ],
      devServer: {
        port: 9000,
        contentBase: 'dist'
      }
    };
    

    和包装。json:

    {
      "name": "ant-design-app",
      "version": "1.0.0",
      "description": "Ant design",
      "main": "index.js",
      "scripts": {
        "start": "webpack-dev-server --config webpack.config.js --progress --inline",
        "build": "webpack -p --config webpack.config.js"
       },
       "repository": {
         "type": "git",
         "url": ""
       },
       "author": "Sivadass",
       "license": "MIT",
       "dependencies": {
          "antd": "^3.1.4",
          "moment": "^2.20.1",
          "react": "^16.2.0",
          "react-dom": "^16.2.0",
       },
       "devDependencies": {
          "babel-core": "^6.24.1",
          "babel-loader": "^7.0.0",
          "babel-plugin-add-module-exports": "^0.2.1",
          "babel-plugin-import": "^1.6.3",
          "babel-plugin-transform-decorators-legacy": "^1.3.4",
          "babel-preset-env": "^1.6.0",
          "babel-preset-react": "^6.24.1",
          "babel-preset-stage-2": "^6.24.1",
          "css-loader": "^0.28.9",
          "extract-text-webpack-plugin": "^2.1.2",
          "file-loader": "^0.11.2",
          "less": "^2.7.3",
          "less-loader": "^4.0.5",
          "less-vars-to-js": "^1.2.1",
          "node-sass": "^4.7.2",
          "postcss-loader": "^2.0.10",
          "sass-loader": "^6.0.6",
          "style-loader": "^0.18.2",
          "sugarss": "^1.0.1",
          "webpack": "^3.10.0",
          "webpack-dev-server": "^2.11.1"
        }
     }
    

    Ant主题变量配置(Ant主题变量较少):

      @primary-color: #4099ff;
      @font-size-base: 16px;
      @font-family : "Proxima Nova", Helvetica Neue, Helvetica, Arial, sans-serif;
      @btn-font-size-lg: 15px;
      @btn-padding-lg : 8px 32px;
      @btn-padding-sm : 4px 16px;
    
        4
  •  0
  •   SuNdAr RaJa    2 年前

    我刚刚在索引中添加了那个import语句。scss文件,则警告消失

    @import "antd/dist/antd.css";

        5
  •  0
  •   Nafiz Bayrak    2 年前

    React解决方案:

    在索引中。js文件夹

    • 导入“antd/dist/antd”。css

    替换为

    • 导入“antd/dist/antd”。最小css