想使用antd按需加载,代码如下:
import { Button } from 'antd';
.babelrc配置如下:
{
"presets": ["es2015","react","stage-0","env"],
"plugins": ["react-hot-loader/babel",["import", { "libraryName": "antd", "style": true }]],
"env": {
"production":{
"preset":["react-optimize"]
}
}
}
但是会报错,
错误信息赋值如下:
ERROR in ./node_modules/antd/lib/button/style/index.less
Module parse failed:
/Users/Nealyang/Documents/code/code-work/Demo/nodejs/React-Express-Blog-Demo/node_modules/antd/lib/button/style/index.less
Unexpected character '@' (1:0) You may need an appropriate loader to
handle this file type. | @import "../../style/themes/default"; |
@import "../../style/mixins/index"; | @import "./mixin"; @
./node_modules/antd/lib/button/style/index.js 5:0-23 @
./app/containers/index.js @ ./app/index.js @ multi
react-hot-loader/patch
webpack-hot-middleware/client?path=http://127.0.0.1:3000/__webpa...
babel-polyfill ./app/index.js
我之前webpack只处理了css的样式,后来又加上了less-loader,但是依旧不行。
webpack.js代码如下:
{
test: /\.css$/,
exclude: /node_modules/,
use: ['style-loader',
{
loader: 'css-loader',
options: {
modules: true,
localIdentName: '[name]-[local]-[hash:base64:5]',
importLoaders: 1
}
},
'postcss-loader'
]
},
{
test: /antd.*\.less$/,
exclude: /node_modules/,
use: ["style-loader", {loader: 'css-loader', options: {sourceMap: 1}}, "postcss-loader", "less-loader"]
},
查了各种解决方案,还是不行,求大神指点。ps:css我一直用的css-modules
当webpack.js配置如下
{
test: /\.less$/,
use: ["style-loader", 'css-loader', "postcss-loader", "less-loader"]
},
错误如下:
我之前也看到过出现该问题在最下面:原因应该是您webpack的loaders配置加入了exclude(将antd资源排除其外)和include(未能将antd资源包含其内)。
希望能帮到您。