パッケージの詳細

next-plugin-antd-less

SolidZORO29.9kMIT1.8.0

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

next, nextjs, next.js, less

readme

Next.js + Antd (with Less)

version license size download

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

Demo

Demo w/ Next.js v12 by mkn

Demo w/ CRA v5 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
  • Support Antd Pro

Compatibility

  • Next.js v11 / v12
  • CRA v4 / v5

Installation

yarn add next-plugin-antd-less
yarn add --dev babel-plugin-import

Usage

for Next.js

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

module.exports = withAntdLess({
  modifyVars: { '@primary-color': '#04f' }, // optional
  lessVarsFilePath: './src/styles/variables.less', // optional 
  lessVarsFilePathAppendToEndOfContent: false, // optional
  // optional https://github.com/webpack-contrib/css-loader#object
  cssLoaderOptions: {
    // ... 
    mode: "local",
    localIdentName: __DEV__ ? "[local]--[hash:base64:4]" : "[hash:base64:8]", // invalid! for Unify getLocalIdent (Next.js / CRA), Cannot set it, but you can rewritten getLocalIdentFn
    exportLocalsConvention: "camelCase",
    exportOnlyLocals: false,
    // ...
    getLocalIdent: (context, localIdentName, localName, options) => {
      return "whatever_random_class_name";
    },
  },

  // for Next.js ONLY
  nextjs: {
    localIdentNameFollowDev: true, // default false, for easy to debug on PROD mode
  },

  // 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',
        cssLoaderOptions: {
          localIdentName: __DEV__ ? "[local]--[hash:base64:4]" : "[hash:base64:8]",
        },
      },
    },
  ],
};

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.

Default ClassName

MODE className e.g.
DEV [local]--[hash:base64:4] comp-wrapper--2Rra
PROD [hash:base64:8] 2Rra8Ryx

for Unify getLocalIdent (Next.js / CRA), Cannot set it, but you can rewritten getLocalIdentFn

localIdentName is invalid? How to rewritten?

you can defind your own localIdentName in pluginOptions.cssLoaderOptions.modules.getLocalIdent

  options: {
  lessVarsFilePath: './src/styles/variables.less'
  // ...
  // https://github.com/webpack-contrib/css-loader/tree/b7a84414fb3f6e6ff413cbbb7004fa74a78da331#getlocalident
  //
  // and you can see file 
  // https://github.com/SolidZORO/next-plugin-antd-less/getCssModuleLocalIdent.js
  getLocalIdent: (context, _, exportName, options) => {
    return 'whatever_random_class_name';
  }
  // ...
}

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
@import '~antd/lib/style/themes/default.less'; // <-- you need to import antd variables once in your project

@primary-color: #04f; // change antd primary-color
// 🔰️ Tips: if your use babel import plugin and set `libraryDirectory`, please keep `libraryDirectory` and `less path` consistent.

// lib
['import', { libraryName: 'antd', libraryDirectory: 'lib', style: true }]
// `@import '~antd/lib/style/themes/default.less';` <-- use `lib`

// es
  ['import', { libraryName: 'antd', libraryDirectory: 'es', style: true }]
// --> `@import '~antd/es/style/themes/default.less';` <-- use `es`
// plugin options
lessVarsFilePath: './src/styles/variables.less'

@seeMore issues #36, #74

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 © Jason Feng

更新履歴

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.8.0 (2022-01-09)

Features

  • add nextjs.localIdentNameFollowDev config (2bfcad8)

1.7.0 (2021-12-23)

Features

1.6.0 (2021-12-10)

Features

  • for Unify getLocalIdent (Next.js / CRA) (615e95d)

1.5.2 (2021-11-01)

Bug Fixes

  • remove the optional chain, for compatible deployment to vercel (e8b31f4)

1.5.1 (2021-11-01)

1.5.0 (2021-11-01)

Bug Fixes

  • compatible with custom getLocalIdent in Next.js 12 (a20649e)

1.4.4 (2021-10-19)

Chore

  • upgrade Less to 4.1.2 thanks @nring

