大前端-万物皆可JS

react+nextjs+antd+less配置

2019-12-30  本文已影响0人  soojade

插件安装

  "@zeit/next-less": "^1.0.1",
  "antd": "^3.26.5",
  "babel-plugin-import": "^1.13.0",
  "less": "^3.10.3",
  "less-vars-to-js": "^1.3.0",
  "null-loader": "^3.0.0",

配置 less

const withLess = require('@zeit/next-less')
const lessToJS = require('less-vars-to-js')
const fs = require('fs')
const path = require('path')

// 自定义less主题
const themeVariables = lessToJS(
    fs.readFileSync(path.resolve(__dirname, './assets/antd-custom.less'), 'utf8')
)

module.exports = withLess({
    lessLoaderOptions: {
        javascriptEnabled: true,
        modifyVars: themeVariables, // 上面定义的主题
    },
    webpack: (config, { isServer }) => {
        if (isServer) { // 为true使用在服务端, 为false使用在客户端
            const antStyles = /antd\/.*?\/style.*?/
            const origExternals = [...config.externals]
            config.externals = [
                (context, request, callback) => {
                    if (request.match(antStyles)) return callback()
                    if (typeof origExternals[0] === 'function') {
                        origExternals[0](context, request, callback)
                    } else {
                        callback()
                    }
                },
                ...(typeof origExternals[0] === 'function' ? [] : origExternals),
            ]

            config.module.rules.unshift({
                test: antStyles,
                use: 'null-loader',
            })
        }
        return config
    },
})

自定义less主题文件

@primary-color: #18ecff; // 全局主色
@link-color: #3a9af5; // 链接色
@success-color: #7af53c; // 成功色
@warning-color: #f6a708; // 警告色
@error-color: #f6101b; // 错误色
@font-size-base: 14px; // 主字号
@heading-color: rgba(0, 0, 0, 0.85); // 标题色
@text-color: rgba(0, 0, 0, 0.65); // 主文本色
@text-color-secondary : rgba(0, 0, 0, .45); // 次文本色
@disabled-color : rgba(0, 0, 0, .25); // 失效色
@border-radius-base: 4px; // 组件/浮层圆角
@border-color-base: #d9d9d9; // 边框色
@box-shadow-base: 0 2px 8px rgba(0, 0, 0, 0.15); // 浮层阴影

按需导入

{
  "presets": [
    "next/babel"
  ],
  "plugins": [
    [
      "import",
      {
        "libraryName": "antd",
        "style": true
      }
    ]
  ]
}
上一篇 下一篇

猜你喜欢

热点阅读