Skip to content

🎩 Use Antd (Less) with Next.js, Zero Dependency on other Next-Plugins. Support Webpack 4 & 5.

License

Notifications You must be signed in to change notification settings

countcain/next-plugin-antd-less

Β 
Β 

Repository files navigation

Next.js + Antd (with Less)

NPM version License: MIT

Use Antd (Less) w/ Next.js, Zero Dependency on other Next-Plugins.

πŸ“Œ Demo

Demo w/ Next.js v11 by mkn

Demo w/ CRA v4 by mkr

Yep! this plugin supports both Next.js and CRA since v1.0.

πŸŽ‰ Features

  • Zero Dependency on other Next.js Plugins
  • Support Both Next.js & CRA Project
  • Support Hot-Update After modifying Antd less vars
  • Support Serverless Mode

🌊 Compatibility

  • next v9.0 (webpack 4)
  • next v10 & v11 (webpack 4 & 5)
  • less v3.0+

πŸ“¦ Installation

yarn add next-plugin-antd-less

πŸ”° Usage

// next.config.js
const withAntdLess = require('next-plugin-antd-less');

module.exports = withAntdLess({
  // optional
  modifyVars: { '@primary-color': '#04f' },
  // optional
  lessVarsFilePath: './src/styles/variables.less',
  // optional
  lessVarsFilePathAppendToEndOfContent: false,
  // optional https://github.com/webpack-contrib/css-loader#object
  cssLoaderOptions: {},

  // Other Config Here...

  webpack(config) {
    return config;
  },

  // ONLY for Next.js 10, if you use Next.js 11, delete this block
  future: {
    webpack5: true,
  },
});

Add a .babelrc.js

// .babelrc.js
module.exports = {
  presets: [['next/babel']],
  plugins: [['import', { libraryName: 'antd', style: true }]],
};

Detailed config can be found in next.config.js file.

for CRA / CRA-Co

const cracoPluginLess = require('next-plugin-antd-less/overrideWebpackConfig');

module.exports = {
  babel: cracoBabel,
  plugins: [
    cracoPluginAnalyze,
    {
      plugin: cracoPluginLess,
      options: {
        modifyVars: {
          '@THEME--DARK': 'theme-dark',
        },
        lessVarsFilePath: './src/styles/variables.less',
      },
    },
  ],
};

Detailed config can be found in craco.config.js file.

πŸ–ŒοΈ FAQ

Reference Project?

If you have any problem, please check mkn (Next.js) and mkr (CRA) first, I update these two repo's every time I update this plugin.

How to import global CSS style (e.g. styles.css)?

// ./page/_app.tsx
//
// use `import` or `require` syntax,
import './styles.css';

How to import global Less style (e.g. styles.less)?

// ./page/_app.tsx
//
// use `require` syntax,
require('./styles.less');

How to overwrite antd less variables?

// ./src/styles/variables.less
//
@primary-color: #04f; // change antd primary-color
// plugin options
lessVarsFilePath: './src/styles/variables.less'

@seeMore issues #36

🎩 Background

Issues

Since Next.js 9.3 supports sass and css by default, but does not support less. If you use Next.js > 9.3 and use the official less plugin, you will definitely encounter the following problems.

  1. CIL Warning Warning: Built-in CSS support is being disabled due to custom CSS configuration being detected.

  2. Does not support automatic recognition of css modules, e.g. a.module.less and a.less

Solution

  1. Find sassModule and copy onec and replace the sass-loader inside with less-loader.

  2. Then enable the modules.auto option of css-loader. This can simply match all *.less (no need to match it is *.module.less or *.less), and hand it over to css-loader.

This is the lowest cost way, And CLI will no longer show this disgusting warning. The important thing is that there is Zero Dependency on other Next-Plugins..

πŸ“œ License

Β© MIT

About

🎩 Use Antd (Less) with Next.js, Zero Dependency on other Next-Plugins. Support Webpack 4 & 5.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%