1.4.3 (2021-09-16)

Features

1.4.2 (2021-08-23)

Bug Fixes

  • remove background-image, compatible CRA and Next.js (另外还有写在注释里的碎碎念) (60c10de)

1.4.1 (2021-08-23)

Bug Fixes

  • downgrading less-loader to ^7, support CRA (^10 minimun wp5) (2d65318)

1.4.0 (2021-08-22)

Features

  • add echoIsServerInfo() Fn for Debug (9daff7c)
  • next-image-loader supported *.less (1860ea2), closes #59 #39
  • support *.svg with babel-plugin-inline-react-svg (daec84b), closes #72
  • update less-loader to v10 (9e495ae)

Chore

1.3.0 (2021-06-16)

Features

Chore

1.2.2 (2021-06-03)

Features

  • support serverless mode (d187df8)

1.2.1 (2021-05-15)

Features

  • file-loader supported *.less, e.g. background-image: url('img.jpg') (99011d4), closes #39

1.2.0 (2021-05-09)

Chore

  • format code by prettier config (e300a14)

1.1.4 (2021-04-29)

Bug Fixes

  • Make lessVarsFilePathAppendToEndOfContent not to include twice (138e0c3)

1.1.3 (2021-04-28)

Features

  • add options lessVarsFilePathAppendToEndOfContent (f42bbe2), closes #40

1.1.2 (2021-04-25)

Bug Fixes

  • downgrade less-loader version to ^7.0 (TypeError: this.getOptions is not a function) (84df302), closes #30

1.1.1 (2021-04-25)

1.1.0 (2021-04-25)

Refactor

  • update less-loader to 8.0 (a48cb89)

1.0.10 (2021-04-25)

Refactor

1.0.9 (2021-04-25)

Bug Fixes

  • cannot set property '.less' of undefined (42ba69a)
  • cannot set property '.less' of undefined (8b178ac)
  • fixed pluginOptions.modifyVars to be optional (5e941f0), closes #42

1.0.8 (2021-04-09)

Refactor

  • close debug console.log (3b034e5)

1.0.7 (2021-04-09)

Bug Fixes

  • fixed config localIdentName (82fd24e)

1.0.6 (2021-04-07)

Bug Fixes

  • fixed not options lessVarsFilePath has error (686327c)

1.0.5 (2021-04-05)

Refactor

  • remove all optional chaining, compatibility vercel deploy (9c6886c)

1.0.4 (2021-04-05)

Bug Fixes

  • fixed nest.js style lose (92fa666)

1.0.3 (2021-04-05)

Bug Fixes

1.0.2 (2021-04-04)

Chore

  • remove deps less-vars-to-js (c605968)

1.0.1 (2021-04-04)

1.0.0 (2021-04-04)

Features

  • supports both Next.js and CRA-Co (418ec5c)

Chore

Refactor

  • clear variable names (590f116)
  • split overrideWebpackConfig (aded75c)

0.3.0 (2021-02-07)

Bug Fixes

  • downgrading to less-loader 7.3.0 resolved issue (5cc2f20), closes #17

0.2.2 (2021-02-02)

Refactor

  • config.module.rules INDEX compatible w/ webpack 4 and 5 (d73be86)
  • use RULES_INDEX, more clearly code comment (bae11d5)

0.2.1 (2021-02-02)

Chore

  • update deps less-loader@8.0.0 (5de28ae)

0.2.0 (2021-02-01)

Refactor

0.1.4 (2021-01-26)

0.1.3 (2020-11-25)

Bug Fixes

0.1.2 (2020-11-25)

Features

0.1.1 (2020-11-25)

0.1.0 (2020-11-25)

Features

0.0.6 (2020-08-27)

0.0.5 (2020-08-27)

Bug Fixes

  • unintended overwriting of sass module rule (deffefe)

0.0.4 (2020-08-05)

Performance Improvements

  • use clone instend of lodash.clone (e6443fa)

0.0.3 (2020-08-05)

0.0.2 (2020-08-05)

Features

  • handle less file with antd (5bdf1c3)