Skip to content

Commit fb04ceb

Browse files
docs: plugin.
1 parent 1e9fc0d commit fb04ceb

3 files changed

Lines changed: 147 additions & 0 deletions

File tree

docs/loader.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ augmented exports to be present on the original JS/TS module. Use the resolver p
5353
to automatically append `?knighted-css` for any module import that has a generated
5454
sidecar `.d.ts` file.
5555

56+
See [docs/plugin.md](./plugin.md) for full resolver plugin documentation.
57+
5658
```js
5759
// rspack.config.js
5860
import { knightedCssResolverPlugin } from '@knighted/css/plugin'

docs/plugin.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Resolver plugin (`knightedCssResolverPlugin`)
2+
3+
`knightedCssResolverPlugin` is the resolver companion for declaration mode. It teaches your
4+
bundler to rewrite module imports to `?knighted-css` (and `&combined` when applicable) when a
5+
matching declaration sidecar exists, so runtime exports stay aligned with the generated types.
6+
7+
Without the plugin, TypeScript may compile, but the bundler will load the original module
8+
without the loader query. That means `knightedCss`, `stableSelectors`, or other injected exports
9+
will be missing at runtime.
10+
11+
## What it does
12+
13+
- Scans resolved JS/TS module requests.
14+
- Looks for a declaration sidecar (`.d.ts`) generated by `knighted-css-generate-types --mode declaration`.
15+
- If a sidecar is found, rewrites the request to append `?knighted-css`.
16+
- Optionally enforces strict sidecar matches using a manifest to avoid accidental rewrites.
17+
- Optionally marks “combined” entries (via `combinedPaths`) so the query includes `&combined`.
18+
19+
## Options
20+
21+
```ts
22+
import { KnightedCssResolverPlugin } from '@knighted/css/plugin'
23+
24+
new KnightedCssResolverPlugin({
25+
rootDir: string,
26+
tsconfig: string | Record<string, unknown>,
27+
conditions: string[],
28+
extensions: string[],
29+
debug: boolean,
30+
combinedPaths: Array<string | RegExp>,
31+
strictSidecar: boolean,
32+
manifestPath: string,
33+
})
34+
```
35+
36+
- `rootDir` (optional): Base directory used for resolver scoping. Defaults to `process.cwd()`.
37+
- `tsconfig` (optional): Path to a tsconfig or an in-memory tsconfig object for path resolution.
38+
- `conditions` (optional): Custom `package.json` export conditions to honor during resolution.
39+
- `extensions` (optional): File extensions considered as script modules. Defaults to
40+
`.ts`, `.tsx`, `.js`, `.jsx`, `.mts`, `.cts`, `.mjs`, `.cjs`.
41+
- `debug` (optional): Logs rewrite decisions and a summary of cache hits/misses.
42+
- `combinedPaths` (optional): List of strings or regexes. Any resolved path that matches will
43+
receive `&combined` alongside `?knighted-css`.
44+
- `strictSidecar` (optional): When true, only modules present in the manifest are rewritten.
45+
Defaults to true when `manifestPath` is provided.
46+
- `manifestPath` (optional): Path to the sidecar manifest generated by
47+
`knighted-css-generate-types --manifest`. Used for strict matching.
48+
49+
## Basic usage (declaration mode)
50+
51+
```js
52+
// rspack.config.js
53+
import { KnightedCssResolverPlugin } from '@knighted/css/plugin'
54+
55+
export default {
56+
resolve: {
57+
plugins: [new KnightedCssResolverPlugin()],
58+
},
59+
}
60+
```
61+
62+
```ts
63+
// Type generation
64+
knighted-css-generate-types --root . --include src --mode declaration
65+
```
66+
67+
This lets you write clean imports while still receiving `knightedCss` at runtime:
68+
69+
```ts
70+
import Button, { knightedCss } from './button.js'
71+
```
72+
73+
## Strict sidecar + manifest (recommended)
74+
75+
Use strict sidecars to avoid rewriting modules that merely happen to have a `.d.ts` next to
76+
them. This ensures only declaration mode sidecars generated by the CLI trigger rewrites.
77+
78+
```sh
79+
knighted-css-generate-types --root . --include src --mode declaration --manifest .knighted-css/knighted-manifest.json
80+
```
81+
82+
```js
83+
// rspack.config.js
84+
import path from 'node:path'
85+
import { KnightedCssResolverPlugin } from '@knighted/css/plugin'
86+
87+
export default {
88+
resolve: {
89+
plugins: [
90+
new KnightedCssResolverPlugin({
91+
strictSidecar: true,
92+
manifestPath: path.resolve('.knighted-css/knighted-manifest.json'),
93+
}),
94+
],
95+
},
96+
}
97+
```
98+
99+
## Combined imports
100+
101+
If you have modules that are consumed with combined exports (`?knighted-css&combined`),
102+
set `combinedPaths` to ensure the resolver appends `&combined` during rewrites.
103+
104+
Example of an import that relies on `&combined` at runtime:
105+
106+
```ts
107+
import { Card, knightedCss } from './combined-card.js'
108+
```
109+
110+
And how you would use `combinedPaths` to support that:
111+
112+
```js
113+
import path from 'node:path'
114+
import { KnightedCssResolverPlugin } from '@knighted/css/plugin'
115+
116+
export default {
117+
resolve: {
118+
plugins: [
119+
new KnightedCssResolverPlugin({
120+
combinedPaths: [
121+
path.resolve('src/components/combined-card.tsx'),
122+
/src\/views\/.*\.tsx$/,
123+
],
124+
}),
125+
],
126+
},
127+
}
128+
```
129+
130+
## Debugging
131+
132+
Enable `debug: true` to log each decision and a final summary that includes counts for
133+
rewrites, cache hits, marker misses, and manifest misses.
134+
135+
```js
136+
import { KnightedCssResolverPlugin } from '@knighted/css/plugin'
137+
138+
export default {
139+
resolve: {
140+
plugins: [new KnightedCssResolverPlugin({ debug: true })],
141+
},
142+
}
143+
```

docs/type-generation.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Wire it into `postinstall` or your build so new selectors land automatically.
4040
| `declaration` | Plain JS/TS imports | `.d.ts` augmentations next to modules | Required (append `?knighted-css`) | Cleaner imports when you accept resolver overhead |
4141

4242
If you use declaration mode, prefer enabling strict sidecars + a manifest so the resolver only rewrites imports that the CLI generated.
43+
See [docs/plugin.md](./plugin.md) for resolver plugin details and configuration options.
4344

4445
### Relationship to the loader
4546

@@ -80,6 +81,7 @@ import Button, { knightedCss, stableSelectors } from './button.js'
8081
> [!IMPORTANT]
8182
> Declaration mode requires a resolver plugin to append `?knighted-css` (and `&combined` when applicable)
8283
> at build time so runtime exports match the generated types.
84+
> See [docs/plugin.md](./plugin.md) for resolver plugin configuration.
8385
8486
### Sidecar manifests + strict resolver mode
8587

0 commit comments

Comments
 (0)