Skip to content

Commit

Permalink
Fix vite build & auto update outfile after init & ignore suffix & Add…
Browse files Browse the repository at this point in the history
… loop combo log & auto reload config
  • Loading branch information
TheEdgesofBen committed Feb 12, 2023
1 parent 2aba499 commit 73d95fb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "module",
"ignorePatterns": ["*.config.js"]
}
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ TODOS:
- Improve gathering classes from files &llow value function with "," and spaces [Check]
- Possibility to add classes manuely in config [Check]
- Ignore dashes in value functions
- Fix update output file (Class map + count)
- Auto update by config & combo files changes
- Loop combo error handling
- ignore suffix not working
- Ignore dashes in value functions [Check]
- Fix update output file (Class map + count) [Check]
- Reload config by changes [Check]
- Loop combo error handling [Check]
- ignore suffix not working [Check]
- Comments & Refactoring
- Error handling for syntax errors from css outfile file
Expand Down Expand Up @@ -54,6 +54,11 @@ function init(opts) {

watcher.on("change", () => {
console.log("ComboCSS change detected", new Date());

config = JSON.parse(fs.readFileSync("combo.config.json"));

if (!config) config = opts;

process(config);
});
}
Expand All @@ -62,7 +67,7 @@ module.exports = (opts = {}) => {
if (!initialized) init(opts);

return {
postcssPlugin: "project-combo",
postcssPlugin: "combocss",
};
};

Expand Down
44 changes: 41 additions & 3 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const postcss = require("postcss");
const { AtRule, Rule } = require("postcss");
const { isResponsive, getResponsiveSize, findNode, isIgnored, camelToDash, escapeCssSelector, currentFileClasses } = require("./utils");

var filesLastModified = {};
let filesLastModified = {};
let classesMap = {};

function process(opts) {
let rawFilesClasses = {};
Expand Down Expand Up @@ -71,6 +72,10 @@ function updateOutputFile(filesClasses, opts) {
let newNode = null;
let oldNode = null;

if (!classesMap[selector]) classesMap[selector] = [];

if (!classesMap[selector].includes(fileName)) classesMap[selector].push(fileName);

newNode = createNode(selector, classes[selector], opts);

oldNode = findNode(newNode, root);
Expand All @@ -85,6 +90,30 @@ function updateOutputFile(filesClasses, opts) {
});
});

Object.keys(classesMap).forEach((selector) => {
classesMap[selector].forEach((fileName, fileNameIndex) => {
if (filesClasses[fileName] && !filesClasses[fileName][selector]) {
classesMap[selector].splice(fileNameIndex, 1);
}
});
});

Object.keys(classesMap).forEach((selector) => {
let node = null;
let foundNode = null;

if (classesMap[selector].length == 0) {
delete classesMap[selector];

node = createNode(selector, { rules: [] }, opts);
foundNode = findNode(node, root);

if (foundNode) foundNode.remove();
}
});

/*
Object.keys(currentFileClasses).forEach((fileName) => {
currentFileClasses[fileName].forEach((selector) => {
if (!(filesClasses[fileName] || {})[selector]) {
Expand All @@ -108,6 +137,8 @@ function updateOutputFile(filesClasses, opts) {
});
});
*/

let result = root.toResult();

//console.log("End", currentFileClasses, result.css);
Expand Down Expand Up @@ -362,10 +393,11 @@ function getDeclarationParts(className) {
if (className.indexOf("-") == 0 || className.indexOf("-") == 1) isNegativeValue = true;
if (className.indexOf("!") == 0) isImportantValue = true;

declarationParts = className.split(":")[0].split("-");
declarationParts = className.split(":")[0].split(/\-(?![^\(]*\))/g);

if (declarationParts[1] && declarationParts[1].match(/(\(.*?\))/)) {
let valueFunc = declarationParts[1].match(/(\(.*?\))/)[0];
let valueFunc = declarationParts[1].split("(");
valueFunc = "(" + valueFunc.splice(1, valueFunc.length).join("(");
let resolvedValueFunc = valueFunc.replaceAll("_", " ");
declarationParts[1] = declarationParts[1].replace(valueFunc, resolvedValueFunc);
}
Expand Down Expand Up @@ -427,6 +459,12 @@ function createClassRules(cssClass, opts, customClassesMap) {
});
} else selectors = [classParts[1]];

if (customClassesMap[getComboClassPart(classParts[1])] && selectors.includes(getComboClassPart(classParts[1]))) {
console.error("Error combo class includes it self: ", getComboClassPart(classParts[1]));

return rules;
}

selectors.forEach((selector) => {
classParts = getClassParts(selector, opts);

Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function isIgnored(className, opts) {
if (ignoredPrefix) return true;

let ignoredSuffix = opts.ignore.suffix.find((suffix) => {
return className.indexOf(suffix) == className.length - suffix.length - 1;
return className.indexOf(suffix) > -1 && className.indexOf(suffix) == className.length - suffix.length;
});

if (ignoredSuffix) return true;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "combocss",
"version": "0.0.8",
"version": "0.0.9",
"description": "PostCSS plugin for leto",
"keywords": [
"conbocss",
Expand Down

0 comments on commit 73d95fb

Please sign in to comment.