style 设置为 true 后未加载组件样式
https://github.com/ant-design/ant-design/issues/900#issue-127675746
And that will convert this
import { Button } from 'antd';
ReactDOM.render(<Button>xxxx</Button>, mountNode);
to
var _button = require('antd/lib/button');
require('antd/style/components/button');
ReactDOM.render(<Button>xxxx</Button>, mountNode);
但是实际上样式并没有被成功加载
同样的问题
//.babelrc
{
"presets": ["react", "es2015"],
"plugins": [
"syntax-object-rest-spread",
["antd", { "style": "css", "libraryName": "antd-mobile" }]
]
}
//package.json
{
"dependencies": {
"antd-mobile": "^0.7.8",
"react": "^15.3.1",
"react-dom": "^15.3.1",
"rmc-list-view": "^0.6.1"
},
"devDependencies": {
"babel-loader": "^6.2.5",
"babel-plugin-antd": "^0.5.1",
"babel-plugin-syntax-object-rest-spread": "^6.13.0",
"babel-preset-es2015": "^6.14.0",
"babel-preset-react": "^6.11.1",
"css-loader": "^0.24.0",
"html-webpack-plugin": "^2.22.0",
"style-loader": "^0.13.1",
"webpack": "^2.1.0-beta.21",
"webpack-dev-server": "^1.15.0"
}
}
我遇到了同样的问题,暂时的解决办法:
- 因为 antd 默认引入的是 less,所以你先装一个 webpack 的 less-loader
- 然后你引入组件的时候使用这种繁琐的写法:
import Tag from 'antd/lib/tag';
require('antd/lib/tag/style');
实际上你按照插件文档设置 css options 是没有用的,我之前在 #47 就被坑过。 这当然是插件本身的 bug。
@mydearxym 能否给出详细配置,我的 less-loader 编译 less 出错,好像没生效
demo_ethereum/node_modules/antd/lib/style/index.less:1
(function (exports, require, module, __filename, __dirname) { @import "./themes/default";
^
SyntaxError: Unexpected token ILLEGAL
这里面还有可能是你的 css loader 设置了 css module。。。我就是因为这个样式加载不了,解决方法:
{
test: /\.less$/,
loader: `style!css?${cssLoaderWithoutModule}!postcss!less?${lessLoader}`,
include: [
path.resolve(__dirname, 'node_modules'),
],
}
如果我需要使用css modules功能怎么办?
cssLoaderWithoutModule lessLoader 这两个变量是什么值?
我的解决方案 ["import", [{ "libraryName": "antd", "style": true }]]改成 ["import", [{ "libraryName": "antd", "style": "true" }]]
使用less-loader有效
{
test: /\.less$/,
loader: `style!css!less`,
include: path.resolve(__dirname, 'node_modules'),
}
@viix
include: path.resolve(__dirname, 'node_modules'),
ReferenceError: path is not defined
@zhangxianggui
还是不行啊
@JustYJQ
var path = require('path')
https://segmentfault.com/q/1010000007626617
这个题主采纳的答案解决了。
@Honghe 我也遇到这个问题 请问你怎么解决的
@zhangxianggui You save my day!
First, Please see api doc at https://github.com/ant-design/babel-plugin-import#style
I will try to give a simple explanation:
- Since antd have below file structure https://unpkg.com/[email protected]/lib/affix/style/
node_modules/antd/lib/button/style jiangkai@jiangkaideMacBook-Pro
├── css.js
├── index.css
├── index.d.ts
├── index.js
├── index.less
└── mixin.less
-
If you set
style: true, babel-plugin-import will load style byrequire('antd/lib/message/style'), which means this file https://unpkg.com/[email protected]/lib/button/style/index.js, as you can see, it's css file. -
If you set
style: 'css', babel-plugin-import will load style byrequire('antd/lib/button/style/css'), which means https://unpkg.com/[email protected]/lib/button/style/index.js, it's less file. -
Last tip, you need configure webpack loader(
css-loader/less-loader) to handlecss/lessfile by yourself.
Going to close this issue, If anyone still got problems, please provide a reproducible demo.
无论是设置为css还是设置为true,样式都没能加载。
折腾了这么旧终于可用了。贴上解决方案: "webpack": "^3.5.4"
// .babelrc or babel-loader option
"plugins": [
["import", { "libraryName": "antd", "style": "css" }]
]
//webpack.config.js设置
rules: [
{
test: /\.css$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader', options: { modules: true, importLoaders: 1 } },
{ loader: 'postcss-loader' }
],
exclude: /node_modules/
},
{
test: /\.css$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader' }
],
include: /node_modules/
}
]
U can see https://github.com/ant-design/ant-design/issues/3442#issuecomment-374877201
我特么就服了,题主问的是style 设置为 true后样式消失的问题,你们让他吧true再改成css ,跟没说有啥区别。谁能说个答案呢!
{ test: /.css$/i, // include: /src/, use: [ { loader: "style-loader" }, { loader: "css-loader", options: { modules: true } }, { loader: "postcss-loader", options: postcssOpts } ] }
{ test: /.less$/i, loaders: ['style-loader', 'css-loader', 'less-loader'] },
["import", [{ style: true, libraryName: "antd-mobile" }]]
部门大佬写的,我这里遇到的问题是通过css modules来写样式之后,自己向组件内传样式不生效,不知道跟你们的相不相同