You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/combined-queries.md
+45-7Lines changed: 45 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
# Knighted CSS Combined Loader Reference
2
2
3
-
This document summarizes how `?knighted-css&combined` behaves for different module export shapes and how to structure your imports accordingly. Use it as guidance when filing documentation feedback for `@knighted/css`.
3
+
This document summarizes how `?knighted-css&combined` behaves for different module export shapes and how to structure your imports accordingly, plus how the double-extension proxy import replaces most combined-query use cases.
4
4
5
5
> [!NOTE]
6
-
> TypeScript reads literal selector tokens from the generated `.knighted-css.ts` modules (emitted by `knighted-css-generate-types`). Append `&types` to combined imports only when you also need `stableSelectors` at runtime—the loader still exports the map, while the double-extension modules keep your editors in sync.
6
+
> TypeScript reads literal selector tokens from the generated `.knighted-css.ts` modules (emitted by `knighted-css-generate-types`). The double-extension modules also re-export the original module exports and `knightedCss`, so a single import can provide component exports, typed selectors, and the compiled stylesheet string without helper casts.
7
7
8
8
> [!TIP]
9
-
> Prefer importing `asKnightedCssCombinedModule` from `@knighted/css/loader-helpers` when you want a runtime helper—the file has zero Node dependencies, so both browser and Node builds stay green.
9
+
> Prefer the double-extension import when you run `knighted-css-generate-types`—it removes the need for `asKnightedCssCombinedModule`. Use the helper only for `?knighted-css&combined` queries or when you cannot rely on generated proxy modules.
> Namespace imports (`import * as combined …`) are the most reliable pattern for `&named-only` queries because you intentionally drop the default export. Keep using the helper type to narrow the namespace.
45
45
46
+
If you run `knighted-css-generate-types`, use the unified proxy import instead:
Prefer `?knighted-css&combined&named-only` plus the [named exports only](#named-exports-only) snippet when you intentionally avoid default exports but still need the named members and `knightedCss`.
74
92
75
93
## Adding stable selectors (`&types`)
76
94
77
95
Append `&types` whenever you need the runtime `stableSelectors` map in addition to `knightedCss`. Configure the loader’s `stableNamespace` option (or pass `--stable-namespace` to the CLI) so runtime exports and generated `.knighted-css.ts` modules stay aligned.
78
96
97
+
If you run `knighted-css-generate-types`, the double-extension proxy already exports `stableSelectors` and keeps the literal types in sync—no `&types` query or helper required.
98
+
79
99
### Named exports with stable selectors
80
100
81
101
_Use when you only consume named exports (no default) but still want typed `stableSelectors`._
- The loader always injects `knightedCss` alongside the module’s exports.
145
-
-To avoid synthetic defaults (and TypeScript warnings) for modules that only expose named exports, add `&named-only` and use a namespace import.
146
-
-Namespace imports plus `KnightedCssCombinedModule<typeof import('./module')>` work universally; default imports are optional conveniences when the source module exposes a default you actually consume.
147
-
- Add `&types` when you also need the `stableSelectors`map. Configure the namespace globally (loader option or CLI flag) so runtime + generated types stay consistent.
183
+
-When you run `knighted-css-generate-types`, prefer the double-extension proxy import to get component exports, `knightedCss`, and `stableSelectors` in one place.
184
+
-Use `?knighted-css&combined` (plus `asKnightedCssCombinedModule`) when you need runtime combined imports without generated proxy modules.
185
+
- Add `&types`only when you need a runtime selector map and cannot rely on the generated proxy files.
148
186
149
187
## When to use `KnightedCssCombinedModule`
150
188
151
-
The helper type still earns its keep when you need to narrow combined results without `asKnightedCssCombinedModule` (for example, in test doubles or custom wrappers):
189
+
The helper type still earns its keep when you need to narrow combined results without `asKnightedCssCombinedModule` (for example, in test doubles, custom wrappers, or when you skip generated proxies):
Copy file name to clipboardExpand all lines: docs/type-generation.md
+57-10Lines changed: 57 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Type generation (`*.knighted-css*`)
2
2
3
-
Use the `knighted-css-generate-types` CLI to create selector manifests that TypeScript can import. The CLI scans for specifiers ending in `.knighted-css` (for example `./button.module.scss.knighted-css.ts`), compiles the stylesheet once, and writes a sibling module that exports the literal selector tokens.
3
+
Use the `knighted-css-generate-types` CLI to generate modules for `.knighted-css` double-extension imports. For stylesheets, it emits a sibling module with literal selector tokens. For JavaScript/TypeScript module specifiers, the generated file acts as a unified proxy that re-exports the module’s exports plus `knightedCss`.
4
4
5
5
## Running the CLI
6
6
@@ -20,6 +20,21 @@ Typical script entry:
20
20
21
21
Wire it into `postinstall` or your build so new selectors land automatically.
22
22
23
+
### Options
24
+
25
+
-`--root` / `-r` – project root (defaults to `process.cwd()`).
26
+
-`--include` / `-i` – additional directories or files to scan (repeatable).
27
+
-`--out-dir` – directory for the selector module manifest cache (defaults to `<root>/.knighted-css`).
28
+
-`--stable-namespace` – namespace prefix shared by the generated selector maps and loader runtime.
29
+
-`--auto-stable` – enable auto-stable selector generation during extraction (mirrors the loader’s auto-stable behavior).
30
+
31
+
### Relationship to the loader
32
+
33
+
-`.knighted-css*` imports include the generated selector map and, for module specifiers, re-exports plus `knightedCss`.
34
+
-`?knighted-css` imports are purely runtime (see [docs/loader.md](./loader.md)). Append `&types` only when you also need the selector map at runtime; the compiler still reads the literal tokens from the generated modules.
35
+
36
+
For CSS Modules or Sass files that need stable selectors, import the generated `.knighted-css` module for types. For JS/TS component modules, the generated proxy already provides `knightedCss` alongside the exports, so you can rely on the proxy in place of separate `?knighted-css` runtime imports when appropriate.
37
+
23
38
## Minimal usage
24
39
25
40
```ts
@@ -28,18 +43,50 @@ import selectors from './button.module.scss.knighted-css.js'
Because the generated module lives next to the source stylesheet, TypeScript’s normal resolution logic applies—no custom `paths` entries required. Use the manifest in conjunction with runtime helpers such as `mergeStableClass` or `stableClassName` to keep hashed class names in sync.
32
56
33
-
### Options
57
+
##Rspack watch hook
34
58
35
-
-`--root` / `-r` – project root (defaults to `process.cwd()`).
36
-
-`--include` / `-i` – additional directories or files to scan (repeatable).
37
-
-`--out-dir` – directory for the selector module manifest cache (defaults to `<root>/.knighted-css`).
38
-
-`--stable-namespace` – namespace prefix shared by the generated selector maps and loader runtime.
59
+
If you want the CLI to rerun during dev, hook it into Rspack’s watch pipeline. This keeps the generated `.knighted-css` proxy modules in sync whenever source files change. You can also scope the `--include` list using `compiler.modifiedFiles` to avoid rescanning the entire project on every rebuild.
39
60
40
-
### Relationship to the loader
61
+
```js
62
+
// rspack.config.js
63
+
import { exec } from'node:child_process'
41
64
42
-
-`.knighted-css*` imports are purely for types; they never include the compiled CSS string.
43
-
-`?knighted-css` imports are purely runtime (see [docs/loader.md](./loader.md)). Append `&types` only when you also need the selector map at runtime; the compiler still reads the literal tokens from the generated modules.
Keep both hooks in mind when authoring CSS Modules or Sass files that need stable selectors: import the generated module for types, and import the loader query when you need the runtime stylesheet.
92
+
Scope the `--include` paths to the folders that actually import `.knighted-css` to keep the watch step fast. When `modifiedFiles` is empty (for example on the first run), fall back to a stable include root like `src`.
0 commit comments