less-vars-to-js 是一个用于将 LESS 变量转换为 JavaScript 对象的工具。它可以帮助你在使用 LESS 编写样式的同时,可以方便地在 JavaScript 代码中使用这些变量。

文档

安装

npm install less-vars-to-js -D

参数说明

参数 说明
resolveVariables Resolves variables when they are defined in the same file.
dictionary When resolveVariables is true, passes an object to use when the value cannot be resolved in the same file.
stripPrefix Removes the @ or $ in the returned object keys.

示例

import lessToJs from "less-vars-to-js";
import fs from "fs";

// 将less文件读取到字符串
const paletteLess = fs.readFileSync("vars.less", "utf8");

// 解析less变量
const palette = lessToJs(paletteLess, {
  resolveVariables: true,
  stripPrefix: true,
});

console.log(palette);
// { blue: '#0d3880', pink: '#e60278' }