css-module-sync 0.1.33 → 1.0.1

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/package.json CHANGED
@@ -1,18 +1,10 @@
1
1
  {
2
2
  "name": "css-module-sync",
3
- "version": "0.1.33",
3
+ "version": "1.0.1",
4
4
  "description": "Auto sync css module classes with react components",
5
5
  "author": "Max Matinpalo",
6
6
  "type": "module",
7
7
  "license": "MIT",
8
- "homepage": "https://github.com/max-matinpalo/css-module-sync#readme",
9
- "repository": {
10
- "type": "git",
11
- "url": "https://github.com/max-matinpalo/css-module-sync.git"
12
- },
13
- "bugs": {
14
- "url": "https://github.com/max-matinpalo/css-module-sync/issues"
15
- },
16
8
  "bin": {
17
9
  "css-sync": "./src/css_sync.js"
18
10
  }
@@ -1,4 +1,4 @@
1
- const BLOCK_MIN_DECLS = 7;
1
+ const BLOCK_MIN_DECLS = 5;
2
2
 
3
3
  /**
4
4
  * css_formatter.js
package/src/css_sync.js CHANGED
@@ -109,23 +109,32 @@ async function format_css_only(css_path) {
109
109
  }
110
110
 
111
111
  async function sync_file(tsx_path) {
112
+ let tsx;
113
+ try { tsx = await fs.readFile(tsx_path, "utf-8"); } catch { return; }
114
+
115
+ const used_ordered = extract_classes(tsx);
116
+ const dir = path.dirname(tsx_path);
117
+ const name = path.basename(tsx_path, path.extname(tsx_path));
118
+ const module_path = path.join(dir, `${name}.module.css`);
119
+ const css_exists = existsSync(module_path);
120
+
121
+ if (FLAGS.gen && used_ordered.length === 0 && !css_exists) return;
122
+
112
123
  const css_path = await resolve_css_path(tsx_path);
113
124
  if (!css_path) return;
114
- let tsx, css;
115
- try {
116
- [tsx, css] = await Promise.all([fs.readFile(tsx_path, "utf-8"), fs.readFile(css_path, "utf-8")]);
117
- } catch (e) { return; }
118
- if (FLAGS.gen) {
125
+
126
+ let css;
127
+ try { css = await fs.readFile(css_path, "utf-8"); } catch { return; }
128
+
129
+ if (FLAGS.gen && used_ordered.length > 0) {
119
130
  const target = `import styles from "./${path.basename(css_path)}";`;
120
- let found = false;
121
- const next = tsx.replace(/^import\s+styles\s+from\s+["'].+["'];?\n?/gm, (m) => {
122
- if (m.includes(`./${path.basename(css_path)}`)) { found = true; return m; }
123
- return "";
124
- });
125
- const final = found ? next : `${target}\n${next}`;
131
+ const next = tsx.replace(/^import\s+styles\s+from\s+["'][^"']+["'];?\s*\n?/gm, "");
132
+ const m = next.match(/^(\s*(?:(?:\/\*.*?\*\/)\s*|\/\/.*\s*)*)(["']use client["'];?\s*\n)?/s);
133
+ const i = m ? m[0].length : 0;
134
+ const final = `${next.slice(0, i)}${target}\n${next.slice(i)}`;
126
135
  if (final !== tsx) await fs.writeFile(tsx_path, tsx = final);
127
136
  }
128
- const used_ordered = extract_classes(tsx);
137
+
129
138
  let root;
130
139
  try { root = parse(css); } catch { return; }
131
140
  const class_to_idxs = new Map();
@@ -214,14 +223,51 @@ async function main() {
214
223
  await run_scan();
215
224
  if (FLAGS.watch) {
216
225
  const debouncers = new Map();
226
+ let cache = new Set();
227
+ const refresh_cache = async () => {
228
+ const files = await fs.readdir(TARGET_DIR, { recursive: true });
229
+ cache = new Set(files.map(f => path.resolve(TARGET_DIR, f)));
230
+ };
231
+ await refresh_cache();
232
+
217
233
  console.log("Watching for changes...");
218
- watch(TARGET_DIR, { recursive: true }, (_, filename) => {
234
+ watch(TARGET_DIR, { recursive: true }, async (event, filename) => {
219
235
  if (!filename) return;
220
- const name = filename.toString();
221
- const is_tsx = /\.(jsx|tsx)$/.test(name);
222
- const is_css = /\.css$/.test(name);
223
- if (!is_tsx && !is_css) return;
224
- const full_path = path.resolve(TARGET_DIR, name);
236
+ const full_path = path.resolve(TARGET_DIR, filename.toString());
237
+ const exists = existsSync(full_path);
238
+ const is_tsx = /\.(jsx|tsx)$/.test(full_path);
239
+
240
+ if (event === "rename") {
241
+ const was_cached = cache.has(full_path);
242
+ if (!exists) setTimeout(() => { if (!existsSync(full_path)) cache.delete(full_path); }, 100);
243
+ else {
244
+ cache.add(full_path);
245
+ if (is_tsx && FLAGS.gen && !was_cached) setTimeout(async () => {
246
+ const dir = path.dirname(full_path);
247
+ const name = path.basename(full_path, path.extname(full_path));
248
+ const orphans = Array.from(cache).filter(p => {
249
+ if (!p.endsWith(".module.css") || path.dirname(p) !== dir || !existsSync(p)) return false;
250
+ const base = path.basename(p, ".module.css");
251
+ return !existsSync(path.join(dir, `${base}.tsx`)) && !existsSync(path.join(dir, `${base}.jsx`));
252
+ });
253
+ if (orphans.length === 1) {
254
+ const orphan = orphans[0];
255
+ const next_css = path.join(dir, `${name}.module.css`);
256
+ if (!existsSync(next_css)) {
257
+ await fs.rename(orphan, next_css);
258
+ cache.delete(orphan);
259
+ cache.add(next_css);
260
+ console.log(`Renamed: ${path.basename(orphan)} -> ${path.basename(next_css)}`);
261
+ }
262
+ } else if (orphans.length > 1) {
263
+ console.warn(`[RENAME] Multiple orphan .module.css in ${dir}; skipping:\n` +
264
+ orphans.map(p => `- ${path.basename(p)}`).join("\n"));
265
+ }
266
+ }, 150);
267
+ }
268
+ }
269
+
270
+ if (!is_tsx && !/\.css$/.test(full_path)) return;
225
271
  if (debouncers.has(full_path)) clearTimeout(debouncers.get(full_path));
226
272
  debouncers.set(full_path, setTimeout(() => {
227
273
  debouncers.delete(full_path);
@@ -39,38 +39,41 @@
39
39
  "height",
40
40
  "min-height",
41
41
  "max-height",
42
- "aspect-ratio",
43
42
  "margin...",
44
43
  "padding...",
45
- "border..."
44
+ "border...",
45
+ "aspect-ratio"
46
46
  ]
47
47
  },
48
48
  {
49
- "category": "COLOR",
49
+ "category": "SCROLL",
50
+ "keywords": [
51
+ "overflow...",
52
+ "scroll...",
53
+ "scrollbar...",
54
+ "overscroll-behavior...",
55
+ "-webkit-overflow-scrolling"
56
+ ]
57
+ },
58
+ {
59
+ "category": "VISUALS",
50
60
  "keywords": [
51
61
  "color",
52
62
  "background...",
53
63
  "opacity",
54
- "box-shadow"
64
+ "box-shadow",
65
+ "backdrop-filter",
66
+ "-webkit-backdrop-filter"
55
67
  ]
56
68
  },
57
69
  {
58
70
  "category": "TYPO",
59
71
  "keywords": [
60
- "font",
61
- "font-family",
62
- "font-size",
63
- "font-weight",
64
- "font-style",
65
72
  "font...",
66
- "font-stretch",
67
73
  "line-height",
74
+ "text...",
68
75
  "letter-spacing",
69
76
  "word-spacing",
70
- "text-align",
71
- "text-transform",
72
- "text-decoration....",
73
- "text-underline-offset",
74
77
  "white-space"
75
78
  ]
76
79
  },