$ cnpm install @uyun/cli-webpack-less-themes
UED 设计会根据 基础色板,中性色板 设计出一套 标准全局变量,以上变量可以通过 主题管理后台 进行管理。 主题管理后台会生成,less 标准全局变量,存入到 @uyun/everest/styles。 css 标准全局变量,存入到 @uyun/runtime, 并提供接口获取 css 变量。
项目样式文件中必须使用 less 标准全局变量,样式文件中引用的 less 标准全局变量 会在编译的过程中转换成引用 css 标准全局变量,@uyun/runtime 会在项目运行的时候将 css 标准全局变量 插入到 DOM 结构中。
less
标准全局变量使用方式在项目可以直接使用主题管理后台中的 标准全局变量, 无需导入@uyun/everest-styles
。因为变量会在编译的过程中转化成css变量,例: @page-bg => --var(page-bg)
, 所有不会产生 less 编译错误
使用步骤如下:
@uyun/cli-webpack-less-themes
yarn add @uyun/cli-webpack-less-themes
@uyun/cli
脚手架配置文件 everest.config.js
plugins: [
'@uyun/cli-webpack-less-themes'
]
/** @import "~@uyun/everest-styles"; 注意无需导入 @uyun/everest-styles **/
body {
background: @page-bg;
}
less
变量替换成标准全局变量实现方案方案步骤如下:
less
标准全局变量// 配置 less-loader 参数 options.modifyVars
{
loader: 'less-loader',
options: {
/**
* 获取 modifyVars 方式
* 1. 标准主题管理后台中提供接口获取 modifyVars
* 2. 插件 @uyun/cli-webpack-less-themes/lib/get-css-vars.js 中也提供了方法获取
* **/
modifyVars: {
'@page-bg': 'var(--page-bg)'
},
},
}
// 源码
body {
background: @page-bg;
}
// 编译后
body {
background: var(--page-bg);
}
less
原生函数, 将其转换成 css 变量/**
* 1. 覆盖 less 原生函数
* 2. less函数原生函数源码
**/
function makeRegistry( base ) {
return {
_data: {},
add: function(name, func) {
// precautionary case conversion, as later querying of
// the registry by function-caller uses lower case as well.
name = name.toLowerCase();
if (this._data.hasOwnProperty(name)) {
//TODO warn
}
this._data[name] = func;
},
addMultiple: function(functions) {
Object.keys(functions).forEach(
function(name) {
this.add(name, functions[name]);
}.bind(this));
},
get: function(name) {
return this._data[name] || ( base && base.get( name ));
},
inherit : function() {
return makeRegistry( this );
}
};
}
module.exports = makeRegistry( null );
/**
* 例: fade(@page-bg, 30%) 函数覆盖
* 1. 当参数 color 值为 css 变量
* 2. fade 函数返回值参数, 都是 less ast-node 对象
* 3. less 函数转换成 css 变量形式, 例 fade(@page-bg, 30%) => fade__page-bg__30
* 4. fade__page-bg__30 记录到 node 全局变量 global.variables 中
* 5. 根据 global.variables 的值生成 css 变量与变量计算出的值, 例 fade__page-bg__30 会根据参数重新调用 less的fade 函数算出来值赋值给css 变量
* 6. 开发环境 webpack.devServerConfig 配置覆盖将 global.variables 数据返回到前端, 然后将获取到变量插入到 DOM 结构中
* 7. 生产环境 将 global.variables 的值写入打包后前端代码中
**/
functionRegistry.addMultiple({
fade (color, amount) {
if (color.name === 'var') {
if (!global.variables) {
global.variables = {}
}
if (!global.variables[method]) {
global.variables[method] = {}
}
const value = color.args[0].value
const util = amount.value
const variable = `${method}__${value.replace('--', '')}__${util}`
global.variables[method][variable] = [color, amount]
return new Call('var', [new Keyword(`--${variable}`)])
}
const fade = functionRegistry.get('fade')
return fade(color, amount)
}
})
less
自定义函数, 将其转换成 css 变量/**
* 例: ~`colorPalette('@{purple-6}', 1) ` 函数覆盖
* 1. 当参数 color 值为 css 变量, @{purple-6} 会被转换成 var(--purple-6)
* 2. colorPalette 函数返回值是一个字符串
* 3. less 函数转换成 css 变量形式, 例 ~`colorPalette('@{purple-6}', 1) ` => colorPalette__purple-6__1
* 4. fade__page-bg__30 记录到 node 全局变量 global.variables 中
* 5. 根据 global.variables 的值生成 css 变量与变量计算出的值, 例 colorPalette__purple-6__1 会根据参数重新调用 less 的 colorPalette 函数算出来值赋值给css 变量
**/
.colorPaletteMixin() {
@functions: ~`(function() {
this.colorPalette = function(color, index) {
// color var(--purple-6)
var method = 'colorPalette'
var _color = color.replace('var(--', '').replace(')', '')
var name = 'colorPalette__' + _color + '__' + index
var variable = 'var(--'+name+')'
if (!global.variables) {
global.variables = {}
}
if (!global.variables[method]) {
global.variables[method] = {}
}
global.variables[method][name] = [_color, index]
return variable
};
})()`;
}
// It is hacky way to make this function will be compiled preferentially by less
// resolve error: `ReferenceError: colorPalette is not defined`
// https://github.com/ant-design/ant-motion/issues/44
.colorPaletteMixin();
Copyright 2013 - present © cnpmjs.org