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

import { Component } from 'antd' does not work.

Open eminx opened this issue 7 years ago • 12 comments

The plugin imports don't seem to work.

In my code: import { Layout } from 'antd';

It gives the error: ReferenceError: Layout is not defined.

When I change my code to: const Layout = require('antd').Layout;

it works.

Can someone please help me fix that? This started out of nothing...

eminx avatar Jan 24 '18 22:01 eminx

No one out there? :(

eminx avatar Jan 25 '18 22:01 eminx

Reproduce repo.

yesmeck avatar Jan 31 '18 13:01 yesmeck

https://github.com/eminx/Nodal

See the importing of antd components are with /lib at the end. This was the fix I could do. Like this:

import { Button } from 'antd/lib';

If you want to reproduce, just remove the /lib and you'll see...

Thanks!

eminx avatar Feb 02 '18 21:02 eminx

For example in this file: https://github.com/eminx/Nodal/blob/master/client/UIComponents/CardArticle.jsx

eminx avatar Feb 02 '18 21:02 eminx

update your react and react-dom 16 +

lefter avatar Feb 22 '18 04:02 lefter

react and react-dom are all 16.2.0. But I also have the same Reference Error of any Component I tried to use.

Probably Meteor and babel-plugin-import is not compatible for some reason. As I see @eminx is using Meteor framework also.

ghost avatar Feb 22 '18 17:02 ghost

Exactly. @gchansc Do you also use meteor? In that case it must be related to it. The weird thing is everything was working fine in the beginning. This all happened at some ambiguous point. Fortunately I found the work around with /lib at the end though...

eminx avatar Feb 27 '18 02:02 eminx

@eminx Yes. I'm testing Meteor with antd. Then I ran into this issue. Seems the current workaround is to add /lib when import.

Happy coding! ^^

ghost avatar Feb 27 '18 05:02 ghost

It's clear that problem with meteor builder. Try to build client code separetely on your host and give us feedback.

cloudever avatar Mar 09 '18 16:03 cloudever

Since this is an issue that has to do with Meteor, and not antd, I'm closing it.

eminx avatar Mar 13 '18 02:03 eminx

Hello!

After almost three years I'm reopening the same issue :)

Basically I get why we need to do the /lib hack: Because we have the .babelrc file defined with this configuration for treeshaking:

{
  "presets": ["meteor"],
  "plugins": [
    ["import", { "libraryName": "antd", "style": true }, "antd"],
    [
      "import",
      {
        "libraryName": "@ant-design/icons",
        "libraryDirectory": "lib/icons",
        "camel2DashComponentName": false
      },
      "@ant-design/icons"
    ]
  ]
}

But unfortunately the treeshaking itself is not working. Because well, I'm using only a few icons and it seems like they are over 1mb in the bundle!

How to make this tree-shaking work on Meteor so the bundle is for humans?

Screenshot 2021-02-06 at 02 36 16

eminx avatar Feb 06 '21 00:02 eminx

with vite and vue3 (with typescript) working import like that:

import { createApp } from "vue";
import App from "./App.vue";
import Antd from "ant-design-vue";
import "ant-design-vue/dist/antd.css";
const app = createApp(App);
// components
app.use(Antd);

// mount
app.mount('#app');

with webpack and vue3 (with typescript) working import like that:

import { createApp } from "vue";
import App from "./App.vue";
import Antd from "ant-design-vue/lib";
import "ant-design-vue/dist/antd.css";
const app = createApp(App);
// components
app.use(Antd);

// mount
app.mount('#app');

UPD: Need to configure babel-plugin-import and you can use only “ant-design-vue” path.

designervoid avatar Aug 02 '21 15:08 designervoid