babel-plugin-import icon indicating copy to clipboard operation
babel-plugin-import copied to clipboard

style 设置为 true 后未加载组件样式

Open ITJesse opened this issue 10 years ago • 38 comments

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);

但是实际上样式并没有被成功加载

ITJesse avatar Aug 24 '16 06:08 ITJesse

同样的问题

//.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"
  }
}

hufeng avatar Aug 26 '16 08:08 hufeng

我遇到了同样的问题,暂时的解决办法:

  1. 因为 antd 默认引入的是 less,所以你先装一个 webpack 的 less-loader
  2. 然后你引入组件的时候使用这种繁琐的写法:
import Tag from 'antd/lib/tag';
require('antd/lib/tag/style');

实际上你按照插件文档设置 css options 是没有用的,我之前在 #47 就被坑过。 这当然是插件本身的 bug。

mydearxym avatar Sep 09 '16 11:09 mydearxym

@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

Honghe avatar Sep 12 '16 09:09 Honghe

这里面还有可能是你的 css loader 设置了 css module。。。我就是因为这个样式加载不了,解决方法:

    {
      test: /\.less$/,
      loader: `style!css?${cssLoaderWithoutModule}!postcss!less?${lessLoader}`,
      include: [
        path.resolve(__dirname, 'node_modules'),
      ],
    }

zation avatar Oct 05 '16 15:10 zation

如果我需要使用css modules功能怎么办?

crowphy avatar Oct 16 '16 07:10 crowphy

cssLoaderWithoutModule lessLoader 这两个变量是什么值?

cike8899 avatar Oct 16 '16 09:10 cike8899

我的解决方案 ["import", [{ "libraryName": "antd", "style": true }]]改成 ["import", [{ "libraryName": "antd", "style": "true" }]]

zhangxianggui avatar Nov 23 '16 08:11 zhangxianggui

使用less-loader有效

{
        test: /\.less$/,
        loader: `style!css!less`,
        include: path.resolve(__dirname, 'node_modules'),
}

viix avatar Dec 05 '16 08:12 viix

@viix
include: path.resolve(__dirname, 'node_modules'), ReferenceError: path is not defined

change9326 avatar Dec 07 '16 09:12 change9326

@zhangxianggui
还是不行啊

change9326 avatar Dec 07 '16 10:12 change9326

@JustYJQ var path = require('path')

viix avatar Dec 08 '16 10:12 viix

https://segmentfault.com/q/1010000007626617

这个题主采纳的答案解决了。

mytharcher avatar Jan 13 '17 03:01 mytharcher

@Honghe 我也遇到这个问题 请问你怎么解决的

geekpeng avatar Mar 13 '17 06:03 geekpeng

@zhangxianggui You save my day!

fred104 avatar May 15 '17 08:05 fred104

First, Please see api doc at https://github.com/ant-design/babel-plugin-import#style

I will try to give a simple explanation:

  1. 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
  1. If you set style: true, babel-plugin-import will load style by require('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.

  2. If you set style: 'css', babel-plugin-import will load style by require('antd/lib/button/style/css'), which means https://unpkg.com/[email protected]/lib/button/style/index.js, it's less file.

  3. Last tip, you need configure webpack loader(css-loader/less-loader) to handle css/less file by yourself.

Going to close this issue, If anyone still got problems, please provide a reproducible demo.

paranoidjk avatar May 30 '17 17:05 paranoidjk

无论是设置为css还是设置为true,样式都没能加载。

Pines-Cheng avatar Jul 11 '17 02:07 Pines-Cheng

折腾了这么旧终于可用了。贴上解决方案: "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/
            }
] 

参考:CSS Modules: How do I disable local scope for a file

oufeng avatar Aug 16 '17 08:08 oufeng

U can see https://github.com/ant-design/ant-design/issues/3442#issuecomment-374877201

JX-Zhuang avatar Mar 21 '18 09:03 JX-Zhuang

我特么就服了,题主问的是style 设置为 true后样式消失的问题,你们让他吧true再改成css ,跟没说有啥区别。谁能说个答案呢!

zhangyang110 avatar Mar 29 '18 11:03 zhangyang110

{ 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来写样式之后,自己向组件内传样式不生效,不知道跟你们的相不相同

vevlins avatar Apr 28 '18 08:04 vevlins