I am using antd in my nextjs project. Importing the entire css bundle via import 'antd/dist/antd.css' works, however I would like to have modular imports to reduce by page load speed.
So I followed instruction from ant-design/babel-plugin-import to configure modular imports. However, I get an error when I try a modular import (in my case Table component of antd):
[ error ] ./node_modules/antd/es/button/style/index.css
Global CSS cannot be imported from within node_modules.
Read more: https://err.sh/next.js/css-npm
Location: node_modules/antd/es/button/style/css.js
/Users/subodh/workspace/next-antd-babel-import-plugin-bug/node_modules/antd/es/table/style/css.js:1
import '../../style/index.css';
^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected string
Steps to reproduce the behavior, please provide code snippets or a repository:
npm installnpm run devThe error is triggered due to this line:
import { Table } from "antd";
There should be no error and the above modular import should work fine:
Not applicable
OS: macOS
{
"name": "next-antd-babel-import-plugin-bug",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"antd": "^4.0.4",
"babel-plugin-import": "^1.13.0",
"babel-plugin-inline-import": "^3.0.0",
"next": "9.3.2",
"react": "16.13.1",
"react-dom": "16.13.1",
"sass": "^1.26.3"
}
}
{
"presets": [
"next/babel"
],
"plugins": [
[
"import",
{
"libraryName": "antd",
"style": "css",
"libraryDirectory": "es"
}
]
]
}
import '../styles.scss'
function MyApp({Component, pageProps}) {
return <Component className="app" {...pageProps} />
}
export default MyApp
$backgroundColor: #2ecc71;
.app {
background-color: $backgroundColor;
}
import { Table } from "antd";
const columns = [
{
title: "Name",
dataIndex: "title",
key: "title"
},
{
title: "Age",
dataIndex: "age",
key: "age"
}
];
export class Home extends React.Component {
constructor(props){
super(props);
this.state = {
data: [
{
title: "John Doe",
age: 28
},
{
title: "Johnny Doe",
age: 29
}
]
}
}
render() {
return (
<>
<main>
<h1>Hello World</h1>
<Table
columns={columns}
dataSource={this.state.data}
pagination={false}
rowKey="id"
/>
</main>
</>
);
}
}
export default Home
I think it is related to this issue https://github.com/zeit/next.js/issues/9830
I think I figured out what the problem is. Antd uses .less files but Nextjs' builtin CSS support does not support .less files. So we need to use @zeit/next-less.
However, if we use @zeit/next-less, it disables the built in CSS support for .scss files. This causes problems for me because I use .scss files.
Is there a workaround to this?
Same problem here: https://github.com/lucasbastianik/next-with-antd-modularized-error
any update?
https://github.com/zeit/next-plugins/issues/598#issuecomment-618461761
I use this configure, it's working.
https://github.com/ye-will/next-plugin-antd
This plugin may help.