@bamboocss/generator 1.62.1 → 1.64.1

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 (3) hide show
  1. package/dist/index.cjs +119 -35
  2. package/dist/index.mjs +119 -35
  3. package/package.json +7 -7
package/dist/index.cjs CHANGED
@@ -269,37 +269,34 @@ function generateCvaFn(ctx) {
269
269
  //#endregion
270
270
  //#region src/artifacts/js/cx.ts
271
271
  /**
272
- * `cx` joins class names. It does not resolve conflicts between them, in any build.
272
+ * `cx` joins class names and merges them: of the classes that set one property under the same
273
+ * conditions, it keeps the last.
273
274
  *
274
- * It used to, when the class names happened to carry a property to compare — atomic mode
275
- * with `hash.className` off. That made it a correctness tool in development and a plain
276
- * join in a hashed production build, from the same source, with no error either way. An
277
- * override that worked locally silently stopped working when it shipped.
275
+ * It once merged by reading the property off a readable class name — atomic mode with
276
+ * `hash.className` off. That made it a correctness tool in development and a plain join in a
277
+ * hashed production build, from the same source, with no error either way: an override that
278
+ * worked locally silently stopped working when it shipped, and the merge was removed. A hashed
279
+ * name was an opaque digest with no property to compare.
278
280
  *
279
- * The two could not be reconciled by teaching the matcher to read hashed names: under
280
- * `hash: true` a class is an opaque digest with no property to compare.
281
+ * A utility class's name now carries what the merge reads in every build, hashed or not: `_`, six
282
+ * characters coding the property and conditions, `:`, then the value. So development and
283
+ * production merge alike. @see mergeableAtomName
281
284
  *
282
- * So precedence is decided where it can be decided the same way everywhere: by
283
- * {@link https://bamboocss.com/docs/concepts/cascade-layers cascade layers}. A component
284
- * whose styles a consumer will override belongs in `recipes` — write it with `cva`/`sva`,
285
- * not bare `css()` — and the consumer's `css()` in `utilities` wins by layer, in every
286
- * build. Two `css()` outputs joined with `cx` are in the same layer and resolve by source
287
- * order; when you own both, merge the style objects with `css(a, b)` instead.
285
+ * The compiler still merges every Bamboo style a `cx()` call holds before a class exists, by property
286
+ * and conditions as this does, later arguments winning. What reaches the runtime `cx` is what it
287
+ * could not see — a class passed in, or one assembled at runtime.
288
288
  */
289
289
  const declaration = outdent.default`
290
290
  type Argument = string | boolean | null | undefined | Argument[]
291
291
 
292
292
  /**
293
- * Join classNames into a single string.
293
+ * Join classNames into a single string, keeping the last of the classes that set one property
294
+ * under the same conditions.
294
295
  *
295
- * This does **not** resolve conflicts between them: \`cx('px_4', 'px_2')\` keeps both, and
296
- * the browser picks by their order in the stylesheet rather than by the order you passed
297
- * them. That is true of every build — atomic, hashed and grouped alike.
298
- *
299
- * To override a style rather than append to it, let the cascade decide: styles from
300
- * \`cva\`/\`sva\` sit in the \`recipes\` layer and \`css()\` in \`utilities\`, so a consumer's
301
- * \`css()\` always wins. Between two \`css()\` calls you own, merge the objects instead —
302
- * \`css(base, override)\` resolves per property before any class name exists.
296
+ * \`cx(css({ paddingX: '4' }), props.className)\` keeps the caller's \`paddingX\` wherever it
297
+ * passes one, as \`css(a, b)\` keeps the later declaration — at any depth, since a wrapper's own
298
+ * \`cx\` has already merged what it hands down. Any other class, a recipe's own or one written by
299
+ * hand, is kept as written.
303
300
  */
304
301
  export declare function cx(...args: Argument[]): string
305
302
 
@@ -327,39 +324,126 @@ const declaration = outdent.default`
327
324
  ...keys: K
328
325
  ): [Record<string, unknown>, Record<string, unknown>]
