@better-css-modules/core 0.1.3 → 0.2.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/README.md +38 -45
- package/dist/dts-BLFmeUu6.d.mts +276 -0
- package/dist/dts-YHloeOKZ.mjs +1524 -0
- package/dist/index.d.mts +16 -58
- package/dist/index.mjs +2416 -85
- package/dist/internal.d.mts +23 -0
- package/dist/internal.mjs +95 -0
- package/package.json +31 -20
- package/dist/generator-BlFEyanZ.mjs +0 -105
- package/dist/generator-Dy0W5B1c.cjs +0 -179
- package/dist/index.cjs +0 -130
- package/dist/index.d.cts +0 -62
- package/dist/loader.cjs +0 -16
- package/dist/loader.d.cts +0 -3
- package/dist/loader.d.mts +0 -4
- package/dist/loader.mjs +0 -16
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { f as ResolvedConfig, i as createLayerWrapper, r as LayerConfig, t as GenerateResult } from "./dts-BLFmeUu6.mjs";
|
|
2
|
+
//#region src/watcher.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Run `generate` and print how many `.d.ts` files it wrote and which it
|
|
5
|
+
* removed, and its diagnostics on stderr.
|
|
6
|
+
*/
|
|
7
|
+
export declare function generateAndPrint(config: ResolvedConfig): Promise<GenerateResult>;
|
|
8
|
+
/**
|
|
9
|
+
* What a watcher runs on each changed file: bring the `.d.ts` of a CSS Modules
|
|
10
|
+
* file the config includes in line with it, and print what it did, its
|
|
11
|
+
* diagnostics, or why it failed. Other files are left alone.
|
|
12
|
+
*/
|
|
13
|
+
export declare function createSync(config: ResolvedConfig): (file: string) => Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Regenerate `.d.ts` files as the included CSS Modules files change. Callers
|
|
16
|
+
* run `generate` first; the watcher only reacts to changes after that.
|
|
17
|
+
* Without `persistent`, the watcher does not keep the process alive.
|
|
18
|
+
*/
|
|
19
|
+
export declare function startWatcher(config: ResolvedConfig, { persistent }?: {
|
|
20
|
+
persistent?: boolean;
|
|
21
|
+
}): import("chokidar").FSWatcher;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { type LayerConfig, createLayerWrapper };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { h as formatDiagnostic, i as createLayerWrapper, l as createMatcher, n as regenerateDts, t as generate } from "./dts-YHloeOKZ.mjs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { watch } from "chokidar";
|
|
4
|
+
//#region src/watcher.ts
|
|
5
|
+
/**
|
|
6
|
+
* Extract the static base directory from a glob pattern.
|
|
7
|
+
* e.g. "src/**\/*.module.css" → "src"
|
|
8
|
+
*/
|
|
9
|
+
function extractBaseDir(pattern) {
|
|
10
|
+
const staticParts = [];
|
|
11
|
+
for (const part of pattern.split("/")) {
|
|
12
|
+
if (/[*?{}[\]()!]/.test(part)) break;
|
|
13
|
+
staticParts.push(part);
|
|
14
|
+
}
|
|
15
|
+
return staticParts.join("/") || ".";
|
|
16
|
+
}
|
|
17
|
+
/** Print progress unless the config is silent. */
|
|
18
|
+
function logger(config) {
|
|
19
|
+
return (message) => {
|
|
20
|
+
if (!config.silent) console.log(`[better-css-modules] ${message}`);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Run `generate` and print how many `.d.ts` files it wrote and which it
|
|
25
|
+
* removed, and its diagnostics on stderr.
|
|
26
|
+
*/
|
|
27
|
+
async function generateAndPrint(config) {
|
|
28
|
+
const cwd = process.cwd();
|
|
29
|
+
const log = logger(config);
|
|
30
|
+
const result = await generate(config);
|
|
31
|
+
log(`generated ${result.files.length} file(s)`);
|
|
32
|
+
for (const dtsPath of result.removed) log(`removed: ${path.relative(cwd, dtsPath)}`);
|
|
33
|
+
for (const diagnostic of result.diagnostics) console.error(formatDiagnostic(diagnostic, cwd));
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* What a watcher runs on each changed file: bring the `.d.ts` of a CSS Modules
|
|
38
|
+
* file the config includes in line with it, and print what it did, its
|
|
39
|
+
* diagnostics, or why it failed. Other files are left alone.
|
|
40
|
+
*/
|
|
41
|
+
function createSync(config) {
|
|
42
|
+
const cwd = process.cwd();
|
|
43
|
+
const matches = createMatcher(config);
|
|
44
|
+
const log = logger(config);
|
|
45
|
+
const syncOne = async (file) => {
|
|
46
|
+
try {
|
|
47
|
+
const { generated, removed, diagnostics } = await regenerateDts(file, config);
|
|
48
|
+
if (generated) log(`generated: ${path.relative(cwd, generated)}`);
|
|
49
|
+
if (removed) log(`removed: ${path.relative(cwd, removed)}`);
|
|
50
|
+
for (const diagnostic of diagnostics) console.error(formatDiagnostic(diagnostic, cwd));
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.error(`[better-css-modules] error processing ${path.relative(cwd, file)}:`, error);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const pending = /* @__PURE__ */ new Map();
|
|
56
|
+
return (file) => {
|
|
57
|
+
if (!matches(file)) return Promise.resolve();
|
|
58
|
+
const next = (pending.get(file) ?? Promise.resolve()).then(() => syncOne(file));
|
|
59
|
+
pending.set(file, next);
|
|
60
|
+
next.then(() => {
|
|
61
|
+
if (pending.get(file) === next) pending.delete(file);
|
|
62
|
+
});
|
|
63
|
+
return next;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Regenerate `.d.ts` files as the included CSS Modules files change. Callers
|
|
68
|
+
* run `generate` first; the watcher only reacts to changes after that.
|
|
69
|
+
* Without `persistent`, the watcher does not keep the process alive.
|
|
70
|
+
*/
|
|
71
|
+
function startWatcher(config, { persistent = true } = {}) {
|
|
72
|
+
const { root } = config;
|
|
73
|
+
const included = config.include.filter((pattern) => !pattern.startsWith("!"));
|
|
74
|
+
const baseDirs = [...new Set(included.map(extractBaseDir))];
|
|
75
|
+
const sync = createSync(config);
|
|
76
|
+
const outDir = path.resolve(root, config.outDir);
|
|
77
|
+
const ignored = (watched) => {
|
|
78
|
+
const resolved = path.resolve(root, watched);
|
|
79
|
+
if (resolved.split(path.sep).includes("node_modules")) return true;
|
|
80
|
+
return outDir !== root && (resolved === outDir || resolved.startsWith(outDir + path.sep));
|
|
81
|
+
};
|
|
82
|
+
const watcher = watch(baseDirs, {
|
|
83
|
+
cwd: root,
|
|
84
|
+
ignoreInitial: true,
|
|
85
|
+
ignored,
|
|
86
|
+
persistent
|
|
87
|
+
});
|
|
88
|
+
const onEvent = (relativePath) => sync(path.resolve(root, relativePath));
|
|
89
|
+
watcher.on("add", onEvent);
|
|
90
|
+
watcher.on("change", onEvent);
|
|
91
|
+
watcher.on("unlink", onEvent);
|
|
92
|
+
return watcher;
|
|
93
|
+
}
|
|
94
|
+
//#endregion
|
|
95
|
+
export { createLayerWrapper, createSync, generateAndPrint, startWatcher };
|
package/package.json
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-css-modules/core",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Analysis, type generation, unused-class detection, token checks, watcher and config for better-css-modules",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"css-modules",
|
|
7
|
+
"design-tokens",
|
|
8
|
+
"dts",
|
|
9
|
+
"typescript"
|
|
10
|
+
],
|
|
4
11
|
"license": "MIT",
|
|
5
12
|
"author": "k8o",
|
|
6
13
|
"repository": {
|
|
@@ -12,39 +19,43 @@
|
|
|
12
19
|
"dist"
|
|
13
20
|
],
|
|
14
21
|
"type": "module",
|
|
15
|
-
"main": "./dist/index.
|
|
16
|
-
"module": "./dist/index.mjs",
|
|
22
|
+
"main": "./dist/index.mjs",
|
|
17
23
|
"types": "./dist/index.d.mts",
|
|
18
24
|
"exports": {
|
|
19
25
|
".": {
|
|
20
|
-
"types":
|
|
21
|
-
|
|
22
|
-
"require": "./dist/index.d.cts"
|
|
23
|
-
},
|
|
24
|
-
"import": "./dist/index.mjs",
|
|
25
|
-
"require": "./dist/index.cjs"
|
|
26
|
+
"types": "./dist/index.d.mts",
|
|
27
|
+
"default": "./dist/index.mjs"
|
|
26
28
|
},
|
|
27
|
-
"./
|
|
28
|
-
"types":
|
|
29
|
-
|
|
30
|
-
"require": "./dist/loader.d.cts"
|
|
31
|
-
},
|
|
32
|
-
"import": "./dist/loader.mjs",
|
|
33
|
-
"require": "./dist/loader.cjs"
|
|
29
|
+
"./internal": {
|
|
30
|
+
"types": "./dist/internal.d.mts",
|
|
31
|
+
"default": "./dist/internal.mjs"
|
|
34
32
|
}
|
|
35
33
|
},
|
|
36
34
|
"dependencies": {
|
|
35
|
+
"@csstools/css-syntax-patches-for-csstree": "1.1.14",
|
|
37
36
|
"chokidar": "5.0.0",
|
|
37
|
+
"css-tree": "3.2.1",
|
|
38
38
|
"fast-glob": "3.3.3",
|
|
39
39
|
"jiti": "2.6.1",
|
|
40
|
-
"
|
|
41
|
-
"
|
|
40
|
+
"oxc-parser": "0.151.0",
|
|
41
|
+
"oxc-resolver": "11.24.2",
|
|
42
|
+
"picomatch": "4.0.7",
|
|
43
|
+
"postcss": "8.5.8",
|
|
44
|
+
"source-map-js": "1.2.1"
|
|
42
45
|
},
|
|
43
46
|
"devDependencies": {
|
|
44
|
-
"@types/
|
|
47
|
+
"@types/css-tree": "3.2.0",
|
|
48
|
+
"@types/node": "24.12.0",
|
|
49
|
+
"@types/picomatch": "4.0.2",
|
|
50
|
+
"lightningcss": "1.33.0",
|
|
51
|
+
"oxc-walker": "1.1.1",
|
|
52
|
+
"postcss-modules": "9.0.1",
|
|
53
|
+
"typescript": "7.0.2"
|
|
54
|
+
},
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=24"
|
|
45
57
|
},
|
|
46
58
|
"scripts": {
|
|
47
|
-
"test": "vp test",
|
|
48
59
|
"typecheck": "tsc --noEmit"
|
|
49
60
|
}
|
|
50
61
|
}
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { createJiti } from "jiti";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import fs from "node:fs";
|
|
4
|
-
import postcss from "postcss";
|
|
5
|
-
import fs$1 from "node:fs/promises";
|
|
6
|
-
import fg from "fast-glob";
|
|
7
|
-
//#region src/config.ts
|
|
8
|
-
const defaultConfig = {
|
|
9
|
-
include: ["src/**/*.module.css"],
|
|
10
|
-
exclude: [],
|
|
11
|
-
outDir: "__generated__",
|
|
12
|
-
watch: false,
|
|
13
|
-
silent: false
|
|
14
|
-
};
|
|
15
|
-
function defineConfig(config) {
|
|
16
|
-
return {
|
|
17
|
-
...defaultConfig,
|
|
18
|
-
...config
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
async function loadConfig(cwd = process.cwd()) {
|
|
22
|
-
const configFileName = "better-css-modules.config";
|
|
23
|
-
for (const ext of [
|
|
24
|
-
".ts",
|
|
25
|
-
".mts",
|
|
26
|
-
".cts",
|
|
27
|
-
".js",
|
|
28
|
-
".mjs",
|
|
29
|
-
".cjs"
|
|
30
|
-
]) {
|
|
31
|
-
const configPath = path.resolve(cwd, configFileName + ext);
|
|
32
|
-
if (fs.existsSync(configPath)) {
|
|
33
|
-
const mod = await createJiti(cwd).import(configPath);
|
|
34
|
-
const loaded = mod.default ?? mod;
|
|
35
|
-
return {
|
|
36
|
-
...defaultConfig,
|
|
37
|
-
...loaded
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return defaultConfig;
|
|
42
|
-
}
|
|
43
|
-
//#endregion
|
|
44
|
-
//#region src/parser.ts
|
|
45
|
-
/**
|
|
46
|
-
* Extract class names from CSS file contents.
|
|
47
|
-
*/
|
|
48
|
-
function extractClassNames(css) {
|
|
49
|
-
const root = postcss.parse(css);
|
|
50
|
-
const classNames = /* @__PURE__ */ new Set();
|
|
51
|
-
root.walkRules((rule) => {
|
|
52
|
-
const selectorParts = rule.selector.split(/\s*,\s*/);
|
|
53
|
-
for (const selector of selectorParts) {
|
|
54
|
-
const matches = selector.matchAll(/\.([a-zA-Z_-][\w-]*)/g);
|
|
55
|
-
for (const match of matches) classNames.add(match[1]);
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
return [...classNames].sort();
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Read a CSS Modules file and extract class names.
|
|
62
|
-
*/
|
|
63
|
-
async function parseFile(filePath) {
|
|
64
|
-
return extractClassNames(await fs$1.readFile(filePath, "utf-8"));
|
|
65
|
-
}
|
|
66
|
-
//#endregion
|
|
67
|
-
//#region src/generator.ts
|
|
68
|
-
/**
|
|
69
|
-
* Generate .d.ts content from an array of class names.
|
|
70
|
-
*/
|
|
71
|
-
function generateDts(classNames) {
|
|
72
|
-
return `declare const styles: {\n${classNames.map((name) => {
|
|
73
|
-
return ` readonly ${/^[a-zA-Z_$][\w$]*$/.test(name) ? name : `"${name}"`}: string;`;
|
|
74
|
-
}).join("\n")}\n};\nexport default styles;\n`;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Generate and write a .d.ts file for a CSS Modules file.
|
|
78
|
-
*
|
|
79
|
-
* @param cssFilePath - Path to the source CSS Modules file
|
|
80
|
-
* @param classNames - Array of extracted class names
|
|
81
|
-
* @param outDir - Output directory
|
|
82
|
-
* @param cwd - Working directory
|
|
83
|
-
*/
|
|
84
|
-
async function writeDts(cssFilePath, classNames, outDir, cwd = process.cwd()) {
|
|
85
|
-
const dtsFileName = path.relative(cwd, cssFilePath) + ".d.ts";
|
|
86
|
-
const dtsPath = path.resolve(cwd, outDir, dtsFileName);
|
|
87
|
-
await fs$1.mkdir(path.dirname(dtsPath), { recursive: true });
|
|
88
|
-
await fs$1.writeFile(dtsPath, generateDts(classNames), "utf-8");
|
|
89
|
-
return dtsPath;
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Batch-generate .d.ts files for all CSS Modules files based on the config.
|
|
93
|
-
*/
|
|
94
|
-
async function generateAll(config, cwd = process.cwd()) {
|
|
95
|
-
const files = await fg(config.include, {
|
|
96
|
-
cwd,
|
|
97
|
-
ignore: config.exclude,
|
|
98
|
-
absolute: true
|
|
99
|
-
});
|
|
100
|
-
return await Promise.all(files.map(async (file) => {
|
|
101
|
-
return writeDts(file, await parseFile(file), config.outDir, cwd);
|
|
102
|
-
}));
|
|
103
|
-
}
|
|
104
|
-
//#endregion
|
|
105
|
-
export { parseFile as a, extractClassNames as i, generateDts as n, defineConfig as o, writeDts as r, loadConfig as s, generateAll as t };
|
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
//#region \0rolldown/runtime.js
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
-
key = keys[i];
|
|
11
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
-
get: ((k) => from[k]).bind(null, key),
|
|
13
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
-
value: mod,
|
|
20
|
-
enumerable: true
|
|
21
|
-
}) : target, mod));
|
|
22
|
-
//#endregion
|
|
23
|
-
let jiti = require("jiti");
|
|
24
|
-
let node_path = require("node:path");
|
|
25
|
-
node_path = __toESM(node_path);
|
|
26
|
-
let node_fs = require("node:fs");
|
|
27
|
-
node_fs = __toESM(node_fs);
|
|
28
|
-
let postcss = require("postcss");
|
|
29
|
-
postcss = __toESM(postcss);
|
|
30
|
-
let node_fs_promises = require("node:fs/promises");
|
|
31
|
-
node_fs_promises = __toESM(node_fs_promises);
|
|
32
|
-
let fast_glob = require("fast-glob");
|
|
33
|
-
fast_glob = __toESM(fast_glob);
|
|
34
|
-
//#region src/config.ts
|
|
35
|
-
const defaultConfig = {
|
|
36
|
-
include: ["src/**/*.module.css"],
|
|
37
|
-
exclude: [],
|
|
38
|
-
outDir: "__generated__",
|
|
39
|
-
watch: false,
|
|
40
|
-
silent: false
|
|
41
|
-
};
|
|
42
|
-
function defineConfig(config) {
|
|
43
|
-
return {
|
|
44
|
-
...defaultConfig,
|
|
45
|
-
...config
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
async function loadConfig(cwd = process.cwd()) {
|
|
49
|
-
const configFileName = "better-css-modules.config";
|
|
50
|
-
for (const ext of [
|
|
51
|
-
".ts",
|
|
52
|
-
".mts",
|
|
53
|
-
".cts",
|
|
54
|
-
".js",
|
|
55
|
-
".mjs",
|
|
56
|
-
".cjs"
|
|
57
|
-
]) {
|
|
58
|
-
const configPath = node_path.default.resolve(cwd, configFileName + ext);
|
|
59
|
-
if (node_fs.default.existsSync(configPath)) {
|
|
60
|
-
const mod = await (0, jiti.createJiti)(cwd).import(configPath);
|
|
61
|
-
const loaded = mod.default ?? mod;
|
|
62
|
-
return {
|
|
63
|
-
...defaultConfig,
|
|
64
|
-
...loaded
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return defaultConfig;
|
|
69
|
-
}
|
|
70
|
-
//#endregion
|
|
71
|
-
//#region src/parser.ts
|
|
72
|
-
/**
|
|
73
|
-
* Extract class names from CSS file contents.
|
|
74
|
-
*/
|
|
75
|
-
function extractClassNames(css) {
|
|
76
|
-
const root = postcss.default.parse(css);
|
|
77
|
-
const classNames = /* @__PURE__ */ new Set();
|
|
78
|
-
root.walkRules((rule) => {
|
|
79
|
-
const selectorParts = rule.selector.split(/\s*,\s*/);
|
|
80
|
-
for (const selector of selectorParts) {
|
|
81
|
-
const matches = selector.matchAll(/\.([a-zA-Z_-][\w-]*)/g);
|
|
82
|
-
for (const match of matches) classNames.add(match[1]);
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
return [...classNames].sort();
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Read a CSS Modules file and extract class names.
|
|
89
|
-
*/
|
|
90
|
-
async function parseFile(filePath) {
|
|
91
|
-
return extractClassNames(await node_fs_promises.default.readFile(filePath, "utf-8"));
|
|
92
|
-
}
|
|
93
|
-
//#endregion
|
|
94
|
-
//#region src/generator.ts
|
|
95
|
-
/**
|
|
96
|
-
* Generate .d.ts content from an array of class names.
|
|
97
|
-
*/
|
|
98
|
-
function generateDts(classNames) {
|
|
99
|
-
return `declare const styles: {\n${classNames.map((name) => {
|
|
100
|
-
return ` readonly ${/^[a-zA-Z_$][\w$]*$/.test(name) ? name : `"${name}"`}: string;`;
|
|
101
|
-
}).join("\n")}\n};\nexport default styles;\n`;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Generate and write a .d.ts file for a CSS Modules file.
|
|
105
|
-
*
|
|
106
|
-
* @param cssFilePath - Path to the source CSS Modules file
|
|
107
|
-
* @param classNames - Array of extracted class names
|
|
108
|
-
* @param outDir - Output directory
|
|
109
|
-
* @param cwd - Working directory
|
|
110
|
-
*/
|
|
111
|
-
async function writeDts(cssFilePath, classNames, outDir, cwd = process.cwd()) {
|
|
112
|
-
const dtsFileName = node_path.default.relative(cwd, cssFilePath) + ".d.ts";
|
|
113
|
-
const dtsPath = node_path.default.resolve(cwd, outDir, dtsFileName);
|
|
114
|
-
await node_fs_promises.default.mkdir(node_path.default.dirname(dtsPath), { recursive: true });
|
|
115
|
-
await node_fs_promises.default.writeFile(dtsPath, generateDts(classNames), "utf-8");
|
|
116
|
-
return dtsPath;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Batch-generate .d.ts files for all CSS Modules files based on the config.
|
|
120
|
-
*/
|
|
121
|
-
async function generateAll(config, cwd = process.cwd()) {
|
|
122
|
-
const files = await (0, fast_glob.default)(config.include, {
|
|
123
|
-
cwd,
|
|
124
|
-
ignore: config.exclude,
|
|
125
|
-
absolute: true
|
|
126
|
-
});
|
|
127
|
-
return await Promise.all(files.map(async (file) => {
|
|
128
|
-
return writeDts(file, await parseFile(file), config.outDir, cwd);
|
|
129
|
-
}));
|
|
130
|
-
}
|
|
131
|
-
//#endregion
|
|
132
|
-
Object.defineProperty(exports, "__toESM", {
|
|
133
|
-
enumerable: true,
|
|
134
|
-
get: function() {
|
|
135
|
-
return __toESM;
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
Object.defineProperty(exports, "defineConfig", {
|
|
139
|
-
enumerable: true,
|
|
140
|
-
get: function() {
|
|
141
|
-
return defineConfig;
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
Object.defineProperty(exports, "extractClassNames", {
|
|
145
|
-
enumerable: true,
|
|
146
|
-
get: function() {
|
|
147
|
-
return extractClassNames;
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
Object.defineProperty(exports, "generateAll", {
|
|
151
|
-
enumerable: true,
|
|
152
|
-
get: function() {
|
|
153
|
-
return generateAll;
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
Object.defineProperty(exports, "generateDts", {
|
|
157
|
-
enumerable: true,
|
|
158
|
-
get: function() {
|
|
159
|
-
return generateDts;
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
Object.defineProperty(exports, "loadConfig", {
|
|
163
|
-
enumerable: true,
|
|
164
|
-
get: function() {
|
|
165
|
-
return loadConfig;
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
Object.defineProperty(exports, "parseFile", {
|
|
169
|
-
enumerable: true,
|
|
170
|
-
get: function() {
|
|
171
|
-
return parseFile;
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
Object.defineProperty(exports, "writeDts", {
|
|
175
|
-
enumerable: true,
|
|
176
|
-
get: function() {
|
|
177
|
-
return writeDts;
|
|
178
|
-
}
|
|
179
|
-
});
|
package/dist/index.cjs
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_generator = require("./generator-Dy0W5B1c.cjs");
|
|
3
|
-
let node_path = require("node:path");
|
|
4
|
-
node_path = require_generator.__toESM(node_path);
|
|
5
|
-
let node_fs_promises = require("node:fs/promises");
|
|
6
|
-
node_fs_promises = require_generator.__toESM(node_fs_promises);
|
|
7
|
-
let fast_glob = require("fast-glob");
|
|
8
|
-
fast_glob = require_generator.__toESM(fast_glob);
|
|
9
|
-
let chokidar = require("chokidar");
|
|
10
|
-
let picomatch = require("picomatch");
|
|
11
|
-
picomatch = require_generator.__toESM(picomatch);
|
|
12
|
-
//#region src/scanner.ts
|
|
13
|
-
/**
|
|
14
|
-
* Scan TS/TSX file contents for CSS Modules class name usage.
|
|
15
|
-
* Detects patterns like styles.foo / styles["foo"] / styles['foo'].
|
|
16
|
-
*/
|
|
17
|
-
function findUsedClassNames(tsContent) {
|
|
18
|
-
const used = /* @__PURE__ */ new Set();
|
|
19
|
-
for (const match of tsContent.matchAll(/styles\.([a-zA-Z_$][\w$]*)/g)) used.add(match[1]);
|
|
20
|
-
for (const match of tsContent.matchAll(/styles\[["']([^"']+)["']\]/g)) used.add(match[1]);
|
|
21
|
-
return used;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Identify the source file of a CSS Modules import.
|
|
25
|
-
* Detects patterns like import styles from './Foo.module.css'.
|
|
26
|
-
*/
|
|
27
|
-
function findCssModuleImports(tsContent) {
|
|
28
|
-
const imports = [];
|
|
29
|
-
for (const match of tsContent.matchAll(/import\s+\w+\s+from\s+['"]([^'"]+\.module\.css)['"]/g)) imports.push(match[1]);
|
|
30
|
-
return imports;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Scan TS/TSX files under the specified directory
|
|
34
|
-
* and detect unused CSS Modules class names.
|
|
35
|
-
*/
|
|
36
|
-
async function scanUnusedClasses(cwd) {
|
|
37
|
-
const tsFiles = await (0, fast_glob.default)([
|
|
38
|
-
"**/*.{ts,tsx,js,jsx}",
|
|
39
|
-
"!node_modules",
|
|
40
|
-
"!dist",
|
|
41
|
-
"!__generated__"
|
|
42
|
-
], {
|
|
43
|
-
cwd,
|
|
44
|
-
absolute: true
|
|
45
|
-
});
|
|
46
|
-
const warnings = [];
|
|
47
|
-
for (const tsFile of tsFiles) {
|
|
48
|
-
const content = await node_fs_promises.default.readFile(tsFile, "utf-8");
|
|
49
|
-
const cssImports = findCssModuleImports(content);
|
|
50
|
-
if (cssImports.length === 0) continue;
|
|
51
|
-
const usedNames = findUsedClassNames(content);
|
|
52
|
-
for (const cssImport of cssImports) {
|
|
53
|
-
const cssPath = node_path.default.resolve(node_path.default.dirname(tsFile), cssImport);
|
|
54
|
-
try {
|
|
55
|
-
const classNames = await require_generator.parseFile(cssPath);
|
|
56
|
-
for (const className of classNames) if (!usedNames.has(className)) warnings.push({
|
|
57
|
-
cssFile: cssPath,
|
|
58
|
-
className
|
|
59
|
-
});
|
|
60
|
-
} catch {}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return warnings;
|
|
64
|
-
}
|
|
65
|
-
//#endregion
|
|
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
|
-
}
|
|
80
|
-
function startWatcher(config, cwd = process.cwd()) {
|
|
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, {
|
|
85
|
-
cwd,
|
|
86
|
-
ignoreInitial: false
|
|
87
|
-
});
|
|
88
|
-
function shouldProcess(filePath) {
|
|
89
|
-
return isMatch(filePath) && !isExcluded(filePath);
|
|
90
|
-
}
|
|
91
|
-
watcher.on("add", async (filePath) => {
|
|
92
|
-
if (!shouldProcess(filePath)) return;
|
|
93
|
-
await regenerate(filePath, config.outDir, cwd, config.silent);
|
|
94
|
-
});
|
|
95
|
-
watcher.on("change", async (filePath) => {
|
|
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);
|
|
102
|
-
});
|
|
103
|
-
return watcher;
|
|
104
|
-
}
|
|
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);
|
|
114
|
-
try {
|
|
115
|
-
const dtsPath = await require_generator.writeDts(absolutePath, await require_generator.parseFile(absolutePath), outDir, cwd);
|
|
116
|
-
if (!silent) console.log(`[better-css-modules] generated: ${dtsPath}`);
|
|
117
|
-
} catch (err) {
|
|
118
|
-
if (!silent) console.error(`[better-css-modules] error processing ${relativePath}:`, err);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
//#endregion
|
|
122
|
-
exports.defineConfig = require_generator.defineConfig;
|
|
123
|
-
exports.extractClassNames = require_generator.extractClassNames;
|
|
124
|
-
exports.generateAll = require_generator.generateAll;
|
|
125
|
-
exports.generateDts = require_generator.generateDts;
|
|
126
|
-
exports.loadConfig = require_generator.loadConfig;
|
|
127
|
-
exports.parseFile = require_generator.parseFile;
|
|
128
|
-
exports.scanUnusedClasses = scanUnusedClasses;
|
|
129
|
-
exports.startWatcher = startWatcher;
|
|
130
|
-
exports.writeDts = require_generator.writeDts;
|
package/dist/index.d.cts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import * as chokidar from "chokidar";
|
|
2
|
-
|
|
3
|
-
//#region src/config.d.ts
|
|
4
|
-
interface Config {
|
|
5
|
-
/** Glob patterns for target files */
|
|
6
|
-
include: string[];
|
|
7
|
-
/** Exclusion patterns */
|
|
8
|
-
exclude: string[];
|
|
9
|
-
/** Output directory */
|
|
10
|
-
outDir: string;
|
|
11
|
-
/** Watch mode (for CLI) */
|
|
12
|
-
watch: boolean;
|
|
13
|
-
/** Suppress console output */
|
|
14
|
-
silent: boolean;
|
|
15
|
-
}
|
|
16
|
-
declare function defineConfig(config: Partial<Config>): Config;
|
|
17
|
-
declare function loadConfig(cwd?: string): Promise<Config>;
|
|
18
|
-
//#endregion
|
|
19
|
-
//#region src/parser.d.ts
|
|
20
|
-
/**
|
|
21
|
-
* Extract class names from CSS file contents.
|
|
22
|
-
*/
|
|
23
|
-
declare function extractClassNames(css: string): string[];
|
|
24
|
-
/**
|
|
25
|
-
* Read a CSS Modules file and extract class names.
|
|
26
|
-
*/
|
|
27
|
-
declare function parseFile(filePath: string): Promise<string[]>;
|
|
28
|
-
//#endregion
|
|
29
|
-
//#region src/generator.d.ts
|
|
30
|
-
/**
|
|
31
|
-
* Generate .d.ts content from an array of class names.
|
|
32
|
-
*/
|
|
33
|
-
declare function generateDts(classNames: string[]): string;
|
|
34
|
-
/**
|
|
35
|
-
* Generate and write a .d.ts file for a CSS Modules file.
|
|
36
|
-
*
|
|
37
|
-
* @param cssFilePath - Path to the source CSS Modules file
|
|
38
|
-
* @param classNames - Array of extracted class names
|
|
39
|
-
* @param outDir - Output directory
|
|
40
|
-
* @param cwd - Working directory
|
|
41
|
-
*/
|
|
42
|
-
declare function writeDts(cssFilePath: string, classNames: string[], outDir: string, cwd?: string): Promise<string>;
|
|
43
|
-
/**
|
|
44
|
-
* Batch-generate .d.ts files for all CSS Modules files based on the config.
|
|
45
|
-
*/
|
|
46
|
-
declare function generateAll(config: Config, cwd?: string): Promise<string[]>;
|
|
47
|
-
//#endregion
|
|
48
|
-
//#region src/scanner.d.ts
|
|
49
|
-
interface UnusedClassWarning {
|
|
50
|
-
cssFile: string;
|
|
51
|
-
className: string;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Scan TS/TSX files under the specified directory
|
|
55
|
-
* and detect unused CSS Modules class names.
|
|
56
|
-
*/
|
|
57
|
-
declare function scanUnusedClasses(cwd: string): Promise<UnusedClassWarning[]>;
|
|
58
|
-
//#endregion
|
|
59
|
-
//#region src/watcher.d.ts
|
|
60
|
-
declare function startWatcher(config: Config, cwd?: string): chokidar.FSWatcher;
|
|
61
|
-
//#endregion
|
|
62
|
-
export { type Config, type UnusedClassWarning, defineConfig, extractClassNames, generateAll, generateDts, loadConfig, parseFile, scanUnusedClasses, startWatcher, writeDts };
|
package/dist/loader.cjs
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
const require_generator = require("./generator-Dy0W5B1c.cjs");
|
|
2
|
-
//#region src/loader.ts
|
|
3
|
-
let configPromise;
|
|
4
|
-
function getConfig(cwd) {
|
|
5
|
-
if (!configPromise) configPromise = require_generator.loadConfig(cwd);
|
|
6
|
-
return configPromise;
|
|
7
|
-
}
|
|
8
|
-
async function betterCssModulesLoader(source) {
|
|
9
|
-
const resourcePath = this.resourcePath;
|
|
10
|
-
const cwd = this.rootContext || process.cwd();
|
|
11
|
-
const config = await getConfig(cwd);
|
|
12
|
-
await require_generator.writeDts(resourcePath, require_generator.extractClassNames(source), config.outDir, cwd);
|
|
13
|
-
return source;
|
|
14
|
-
}
|
|
15
|
-
//#endregion
|
|
16
|
-
module.exports = betterCssModulesLoader;
|
package/dist/loader.d.cts
DELETED