@lingxiteam/bundle-less-theme-tools
Advanced tools
Comparing version 0.0.1-alpha.16 to 0.0.1-alpha.17
68
index.js
@@ -14,6 +14,10 @@ | ||
const cacheImportPath = {}; | ||
// 缓存变量值映射 | ||
// 缓存变量信息(第一次出现的值value 和 与第一次值不同的出现次数)映射,例如:{ '@xxx-xxx': { value: 'xxx', diffCount: xx } } | ||
const varsMapping = {}; | ||
// TODO: 预留,记录每个文件的变量信息映射,例如: { 'xx/xx/xx.less': fileVars } | ||
const filesVarsMapping = {}; | ||
function bundleTheme (filePath) { | ||
// 例如:{ '@xxx-xxx': { originName: '@xxx-xxx', value: 'xxx' } } | ||
const fileVarsMapping = {}; | ||
// 读取文件内容 | ||
@@ -32,8 +36,38 @@ const fileContent = fs.readFileSync(filePath) || ''; | ||
const lessVarsDefineMatch = /^@(.*):(.*);/; | ||
if(lessVarsDefineMatch.test(line)) { | ||
if (lessVarsDefineMatch.test(line)) { | ||
const [, key = '', value = ''] = line.match(lessVarsDefineMatch); | ||
// 缓存 less 变量值 | ||
if(key && value) { | ||
varsMapping[key.trim()] = value.trim(); | ||
const keyTrim = key?.trim(); | ||
const valueTrim = value?.trim(); | ||
if (keyTrim && valueTrim) { | ||
// 缓存第一次出现的 less 变量值 | ||
if (!varsMapping[keyTrim]) { | ||
varsMapping[keyTrim] = { | ||
value: valueTrim, | ||
diffCount: 0, | ||
} | ||
return line; | ||
} | ||
// 非第二次出现,但值相同 | ||
if (valueTrim === varsMapping[keyTrim]) { | ||
return line; | ||
} | ||
// 值不同,在文件变量映射中记录并生成新的变量名 | ||
//(TODO: 改名字容易,但是万一出现,这个名字的定义,被其他引用这个文件的文件里引用了,还要分析出所有直接引用或者间接引用这个文件的文件) | ||
let newLine = line; | ||
varsMapping[keyTrim].diffCount += 1; | ||
const newVarName = `${keyTrim}-${varsMapping[keyTrim].diffCount}`; | ||
fileVarsMapping[newVarName] = { | ||
originName: keyTrim, | ||
value: valueTrim, | ||
} | ||
newLine = newLine.replace(keyTrim, newVarName) | ||
filesVarsMapping[filePath] = fileVarsMapping; | ||
return newLine; | ||
} | ||
return line; | ||
} | ||
@@ -105,3 +139,25 @@ | ||
} | ||
return line; | ||
// 存储原名与更名的变量名的映射 | ||
const varsOriginNameMapping = {}; | ||
const fileVarsOriginNames = Object.keys(fileVarsMapping).map(v => { | ||
const varInfo = fileVarsMapping[v]; | ||
const originName = varInfo.originName.replace('@', ''); | ||
varsOriginNameMapping[originName] = v; | ||
return originName; | ||
}); | ||
let newLine = line; | ||
// 将本文件重命名的变量名应用到样式中去(前缀、使用的样式变量) | ||
fileVarsOriginNames.forEach(varOriginName => { | ||
const matchClassRegExp = new RegExp(`@{${varOriginName}}`); | ||
const matchVarsRegExp = new RegExp(`@${varOriginName}`) | ||
// remark: 如果一个文件同时定义了两个相同less变量,取最后出现的 | ||
newLine = newLine.replace(matchClassRegExp, `@{${varsOriginNameMapping[varOriginName]}}`); | ||
newLine = newLine.replace(matchVarsRegExp, `@${varsOriginNameMapping[varOriginName]}`); | ||
}) | ||
return newLine; | ||
}) | ||
@@ -108,0 +164,0 @@ .join('\n'); |
{ | ||
"name": "@lingxiteam/bundle-less-theme-tools", | ||
"version": "0.0.1-alpha.16", | ||
"version": "0.0.1-alpha.17", | ||
"description": "A tool for bundling less theme files.", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"license": "MIT", | ||
"gitHead": "46415e4b23aca7e2c2b6257a3100c184fff4a14a" | ||
"gitHead": "a84b93f6b40e6101d661a6f9fac9caf3ccc9e785" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
8608
170
0