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
Run `npxknighted-css-generate-types--root .` to scan your project for `?knighted-css&types` imports. The CLI:
204
+
205
+
- extracts selectors via the loader, then writes literal module declarations into `node_modules/@knighted/css/node_modules/.knighted-css`
206
+
- updates the packaged stub at `node_modules/@knighted/css/types-stub/index.d.ts`
207
+
- exposes the declarations automatically because `types.d.ts` references the stub, so no `tsconfig` wiring is required
208
+
209
+
Re-run the command whenever imports change (add it to a `types:css` npm script or your build). If you need a different destination, pass `--out-dir` and/or `--types-root` to override the defaults.
210
+
201
211
Sass/Less projects can import the shared mixins directly:
202
212
203
213
```scss
@@ -238,15 +248,84 @@ Need a zero-JS approach? Import the optional layer helper and co-locate your fal
238
248
239
249
Override the namespace via `:root { --knighted-stable-namespace: 'acme'; }` if you want a different prefix in pure CSS.
The map ships as `asconst`, so every key/value pair is type-safe without additional tooling. Need the combined import? Add the flag there too and destructure everything from one place:
Namespaces default to `knighted`, but you can configure a global fallback via the loader’s `stableNamespace` option:
276
+
277
+
```js
278
+
{
279
+
loader: '@knighted/css/loader',
280
+
options: {
281
+
stableNamespace: 'storybook',
282
+
},
283
+
}
284
+
```
285
+
286
+
All imports share the namespace resolved by the loader (or the `knighted-css-generate-types` CLI). Use the loader option or CLI flag to align runtime + type generation, and the loader still emits highlighted warnings when the namespace trims to an empty value or when no selectors match. For best editor support, keep `&types` at the end of the query (`?knighted-css&combined&types`, `?knighted-css&combined&named-only&types`, etc.).
287
+
241
288
#### TypeScript support for loader queries
242
289
243
290
Loader query types ship directly with `@knighted/css`. Reference them once in your project—either by adding `"types": ["@knighted/css/loader-queries"]` to `tsconfig.json` or dropping `/// <reference types="@knighted/css/loader-queries" />` into a global `.d.ts`—and the following ambient modules become available everywhere:
|**Named exports only**|`?knighted-css&combined&named-only`|[Snippet](#named-exports-only)|`&named-only` disables the synthetic default export so you only destructure the original named members plus `knightedCss`. |
10
-
|**Default export only**|`?knighted-css&combined`|[Snippet](#default-export-only)| Loader mirrors the default export and adds `knightedCss`, so default-import code keeps working. |
11
-
|**Default + named exports**|`?knighted-css&combined` (append `&named-only` when you never consume the default) |[Snippet](#default-and-named-exports)| Without the flag you get both default + named exports; adding it drops the synthetic default for stricter codebases. |
|**Named exports only**|`?knighted-css&combined&named-only`|[Snippet](#named-exports-only)|`&named-only` disables the synthetic default export so you only destructure the original named members plus `knightedCss`. |
10
+
|**Default export only**|`?knighted-css&combined`|[Snippet](#default-export-only)| Loader mirrors the default export and adds `knightedCss`, so default-import code keeps working. |
11
+
|**Default + named exports**|`?knighted-css&combined` (append `&named-only` when you never consume the default) |[Snippet](#default-and-named-exports)| Without the flag you get both default + named exports; adding it drops the synthetic default for stricter codebases. |
12
+
|**Named exports + stable selector map**|`?knighted-css&combined&named-only&types`|[Snippet](#named-exports-with-stable-selectors)| Adds a `stableSelectors` named export; configure namespaces via the loader option or CLI flag. |
13
+
|**Default export only + stable selector map**|`?knighted-css&combined&types`|[Snippet](#default-export-with-stable-selectors)| Keep your default-import flow and add `stableSelectors`; namespaces come from loader/CLI configuration. |
14
+
|**Default + named exports + stable selectors**|`?knighted-css&combined&types` (append `&named-only` if you skip the default) |[Snippet](#default-and-named-exports-with-stable-selectors)| Best of both worlds—`stableSelectors` is exported alongside `knightedCss`; add `&named-only` if you don’t use the default. |
12
15
13
16
## Named exports only
14
17
@@ -50,8 +53,56 @@ const {
50
53
51
54
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`.
Need a different prefix? Configure the loader’s `stableNamespace` option (or pass `--stable-namespace` to the CLI) so both runtime and generated types stay in sync.
stableSelectors.card// "knighted-card" (or your configured namespace)
99
+
```
100
+
101
+
Append `&named-only` before `&types` when you want to drop the synthetic default export while still receiving `stableSelectors`.
102
+
53
103
## Key Takeaways
54
104
55
105
- The loader always injects `knightedCss` alongside the module’s exports.
56
106
- To avoid synthetic defaults (and TypeScript warnings) for modules that only expose named exports, add `&named-only` and use a namespace import.
57
107
- Namespace imports plus `KnightedCssCombinedModule<typeof import('./module')>` work universally; default imports are optional conveniences when the source module exposes a default you actually consume.
108
+
- Add `&types` when you also need the `stableSelectors` map. Configure the namespace globally (loader option or CLI flag) so runtime + generated types stay consistent.
0 commit comments