Use ANTD (menos) com Next.js, dependência zero de outros próximos plugins.
Demo W/ Next.js v12 por mkn
Demonstração com CRA V5 por MKR
Sim! Este plug -in suporta o próximo.js e o CRA desde a v1.0.
yarn add next-plugin-antd-less
yarn add --dev babel-plugin-import // 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 ,
} ,
} ) ; Adicione um .babelrc.js
// .babelrc.js
module . exports = {
presets : [ [ 'next/babel' ] ] ,
plugins : [ [ 'import' , { libraryName : 'antd' , style : true } ] ] ,
} ; Config detalhado pode ser encontrado no arquivo next.config.js .
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]" ,
} ,
} ,
} ,
] ,
} ; Config detalhado pode ser encontrado no arquivo craco.config.js .
Se você tiver algum problema, verifique MKN (Next.js) e MKR (CRA) primeiro, atualizo esses dois repositórios toda vez que atualizo este plug -in.
| MODO | ClassName | por exemplo |
|---|---|---|
| Dev | [local]--[hash:base64:4] | comp-wrapper--2Rra |
| Prod | [hash:base64:8] | 2Rra8Ryx |
Para o Unify GetLocalident (Next.js / CRA), não pode defini -lo, mas você pode reescrever GetLocalIdentFn
Você pode definir seu próprio localIdentName em 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' ;
}
// ...
}CSS (por exemplo, Styles.css)? // ./page/_app.tsx
//
// use `import` or `require` syntax,
import './styles.css' ;Less estilo global (por exemplo, estilos.less)? // ./page/_app.tsx
//
// use `require` syntax,
require ( './styles.less' ) ;antd menos variáveis? // ./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
Desde Next.js 9.3 suporta sass e css por padrão, mas não suporta less . Se você usar o Next.js> 9.3 e usar o plug -in Official, definitivamente encontrará os seguintes problemas.
Warning: Built-in CSS support is being disabled due to custom CSS configuration being detected.
Não suporta o reconhecimento automático dos módulos CSS, por exemplo, a.module.less e a.less
Encontre Sassmodule e Copie o ONEC e substitua o sass-loader por dentro por less-loader .
Em seguida css-loader ative os modules.auto . Isso pode simplesmente corresponder a tudo *.less (não é necessário corresponder a *.module.less ou *.less ) e entregá-lo ao css-loader .
Esta é a maneira mais baixa de custo, e a CLI não mostrará mais esse aviso nojento. O importante é que não há dependência zero de outros próximos plugins. .
MIT © Jason Feng