@knighted/css 1.0.0-alpha.1 → 1.0.0-alpha.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.
@@ -1,4 +1,4 @@
1
- import type { DependencyTreeOptions } from 'dependency-tree';
1
+ import type { Options as DependencyTreeOpts } from 'dependency-tree';
2
2
  import { type TransformOptions as LightningTransformOptions } from 'lightningcss';
3
3
  export declare const DEFAULT_EXTENSIONS: string[];
4
4
  type LightningCssConfig = boolean | Partial<Omit<LightningTransformOptions<never>, 'code'>>;
@@ -11,7 +11,7 @@ export interface CssOptions {
11
11
  cwd?: string;
12
12
  filter?: (filePath: string) => boolean;
13
13
  lightningcss?: LightningCssConfig;
14
- dependencyTree?: Partial<Omit<DependencyTreeOptions, 'filename' | 'directory'>>;
14
+ dependencyTree?: Partial<Omit<DependencyTreeOpts, 'filename' | 'directory'>>;
15
15
  resolver?: CssResolver;
16
16
  peerResolver?: PeerLoader;
17
17
  }
@@ -4,14 +4,7 @@ const css_js_1 = require("./css.cjs");
4
4
  const DEFAULT_EXPORT_NAME = 'knightedCss';
5
5
  const loader = async function loader(source) {
6
6
  const rawOptions = (typeof this.getOptions === 'function' ? this.getOptions() : {});
7
- const queryParams = typeof this.resourceQuery === 'string' && this.resourceQuery.startsWith('?')
8
- ? new URLSearchParams(this.resourceQuery.slice(1))
9
- : undefined;
10
- const queryExportName = queryParams?.get('exportName')?.trim();
11
- const isValidIdentifier = typeof queryExportName === 'string' &&
12
- /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(queryExportName);
13
- const { exportName = DEFAULT_EXPORT_NAME, ...cssOptions } = rawOptions;
14
- const resolvedExportName = isValidIdentifier ? queryExportName : exportName;
7
+ const cssOptions = rawOptions;
15
8
  const normalizedOptions = {
16
9
  ...cssOptions,
17
10
  cwd: cssOptions.cwd ?? this.rootContext ?? process.cwd(),
@@ -22,8 +15,11 @@ const loader = async function loader(source) {
22
15
  this.addDependency(file);
23
16
  }
24
17
  const input = typeof source === 'string' ? source : source.toString('utf8');
25
- const injection = `\n\nexport const ${resolvedExportName} = ${JSON.stringify(css)};\n`;
26
- const output = `${input}${injection}`;
18
+ const injection = `\n\nexport const ${DEFAULT_EXPORT_NAME} = ${JSON.stringify(css)};\n`;
19
+ const isStyleModule = this.resourcePath.endsWith('.css.ts');
20
+ const output = isStyleModule
21
+ ? `${injection}export default {};\n`
22
+ : `${input}${injection}`;
27
23
  return output;
28
24
  };
29
25
  exports.default = loader;
@@ -1,11 +1,6 @@
1
1
  import type { LoaderDefinitionFunction } from 'webpack';
2
2
  import { type CssOptions } from './css.cjs';
3
3
  export interface KnightedCssLoaderOptions extends CssOptions {
4
- /**
5
- * Named export that will contain the compiled CSS string.
6
- * Defaults to "knightedCss".
7
- */
8
- exportName?: string;
9
4
  }
10
5
  declare const loader: LoaderDefinitionFunction<KnightedCssLoaderOptions>;
11
6
  export default loader;
package/dist/css.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { DependencyTreeOptions } from 'dependency-tree';
1
+ import type { Options as DependencyTreeOpts } from 'dependency-tree';
2
2
  import { type TransformOptions as LightningTransformOptions } from 'lightningcss';
3
3
  export declare const DEFAULT_EXTENSIONS: string[];
4
4
  type LightningCssConfig = boolean | Partial<Omit<LightningTransformOptions<never>, 'code'>>;
@@ -11,7 +11,7 @@ export interface CssOptions {
11
11
  cwd?: string;
12
12
  filter?: (filePath: string) => boolean;
13
13
  lightningcss?: LightningCssConfig;
14
- dependencyTree?: Partial<Omit<DependencyTreeOptions, 'filename' | 'directory'>>;
14
+ dependencyTree?: Partial<Omit<DependencyTreeOpts, 'filename' | 'directory'>>;
15
15
  resolver?: CssResolver;
16
16
  peerResolver?: PeerLoader;
17
17
  }
package/dist/loader.d.ts CHANGED
@@ -1,11 +1,6 @@
1
1
  import type { LoaderDefinitionFunction } from 'webpack';
2
2
  import { type CssOptions } from './css.js';
3
3
  export interface KnightedCssLoaderOptions extends CssOptions {
4
- /**
5
- * Named export that will contain the compiled CSS string.
6
- * Defaults to "knightedCss".
7
- */
8
- exportName?: string;
9
4
  }
10
5
  declare const loader: LoaderDefinitionFunction<KnightedCssLoaderOptions>;
11
6
  export default loader;
package/dist/loader.js CHANGED
@@ -2,14 +2,7 @@ import { cssWithMeta } from './css.js';
2
2
  const DEFAULT_EXPORT_NAME = 'knightedCss';
3
3
  const loader = async function loader(source) {
4
4
  const rawOptions = (typeof this.getOptions === 'function' ? this.getOptions() : {});
5
- const queryParams = typeof this.resourceQuery === 'string' && this.resourceQuery.startsWith('?')
6
- ? new URLSearchParams(this.resourceQuery.slice(1))
7
- : undefined;
8
- const queryExportName = queryParams?.get('exportName')?.trim();
9
- const isValidIdentifier = typeof queryExportName === 'string' &&
10
- /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(queryExportName);
11
- const { exportName = DEFAULT_EXPORT_NAME, ...cssOptions } = rawOptions;
12
- const resolvedExportName = isValidIdentifier ? queryExportName : exportName;
5
+ const cssOptions = rawOptions;
13
6
  const normalizedOptions = {
14
7
  ...cssOptions,
15
8
  cwd: cssOptions.cwd ?? this.rootContext ?? process.cwd(),
@@ -20,8 +13,11 @@ const loader = async function loader(source) {
20
13
  this.addDependency(file);
21
14
  }
22
15
  const input = typeof source === 'string' ? source : source.toString('utf8');
23
- const injection = `\n\nexport const ${resolvedExportName} = ${JSON.stringify(css)};\n`;
24
- const output = `${input}${injection}`;
16
+ const injection = `\n\nexport const ${DEFAULT_EXPORT_NAME} = ${JSON.stringify(css)};\n`;
17
+ const isStyleModule = this.resourcePath.endsWith('.css.ts');
18
+ const output = isStyleModule
19
+ ? `${injection}export default {};\n`
20
+ : `${input}${injection}`;
25
21
  return output;
26
22
  };
27
23
  export default loader;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knighted/css",
3
- "version": "1.0.0-alpha.1",
3
+ "version": "1.0.0-alpha.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",
@@ -17,41 +17,26 @@
17
17
  "require": "./dist/cjs/loader.cjs"
18
18
  }
19
19
  },
