@better-css-modules/core 0.1.1 → 0.1.2

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/README.md CHANGED
@@ -21,10 +21,10 @@ import {
21
21
  generateAll,
22
22
  scanUnusedClasses,
23
23
  startWatcher,
24
- } from '@better-css-modules/core';
24
+ } from "@better-css-modules/core";
25
25
 
26
26
  // Extract class names from CSS
27
- const classNames = extractClassNames('.container { color: red; }');
27
+ const classNames = extractClassNames(".container { color: red; }");
28
28
  // => ['container']
29
29
 
30
30
  // Generate .d.ts content
@@ -45,16 +45,25 @@ A webpack-compatible loader is available at `@better-css-modules/core/loader`. T
45
45
  ## Configuration
46
46
 
47
47
  ```ts
48
- import { defineConfig } from '@better-css-modules/core';
48
+ import { defineConfig } from "@better-css-modules/core";
49
49
 
50
50
  export default defineConfig({
51
- include: ['src/**/*.module.css'],
51
+ include: ["src/**/*.module.css"],
52
52
  exclude: [],
53
- outDir: '__generated__',
53
+ outDir: "__generated__",
54
54
  watch: false,
55
+ silent: false,
55
56
  });
56
57
  ```
57
58
 
59
+ | Option | Type | Default | Description |
60
+ | --------- | ---------- | ------------------------- | -------------------------------------------- |
61
+ | `include` | `string[]` | `["src/**/*.module.css"]` | Glob patterns for target CSS Modules files |
62
+ | `exclude` | `string[]` | `[]` | Glob patterns to exclude |
63
+ | `outDir` | `string` | `"__generated__"` | Output directory for generated `.d.ts` files |
64
+ | `watch` | `boolean` | `false` | Enable watch mode (CLI only) |
65
+ | `silent` | `boolean` | `false` | Suppress console output |
66
+
58
67
  ## License
59
68
 
60
69
  MIT
@@ -9,7 +9,8 @@ const defaultConfig = {
9
9
  include: ["src/**/*.module.css"],
10
10
  exclude: [],
11
11
  outDir: "__generated__",
12
- watch: false
12
+ watch: false,
13
+ silent: false
13
14
  };
