gpt4 book ai didi

reactjs - 如何使用 Webpack 3 配置 Ant Design

转载 作者:行者123 更新时间:2023-12-05 08:14:54 24 4
gpt4 key购买 nike

修复和解决方案:

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

  1. 将 .less 添加到扩展部分
  2. 删除了 webpack.config.json 中不必要的代码,例如删除了 extract-text-webpack-plugin
  3. less 从 3.0.1 降级到 2.7.0 修复了一堆错误:antd 中 index.less 文件中未定义的长度。

更新 3

这是我的 package.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. 删除上下文选项后://上下文:path.resolve(__dirname, 'dist'),在 webpage.config.js 中

  2. 已添加

    javascriptEnabled: true在 webpack.config.json 的 less-loader 下

我收到了这些错误。

ERROR in ./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 (line 1, column -1) @ ./node_modules/antd/lib/button/style/index.js 5:0-23 @ ./src/index.js @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js

ERROR in ./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 (line 1, column -1) @ ./node_modules/antd/lib/button/style/index.js 3:0-33 @ ./src/index.js @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js

ERROR in ./node_modules/antd/lib/style/index.less Module build failed: ModuleBuildError: 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 (line 1, column -1) at runLoaders (F:\temp\Playground\mobx-reactjsx\node_modules\webpack\lib\NormalModule.js:195:19) at F:\temp\Playground\mobx-reactjsx\node_modules\loader-runner\lib\LoaderRunner.js:364:11 at F:\temp\Playground\mobx-reactjsx\node_modules\loader-runner\lib\LoaderRunner.js:230:18 at context.callback (F:\temp\Playground\mobx-reactjsx\node_modules\loader-runner\lib\LoaderRunner.js:111:13) at at process._tickCallback (internal/process/next_tick.js:169:7) @ ./node_modules/antd/lib/style/index.less @ ./node_modules/antd/lib/button/style/index.js @ ./src/index.js @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js

ERROR in ./node_modules/antd/lib/button/style/index.less Module build failed: ModuleBuildError: 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 (line 1, column -1) at runLoaders (F:\temp\Playground\mobx-reactjsx\node_modules\webpack\lib\NormalModule.js:195:19) at F:\temp\Playground\mobx-reactjsx\node_modules\loader-runner\lib\LoaderRunner.js:364:11 at F:\temp\Playground\mobx-reactjsx\node_modules\loader-runner\lib\LoaderRunner.js:230:18 at context.callback (F:\temp\Playground\mobx-reactjsx\node_modules\loader-runner\lib\LoaderRunner.js:111:13) at at process._tickCallback (internal/process/next_tick.js:169:7) @ ./node_modules/antd/lib/button/style/index.less @ ./node_modules/antd/lib/button/style/index.js @ ./src/index.js @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js Child extract-text-webpack-plugin node_modules/extract-text-webpack-plugin/dist node_modules/css-loader/index.js!node_modules/less-loader/dist/cjs.js??ref--1-2!node_modules/antd/lib/style/index.less: [0] ./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 302 bytes {0} [built] [failed] [1 error]

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

(line 1, column -1) Child extract-text-webpack-plugin node_modules/extract-text-webpack-plugin/dist node_modules/css-loader/index.js!node_modules/less-loader/dist/cjs.js??ref--1-2!node_modules/antd/lib/button/style/index.less: [0] ./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 302 bytes {0} [built] [failed] [1 error]

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

(line 1, column -1) webpack: Failed to compile.


更新 1:

这是我的 webpack.config.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"
})
},]
}
};

现在错误信息变成:

ERROR in multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js Module not found: Error: Can't resolve './src/index.js' in '/Users/Winston/tmp/Playground/mobx-reactjsx/src' @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js

(项目结构) project structure

我在 projectfolder/src 下有 index.js


我正在尝试将 Ant Design 与 Less support 和 Import on Demand 功能集成在一起: 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",

webpack.config.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>

.babelrc

{

"presets": [

"react",

"es2015",

"stage-1"

],

"plugins": [

["import", {"libraryName": "antd", "style": true} ],

"transform-decorators-legacy"

]

}

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

最佳答案

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

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

关于reactjs - 如何使用 Webpack 3 配置 Ant Design,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48832008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com