20
- "engines": {
21
- "node": ">= 22.15.0",
22
- "npm": ">= 10.9.0"
23
- },
24
- "engineStrict": true,
25
20
  "keywords": [
26
21
  "css",
27
- "extract-css",
28
- "styles",
29
22
  "dependency-graph",
30
- "module-dependencies",
31
- "lit",
32
- "web-components",
33
- "shadow-dom",
23
+ "build-time",
24
+ "compile",
25
+ "styles",
34
26
  "sass",
35
27
  "scss",
36
28
  "less",
37
29
  "vanilla-extract",
38
- "lightningcss",
39
- "build-time",
40
- "static-analysis",
41
- "css-in-js-zero-runtime"
30
+ "lightningcss"
42
31
  ],
43
32
  "scripts": {
44
33
  "build": "duel",
45
34
  "check-types": "tsc --noEmit",
46
35
  "test": "c8 --reporter=text --reporter=text-summary --reporter=lcov --include \"src/**/*.ts\" tsx --test test/**/*.test.ts",
47
- "prettier": "prettier --write .",
48
- "prettier:check": "prettier --check .",
49
- "lint": "oxlint src test",
50
- "prepack": "npm run build",
51
- "prepare": "husky"
36
+ "prepack": "npm run build"
52
37
  },
