next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Update Ant Design Examples for New CSS Support (9.2)

Open poyiding opened this issue 6 years ago • 74 comments

import ant-design library throw error

can you provide an example import ant-design use [RFC] css support? thanks. when i use [RFC] css support and import ant-design library, it throw error :

./node_modules/antd/lib/button/style/index.css
Global CSS cannot be imported from files other than your Custom <App>. Please move all global CSS imports to pages/_app.js.

here is my next-config.js

module.exports = {
    experimental: {
      css: true
    },
  }

poyiding avatar Dec 26 '19 02:12 poyiding

Maybe not support css from node_modules, I guess.

ladieman217 avatar Dec 26 '19 05:12 ladieman217

Maybe not support css from node_modules, I guess.

if you don't use [RFC] css support , you can support css from node_modules with-css plugin.

poyiding avatar Dec 26 '19 07:12 poyiding

Linking to #8626

timneutkens avatar Dec 26 '19 11:12 timneutkens

I’d be also interested in seeing this. Our teams use antd with next-css/next-less and some hacks on top from the Next.js examples folder. That setup is quite clumsy and fragile. I’m looking forward to switching to the build-in css support, but have no clue how to deal with less in this case.

antd is a pretty popular ui framework, so I guess quite a lot of devs will benefit from a new example.

kachkaev avatar Dec 26 '19 13:12 kachkaev

Has anyone got ant-design working with Next 9.2 yet? (without @zeit/next-css)

stephankaag avatar Jan 20 '20 08:01 stephankaag

@stephankaag, I have used withLess to use antd v4.0.0.rc1, it works!

jonahfang avatar Jan 24 '20 05:01 jonahfang

I'm still seeing issues with antd 4.0.

Configuration is similar to [https://levelup.gitconnected.com/lets-create-a-project-with-nextjs-antd-and-deploy-with-now-sh-e38772348312|this guide]

ChuckJonas avatar Jan 24 '20 08:01 ChuckJonas

@stephankaag, I have used withLess to use antd v4.0.0.rc1, it works!

Of course, because that combination always worked. I am trying to make things work WITHOUT @zeit/next-css or @zeit/next-less.

stephankaag avatar Jan 24 '20 08:01 stephankaag

Anyone has successfully integrated next 9.2 with ant design? (without next-css plugin)

dpyzo0o avatar Feb 04 '20 07:02 dpyzo0o

@stephankaag The combination works very well (I'm using withCss combined with withLess and custom less variables), but how is your bundle size? I have migrated to antd4. Seems like the tree shaking doesn't work as expected from v4. I'm having more than 1,3mb bundle size, where most of it are the antd icons. According to this https://github.com/ant-design/babel-plugin-import/issues/402 the "libraryDirectory": "es" is necessary (the official with-ant-less example doesn't have it). But adding this gives me the following error:

[ error ] /home/maciek/Dokumenty/websites/bookingapp/frontend/node_modules/antd/es/notification/index.js:3
import * as React from 'react';
       ^

SyntaxError: Unexpected token *
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.antd/es/notification (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:10975:18)
    at __webpack_require__ (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:23:31)
    at Module../lib/withData.js (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:8200:78)
    at __webpack_require__ (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:23:31)
    at Module../pages/_app.js (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:10614:71)
    at __webpack_require__ (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:23:31)
    at Object.0 (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:10754:18)
    at __webpack_require__ (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:23:31)
    at /home/maciek/Dokument

chemicalkosek avatar Feb 06 '20 12:02 chemicalkosek

@stephankaag The combination works very well (I'm using withCss combined with withLess and custom less variables), but how is your bundle size? I have migrated to antd4. Seems like the tree shaking doesn't work as expected from v4. I'm having more than 1,3mb bundle size, where most of it are the antd icons. According to this ant-design/babel-plugin-import#402 the "libraryDirectory": "es" is necessary (the official with-ant-less example doesn't have it). But adding this gives me the following error:

[ error ] /home/maciek/Dokumenty/websites/bookingapp/frontend/node_modules/antd/es/notification/index.js:3
import * as React from 'react';
       ^

SyntaxError: Unexpected token *
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.antd/es/notification (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:10975:18)
    at __webpack_require__ (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:23:31)
    at Module../lib/withData.js (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:8200:78)
    at __webpack_require__ (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:23:31)
    at Module../pages/_app.js (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:10614:71)
    at __webpack_require__ (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:23:31)
    at Object.0 (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:10754:18)
    at __webpack_require__ (/home/maciek/Dokumenty/websites/bookingapp/frontend/.next/server/static/development/pages/_app.js:23:31)
    at /home/maciek/Dokument

@chemicalkosek did you manage to resolve this? I've been running into the exact same issue on Nextjs 9.2.1

DuncanAForbes avatar Feb 06 '20 13:02 DuncanAForbes

Anyone manage to resolve this issue?

jeromemmiranda avatar Feb 06 '20 23:02 jeromemmiranda

Any updates to this issue?

gjvpaet avatar Feb 06 '20 23:02 gjvpaet

My solution:

yarn add esm
"scripts": {
    "dev": "NODE_OPTIONS=\"-r esm\" next",
    "build": "NODE_OPTIONS=\"-r esm\" next build",
    "start": "NODE_OPTIONS=\"-r esm\" next start"
}

i-tengfei avatar Feb 12 '20 03:02 i-tengfei

@i-tengfei Wow, it's working! Thank you very much!

chemicalkosek avatar Feb 12 '20 17:02 chemicalkosek

@i-tengfei are you using the same configuration as the example?

If not, mind posting your next.config & babelrc?

ChuckJonas avatar Feb 12 '20 19:02 ChuckJonas

Btw I'm not using the new CSS support. What's working is next with antd4 tree shaked (tree shaking never worked with antd in the official example, because of the lack of "libraryDirectory":"es"). Bundle size cut about 700-800kb I believe. I'm still using withCss and withLess tho.

chemicalkosek avatar Feb 12 '20 19:02 chemicalkosek

@chemicalkosek do you still see:

chunk styles [mini-css-extract-plugin]
Conflicting order between

with your setup?

ChuckJonas avatar Feb 12 '20 20:02 ChuckJonas

Yes, I do, but afaik that's just a warning and everything works just fine. I have once researched this issue and I believe it's because you have in one component:

import { Button, Input } from 'antd'

and in another component:

import { Input, Button } from 'antd'

Supposedly it's what's creating this issue, but I'm not really sure of it You can hide those warnings, but I didn't try. I'll try tomorrow though, those can be pretty annoying. Source 1: https://github.com/webpack-contrib/mini-css-extract-plugin#remove-order-warnings Source 2: https://github.com/webpack-contrib/mini-css-extract-plugin/issues/250 I'm very happy about the tree shaking now :) And with antd4 the icons are no longer included as they were with antd3, where the icons made like half of antd3 size. You can import icons selectively now.

chemicalkosek avatar Feb 12 '20 21:02 chemicalkosek

Unfortunately, my experience is it's not just a warning you can just ignore. I've seen issues with components not getting styled as I've added more antd components to the page. Happens very inconsistently. Organizing imports consistently doesn't make it go away either

ChuckJonas avatar Feb 12 '20 21:02 ChuckJonas