next-style 2.2.1 → 2.2.2
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.
- package/README.md +43 -10
- package/dist/index.cjs +24 -24
- package/dist/index.d.ts +204 -1
- package/dist/index.js +21 -21
- package/dist/postcss-plugin/index.cjs +100 -70
- package/dist/postcss-plugin/index.d.ts +1 -6
- package/dist/postcss-plugin/index.js +100 -70
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -246,6 +246,7 @@ All CSS properties and values are typed via [csstype](https://github.com/frenic/
|
|
|
246
246
|
```tsx
|
|
247
247
|
import { css, type CSSObject } from "next-style"
|
|
248
248
|
|
|
249
|
+
// Type a reusable style object before passing it to css()
|
|
249
250
|
const base: CSSObject = {
|
|
250
251
|
fontSize: "16px", // ✅
|
|
251
252
|
colour: "red", // ❌ TypeScript error: unknown property
|
|
@@ -262,7 +263,48 @@ const el = css(base)
|
|
|
262
263
|
| `CSSObject` | Full style object — properties, at-rules, and pseudos |
|
|
263
264
|
| `CSSProperties` | CSS properties only, no at-rules or pseudos |
|
|
264
265
|
|
|
265
|
-
##
|
|
266
|
+
## Advanced
|
|
267
|
+
|
|
268
|
+
### `createTransformer`
|
|
269
|
+
|
|
270
|
+
For build tooling, SWC/Babel plugins, and test harnesses that need an isolated style collector independent of the global runtime:
|
|
271
|
+
|
|
272
|
+
```ts
|
|
273
|
+
import { createTransformer } from "next-style"
|
|
274
|
+
|
|
275
|
+
const { collector, transformCssCall } = createTransformer()
|
|
276
|
+
|
|
277
|
+
const className = transformCssCall({ color: "red", fontSize: "16px" })
|
|
278
|
+
// → "ns-abc123"
|
|
279
|
+
|
|
280
|
+
const css = collector.getAllStyles()
|
|
281
|
+
// → ".ns-abc123 { color: red; font-size: 16px; }"
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### `StyleCollector`
|
|
285
|
+
|
|
286
|
+
The class powering both the runtime and `createTransformer`. Exposed for custom integrations:
|
|
287
|
+
|
|
288
|
+
```ts
|
|
289
|
+
import { StyleCollector } from "next-style"
|
|
290
|
+
|
|
291
|
+
const collector = new StyleCollector()
|
|
292
|
+
collector.addStyle({ color: "red" }) // → "ns-abc123"
|
|
293
|
+
collector.addGlobalStyle("body", { margin: "0" })
|
|
294
|
+
collector.getAllStyles() // full CSS string
|
|
295
|
+
collector.flush("/custom/path/styles.css") // write to disk
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### How the PostCSS bridge works
|
|
299
|
+
|
|
300
|
+
Because PostCSS runs in a separate process from the module graph, in-memory style collectors cannot be shared. next-style solves this with a file-based bridge:
|
|
301
|
+
|
|
302
|
+
1. Every `css()` / `global()` call writes compiled CSS to `node_modules/.cache/next-style.css`
|
|
303
|
+
2. The PostCSS plugin reads that file and replaces `@import "next-style"` with its contents
|
|
304
|
+
|
|
305
|
+
This is why `@import "next-style"` must appear in `globals.css` — it's the injection point.
|
|
306
|
+
|
|
307
|
+
### CSS Variables
|
|
266
308
|
|
|
267
309
|
next-style pairs naturally with CSS custom properties for design tokens:
|
|
268
310
|
|
|
@@ -286,15 +328,6 @@ const card = css({
|
|
|
286
328
|
})
|
|
287
329
|
```
|
|
288
330
|
|
|
289
|
-
## How it works
|
|
290
|
-
|
|
291
|
-
Because PostCSS runs in a separate process from the module graph, next-style uses a file-based bridge:
|
|
292
|
-
|
|
293
|
-
1. Every `css()` / `global()` call registers styles into an in-memory collector during module evaluation
|
|
294
|
-
2. The PostCSS plugin reads the collector and replaces `@import "next-style"` with the compiled CSS
|
|
295
|
-
|
|
296
|
-
This is why `@import "next-style"` must appear in `globals.css` — it's the injection point.
|
|
297
|
-
|
|
298
331
|
## CSS output order
|
|
299
332
|
|
|
300
333
|
Styles are emitted in this order to ensure correct cascade:
|
package/dist/index.cjs
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
${
|
|
3
|
-
${
|
|
1
|
+
"use strict";var H=Object.create;var $=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var J=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty;var Z=(s,e)=>{for(var t in e)$(s,t,{get:e[t],enumerable:!0})},N=(s,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of G(e))!L.call(s,r)&&r!==t&&$(s,r,{get:()=>e[r],enumerable:!(n=k(e,r))||n.enumerable});return s};var Q=(s,e,t)=>(t=s!=null?H(J(s)):{},N(e||!s||!s.__esModule?$(t,"default",{value:s,enumerable:!0}):t,s)),_=s=>N($({},"__esModule",{value:!0}),s);var v={};Z(v,{BREAKPOINTS:()=>f,StyleCollector:()=>p,camelToKebab:()=>R,createTransformer:()=>K,css:()=>z,deduplicateStyles:()=>T,generateClassHash:()=>m,global:()=>F,normalizeMediaQuery:()=>x,styleCollector:()=>g,validateCSSProperty:()=>W});module.exports=_(v);var j=Q(require("fs"),1),E=Q(require("path"),1);function R(s){return s.replace(/[A-Z]/g,e=>`-${e.toLowerCase()}`)}function m(s){let e=typeof s=="string"?s:JSON.stringify(s),t=0;for(let n=0;n<e.length;n++){let r=e.charCodeAt(n);t=(t<<5)-t+r,t=t&t}return Math.abs(t).toString(36)}var f={"@sm":"@media (min-width: 640px)","@md":"@media (min-width: 768px)","@lg":"@media (min-width: 1024px)","@xl":"@media (min-width: 1280px)","@2xl":"@media (min-width: 1536px)"};function x(s){return f[s]??s}function W(s,e){return typeof e=="string"||typeof e=="number"||typeof e=="object"&&!Array.isArray(e)}function T(s){let e=new Map,t=new Set;return s.forEach((n,r)=>{let l=JSON.stringify(n);t.has(l)||(t.add(l),e.set(r,n))}),e}var p=class s{constructor(){this.styles=new Map}addStyle(e){let n=`ns-${m(e)}`;if(this.styles.has(n))return n;let r=this.compileStyle(e);return this.styles.set(n,r),n}addGlobalStyle(e,t){let n=`global:${e}`;if(this.styles.has(n))return;let r=this.buildDeclarations(t);r&&this.styles.set(n,{className:n,css:`${e} {
|
|
2
|
+
${r}}`,mediaQueries:{},pseudoClasses:{},keyframes:"",supports:{},layers:{}})}compileStyle(e){let{mediaQueries:t,pseudoClasses:n,normalStyles:r,keyframes:l,supports:h,container:u,layer:S}=this.parseStyles(e),o=`ns-${m(e)}`,P=this.buildDeclarations(r),I=`.${o} {
|
|
3
|
+
${P}}`,y={};Object.entries(t).forEach(([c,d])=>{let a=x(c),C=this.buildDeclarations(d," ");if(y[a]){let B=y[a].split(`
|
|
4
4
|
`).slice(2,-2).join(`
|
|
5
|
-
`);
|
|
5
|
+
`);y[a]=`${a} {
|
|
6
6
|
.${o} {
|
|
7
|
-
${
|
|
8
|
-
${
|
|
9
|
-
}`}else
|
|
7
|
+
${B}
|
|
8
|
+
${C} }
|
|
9
|
+
}`}else y[a]=`${a} {
|
|
10
10
|
.${o} {
|
|
11
|
-
${
|
|
12
|
-
}`}),Object.entries(
|
|
11
|
+
${C} }
|
|
12
|
+
}`}),Object.entries(u).forEach(([c,d])=>{let a=this.buildDeclarations(d," ");y[c]=`${c} {
|
|
13
13
|
.${o} {
|
|
14
14
|
${a} }
|
|
15
|
-
}`});let
|
|
16
|
-
${a}}`});let
|
|
17
|
-
`,Object.entries(d).forEach(([a,
|
|
18
|
-
${
|
|
19
|
-
`}),
|
|
20
|
-
`});let
|
|
15
|
+
}`});let w={};Object.entries(n).forEach(([c,d])=>{let a=this.buildDeclarations(d);w[c]=`.${o}${c} {
|
|
16
|
+
${a}}`});let b="";Object.entries(l).forEach(([c,d])=>{b+=`@keyframes ${c} {
|
|
17
|
+
`,Object.entries(d).forEach(([a,C])=>{let D=this.buildDeclarations(C," ");b+=` ${a} {
|
|
18
|
+
${D} }
|
|
19
|
+
`}),b+=`}
|
|
20
|
+
`});let M={};Object.entries(h).forEach(([c,d])=>{let a=this.buildDeclarations(d," ");M[c]=`@supports ${c} {
|
|
21
21
|
.${o} {
|
|
22
22
|
${a} }
|
|
23
|
-
}`});let
|
|
24
|
-
${a}}`}),{className:o,css:
|
|
25
|
-
`)}),
|
|
26
|
-
`),
|
|
27
|
-
`),Object.values(
|
|
28
|
-
`}),Object.values(
|
|
29
|
-
`}),Object.values(
|
|
30
|
-
`}),Object.entries(
|
|
31
|
-
`})}),e}extractMinWidth(e){let
|
|
23
|
+
}`});let A={};return Object.entries(S).forEach(([c,d])=>{let a=this.buildDeclarations(d," ");A[c]=`@layer ${c} {
|
|
24
|
+
${a}}`}),{className:o,css:I,mediaQueries:y,pseudoClasses:w,keyframes:b,supports:M,layers:A}}parseStyles(e){let t={},n={},r={},l={},h={},u={},S={};return Object.entries(e).forEach(([i,o])=>{if(i.startsWith("@keyframes "))l[i.slice(11)]=o;else if(i==="@keyframes"&&typeof o=="object")Object.assign(l,o);else if(i.startsWith("@supports")){let P=i.slice(0,9)==="@supports"?i.slice(9).trim():i;h[P]=o}else i.startsWith("@container")?u[i]=o:i.startsWith("@layer")?S[i.slice(7)||"default"]=o:i in f||i.startsWith("@media")?n[i]=o:i.startsWith(":")||i.startsWith("::")?r[i]=o:t[i]=o}),{normalStyles:t,mediaQueries:n,pseudoClasses:r,keyframes:l,supports:h,container:u,layer:S}}buildDeclarations(e,t=" "){let n="";return Object.entries(e).forEach(([r,l])=>{(typeof l=="string"||typeof l=="number")&&(n+=`${t}${R(r)}: ${l};
|
|
25
|
+
`)}),n}getAllStyles(){let e="";return this.styles.forEach(t=>{t.keyframes&&(e+=`${t.keyframes}
|
|
26
|
+
`),t.css&&(e+=`${t.css}
|
|
27
|
+
`),Object.values(t.pseudoClasses).forEach(r=>{e+=`${r}
|
|
28
|
+
`}),Object.values(t.layers).forEach(r=>{e+=`${r}
|
|
29
|
+
`}),Object.values(t.supports).forEach(r=>{e+=`${r}
|
|
30
|
+
`}),Object.entries(t.mediaQueries).sort(([r],[l])=>this.extractMinWidth(r)-this.extractMinWidth(l)).forEach(([,r])=>{e+=`${r}
|
|
31
|
+
`})}),e}extractMinWidth(e){let t=e.match(/min-width:\s*(\d+)px/);return t?parseInt(t[1],10):0}flush(e){try{let t=e??s.defaultCacheFile();j.default.mkdirSync(E.default.dirname(t),{recursive:!0}),j.default.writeFileSync(t,this.getAllStyles(),"utf-8")}catch(t){console.error("Failed to flush styles to cache file:",t)}}static defaultCacheFile(){return E.default.join(process.cwd(),"node_modules",".cache","next-style","styles.css")}getStyleMap(){return new Map(this.styles)}};function K(){let s=new p;return{collector:s,transformCssCall(e){return s.addStyle(e)}}}function z(s){let e=g.addStyle(s);return g.flush(),e}function F(s){Object.entries(s).forEach(([e,t])=>{g.addGlobalStyle(e,t)}),g.flush()}var g=new p;0&&(module.exports={BREAKPOINTS,StyleCollector,camelToKebab,createTransformer,css,deduplicateStyles,generateClassHash,global,normalizeMediaQuery,styleCollector,validateCSSProperty});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,126 @@
|
|
|
1
1
|
import { Properties, Pseudos } from 'csstype';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* The compiled representation of a single `css({})` call.
|
|
5
|
+
* Produced by `StyleCollector` and consumed by the PostCSS plugin.
|
|
6
|
+
*/
|
|
7
|
+
interface CompiledStyle {
|
|
8
|
+
/** Scoped class name, e.g. `"ns-1x2y3z"`. */
|
|
9
|
+
className: string;
|
|
10
|
+
/** Base CSS rule block for this class. */
|
|
11
|
+
css: string;
|
|
12
|
+
/** Map of normalized media query string → CSS rule block. */
|
|
13
|
+
mediaQueries: Record<string, string>;
|
|
14
|
+
/** Map of pseudo selector (e.g. `":hover"`) → CSS rule block. */
|
|
15
|
+
pseudoClasses: Record<string, string>;
|
|
16
|
+
/** Concatenated `@keyframes` blocks referenced by this style. */
|
|
17
|
+
keyframes: string;
|
|
18
|
+
/** Map of `@supports` condition → CSS rule block. */
|
|
19
|
+
supports: Record<string, string>;
|
|
20
|
+
/** Map of `@layer` name → CSS rule block. */
|
|
21
|
+
layers: Record<string, string>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Collects, compiles, and deduplicates styles registered via `css()` and `global()`.
|
|
25
|
+
*
|
|
26
|
+
* A single shared instance is held by the runtime module. The PostCSS plugin
|
|
27
|
+
* calls `getAllStyles()` at build time to drain the collected CSS.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* const collector = new StyleCollector()
|
|
31
|
+
* const className = collector.addStyle({ color: 'red' }) // "ns-abc123"
|
|
32
|
+
* const css = collector.getAllStyles() // ".ns-abc123 { color: red; }"
|
|
33
|
+
*/
|
|
34
|
+
declare class StyleCollector {
|
|
35
|
+
private styles;
|
|
36
|
+
/**
|
|
37
|
+
* Compiles a style object and registers it in the collector.
|
|
38
|
+
* Returns the same class name for identical style objects (deduplication).
|
|
39
|
+
*
|
|
40
|
+
* @param styleObj - Raw style object from a `css({})` call.
|
|
41
|
+
* @returns Scoped class name string.
|
|
42
|
+
*/
|
|
43
|
+
addStyle(styleObj: any): string;
|
|
44
|
+
/**
|
|
45
|
+
* Registers a global style rule (no scoped class).
|
|
46
|
+
* Subsequent calls with the same selector are ignored (idempotent).
|
|
47
|
+
*
|
|
48
|
+
* @param selector - CSS selector string, e.g. `"body"` or `"h1, h2"`.
|
|
49
|
+
* @param styleObj - Style properties to apply to the selector.
|
|
50
|
+
*/
|
|
51
|
+
addGlobalStyle(selector: string, styleObj: any): void;
|
|
52
|
+
private compileStyle;
|
|
53
|
+
private parseStyles;
|
|
54
|
+
private buildDeclarations;
|
|
55
|
+
/**
|
|
56
|
+
* Serialises all collected styles into a single CSS string.
|
|
57
|
+
*
|
|
58
|
+
* Output order:
|
|
59
|
+
* 1. `@keyframes` blocks
|
|
60
|
+
* 2. Base class rules
|
|
61
|
+
* 3. Pseudo-class/element rules
|
|
62
|
+
* 4. `@layer` blocks
|
|
63
|
+
* 5. `@supports` blocks
|
|
64
|
+
* 6. Media queries (ascending `min-width`, mobile-first)
|
|
65
|
+
*
|
|
66
|
+
* @returns Full CSS string ready to be injected or written to a file.
|
|
67
|
+
*/
|
|
68
|
+
getAllStyles(): string;
|
|
69
|
+
private extractMinWidth;
|
|
70
|
+
/**
|
|
71
|
+
* Writes all collected styles to a temp file so the PostCSS plugin
|
|
72
|
+
* (which runs in a separate process) can read them.
|
|
73
|
+
*
|
|
74
|
+
* Call this after all `css()` / `global()` calls have been evaluated,
|
|
75
|
+
* e.g. at the end of a build-time entry point.
|
|
76
|
+
*
|
|
77
|
+
* @param filePath - Destination file path. Defaults to `os.tmpdir()/next-style.css`.
|
|
78
|
+
*/
|
|
79
|
+
flush(filePath?: string): void;
|
|
80
|
+
/**
|
|
81
|
+
* Returns the default cache file path, resolved at call-time from
|
|
82
|
+
* `process.cwd()` so it reflects the actual working directory of the
|
|
83
|
+
* running process rather than the directory at module-load time.
|
|
84
|
+
*/
|
|
85
|
+
static defaultCacheFile(): string;
|
|
86
|
+
/**
|
|
87
|
+
* Returns a snapshot of the internal style map.
|
|
88
|
+
* Intended for inspection and testing — not for direct mutation.
|
|
89
|
+
*/
|
|
90
|
+
getStyleMap(): Map<string, CompiledStyle>;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Creates an isolated `StyleCollector` paired with a transform helper.
|
|
94
|
+
*
|
|
95
|
+
* Primarily used by SWC/Babel transforms and test harnesses that need
|
|
96
|
+
* a fresh collector per file or per test, independent of the shared
|
|
97
|
+
* runtime instance.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* const { collector, transformCssCall } = createTransformer()
|
|
101
|
+
* const className = transformCssCall({ color: 'red' })
|
|
102
|
+
* const css = collector.getAllStyles()
|
|
103
|
+
*/
|
|
104
|
+
declare function createTransformer(): {
|
|
105
|
+
collector: StyleCollector;
|
|
106
|
+
/**
|
|
107
|
+
* Equivalent to calling `css()` against this transformer's isolated collector.
|
|
108
|
+
* @param styleObj - Raw style object.
|
|
109
|
+
* @returns Scoped class name string.
|
|
110
|
+
*/
|
|
111
|
+
transformCssCall(styleObj: any): string;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* CSS properties with full type safety and autocomplete via csstype.
|
|
116
|
+
* Accepts both string and number values (e.g. `fontSize: 16` or `fontSize: '16px'`).
|
|
117
|
+
*/
|
|
3
118
|
type CSSProperties = Properties<string | number>;
|
|
119
|
+
/** @internal Mapped type for pseudo-classes/elements — avoids index signature limitation */
|
|
4
120
|
type PseudoStyles = {
|
|
5
121
|
[P in Pseudos]?: CSSProperties;
|
|
6
122
|
};
|
|
123
|
+
/** @internal At-rule keys (@media, @container, @supports, @layer, @keyframes) */
|
|
7
124
|
type AtRuleStyles = {
|
|
8
125
|
'@sm'?: CSSProperties;
|
|
9
126
|
'@md'?: CSSProperties;
|
|
@@ -28,33 +145,119 @@ type AtRuleStyles = {
|
|
|
28
145
|
* - Feature queries: `'@supports (display: grid)'`
|
|
29
146
|
* - Keyframe animations: `'@keyframes fadeIn'`
|
|
30
147
|
* - Cascade layers: `'@layer utilities'`
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* const style: CSSObject = {
|
|
151
|
+
* fontSize: '16px',
|
|
152
|
+
* '@md': { fontSize: '20px' },
|
|
153
|
+
* ':hover': { color: 'blue' },
|
|
154
|
+
* '@keyframes spin': { to: { transform: 'rotate(360deg)' } },
|
|
155
|
+
* }
|
|
31
156
|
*/
|
|
32
157
|
type CSSObject = CSSProperties & PseudoStyles & AtRuleStyles & {
|
|
33
158
|
[key: string]: any;
|
|
34
159
|
};
|
|
35
160
|
/**
|
|
36
161
|
* Converts a style object into a unique CSS class name.
|
|
162
|
+
*
|
|
37
163
|
* Styles are collected at build time by the PostCSS plugin and emitted as
|
|
38
164
|
* a single static CSS file — there is zero runtime overhead in production.
|
|
165
|
+
* Identical style objects always return the same class name (deduplication).
|
|
166
|
+
*
|
|
167
|
+
* @param styles - A `CSSObject` describing the styles for this element.
|
|
168
|
+
* @returns A stable class name string (e.g. `"ns-1x2y3z"`).
|
|
39
169
|
*
|
|
40
170
|
* @example
|
|
41
171
|
* const button = css({
|
|
42
172
|
* padding: '8px 16px',
|
|
173
|
+
* borderRadius: '6px',
|
|
43
174
|
* backgroundColor: '#7F77DD',
|
|
44
175
|
* ':hover': { backgroundColor: '#534AB7' },
|
|
45
176
|
* '@md': { padding: '10px 20px' },
|
|
46
177
|
* })
|
|
178
|
+
*
|
|
179
|
+
* export function Button() {
|
|
180
|
+
* return <button className={button}>Click me</button>
|
|
181
|
+
* }
|
|
47
182
|
*/
|
|
48
183
|
declare function css(styles: CSSObject): string;
|
|
49
184
|
/**
|
|
50
185
|
* Registers global CSS styles applied directly to selectors (no scoped class).
|
|
51
186
|
*
|
|
187
|
+
* Useful for CSS resets, base typography, and third-party element overrides.
|
|
188
|
+
* Like `css()`, styles are extracted at build time — no runtime cost.
|
|
189
|
+
*
|
|
190
|
+
* @param styles - A record mapping CSS selectors to `CSSObject` style definitions.
|
|
191
|
+
*
|
|
52
192
|
* @example
|
|
53
193
|
* global({
|
|
54
194
|
* '*': { boxSizing: 'border-box', margin: '0' },
|
|
55
195
|
* 'body': { fontFamily: 'Inter, sans-serif', lineHeight: '1.6' },
|
|
196
|
+
* 'h1, h2, h3': { fontWeight: 500 },
|
|
56
197
|
* })
|
|
57
198
|
*/
|
|
58
199
|
declare function global(styles: Record<string, CSSObject>): void;
|
|
59
200
|
|
|
60
|
-
|
|
201
|
+
declare const collector: StyleCollector;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Converts a camelCase CSS property name to kebab-case.
|
|
205
|
+
*
|
|
206
|
+
* @param str - camelCase string, e.g. `"backgroundColor"`.
|
|
207
|
+
* @returns kebab-case string, e.g. `"background-color"`.
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* camelToKebab('fontSize') // "font-size"
|
|
211
|
+
* camelToKebab('borderTopWidth') // "border-top-width"
|
|
212
|
+
*/
|
|
213
|
+
declare function camelToKebab(str: string): string;
|
|
214
|
+
/**
|
|
215
|
+
* Generates a short, stable hash string from a style object or string.
|
|
216
|
+
* Used to produce unique class name suffixes (e.g. `"ns-1x2y3z"`).
|
|
217
|
+
*
|
|
218
|
+
* @param styles - Style object or pre-serialised string.
|
|
219
|
+
* @returns Base-36 hash string.
|
|
220
|
+
*/
|
|
221
|
+
declare function generateClassHash(styles: any): string;
|
|
222
|
+
/**
|
|
223
|
+
* Shorthand breakpoint aliases mapped to their full `@media` query strings.
|
|
224
|
+
*
|
|
225
|
+
* | Key | Expands to |
|
|
226
|
+
* |--------|-------------------------------------|
|
|
227
|
+
* | `@sm` | `@media (min-width: 640px)` |
|
|
228
|
+
* | `@md` | `@media (min-width: 768px)` |
|
|
229
|
+
* | `@lg` | `@media (min-width: 1024px)` |
|
|
230
|
+
* | `@xl` | `@media (min-width: 1280px)` |
|
|
231
|
+
* | `@2xl` | `@media (min-width: 1536px)` |
|
|
232
|
+
*/
|
|
233
|
+
declare const BREAKPOINTS: Record<string, string>;
|
|
234
|
+
/**
|
|
235
|
+
* Resolves a breakpoint shorthand to its full `@media` query string.
|
|
236
|
+
* Passes through unrecognised strings unchanged.
|
|
237
|
+
*
|
|
238
|
+
* @param query - Shorthand key (e.g. `"@md"`) or an arbitrary media query.
|
|
239
|
+
* @returns Full media query string (e.g. `"@media (min-width: 768px)"`).
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* normalizeMediaQuery('@md') // "@media (min-width: 768px)"
|
|
243
|
+
* normalizeMediaQuery('@media (max-width: 600px)') // "@media (max-width: 600px)"
|
|
244
|
+
*/
|
|
245
|
+
declare function normalizeMediaQuery(query: string): string;
|
|
246
|
+
/**
|
|
247
|
+
* Returns `true` if the value is a valid CSS property value that can be
|
|
248
|
+
* serialised to a declaration (string, number, or nested object).
|
|
249
|
+
*
|
|
250
|
+
* @param _key - CSS property name (unused, reserved for future validation).
|
|
251
|
+
* @param value - Value to check.
|
|
252
|
+
*/
|
|
253
|
+
declare function validateCSSProperty(_key: string, value: any): boolean;
|
|
254
|
+
/**
|
|
255
|
+
* Removes duplicate entries from a style map, keeping the first occurrence.
|
|
256
|
+
* Two entries are considered duplicates when their serialised values are identical.
|
|
257
|
+
*
|
|
258
|
+
* @param styles - Map of class name → style data.
|
|
259
|
+
* @returns New map with duplicates removed.
|
|
260
|
+
*/
|
|
261
|
+
declare function deduplicateStyles(styles: Map<string, any>): Map<string, any>;
|
|
262
|
+
|
|
263
|
+
export { BREAKPOINTS, type CSSObject, type CSSProperties, type CompiledStyle, StyleCollector, camelToKebab, createTransformer, css, deduplicateStyles, generateClassHash, global, normalizeMediaQuery, collector as styleCollector, validateCSSProperty };
|
package/dist/index.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
${
|
|
3
|
-
${$}}`,p={};Object.entries(
|
|
1
|
+
import A from"fs";import D from"path";function P(n){return n.replace(/[A-Z]/g,e=>`-${e.toLowerCase()}`)}function b(n){let e=typeof n=="string"?n:JSON.stringify(n),t=0;for(let r=0;r<e.length;r++){let s=e.charCodeAt(r);t=(t<<5)-t+s,t=t&t}return Math.abs(t).toString(36)}var C={"@sm":"@media (min-width: 640px)","@md":"@media (min-width: 768px)","@lg":"@media (min-width: 1024px)","@xl":"@media (min-width: 1280px)","@2xl":"@media (min-width: 1536px)"};function O(n){return C[n]??n}function W(n,e){return typeof e=="string"||typeof e=="number"||typeof e=="object"&&!Array.isArray(e)}function T(n){let e=new Map,t=new Set;return n.forEach((r,s)=>{let l=JSON.stringify(r);t.has(l)||(t.add(l),e.set(s,r))}),e}var y=class n{constructor(){this.styles=new Map}addStyle(e){let r=`ns-${b(e)}`;if(this.styles.has(r))return r;let s=this.compileStyle(e);return this.styles.set(r,s),r}addGlobalStyle(e,t){let r=`global:${e}`;if(this.styles.has(r))return;let s=this.buildDeclarations(t);s&&this.styles.set(r,{className:r,css:`${e} {
|
|
2
|
+
${s}}`,mediaQueries:{},pseudoClasses:{},keyframes:"",supports:{},layers:{}})}compileStyle(e){let{mediaQueries:t,pseudoClasses:r,normalStyles:s,keyframes:l,supports:m,container:f,layer:h}=this.parseStyles(e),o=`ns-${b(e)}`,$=this.buildDeclarations(s),N=`.${o} {
|
|
3
|
+
${$}}`,p={};Object.entries(t).forEach(([c,d])=>{let a=O(c),S=this.buildDeclarations(d," ");if(p[a]){let Q=p[a].split(`
|
|
4
4
|
`).slice(2,-2).join(`
|
|
5
5
|
`);p[a]=`${a} {
|
|
6
6
|
.${o} {
|
|
7
|
-
${
|
|
8
|
-
${
|
|
7
|
+
${Q}
|
|
8
|
+
${S} }
|
|
9
9
|
}`}else p[a]=`${a} {
|
|
10
10
|
.${o} {
|
|
11
|
-
${
|
|
11
|
+
${S} }
|
|
12
12
|
}`}),Object.entries(f).forEach(([c,d])=>{let a=this.buildDeclarations(d," ");p[c]=`${c} {
|
|
13
13
|
.${o} {
|
|
14
14
|
${a} }
|
|
15
|
-
}`});let
|
|
16
|
-
${a}}`});let
|
|
17
|
-
`,Object.entries(d).forEach(([a,
|
|
18
|
-
${
|
|
19
|
-
`}),
|
|
20
|
-
`});let
|
|
15
|
+
}`});let j={};Object.entries(r).forEach(([c,d])=>{let a=this.buildDeclarations(d);j[c]=`.${o}${c} {
|
|
16
|
+
${a}}`});let u="";Object.entries(l).forEach(([c,d])=>{u+=`@keyframes ${c} {
|
|
17
|
+
`,Object.entries(d).forEach(([a,S])=>{let M=this.buildDeclarations(S," ");u+=` ${a} {
|
|
18
|
+
${M} }
|
|
19
|
+
`}),u+=`}
|
|
20
|
+
`});let E={};Object.entries(m).forEach(([c,d])=>{let a=this.buildDeclarations(d," ");E[c]=`@supports ${c} {
|
|
21
21
|
.${o} {
|
|
22
22
|
${a} }
|
|
23
|
-
}`});let
|
|
24
|
-
${a}}`}),{className:o,css:
|
|
25
|
-
`)}),r}getAllStyles(){let e="";return this.styles.forEach(
|
|
26
|
-
`),
|
|
27
|
-
`),Object.values(
|
|
28
|
-
`}),Object.values(
|
|
29
|
-
`}),Object.values(
|
|
30
|
-
`}),Object.entries(
|
|
31
|
-
`})}),e}extractMinWidth(e){let
|
|
23
|
+
}`});let w={};return Object.entries(h).forEach(([c,d])=>{let a=this.buildDeclarations(d," ");w[c]=`@layer ${c} {
|
|
24
|
+
${a}}`}),{className:o,css:N,mediaQueries:p,pseudoClasses:j,keyframes:u,supports:E,layers:w}}parseStyles(e){let t={},r={},s={},l={},m={},f={},h={};return Object.entries(e).forEach(([i,o])=>{if(i.startsWith("@keyframes "))l[i.slice(11)]=o;else if(i==="@keyframes"&&typeof o=="object")Object.assign(l,o);else if(i.startsWith("@supports")){let $=i.slice(0,9)==="@supports"?i.slice(9).trim():i;m[$]=o}else i.startsWith("@container")?f[i]=o:i.startsWith("@layer")?h[i.slice(7)||"default"]=o:i in C||i.startsWith("@media")?r[i]=o:i.startsWith(":")||i.startsWith("::")?s[i]=o:t[i]=o}),{normalStyles:t,mediaQueries:r,pseudoClasses:s,keyframes:l,supports:m,container:f,layer:h}}buildDeclarations(e,t=" "){let r="";return Object.entries(e).forEach(([s,l])=>{(typeof l=="string"||typeof l=="number")&&(r+=`${t}${P(s)}: ${l};
|
|
25
|
+
`)}),r}getAllStyles(){let e="";return this.styles.forEach(t=>{t.keyframes&&(e+=`${t.keyframes}
|
|
26
|
+
`),t.css&&(e+=`${t.css}
|
|
27
|
+
`),Object.values(t.pseudoClasses).forEach(s=>{e+=`${s}
|
|
28
|
+
`}),Object.values(t.layers).forEach(s=>{e+=`${s}
|
|
29
|
+
`}),Object.values(t.supports).forEach(s=>{e+=`${s}
|
|
30
|
+
`}),Object.entries(t.mediaQueries).sort(([s],[l])=>this.extractMinWidth(s)-this.extractMinWidth(l)).forEach(([,s])=>{e+=`${s}
|
|
31
|
+
`})}),e}extractMinWidth(e){let t=e.match(/min-width:\s*(\d+)px/);return t?parseInt(t[1],10):0}flush(e){try{let t=e??n.defaultCacheFile();A.mkdirSync(D.dirname(t),{recursive:!0}),A.writeFileSync(t,this.getAllStyles(),"utf-8")}catch(t){console.error("Failed to flush styles to cache file:",t)}}static defaultCacheFile(){return D.join(process.cwd(),"node_modules",".cache","next-style","styles.css")}getStyleMap(){return new Map(this.styles)}};function K(){let n=new y;return{collector:n,transformCssCall(e){return n.addStyle(e)}}}function z(n){let e=g.addStyle(n);return g.flush(),e}function F(n){Object.entries(n).forEach(([e,t])=>{g.addGlobalStyle(e,t)}),g.flush()}var g=new y;export{C as BREAKPOINTS,y as StyleCollector,P as camelToKebab,K as createTransformer,z as css,T as deduplicateStyles,b as generateClassHash,F as global,O as normalizeMediaQuery,g as styleCollector,W as validateCSSProperty};
|