53
38
  "dependencies": {
54
- "dependency-tree": "^9.0.0",
39
+ "dependency-tree": "^11.2.0",
55
40
  "lightningcss": "^1.30.2"
56
41
  },
57
42
  "peerDependencies": {
@@ -70,24 +55,6 @@
70
55
  "optional": true
71
56
  }
72
57
  },
73
- "devDependencies": {
74
- "@knighted/duel": "^2.1.6",
75
- "@rspack/core": "^1.0.0",
76
- "@types/less": "^3.0.8",
77
- "@types/webpack": "^5.28.5",
78
- "@vanilla-extract/css": "^1.15.2",
79
- "@vanilla-extract/integration": "^8.0.6",
80
- "@vanilla-extract/recipes": "^0.5.7",
81
- "c8": "^10.1.2",
82
- "husky": "^9.1.7",
83
- "less": "^4.2.0",
84
- "lint-staged": "^16.2.7",
85
- "oxlint": "^0.4.1",
86
- "prettier": "^3.7.4",
87
- "sass": "^1.80.7",
88
- "tsx": "^4.19.2",
89
- "typescript": "^5.9.3"
90
- },
91
58
  "files": [
92
59
  "dist"
93
60
  ],
@@ -99,15 +66,5 @@
99
66
  },
100
67
  "bugs": {
101
68
  "url": "https://github.com/knightedcodemonkey/css/issues"
102
- },
103
- "prettier": {
104
- "arrowParens": "avoid",
105
- "printWidth": 90,
106
- "semi": false,
107
- "singleQuote": true
108
- },
109
- "lint-staged": {
110
- "*.{js,jsx,ts,tsx,mjs,cjs,cts,mts}": "oxlint",
111
- "*.{js,jsx,ts,tsx,mjs,cjs,cts,mts,json,md,css,scss,html}": "prettier --check"
112
69
  }
113
70
  }
