forgecss 0.1.2 → 0.1.3

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.
package/cli.js CHANGED
@@ -26,18 +26,16 @@ async function loadConfig(configPath) {
26
26
  const mod = await import(fileUrl);
27
27
  return mod.default ?? mod; // support both default and named export
28
28
  }
29
- async function runForgeCSS() {
29
+ async function runForgeCSS(lookAtPath = null) {
30
30
  if (!config) {
31
+ // The very first run
31
32
  config = await loadConfig(options.config);
32
33
  if (options.watch) {
33
- const watcher = chokidar.watch([
34
- config.styles.sourceDir,
35
- config.ui.sourceDir
36
- ], {
34
+ const watcher = chokidar.watch([config.styles.sourceDir, config.ui.sourceDir], {
37
35
  persistent: true,
38
36
  ignoreInitial: true,
39
37
  ignored: (p, stats) => {
40
- if (path.resolve(p) === path.resolve(config.output)){
38
+ if (path.resolve(p) === path.resolve(config.output)) {
41
39
  return true;
42
40
  }
43
41
  return false;
@@ -47,14 +45,14 @@ async function runForgeCSS() {
47
45
  if (options.verbose) {
48
46
  console.log(`forgecss: Detected change in ${filePath}`);
49
47
  }
50
- runForgeCSS();
48
+ runForgeCSS(filePath);
51
49
  });
52
50
  if (options.verbose) {
53
51
  console.log("forgecss: Watch mode enabled. Listening for file changes...");
54
52
  }
55
53
  }
56
54
  }
57
- ForgeCSS(config).parse();
55
+ ForgeCSS(config).parse(lookAtPath);
58
56
  }
59
57
 
60
58
  runForgeCSS();
package/index.d.ts CHANGED
@@ -19,5 +19,5 @@ export type ForgeCSSOptions = {
19
19
  };
20
20
 
21
21
  export default function forgecss(options?: ForgeCSSOptions): {
22
- parse: () => Promise<void>;
22
+ parse: (filePathToSpecificFile?: string) => Promise<void>;
23
23
  };
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import getAllFiles from "./lib/getAllFiles.js";
2
2
  import { extractStyles } from "./lib/styles.js";
3
- import { extractDeclarations } from "./lib/processFile.js";
3
+ import { deleteDeclarations, extractDeclarations, getDeclarations } from "./lib/processFile.js";
4
4
  import { generateOutputCSS } from "./lib/generator.js";
5
5
 
6
6
  const DEFAULT_OPTIONS = {
@@ -38,21 +38,34 @@ export default function forgecss(options = { styles: {}, ui: {}, mapping: {}, ou
38
38
  }
39
39
 
40
40
  return {
41
- async parse() {
41
+ async parse(lookAtPath = null) {
42
42
  // fetching the styles
43
43
  try {
44
- let files = await getAllFiles(config.styles.sourceDir, config.styles.match);
45
- for (let file of files) {
46
- await extractStyles(file);
44
+ if (lookAtPath) {
45
+ if (config.styles.match.includes(lookAtPath.split('.').pop().toLowerCase())) {
46
+ await extractStyles(lookAtPath);
47
+ }
48
+ } else {
49
+ let files = await getAllFiles(config.styles.sourceDir, config.styles.match);
50
+ for (let file of files) {
51
+ await extractStyles(file);
52
+ }
47
53
  }
48
54
  } catch (err) {
49
55
  console.error(`forgecss: error extracting styles: ${err}`);
50
56
  }
51
57
  // fetching the declarations
52
58
  try {
53
- let files = await getAllFiles(config.ui.sourceDir, config.ui.match);
54
- for (let file of files) {
55
- await extractDeclarations(file);
59
+ if (lookAtPath) {
60
+ if (config.ui.match.includes(lookAtPath.split('.').pop().toLowerCase())) {
61
+ deleteDeclarations(lookAtPath);
62
+ await extractDeclarations(lookAtPath);
63
+ }
64
+ } else {
65
+ let files = await getAllFiles(config.ui.sourceDir, config.ui.match);
66
+ for (let file of files) {
67
+ await extractDeclarations(file);
68
+ }
56
69
  }
57
70
  } catch(err) {
58
71
  console.error(`forgecss: error extracting declarations: ${err}`);
@@ -55,6 +55,11 @@ export async function extractDeclarations(filePath) {
55
55
  console.error(`forgecss: error processing file ${filePath}: ${err}`);
56
56
  }
57
57
  }
58
+ export function deleteDeclarations(filePath) {
59
+ if (DECLARATIONS[filePath]) {
60
+ delete DECLARATIONS[filePath];
61
+ }
62
+ }
58
63
  function pushToDeclarations(filePath, classesString = "") {
59
64
  if (classesString) {
60
65
  classesString.split(" ").forEach((part) => {
@@ -71,7 +76,6 @@ function pushToDeclarations(filePath, classesString = "") {
71
76
  });
72
77
  }
73
78
  }
74
-
75
79
  function traverseNode(node, visitors, stack = []) {
76
80
  if (!node || typeof node.type !== "string") {
77
81
  return;
@@ -108,7 +112,6 @@ function traverseNode(node, visitors, stack = []) {
108
112
  }
109
113
  }
110
114
  }
111
-
112
115
  export function getDeclarations() {
113
116
  return DECLARATIONS;
114
117
  }
package/lib/styles.js CHANGED
@@ -2,23 +2,23 @@ import { readFile } from "fs/promises";
2
2
  import postcss from "postcss";
3
3
  import safeParser from "postcss-safe-parser";
4
4
 
5
- const STYLES = [];
5
+ const STYLES = {};
6
6
 
7
7
  export async function extractStyles(filePath) {
8
8
  const content = await readFile(filePath, 'utf-8');
9
- STYLES.push(postcss.parse(content, { parser: safeParser }));
9
+ STYLES[filePath] = postcss.parse(content, { parser: safeParser });
10
10
  }
11
11
  export function getStylesByClassName(selector) {
12
12
  const decls = [];
13
- for (let root of STYLES) {
14
- root.walkRules((rule) => {
13
+ Object.keys(STYLES).forEach((filePath) => {
14
+ STYLES[filePath].walkRules((rule) => {
15
15
  if (rule.selectors && rule.selectors.includes(`.${selector}`)) {
16
16
  rule.walkDecls((d) => {
17
17
  decls.push({ prop: d.prop, value: d.value, important: d.important });
18
18
  });
19
19
  }
20
20
  });
21
- }
21
+ });
22
22
  return decls;
23
23
  }
24
24
  export function createMediaStyle(config, label, selectors, cache) {
@@ -39,7 +39,7 @@ export function createMediaStyle(config, label, selectors, cache) {
39
39
  const mq = cache[label].mq;
40
40
  selectors.forEach((selector) => {
41
41
  const prefixedSelector = `.${label}_${selector}`;
42
- if (cache[label].classes[prefixedSelector]) return;
42
+ if (cache[label].classes[prefixedSelector]) { return; }
43
43
  cache[label].classes[prefixedSelector] = true;
44
44
  const rule = postcss.rule({ selector: prefixedSelector });
45
45
  const decls = getStylesByClassName(selector);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "forgecss",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "description": "ForgeCSS turns strings into fully generated responsive CSS using a custom DSL.",
6
6
  "author": "Krasimir Tsonev",