ใช้ antd (น้อยกว่า) w/ next.js, การพึ่งพาศูนย์ต่อไปอื่น ๆ
สาธิต w/ next.js v12 โดย mkn
Demo W/ CRA V5 โดย MKR
ใช่! ปลั๊กอินนี้รองรับทั้ง Next.js และ CRA ตั้งแต่ 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 ,
} ,
} ) ; เพิ่ม .babelrc.js
// .babelrc.js
module . exports = {
presets : [ [ 'next/babel' ] ] ,
plugins : [ [ 'import' , { libraryName : 'antd' , style : true } ] ] ,
} ; การกำหนดค่าโดยละเอียดสามารถพบได้ในไฟล์ 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]" ,
} ,
} ,
} ,
] ,
} ; การกำหนดค่าโดยละเอียดสามารถพบได้ในไฟล์ craco.config.js
หากคุณมีปัญหาใด ๆ โปรดตรวจสอบ MKN (next.js) และ MKR (CRA) ก่อนฉันจะอัปเดต REPO สองรายการนี้ทุกครั้งที่ฉันอัปเดตปลั๊กอินนี้
| โหมด | ชื่อชั้นเรียน | เช่น |
|---|---|---|
| คนกิน | [local]--[hash:base64:4] | comp-wrapper--2Rra |
| แยง | [hash:base64:8] | 2Rra8Ryx |
สำหรับ Unify getLocalident (next.js / CRA) ไม่สามารถตั้งค่าได้ แต่คุณสามารถเขียน getLocalidentFn ใหม่
คุณสามารถปิดบัง localIdentName ของคุณเองใน 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 ทั่วโลก (เช่น Styles.css) ได้อย่างไร? // ./page/_app.tsx
//
// use `import` or `require` syntax,
import './styles.css' ;Less ทั่วโลก (เช่นสไตล์ไม่)? // ./page/_app.tsx
//
// use `require` syntax,
require ( './styles.less' ) ;antd ตัวแปรน้อยลงได้อย่างไร? // ./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'@Semore ปัญหา #36, #74
ตั้งแต่ next.js 9.3 รองรับ sass และ css ตามค่าเริ่มต้น แต่ไม่รองรับ less หากคุณใช้ next.js> 9.3 และใช้ปลั๊กอินน้อยลงอย่างเป็นทางการคุณจะพบปัญหาต่อไปนี้อย่างแน่นอน
Warning: Built-in CSS support is being disabled due to custom CSS configuration being detected.
ไม่รองรับการรับรู้โมดูล CSS อัตโนมัติเช่น a.module.less และ a.less
ค้นหา sassmodule และคัดลอก ONEC และแทนที่ sass-loader ภายในด้วย less-loader
จากนั้นเปิดใช้งาน modules.auto ตัวเลือก AUTO ของ css-loader สิ่งนี้สามารถจับคู่ทั้งหมด *.less (ไม่จำเป็นต้องจับคู่มันคือ *.module.less หรือ *.less ) และส่งมอบให้กับ css-loader
นี่เป็นวิธีที่ต่ำที่สุดและ CLI จะไม่แสดงคำเตือนที่น่าขยะแขยงนี้อีกต่อไป สิ่งสำคัญคือ ไม่มีการพึ่งพาศูนย์อื่น ๆ -
MIT © Jason Feng