forgecss 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/inventory.js +66 -0
  2. package/package.json +1 -1
@@ -0,0 +1,66 @@
1
+ import { readFile } from "fs/promises";
2
+ import postcss from "postcss";
3
+ import safeParser from "postcss-safe-parser";
4
+
5
+ let INVENTORY = {};
6
+
7
+ export async function extractStyles(filePath, css = null) {
8
+ const content = css !== null ? css : await readFile(filePath, "utf-8");
9
+ INVENTORY[filePath] = postcss.parse(content, { parser: safeParser });
10
+ }
11
+ export function getStylesByClassName(selector) {
12
+ const decls = [];
13
+ Object.keys(INVENTORY).forEach((filePath) => {
14
+ INVENTORY[filePath].walkRules((rule) => {
15
+ if (rule.selectors && rule.selectors.includes(`.${selector}`)) {
16
+ rule.walkDecls((d) => {
17
+ decls.push({ prop: d.prop, value: d.value, important: d.important });
18
+ });
19
+ }
20
+ });
21
+ });
22
+ if (decls.length === 0) {
23
+ console.warn(`forgecss: no styles found for class "${selector}".`);
24
+ }
25
+ return decls;
26
+ }
27
+ export function invalidateInventory(filePath) {
28
+ if (!filePath) {
29
+ INVENTORY = {};
30
+ return;
31
+ }
32
+ if (INVENTORY[filePath]) {
33
+ delete INVENTORY[filePath];
34
+ }
35
+ }
36
+ export function resolveApplys() {
37
+ let resolvedApplies;
38
+ Object.keys(INVENTORY).forEach((filePath) => {
39
+ INVENTORY[filePath].walkRules((rule) => {
40
+ rule.walkDecls((d) => {
41
+ if (d.prop === "--apply") {
42
+ const classesToApply = d.value
43
+ .split(" ")
44
+ .map((c) => c.trim())
45
+ .filter(Boolean);
46
+ const newRule = postcss.rule({ selector: rule.selector });
47
+ classesToApply.forEach((className) => {
48
+ const styles = getStylesByClassName(className);
49
+ styles.forEach((style) => {
50
+ newRule.append({
51
+ prop: style.prop,
52
+ value: style.value,
53
+ important: style.important
54
+ });
55
+ });
56
+ });
57
+ if (!resolvedApplies) {
58
+ resolvedApplies = postcss.root();
59
+ }
60
+ resolvedApplies.append(newRule);
61
+ }
62
+ });
63
+ });
64
+ });
65
+ return resolvedApplies;
66
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "forgecss",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "type": "module",
5
5
  "description": "ForgeCSS turns strings into fully generated responsive CSS using a custom DSL.",
6
6
  "author": "Krasimir Tsonev",