@better-css-modules/core 0.1.2 → 0.2.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.
- package/README.md +38 -45
- package/dist/dts-BWBpx_ii.mjs +1492 -0
- package/dist/dts-BjIczsCF.d.mts +275 -0
- package/dist/index.d.mts +16 -58
- package/dist/index.mjs +2377 -85
- package/dist/internal.d.mts +23 -0
- package/dist/internal.mjs +94 -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
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @better-css-modules/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The analysis behind better-css-modules: config loading, `.d.ts` generation and the checks. The CLI and every bundler plugin are built on it. Install it next to them for `defineConfig`, which the config file imports.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -8,61 +8,54 @@ Core library for better-css-modules. Provides CSS Modules parsing, type definiti
|
|
|
8
8
|
pnpm add -D @better-css-modules/core
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## The config file
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
loadConfig,
|
|
17
|
-
extractClassNames,
|
|
18
|
-
parseFile,
|
|
19
|
-
generateDts,
|
|
20
|
-
writeDts,
|
|
21
|
-
generateAll,
|
|
22
|
-
scanUnusedClasses,
|
|
23
|
-
startWatcher,
|
|
24
|
-
} from "@better-css-modules/core";
|
|
25
|
-
|
|
26
|
-
// Extract class names from CSS
|
|
27
|
-
const classNames = extractClassNames(".container { color: red; }");
|
|
28
|
-
// => ['container']
|
|
29
|
-
|
|
30
|
-
// Generate .d.ts content
|
|
31
|
-
const dts = generateDts(classNames);
|
|
32
|
-
|
|
33
|
-
// Generate all .d.ts files based on config
|
|
34
|
-
const config = await loadConfig(process.cwd());
|
|
35
|
-
await generateAll(config, process.cwd());
|
|
14
|
+
// better-css-modules.config.ts
|
|
15
|
+
import { defineConfig } from "@better-css-modules/core";
|
|
36
16
|
|
|
37
|
-
|
|
38
|
-
|
|
17
|
+
export default defineConfig({
|
|
18
|
+
globalCss: ["./src/globals.css"],
|
|
19
|
+
layer: "components",
|
|
20
|
+
});
|
|
39
21
|
```
|
|
40
22
|
|
|
41
|
-
|
|
23
|
+
`defineConfig` returns the config as it is and types it. The settings are described in the [project README](https://github.com/k35o/better-css-modules#configuration).
|
|
42
24
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
## Configuration
|
|
25
|
+
## API
|
|
46
26
|
|
|
47
27
|
```ts
|
|
48
|
-
import {
|
|
28
|
+
import {
|
|
29
|
+
check,
|
|
30
|
+
ConfigError,
|
|
31
|
+
formatDiagnostic,
|
|
32
|
+
generate,
|
|
33
|
+
loadConfig,
|
|
34
|
+
} from "@better-css-modules/core";
|
|
49
35
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
})
|
|
36
|
+
try {
|
|
37
|
+
const config = await loadConfig();
|
|
38
|
+
const { files } = await generate(config);
|
|
39
|
+
const { diagnostics, modules, tokens } = await check(config);
|
|
40
|
+
for (const diagnostic of diagnostics) console.log(formatDiagnostic(diagnostic, process.cwd()));
|
|
41
|
+
console.log(`${files.length} types, ${modules} modules, tokens: ${tokens.join(", ")}`);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (!(error instanceof ConfigError)) throw error;
|
|
44
|
+
console.error(error.message);
|
|
45
|
+
process.exitCode = 2;
|
|
46
|
+
}
|
|
57
47
|
```
|
|
58
48
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
49
|
+
- `loadConfig({ cwd?, config? })` loads the config file `config` names, relative to `cwd`, or the `better-css-modules.config.*` in `cwd` (by default the working directory), and returns it as a `ResolvedConfig`: the defaults filled in, checked, and with `root`, the directory every relative path starts from, and `file`, the config file or `null`.
|
|
50
|
+
- `generate(config)` writes the `.d.ts` files and their declaration maps, removes the ones it wrote for stylesheets that are gone, and returns a `GenerateResult`: `files`, `removed` and the syntax `diagnostics`.
|
|
51
|
+
- `check(config)` runs every check and returns a `CheckResult`: the sorted `diagnostics`, how many `modules` it checked, and the token categories (`tokens`) the global CSS restricts.
|
|
52
|
+
- `formatDiagnostic(diagnostic, cwd)` gives the `file:line:col error rule: message` line of the CLI, and `formatGitHubAnnotation(diagnostic, cwd)` the GitHub Actions workflow command of `--format github`.
|
|
53
|
+
- `ConfigError` is a mistake in the config or in the files it names. `loadConfig` throws it, and so does `check` when `include` matches no file, the global CSS cannot be read, or it does not declare the `layer`.
|
|
54
|
+
- The types `Config`, `ResolvedConfig`, `GenerateResult`, `CheckResult`, `Diagnostic` and `RuleId` describe these.
|
|
55
|
+
|
|
56
|
+
`@better-css-modules/core/internal` holds what the CLI and the plugins share. It is for those packages only and outside semver.
|
|
57
|
+
|
|
58
|
+
The rules `check` reports are described in the [project README](https://github.com/k35o/better-css-modules#rules).
|
|
66
59
|
|
|
67
60
|
## License
|
|
68
61
|
|