329
326
  `;
330
- function generateCx(ctx) {
331
- return {
332
- js: outdent.default`
333
- ${ctx.file.import("splitProps", "../helpers")}
327
+ /**
328
+ * The `cx` every build emits: the join, keeping the last class per property and conditions.
329
+ * @see mergeableAtomName
330
+ *
331
+ * Every class a build hands out is a compiled literal, so the joins a page asks for are few and
332
+ * repeat render after render; each is merged once and looked up after that, which is what keeps a
333
+ * merging `cx` as cheap as the join it replaces. The bound is for classes assembled at runtime.
334
+ */
335
+ const cxFunction = (prefix) => outdent.default`
336
+ // Where a class's name starts past the \`prefix\`, which every class this build names carries.
337
+ const cxPrefix = ${JSON.stringify(prefix ? `${prefix}-` : "")}
338
+
339
+ // The merge key a utility atom's name carries — the six characters between its \`_\` and its
340
+ // \`:\`, hashed or readable — or \`undefined\` for a class that is not one, which is kept as
341
+ // written. The colon is what tells an atom from a class written by hand that starts with \`_\`.
342
+ function cxKey(token) {
343
+ let start = 0
344
+ if (cxPrefix) {
345
+ if (!token.startsWith(cxPrefix)) return undefined
346
+ start = cxPrefix.length
347
+ }
348
+ if (token.charCodeAt(start) !== 95 || token.charCodeAt(start + 7) !== 58) return undefined
349
+ return token.slice(start + 1, start + 7)
350
+ }
351
+
352
+ // Adds each class of \`str\` to \`tokens\`, the last of each key in the place its first stood, so an
353
+ // override lands where the class it replaces sat. One that is not an atom is kept as written,
354
+ // duplicates and all. A few classes per element, so the keys are searched in order, not hashed.
355
+ function cxParse(str, tokens, keys) {
356
+ let start = 0
357
+ for (let i = 0; i <= str.length; i++) {
358
+ const code = i < str.length ? str.charCodeAt(i) : 32
359
+ // The class attribute splits on all ASCII whitespace, and a template literal spans lines.
360
+ if (code !== 32 && code !== 9 && code !== 10 && code !== 12 && code !== 13) continue
361
+ if (i > start) {
362
+ const token = str.slice(start, i)
363
+ const key = cxKey(token)
364
+ let slot = -1
365
+ if (key !== undefined) {
366
+ for (let j = 0; j < keys.length; j++) {
367
+ if (keys[j] === key) {
368
+ slot = j
369
+ break
370
+ }
371
+ }
372
+ }
373
+ if (slot === -1) {
374
+ tokens.push(token)
375
+ keys.push(key)
376
+ } else {
377
+ tokens[slot] = token
378
+ }
379
+ }
380
+ start = i + 1
381
+ }
382
+ }
383
+
384
+ // Per class string a component joins onto: its classes parsed once, and every join merged with
385
+ // it, so a miss parses only what was passed in.
386
+ const cxSeen = new Map()
387
+ let cxEntries = 0
388
+
389
+ function cxCached(a, b) {
390
+ let seen = cxSeen.get(a)
391
+ let merged = seen === undefined ? undefined : seen.joined.get(b)
392
+ if (merged !== undefined) return merged
393
+ if (cxEntries >= 4096) {
394
+ cxSeen.clear()
395
+ cxEntries = 0
396
+ seen = undefined
397
+ }
398
+ if (seen === undefined) {
399
+ seen = { tokens: [], keys: [], joined: new Map() }
400
+ cxParse(a, seen.tokens, seen.keys)
401
+ cxSeen.set(a, seen)
402
+ }
403
+ const tokens = seen.tokens.slice()
404
+ cxParse(b, tokens, seen.keys.slice())
405
+ merged = tokens.join(' ')
406
+ seen.joined.set(b, merged)
407
+ cxEntries++
408
+ return merged
409
+ }
334
410
 
335
411
  function cx(a, b) {
336
412
  const n = arguments.length
337
413
 
338
414
  // \`cx(<compiled class literal>, className)\` is what the transform emits at nearly every
339
415
  // call site it cannot fold, so it is the shape that actually runs per render — a wrapper
340
- // forwarding a className down. Answering it here skips the rest-array allocation and the
341
- // loop below. \`b\` covers the whole non-string tail \`cx\` ignores (null/undefined/booleans),
342
- // which is the \`cond && cls\` idiom when the condition is false.
416
+ // forwarding a className down. \`b\` covers the whole non-string tail \`cx\` ignores
417
+ // (null/undefined/booleans), which is the \`cond && cls\` idiom when the condition is false.
343
418
  if (n === 2 && typeof a === 'string') {
344
- if (typeof b === 'string') return a ? (b ? a + ' ' + b : a) : b
419
+ if (typeof b === 'string') return a ? (b ? cxCached(a, b) : a) : b
345
420
  if (b == null || b === false || b === true) return a
346
421
  }
347
422
 
348
423
  let str = ''
349
- // \`arguments\` rather than a rest parameter: this is the variadic tail, and allocating an
350
- // array per call to iterate it once is the cost the fast path above exists to avoid.
424
+ let parts = 0
425
+ // \`arguments\` rather than a rest parameter, as in the join: one pass, no array.
351
426
  for (let i = 0; i < n; i++) {
352
427
  const arg = arguments[i]
353
428
  if (!arg) continue
354
- // Arrays are part of the declared type, so this branch has to handle them.
355
- // Returning '' for \`cx(['a', 'b'])\` would be a lie.
356
429
  const part = Array.isArray(arg) ? cx(...arg) : typeof arg === 'string' ? arg : ''
357
430
  if (!part) continue
358
431
  str && (str += ' ')
359
432
  str += part
433
+ parts++
360
434
  }
361
- return str
435
+ // A single part is returned as it came: a compiled class string holds one class per key, and
436
+ // another \`cx\`'s result is merged already. One joined by hand, a template literal or a
437
+ // \`join\`, is not, and \`no-unlayered-override\` reports the join. Two or more meet here.
438
+ return parts > 1 ? cxCached(str, '') : str
362
439
  }
440
+ `;
441
+ function generateCx(ctx) {
442
+ return {
443
+ js: outdent.default`
444
+ ${ctx.file.import("splitProps", "../helpers")}
445
+
446
+ ${cxFunction(ctx.utility.prefix)}
363
447
 
364
448
  // Each node is [miss, undefined, [key, child, key, child, ...]]. Children are either
365
449
  // another non-negative node index or a negative leaf reference. Arrays avoid object-literal
package/dist/index.mjs CHANGED
@@ -243,37 +243,34 @@ function generateCvaFn(ctx) {
243
243
  //#endregion
244
244
  //#region src/artifacts/js/cx.ts
245
245
  /**
246
- * `cx` joins class names. It does not resolve conflicts between them, in any build.
246
+ * `cx` joins class names and merges them: of the classes that set one property under the same
247
+ * conditions, it keeps the last.
247
248
  *
248
- * It used to, when the class names happened to carry a property to compare — atomic mode
249
- * with `hash.className` off. That made it a correctness tool in development and a plain
250
- * join in a hashed production build, from the same source, with no error either way. An
251
- * override that worked locally silently stopped working when it shipped.
249
+ * It once merged by reading the property off a readable class name — atomic mode with
250
+ * `hash.className` off. That made it a correctness tool in development and a plain join in a
251
+ * hashed production build, from the same source, with no error either way: an override that
252
+ * worked locally silently stopped working when it shipped, and the merge was removed. A hashed
253
+ * name was an opaque digest with no property to compare.
252
254
  *
253
- * The two could not be reconciled by teaching the matcher to read hashed names: under
254
- * `hash: true` a class is an opaque digest with no property to compare.
255
+ * A utility class's name now carries what the merge reads in every build, hashed or not: `_`, six
256
+ * characters coding the property and conditions, `:`, then the value. So development and
257
+ * production merge alike. @see mergeableAtomName
255
258
  *
256
- * So precedence is decided where it can be decided the same way everywhere: by
257
- * {@link https://bamboocss.com/docs/concepts/cascade-layers cascade layers}. A component
258
- * whose styles a consumer will override belongs in `recipes` — write it with `cva`/`sva`,
259
- * not bare `css()` — and the consumer's `css()` in `utilities` wins by layer, in every
260
- * build. Two `css()` outputs joined with `cx` are in the same layer and resolve by source
261
- * order; when you own both, merge the style objects with `css(a, b)` instead.
259
+ * The compiler still merges every Bamboo style a `cx()` call holds before a class exists, by property
260
+ * and conditions as this does, later arguments winning. What reaches the runtime `cx` is what it
261
+ * could not see — a class passed in, or one assembled at runtime.
262
262
  */
263
263
  const declaration = outdent$1`
264
264
  type Argument = string | boolean | null | undefined | Argument[]
265
265
 
266
266
  /**
267
- * Join classNames into a single string.
267
+ * Join classNames into a single string, keeping the last of the classes that set one property
268
+ * under the same conditions.
268
269
  *
269
- * This does **not** resolve conflicts between them: \`cx('px_4', 'px_2')\` keeps both, and
270
- * the browser picks by their order in the stylesheet rather than by the order you passed
271
- * them. That is true of every build — atomic, hashed and grouped alike.
272
- *
273
- * To override a style rather than append to it, let the cascade decide: styles from
274
- * \`cva\`/\`sva\` sit in the \`recipes\` layer and \`css()\` in \`utilities\`, so a consumer's
275
- * \`css()\` always wins. Between two \`css()\` calls you own, merge the objects instead —
276
- * \`css(base, override)\` resolves per property before any class name exists.
270
+ * \`cx(css({ paddingX: '4' }), props.className)\` keeps the caller's \`paddingX\` wherever it
271
+ * passes one, as \`css(a, b)\` keeps the later declaration — at any depth, since a wrapper's own
272
+ * \`cx\` has already merged what it hands down. Any other class, a recipe's own or one written by
273
+ * hand, is kept as written.
277
274
  */
278
275
  export declare function cx(...args: Argument[]): string
279
276
 
@@ -301,39 +298,126 @@ const declaration = outdent$1`
301
298
  ...keys: K
302
299
  ): [Record<string, unknown>, Record<string, unknown>]
303
300
  `;
304
- function generateCx(ctx) {
305
- return {
306
- js: outdent$1`
307
- ${ctx.file.import("splitProps", "../helpers")}
301
+ /**
302
+ * The `cx` every build emits: the join, keeping the last class per property and conditions.
303
+ * @see mergeableAtomName
304
+ *
305
+ * Every class a build hands out is a compiled literal, so the joins a page asks for are few and
306
+ * repeat render after render; each is merged once and looked up after that, which is what keeps a
307
+ * merging `cx` as cheap as the join it replaces. The bound is for classes assembled at runtime.
308
+ */
309
+ const cxFunction = (prefix) => outdent$1`
310
+ // Where a class's name starts past the \`prefix\`, which every class this build names carries.
311
+ const cxPrefix = ${JSON.stringify(prefix ? `${prefix}-` : "")}
312
+
313
+ // The merge key a utility atom's name carries — the six characters between its \`_\` and its
314
+ // \`:\`, hashed or readable — or \`undefined\` for a class that is not one, which is kept as
315
+ // written. The colon is what tells an atom from a class written by hand that starts with \`_\`.
316
+ function cxKey(token) {
317
+ let start = 0
318
+ if (cxPrefix) {
319
+ if (!token.startsWith(cxPrefix)) return undefined
320
+ start = cxPrefix.length
321
+ }
322
+ if (token.charCodeAt(start) !== 95 || token.charCodeAt(start + 7) !== 58) return undefined
323
+ return token.slice(start + 1, start + 7)
324
+ }
325
+
326
+ // Adds each class of \`str\` to \`tokens\`, the last of each key in the place its first stood, so an
327
+ // override lands where the class it replaces sat. One that is not an atom is kept as written,
328
+ // duplicates and all. A few classes per element, so the keys are searched in order, not hashed.
329
+ function cxParse(str, tokens, keys) {
330
+ let start = 0
331
+ for (let i = 0; i <= str.length; i++) {
332
+ const code = i < str.length ? str.charCodeAt(i) : 32
333
+ // The class attribute splits on all ASCII whitespace, and a template literal spans lines.
334
+ if (code !== 32 && code !== 9 && code !== 10 && code !== 12 && code !== 13) continue
335
+ if (i > start) {
336
+ const token = str.slice(start, i)
337
+ const key = cxKey(token)
338
+ let slot = -1
339
+ if (key !== undefined) {
340
+ for (let j = 0; j < keys.length; j++) {
341
+ if (keys[j] === key) {
342
+ slot = j
343
+ break
344
+ }
345
+ }
346
+ }
347
+ if (slot === -1) {
348
+ tokens.push(token)
349
+ keys.push(key)
350
+ } else {
351
+ tokens[slot] = token
352
+ }
353
+ }
354
+ start = i + 1
355
+ }
356
+ }
357
+
358
+ // Per class string a component joins onto: its classes parsed once, and every join merged with
359
+ // it, so a miss parses only what was passed in.
360
+ const cxSeen = new Map()
361
+ let cxEntries = 0
362
+
363
+ function cxCached(a, b) {
364
+ let seen = cxSeen.get(a)
365
+ let merged = seen === undefined ? undefined : seen.joined.get(b)
366
+ if (merged !== undefined) return merged
367
+ if (cxEntries >= 4096) {
368
+ cxSeen.clear()
369
+ cxEntries = 0
370
+ seen = undefined
371
+ }
372
+ if (seen === undefined) {
373
+ seen = { tokens: [], keys: [], joined: new Map() }
374
+ cxParse(a, seen.tokens, seen.keys)
375
+ cxSeen.set(a, seen)
376
+ }
377
+ const tokens = seen.tokens.slice()
378
+ cxParse(b, tokens, seen.keys.slice())
379
+ merged = tokens.join(' ')
380
+ seen.joined.set(b, merged)
381
+ cxEntries++
382
+ return merged
383
+ }
308
384
 
309
385
  function cx(a, b) {
310
386
  const n = arguments.length
311
387
 
312
388
  // \`cx(<compiled class literal>, className)\` is what the transform emits at nearly every
313
389
  // call site it cannot fold, so it is the shape that actually runs per render — a wrapper
314
- // forwarding a className down. Answering it here skips the rest-array allocation and the
315
- // loop below. \`b\` covers the whole non-string tail \`cx\` ignores (null/undefined/booleans),
316
- // which is the \`cond && cls\` idiom when the condition is false.
390
+ // forwarding a className down. \`b\` covers the whole non-string tail \`cx\` ignores
391
+ // (null/undefined/booleans), which is the \`cond && cls\` idiom when the condition is false.
317
392
  if (n === 2 && typeof a === 'string') {
318
- if (typeof b === 'string') return a ? (b ? a + ' ' + b : a) : b
393
+ if (typeof b === 'string') return a ? (b ? cxCached(a, b) : a) : b
319
394
  if (b == null || b === false || b === true) return a
320
395
  }
321
396
 
322
397
  let str = ''
323
- // \`arguments\` rather than a rest parameter: this is the variadic tail, and allocating an
324
- // array per call to iterate it once is the cost the fast path above exists to avoid.
398
+ let parts = 0
399
+ // \`arguments\` rather than a rest parameter, as in the join: one pass, no array.
325
400
  for (let i = 0; i < n; i++) {
326
401
  const arg = arguments[i]
327
402
  if (!arg) continue
328
- // Arrays are part of the declared type, so this branch has to handle them.
329
- // Returning '' for \`cx(['a', 'b'])\` would be a lie.
330
403
  const part = Array.isArray(arg) ? cx(...arg) : typeof arg === 'string' ? arg : ''
331
404
  if (!part) continue
332
405
  str && (str += ' ')
333
406
  str += part
407
+ parts++
334
408
  }
335
- return str
409
+ // A single part is returned as it came: a compiled class string holds one class per key, and
410
+ // another \`cx\`'s result is merged already. One joined by hand, a template literal or a
411
+ // \`join\`, is not, and \`no-unlayered-override\` reports the join. Two or more meet here.
412
+ return parts > 1 ? cxCached(str, '') : str
336
413
  }
414
+ `;
415
+ function generateCx(ctx) {
416
+ return {
417
+ js: outdent$1`
418
+ ${ctx.file.import("splitProps", "../helpers")}
419
+
420
+ ${cxFunction(ctx.utility.prefix)}
337
421
 
338
422
  // Each node is [miss, undefined, [key, child, key, child, ...]]. Children are either
339
423
  // another non-negative node index or a negative leaf reference. Arrays avoid object-literal
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bamboocss/generator",
3
- "version": "1.62.1",
3
+ "version": "1.64.1",
4
4
  "description": "The css generator for css bamboo",
5
5
  "homepage": "https://bamboocss.com",
6
6
  "license": "MIT",
@@ -38,12 +38,12 @@
38
38
  "pluralize": "8.0.0",
39
39
  "postcss": "8.5.26",
40
40
  "ts-pattern": "5.9.0",
41
- "@bamboocss/core": "1.62.1",
42
- "@bamboocss/is-valid-prop": "^1.62.1",
43
- "@bamboocss/logger": "1.62.1",
44
- "@bamboocss/shared": "1.62.1",
45
- "@bamboocss/token-dictionary": "1.62.1",
46
- "@bamboocss/types": "1.62.1"
41
+ "@bamboocss/core": "1.64.1",
42
+ "@bamboocss/is-valid-prop": "^1.64.1",
43
+ "@bamboocss/logger": "1.64.1",
44
+ "@bamboocss/shared": "1.64.1",
45
+ "@bamboocss/token-dictionary": "1.64.1",
46
+ "@bamboocss/types": "1.64.1"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@types/pluralize": "0.0.33"