package/README.md DELETED
@@ -1,162 +0,0 @@
1
- # [`@knighted/css`](https://github.com/knightedcodemonkey/css)
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` is a build-time helper that walks a JavaScript/TypeScript module graph, finds every CSS-like dependency (plain CSS, Sass/SCSS, Less, vanilla-extract), compiles them, and returns a single concatenated stylesheet string. It is designed for workflows where you want fully materialized styles ahead of time—feeding Lit components, server-rendered routes, static site builds, or any pipeline that needs all CSS for a specific entry point without running a full bundler.
8
-
9
- ## Features
10
-
11
- - Traverses module graphs using [`dependency-tree`](https://github.com/dependents/node-dependency-tree) to find transitive style imports.
12
- - Compiles `*.css`, `*.scss`, `*.sass`, `*.less`, and `*.css.ts` (vanilla-extract) files out of the box.
13
- - Optional post-processing via [`lightningcss`](https://github.com/parcel-bundler/lightningcss) for minification, prefixing, and media query optimizations.
14
- - Pluggable resolver/filter hooks for custom module resolution (e.g., Rspack/Vite/webpack aliases) or selective inclusion.
15
- - Peer-resolution helper for optional toolchains (`sass`, `less`, `@vanilla-extract/integration`) so consumers control their dependency graph.
16
-
17
- ## Requirements
18
-
19
- - Node.js `>= 22.15.0`
20
- - npm `>= 10.9.0`
21
- - Install peer toolchains you intend to use (`sass`, `less`, `@vanilla-extract/integration`, `@vanilla-extract/recipes`, etc.).
22
-
23
- ## Installation
24
-
25
- ```bash
26
- npm install @knighted/css
27
- ```
28
-
29
- Install the peers your project is using, for example `less`, or `sass`, etc.
30
-
31
- ## Quick Start
32
-
33
- ```ts
34
- // scripts/extract-styles.ts
35
- import { css } from '@knighted/css'
36
-
37
- const styles = await css('./src/components/app.ts', {
38
- cwd: process.cwd(),
39
- lightningcss: { minify: true },
40
- })
41
-
42
- console.log(styles)
43
- ```
44
-
45
- 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.
46
-
47
- ## API
48
-
49
- ```ts
50
- type CssOptions = {
51
- extensions?: string[] // customize file extensions to scan
52
- cwd?: string // working directory (defaults to process.cwd())
53
- filter?: (filePath: string) => boolean
54
- lightningcss?: boolean | LightningTransformOptions
55
- dependencyTree?: DependencyTreeOptions
56
- resolver?: (
57
- specifier: string,
58
- ctx: { cwd: string },
59
- ) => string | Promise<string | undefined>
60
- peerResolver?: (name: string) => Promise<unknown> // for custom module loading
61
- }
62
-
63
- async function css(entry: string, options?: CssOptions): Promise<string>
64
- ```
65
-
66
- Typical customizations:
67
-
68
- - **filter** – Skip certain paths (e.g., storybook-only styles) before compilation.
69
- - **resolver** – Resolve virtual specifiers the way your bundler does (the repo ships test fixtures for webpack, Vite, and Rspack).
70
- - **lightningcss** – Pass `true` for defaults or a config object for minification/autoprefixing.
71
-
72
- ## Examples
73
-
74
- ### Generate standalone stylesheets
75
-
76
- ```ts
77
- import { writeFile } from 'node:fs/promises'
78
- import { css } from '@knighted/css'
79
-
80
- // Build-time script that gathers all CSS imported by a React route
81
- const sheet = await css('./src/routes/marketing-page.tsx', {
82
- lightningcss: { minify: true, targets: { chrome: 120, safari: 17 } },
83
- })
84
-
85
- await writeFile('./dist/marketing-page.css', sheet)
86
- ```
87
-
88
- ### Inline CSS during SSR
89
-
90
- ```ts
91
- import { renderToString } from 'react-dom/server'
92
- import { css } from '@knighted/css'
93
-
94
- export async function render(url: string) {
95
- const styles = await css('./src/routes/root.tsx')
96
- const html = renderToString(<App url={url} />)
97
- return `<!doctype html><style>${styles}</style>${html}`
98
- }
99
- ```
100
-
101
- ### Bundler loader (`?knighted-css` query)
102
-
103
- When using Webpack/Rspack, add the provided loader so importing a module with a specific query also returns the compiled stylesheet:
104
-
105
- ```js
106
- // webpack.config.js
107
- module.exports = {
108
- module: {
109
- rules: [
110
- {
111
- test: /\.[jt]sx?$/,
112
- resourceQuery: /knighted-css/,
113
- use: [
114
- {
115
- loader: '@knighted/css/loader',
116
- options: {
117
- exportName: 'reactStyles', // optional (default: "knightedCss")
118
- lightningcss: { minify: true }, // all css() options supported
119
- },
120
- },
121
- ],
122
- },
123
- ],
124
- },
125
- }
126
- ```
127
-
128
- ```ts
129
- // lit wrapper
130
- import { LitElement, html, unsafeCSS } from 'lit'
131
- import { Button, reactStyles } from './button.tsx?knighted-css'
132
-
133
- export class ButtonWrapper extends LitElement {
134
- static styles = [unsafeCSS(reactStyles)]
135
- render() {
136
- return html`<${Button} />`
137
- }
138
- }
139
-
140
- // per-import override:
141
- // import { Button, cardCss } from './button.tsx?knighted-css&exportName=cardCss'
142
- // (exportName in the query wins over the loader option)
143
- ```
144
-
145
- The loader appends `export const reactStyles = "/* compiled css */"` to the module, so you can wire it directly into Lit’s `static styles` or any other runtime.
146
-
147
- ## Scripts
148
-
149
- - `npm run build` – Produce CJS/ESM outputs via `@knighted/duel`.
150
- - `npm test` – Runs the Node test suite with `tsx` and reports coverage via `c8`.
151
- - `npm run lint` – Static analysis through `oxlint`.
152
-
153
- ## Contributing
154
-
155
- 1. Clone the repo and install dependencies with `npm install`.
156
- 2. Run `npm test` to ensure fixtures compile across Sass/Less/vanilla-extract.
157
- 3. Add/adjust fixtures in `fixtures/` when adding new language features to keep coverage high.
158
- 4. Open a PR with a description of the change and tests.
159
-
160
- ## License
161
-
162
- MIT © Knighted Code Monkey