14
15
  function defineConfig(config) {
15
16
  return {
@@ -36,7 +36,8 @@ const defaultConfig = {
36
36
  include: ["src/**/*.module.css"],
37
37
  exclude: [],
38
38
  outDir: "__generated__",
39
- watch: false
39
+ watch: false,
40
+ silent: false
40
41
  };
41
42
  function defineConfig(config) {
42
43
  return {
package/dist/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_generator = require("./generator-BMINjrx4.cjs");
2
+ const require_generator = require("./generator-Dy0W5B1c.cjs");
3
3
  let node_path = require("node:path");
4
4
  node_path = require_generator.__toESM(node_path);
5
5
  let node_fs_promises = require("node:fs/promises");
@@ -7,6 +7,8 @@ node_fs_promises = require_generator.__toESM(node_fs_promises);
7
7
  let fast_glob = require("fast-glob");
8
8
  fast_glob = require_generator.__toESM(fast_glob);
9
9
  let chokidar = require("chokidar");
10
+ let picomatch = require("picomatch");
11
+ picomatch = require_generator.__toESM(picomatch);
10
12
  //#region src/scanner.ts
11
13
  /**
12
14
  * Scan TS/TSX file contents for CSS Modules class name usage.
@@ -62,28 +64,58 @@ async function scanUnusedClasses(cwd) {
62
64
  }
63
65
  //#endregion
64
66
  //#region src/watcher.ts
67
+ /**
68
+ * Extract the static base directory from a glob pattern.
69
+ * e.g. "src/**\/*.module.css" → "src"
70
+ */
71
+ function extractBaseDir(pattern) {
72
+ const parts = pattern.split("/");
73
+ const staticParts = [];
74
+ for (const part of parts) {
75
+ if (part.includes("*") || part.includes("{") || part.includes("?")) break;
76
+ staticParts.push(part);
77
+ }
78
+ return staticParts.join("/") || ".";
79
+ }
65
80
  function startWatcher(config, cwd = process.cwd()) {
66
- const watcher = (0, chokidar.watch)(config.include, {
81
+ const baseDirs = [...new Set(config.include.map(extractBaseDir))];
82
+ const isMatch = (0, picomatch.default)(config.include);
83
+ const isExcluded = config.exclude.length > 0 ? (0, picomatch.default)(config.exclude) : () => false;
84
+ const watcher = (0, chokidar.watch)(baseDirs, {
67
85
  cwd,
68
- ignored: config.exclude,
69
86
  ignoreInitial: false
70
87
  });
88
+ function shouldProcess(filePath) {
89
+ return isMatch(filePath) && !isExcluded(filePath);
90
+ }
71
91
  watcher.on("add", async (filePath) => {
72
- await regenerate(filePath, config.outDir, cwd);
92
+ if (!shouldProcess(filePath)) return;
93
+ await regenerate(filePath, config.outDir, cwd, config.silent);
73
94
  });
74
95
  watcher.on("change", async (filePath) => {
75
- await regenerate(filePath, config.outDir, cwd);
96
+ if (!shouldProcess(filePath)) return;
97
+ await regenerate(filePath, config.outDir, cwd, config.silent);
98
+ });
99
+ watcher.on("unlink", async (filePath) => {
100
+ if (!shouldProcess(filePath)) return;
101
+ await removeDts(filePath, config.outDir, cwd, config.silent);
76
102
  });
77
- watcher.on("unlink", () => {});
78
103
  return watcher;
79
104
  }
80
- async function regenerate(relativePath, outDir, cwd) {
81
- const absolutePath = new URL(relativePath, `file://${cwd}/`).pathname;
105
+ async function removeDts(relativePath, outDir, cwd, silent) {
106
+ const dtsPath = node_path.default.resolve(cwd, outDir, relativePath + ".d.ts");
107
+ try {
108
+ await node_fs_promises.default.rm(dtsPath, { force: true });
109
+ if (!silent) console.log(`[better-css-modules] removed: ${dtsPath}`);
110
+ } catch {}
111
+ }
112
+ async function regenerate(relativePath, outDir, cwd, silent) {
113
+ const absolutePath = node_path.default.resolve(cwd, relativePath);
82
114
  try {
83
115
  const dtsPath = await require_generator.writeDts(absolutePath, await require_generator.parseFile(absolutePath), outDir, cwd);
84
- console.log(`[better-css-modules] generated: ${dtsPath}`);
116
+ if (!silent) console.log(`[better-css-modules] generated: ${dtsPath}`);
85
117
  } catch (err) {
86
- console.error(`[better-css-modules] error processing ${relativePath}:`, err);
118
+ if (!silent) console.error(`[better-css-modules] error processing ${relativePath}:`, err);
87
119
  }
88
120
  }
89
121
  //#endregion
package/dist/index.d.cts CHANGED
@@ -10,6 +10,8 @@ interface Config {
10
10
  outDir: string;
11
11
  /** Watch mode (for CLI) */
12
12
  watch: boolean;
13
+ /** Suppress console output */
14
+ silent: boolean;
13
15
  }
14
16
  declare function defineConfig(config: Partial<Config>): Config;
15
17
  declare function loadConfig(cwd?: string): Promise<Config>;
package/dist/index.d.mts CHANGED
@@ -10,6 +10,8 @@ interface Config {
10
10
  outDir: string;
11
11
  /** Watch mode (for CLI) */
12
12
  watch: boolean;
13
+ /** Suppress console output */
14
+ silent: boolean;
13
15
  }
14
16
  declare function defineConfig(config: Partial<Config>): Config;
15
17
  declare function loadConfig(cwd?: string): Promise<Config>;
package/dist/index.mjs CHANGED
@@ -1,8 +1,9 @@
1
- import { a as parseFile, i as extractClassNames, n as generateDts, o as defineConfig, r as writeDts, s as loadConfig, t as generateAll } from "./generator-CZGTIPYk.mjs";
1
+ import { a as parseFile, i as extractClassNames, n as generateDts, o as defineConfig, r as writeDts, s as loadConfig, t as generateAll } from "./generator-BlFEyanZ.mjs";
2
2
  import path from "node:path";
3
3
  import fs from "node:fs/promises";
4
4
  import fg from "fast-glob";
5
5
  import { watch } from "chokidar";
6
+ import picomatch from "picomatch";
6
7
  //#region src/scanner.ts
7
8
  /**
8
9
  * Scan TS/TSX file contents for CSS Modules class name usage.
@@ -58,28 +59,58 @@ async function scanUnusedClasses(cwd) {
58
59
  }
59
60
  //#endregion
60
61
  //#region src/watcher.ts
62
+ /**
63
+ * Extract the static base directory from a glob pattern.
64
+ * e.g. "src/**\/*.module.css" → "src"
65
+ */
66
+ function extractBaseDir(pattern) {
67
+ const parts = pattern.split("/");
68
+ const staticParts = [];
69
+ for (const part of parts) {
70
+ if (part.includes("*") || part.includes("{") || part.includes("?")) break;
71
+ staticParts.push(part);
72
+ }
73
+ return staticParts.join("/") || ".";
74
+ }
61
75
  function startWatcher(config, cwd = process.cwd()) {
62
- const watcher = watch(config.include, {
76
+ const baseDirs = [...new Set(config.include.map(extractBaseDir))];
77
+ const isMatch = picomatch(config.include);
78
+ const isExcluded = config.exclude.length > 0 ? picomatch(config.exclude) : () => false;
79
+ const watcher = watch(baseDirs, {
63
80
  cwd,
64
- ignored: config.exclude,
65
81
  ignoreInitial: false
66
82
  });
83
+ function shouldProcess(filePath) {
84
+ return isMatch(filePath) && !isExcluded(filePath);
85
+ }
67
86
  watcher.on("add", async (filePath) => {
68
- await regenerate(filePath, config.outDir, cwd);
87
+ if (!shouldProcess(filePath)) return;
88
+ await regenerate(filePath, config.outDir, cwd, config.silent);
69
89
  });
70
90
  watcher.on("change", async (filePath) => {
71
- await regenerate(filePath, config.outDir, cwd);
91
+ if (!shouldProcess(filePath)) return;
92
+ await regenerate(filePath, config.outDir, cwd, config.silent);
93
+ });
94
+ watcher.on("unlink", async (filePath) => {
95
+ if (!shouldProcess(filePath)) return;
96
+ await removeDts(filePath, config.outDir, cwd, config.silent);
72
97
  });
73
- watcher.on("unlink", () => {});
74
98
  return watcher;
75
99
  }
76
- async function regenerate(relativePath, outDir, cwd) {
77
- const absolutePath = new URL(relativePath, `file://${cwd}/`).pathname;
100
+ async function removeDts(relativePath, outDir, cwd, silent) {
101
+ const dtsPath = path.resolve(cwd, outDir, relativePath + ".d.ts");
102
+ try {
103
+ await fs.rm(dtsPath, { force: true });
104
+ if (!silent) console.log(`[better-css-modules] removed: ${dtsPath}`);
105
+ } catch {}
106
+ }
107
+ async function regenerate(relativePath, outDir, cwd, silent) {
108
+ const absolutePath = path.resolve(cwd, relativePath);
78
109
  try {
79
110
  const dtsPath = await writeDts(absolutePath, await parseFile(absolutePath), outDir, cwd);
80
- console.log(`[better-css-modules] generated: ${dtsPath}`);
111
+ if (!silent) console.log(`[better-css-modules] generated: ${dtsPath}`);
81
112
  } catch (err) {
82
- console.error(`[better-css-modules] error processing ${relativePath}:`, err);
113
+ if (!silent) console.error(`[better-css-modules] error processing ${relativePath}:`, err);
83
114
  }
84
115
  }
85
116
  //#endregion
package/dist/loader.cjs CHANGED
@@ -1,4 +1,4 @@
1
- const require_generator = require("./generator-BMINjrx4.cjs");
1
+ const require_generator = require("./generator-Dy0W5B1c.cjs");
2
2
  //#region src/loader.ts
3
3
  let configPromise;
4
4
  function getConfig(cwd) {
package/dist/loader.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { i as extractClassNames, r as writeDts, s as loadConfig } from "./generator-CZGTIPYk.mjs";
1
+ import { i as extractClassNames, r as writeDts, s as loadConfig } from "./generator-BlFEyanZ.mjs";
2
2
  //#region src/loader.ts
3
3
  let configPromise;
4
4
  function getConfig(cwd) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-css-modules/core",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "license": "MIT",
5
5
  "author": "k8o",
6
6
  "repository": {
@@ -8,29 +8,41 @@
8
8
  "url": "git+https://github.com/k35o/better-css-modules.git",
9
9
  "directory": "packages/core"
10
10
  },
11
+ "files": [
12
+ "dist"
13
+ ],
11
14
  "type": "module",
15
+ "main": "./dist/index.cjs",
16
+ "module": "./dist/index.mjs",
17
+ "types": "./dist/index.d.mts",
12
18
  "exports": {
13
19
  ".": {
20
+ "types": {
21
+ "import": "./dist/index.d.mts",
22
+ "require": "./dist/index.d.cts"
23
+ },
14
24
  "import": "./dist/index.mjs",
15
25
  "require": "./dist/index.cjs"
16
26
  },
17
27
  "./loader": {
28
+ "types": {
29
+ "import": "./dist/loader.d.mts",
30
+ "require": "./dist/loader.d.cts"
31
+ },
18
32
  "import": "./dist/loader.mjs",
19
33
  "require": "./dist/loader.cjs"
20
34
  }
21
35
  },
22
- "main": "./dist/index.cjs",
23
- "module": "./dist/index.mjs",
24
- "types": "./dist/index.d.ts",
25
- "files": [
26
- "dist"
27
- ],
28
36
  "dependencies": {
29
37
  "chokidar": "5.0.0",
30
38
  "fast-glob": "3.3.3",
31
39
  "jiti": "2.6.1",
40
+ "picomatch": "^4.0.4",
32
41
  "postcss": "8.5.8"
33
42
  },
43
+ "devDependencies": {
44
+ "@types/picomatch": "^4.0.2"
45
+ },
34
46
  "scripts": {
35
47
  "test": "vp test",
36
48
  "typecheck": "tsc --noEmit"