@knighted/css 1.0.2 → 1.0.3

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.
Files changed (2) hide show
  1. package/README.md +247 -0
  2. package/package.json +2 -3
package/README.md ADDED
@@ -0,0 +1,247 @@
1
+ # [`@knighted/css`](../../README.md)
2
+
3
+ ![CI](https://github.com/knightedcodemonkey/css/actions/workflows/ci.yml/badge.svg)
4
+ [![codecov](https://codecov.io/gh/knightedcodemonkey/css/graph/badge.svg?token=q93Qqwvq6l)](https://codecov.io/gh/knightedcodemonkey/css)
5
+ [![NPM version](https://img.shields.io/npm/v/@knighted/css.svg)](https://www.npmjs.com/package/@knighted/css)
6
+
7
+ `@knighted/css` walks your JavaScript/TypeScript module graph, compiles every CSS-like dependency (plain CSS, Sass/SCSS, Less, vanilla-extract), and ships both the concatenated stylesheet string and optional `.knighted-css.*` imports that keep selectors typed. Use it when you need fully materialized styles ahead of runtime—Shadow DOM surfaces, server-rendered routes, static site builds, or any entry point that should inline CSS without spinning up a full bundler.
8
+
9
+ ## Why
10
+
11
+ I needed a single source of truth for UI components that could drop into both light DOM pages and Shadow DOM hosts, without losing encapsulated styling in the latter.
12
+
13
+ ## Quick Links
14
+
15
+ - [Features](#features)
16
+ - [Requirements](#requirements)
17
+ - [Installation](#installation)
18
+ - [Quick Start](#quick-start)
19
+ - [API](#api)
20
+ - [Entry points (`import`)](#entry-points-at-a-glance)
21
+ - [Examples](#examples)
22
+ - [Demo](#demo)
23
+
24
+ ## Features
25
+
26
+ - Traverses module graphs with a built-in walker to find transitive style imports (no bundler required).
27
+ - Resolution parity via [`oxc-resolver`](https://github.com/oxc-project/oxc-resolver): tsconfig `paths`, package `exports` + `imports`, and extension aliasing (e.g., `.css.js` → `.css.ts`) are honored without wiring up a bundler.
28
+ - Compiles `*.css`, `*.scss`, `*.sass`, `*.less`, and `*.css.ts` (vanilla-extract) files out of the box.
29
+ - Optional post-processing via [`lightningcss`](https://github.com/parcel-bundler/lightningcss) for minification, prefixing, media query optimizations, or specificity boosts.
30
+ - Pluggable resolver/filter hooks for custom module resolution (e.g., Rspack/Vite/webpack aliases) or selective inclusion.
31
+ - First-class loader (`@knighted/css/loader`) so bundlers can import compiled CSS alongside their modules via `?knighted-css`.
32
+ - Built-in type generation CLI (`knighted-css-generate-types`) that emits `.knighted-css.*` selector manifests so TypeScript gets literal tokens in lockstep with the loader exports.
33
+
34
+ ## Requirements
35
+
36
+ - Node.js `>= 22.17.0`
37
+ - npm `>= 10.9.0`
38
+ - Install peer toolchains you intend to use (`sass`, `less`, `@vanilla-extract/integration`, etc.).
39
+
40
+ ## Installation
41
+
42
+ ```bash
43
+ npm install @knighted/css
44
+ ```
45
+
46
+ Install the peers your project is using, for example `less`, or `sass`, etc.
47
+
48
+ ## Quick Start
49
+
50
+ ```ts
51
+ // scripts/extract-styles.ts
52
+ import { css } from '@knighted/css'
53
+
54
+ const styles = await css('./src/components/app.ts', {
55
+ cwd: process.cwd(),
56
+ lightningcss: { minify: true },
57
+ })
58
+
59
+ console.log(styles)
60
+ ```
61
+
62
+ Run it with `tsx`/`node` and you will see a fully inlined stylesheet for `app.ts` and every style import it references, regardless of depth.
63
+
64
+ ## API
65
+
66
+ ```ts
67
+ type CssOptions = {
68
+ extensions?: string[] // customize file extensions to scan
69
+ cwd?: string // working directory (defaults to process.cwd())
70
+ filter?: (filePath: string) => boolean
71
+ lightningcss?: boolean | LightningTransformOptions
72
+ specificityBoost?: {
73
+ visitor?: LightningTransformOptions<never>['visitor']
74
+ strategy?: SpecificityStrategy
75
+ match?: SpecificitySelector[]
76
+ }
77
+ moduleGraph?: ModuleGraphOptions
78
+ resolver?: (
79
+ specifier: string,
80
+ ctx: { cwd: string; from?: string },
81
+ ) => string | Promise<string | undefined>
82
+ peerResolver?: (name: string) => Promise<unknown> // for custom module loading
83
+ }
84
+
85
+ async function css(entry: string, options?: CssOptions): Promise<string>
86
+ ```
87
+
88
+ ## Entry points at a glance
89
+
90
+ ### Runtime loader hook (`?knighted-css`)
91
+
92
+ Import any module with the `?knighted-css` query to receive the compiled stylesheet string:
93
+
94
+ ```ts
95
+ import { knightedCss } from './button.js?knighted-css'
96
+ ```
97
+
98
+ See [docs/loader.md](../../docs/loader.md) for the full configuration, combined imports, and `&types` runtime selector map guidance.
99
+
100
+ ### Type generation hook (`*.knighted-css*`)
101
+
102
+ Run `knighted-css-generate-types` so every specifier that ends with `.knighted-css` produces a sibling manifest containing literal selector tokens:
103
+
104
+ ```ts
105
+ import stableSelectors from './button.module.scss.knighted-css.js'
106
+ ```
107
+
108
+ Refer to [docs/type-generation.md](../../docs/type-generation.md) for CLI options and workflow tips.
109
+
110
+ ### Combined + runtime selectors
111
+
112
+ Need the module exports, `knightedCss`, and a runtime `stableSelectors` map from one import? Use `?knighted-css&combined&types` (plus optional `&named-only`). Example:
113
+
114
+ ```ts
115
+ import type { KnightedCssCombinedModule } from '@knighted/css/loader'
116
+ import { asKnightedCssCombinedModule } from '@knighted/css/loader-helpers'
117
+ import type { ButtonStableSelectors } from './button.css.knighted-css.js'
118
+ import * as buttonModule from './button.js?knighted-css&combined&types'
119
+
120
+ const {
121
+ default: Button,
122
+ knightedCss,
123
+ stableSelectors,
124
+ } = asKnightedCssCombinedModule<
125
+ typeof import('./button.js'),
126
+ { stableSelectors: Readonly<Record<keyof ButtonStableSelectors, string>> }
127
+ >(buttonModule)
128
+
129
+ stableSelectors.shell
130
+ ```
131
+
132
+ > [!NOTE]
133
+ > `stableSelectors` here is for runtime use; TypeScript still reads literal tokens from the generated `.knighted-css.*` modules. For a full decision matrix, see [docs/combined-queries.md](../../docs/combined-queries.md).
134
+ > Prefer importing `asKnightedCssCombinedModule` from `@knighted/css/loader-helpers` instead of grabbing it from `@knighted/css/loader`—the helper lives in a Node-free chunk so both browser and server bundles stay happy.
135
+
136
+ ## Examples
137
+
138
+ - [Generate standalone stylesheets](#generate-standalone-stylesheets)
139
+ - [Inline CSS during SSR](#inline-css-during-ssr)
140
+ - [Custom resolver](#custom-resolver-enhanced-resolve-example)
141
+ - [Specificity boost](#specificity-boost)
142
+ - [Bundler loader](../../docs/loader.md#loader-example)
143
+
144
+ ### Generate standalone stylesheets
145
+
146
+ ```ts
147
+ import { writeFile } from 'node:fs/promises'
148
+ import { css } from '@knighted/css'
149
+
150
+ // Build-time script that gathers all CSS imported by a React route
151
+ const sheet = await css('./src/routes/marketing-page.tsx', {
152
+ lightningcss: { minify: true, targets: { chrome: 120, safari: 17 } },
153
+ })
154
+
155
+ await writeFile('./dist/marketing-page.css', sheet)
156
+ ```
157
+
158
+ ### Inline CSS during SSR
159
+
160
+ ```ts
161
+ import { renderToString } from 'react-dom/server'
162
+ import { css } from '@knighted/css'
163
+
164
+ export async function render(url: string) {
165
+ const styles = await css('./src/routes/root.tsx')
166
+ const html = renderToString(<App url={url} />)
167
+ return `<!doctype html><style>${styles}</style>${html}`
168
+ }
169
+ ```
170
+
171
+ ### Custom resolver (enhanced-resolve example)
172
+
173
+ The built-in walker already leans on [`oxc-resolver`](https://github.com/oxc-project/oxc-resolver), so tsconfig `paths`, package `exports` conditions, and common extension aliases work out of the box. If you still need to mirror bespoke behavior (virtual modules, framework-specific loaders, etc.), plug in a custom resolver. Here’s how to use [`enhanced-resolve`](https://github.com/webpack/enhanced-resolve):
174
+
175
+ > [!TIP]
176
+ > Hash-prefixed specifiers defined in `package.json#imports` resolve automatically—no extra loader or `css()` options required. Reach for a custom resolver only when you need behavior beyond what `oxc-resolver` already mirrors.
177
+
178
+ > [!NOTE]
179
+ > Sass-specific prefixes such as `pkg:#button` live outside Node’s resolver and still need a shim. See [docs/sass-import-aliases.md](../../docs/sass-import-aliases.md) for a drop-in helper that strips those markers before `@knighted/css` walks the graph.
180
+
181
+ ```ts
182
+ import { ResolverFactory } from 'enhanced-resolve'
183
+ import { css } from '@knighted/css'
184
+
185
+ const resolver = ResolverFactory.createResolver({
186
+ extensions: ['.ts', '.tsx', '.js'],
187
+ mainFiles: ['index'],
188
+ })
189
+
190
+ async function resolveWithEnhanced(id: string, cwd: string): Promise<string | undefined> {
191
+ return new Promise((resolve, reject) => {
192
+ resolver.resolve({}, cwd, id, {}, (err, result) => {
193
+ if (err) return reject(err)
194
+ resolve(result ?? undefined)
195
+ })
196
+ })
197
+ }
198
+
199
+ const styles = await css('./src/routes/page.tsx', {
200
+ resolver: (specifier, { cwd }) => resolveWithEnhanced(specifier, cwd),
201
+ })
202
+ ```
203
+
204
+ This keeps `@knighted/css` resolution in sync with your bundler’s alias/extension rules.
205
+
206
+ ### Specificity boost
207
+
208
+ Use `specificityBoost` to tweak selector behavior:
209
+
210
+ - **Strategies (built-in)**:
211
+ - `repeat-class` duplicates the last class in matching selectors to raise specificity (useful when you need a real specificity bump).
212
+ - `append-where` appends `:where(.token)` (zero specificity) for a harmless, order-based tie-breaker without changing matching.
213
+ - **Custom visitor**: Supply your own Lightning CSS visitor via `specificityBoost.visitor` for full control.
214
+ - **match filtering**: Provide `match: (string | RegExp)[]` to target selectors. Matches are OR’d; if any entry matches, the strategy applies. If omitted/empty, all selectors are eligible.
215
+
216
+ Example:
217
+
218
+ ```ts
219
+ import { css } from '@knighted/css'
220
+
221
+ const styles = await css('./src/entry.ts', {
222
+ lightningcss: { minify: true },
223
+ specificityBoost: {
224
+ match: ['.card', /^\.btn/], // OR match
225
+ strategy: { type: 'repeat-class', times: 1 },
226
+ },
227
+ })
228
+ ```
229
+
230
+ If you omit `match`, the strategy applies to all selectors. Use `append-where` when you don’t want to change specificity; use `repeat-class` when you do.
231
+
232
+ > [!NOTE]
233
+ > For the built-in strategies, the last class in a matching selector is the one that gets duplicated/appended. If you have multiple similar classes, tighten your `match` (string or RegExp) to target exactly the selector you want boosted.
234
+
235
+ > [!TIP]
236
+ > See [docs/specificity-boost-visitor.md](../../docs/specificity-boost-visitor.md) for a concrete visitor example.
237
+
238
+ ## Demo
239
+
240
+ Want to see everything wired together? Check the full demo app at [css-jsx-app](https://github.com/morganney/css-jsx-app).
241
+
242
+ > [!TIP]
243
+ > This repo also includes a [playwright workspace](../playwright/src/lit-react/lit-host.ts) which serves as an end-to-end demo.
244
+
245
+ ## License
246
+
247
+ MIT © Knighted Code Monkey
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knighted/css",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "A build-time utility that traverses JavaScript/TypeScript module dependency graphs to extract, compile, and optimize all imported CSS into a single, in-memory string.",
5
5
  "type": "module",
6
6
  "main": "./dist/css.js",
@@ -114,8 +114,7 @@
114
114
  "types.d.ts",
115
115
  "stable",
116
116
  "bin",
117
- "types-stub",
118
- "README.md"
117
+ "types-stub"
119
118
  ],
120
119
  "author": "KCM <knightedcodemonkey@gmail.com>",
121
120
  "license": "MIT",