@bamboocss/generator 1.63.0 → 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 -37
  2. package/dist/index.mjs +119 -37
  3. package/package.json +7 -7
package/dist/index.cjs CHANGED
@@ -269,39 +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 the compiler,
283
- * which merges every Bamboo style a `cx()` call holds before a class exists, later arguments
284
- * winning. What reaches this join at runtime is only what the compiler could not see — a class
285
- * passed in, or one assembled at runtime — and no
286
- * {@link https://bamboocss.com/docs/concepts/cascade-layers cascade layer} ranks it above the
287
- * styles it is joined with: recipes compile to the same `utilities` atoms as `css()`, and the
288
- * named `recipes` layer is not emitted. An override a caller chooses belongs in a recipe variant.
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.
289
288
  */
290
289
  const declaration = outdent.default`
291
290
  type Argument = string | boolean | null | undefined | Argument[]
292
291
 
293
292
  /**
294
- * 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.
295
295
  *
296
- * Bamboo styles passed here — a \`css()\` call, a recipe, or a binding holding either — are
297
- * merged by the compiler before any class exists, later arguments winning:
298
- * \`cx(css({ paddingX: '4' }), css({ paddingX: '2' }))\` compiles to the one class for
299
- * \`paddingX: '2'\`.
300
- *
301
- * Anything else is only joined, such as a \`className\` prop or a class assembled at runtime, and
302
- * the join does **not** resolve conflicts: recipes and \`css()\` share one cascade layer, so where
303
- * such a class sets a property these styles set too, the browser picks by stylesheet order rather
304
- * than by the order you passed them. For an override a caller chooses, expose a recipe variant.
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.
305
300
  */
306
301
  export declare function cx(...args: Argument[]): string
307
302
 
@@ -329,39 +324,126 @@ const declaration = outdent.default`
329
324
  ...keys: K
330
325
  ): [Record<string, unknown>, Record<string, unknown>]
331
326
  `;
332
- function generateCx(ctx) {
333
- return {
334
- js: outdent.default`
335
- ${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
+ }
336
410
 
337
411
  function cx(a, b) {
338
412
  const n = arguments.length
339
413
 
340
414
  // \`cx(<compiled class literal>, className)\` is what the transform emits at nearly every
341
415
  // call site it cannot fold, so it is the shape that actually runs per render — a wrapper
342
- // forwarding a className down. Answering it here skips the rest-array allocation and the
343
- // loop below. \`b\` covers the whole non-string tail \`cx\` ignores (null/undefined/booleans),
344
- // 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.
345
418
  if (n === 2 && typeof a === 'string') {
346
- if (typeof b === 'string') return a ? (b ? a + ' ' + b : a) : b
419
+ if (typeof b === 'string') return a ? (b ? cxCached(a, b) : a) : b
347
420
  if (b == null || b === false || b === true) return a
348
421
  }
349
422
 
350
423
  let str = ''
351
- // \`arguments\` rather than a rest parameter: this is the variadic tail, and allocating an
352
- // 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.
353
426
  for (let i = 0; i < n; i++) {
354
427
  const arg = arguments[i]
355
428
  if (!arg) continue
356
- // Arrays are part of the declared type, so this branch has to handle them.
357
- // Returning '' for \`cx(['a', 'b'])\` would be a lie.
358
429
  const part = Array.isArray(arg) ? cx(...arg) : typeof arg === 'string' ? arg : ''
359
430
  if (!part) continue
360
431
  str && (str += ' ')
361
432
  str += part
433
+ parts++
362
434
  }
363
- 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
364
439
  }
440
+ `;
441
+ function generateCx(ctx) {
442
+ return {
443
+ js: outdent.default`
444
+ ${ctx.file.import("splitProps", "../helpers")}
445
+
446
+ ${cxFunction(ctx.utility.prefix)}
365
447
 
366
448
  // Each node is [miss, undefined, [key, child, key, child, ...]]. Children are either
367
449
  // another non-negative node index or a negative leaf reference. Arrays avoid object-literal
package/dist/index.mjs CHANGED
@@ -243,39 +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 the compiler,
257
- * which merges every Bamboo style a `cx()` call holds before a class exists, later arguments
258
- * winning. What reaches this join at runtime is only what the compiler could not see — a class
259
- * passed in, or one assembled at runtime — and no
260
- * {@link https://bamboocss.com/docs/concepts/cascade-layers cascade layer} ranks it above the
261
- * styles it is joined with: recipes compile to the same `utilities` atoms as `css()`, and the
262
- * named `recipes` layer is not emitted. An override a caller chooses belongs in a recipe variant.
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.
263
262
  */
264
263
  const declaration = outdent$1`
265
264
  type Argument = string | boolean | null | undefined | Argument[]
266
265
 
267
266
  /**
268
- * 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.
269
269
  *
270
- * Bamboo styles passed here — a \`css()\` call, a recipe, or a binding holding either — are
271
- * merged by the compiler before any class exists, later arguments winning:
272
- * \`cx(css({ paddingX: '4' }), css({ paddingX: '2' }))\` compiles to the one class for
273
- * \`paddingX: '2'\`.
274
- *
275
- * Anything else is only joined, such as a \`className\` prop or a class assembled at runtime, and
276
- * the join does **not** resolve conflicts: recipes and \`css()\` share one cascade layer, so where
277
- * such a class sets a property these styles set too, the browser picks by stylesheet order rather
278
- * than by the order you passed them. For an override a caller chooses, expose a recipe variant.
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.
279
274
  */
280
275
  export declare function cx(...args: Argument[]): string
281
276
 
@@ -303,39 +298,126 @@ const declaration = outdent$1`
303
298
  ...keys: K
304
299
  ): [Record<string, unknown>, Record<string, unknown>]
305
300
  `;
306
- function generateCx(ctx) {
307
- return {
308
- js: outdent$1`
309
- ${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
+ }
310
384
 
311
385
  function cx(a, b) {
312
386
  const n = arguments.length
313
387
 
314
388
  // \`cx(<compiled class literal>, className)\` is what the transform emits at nearly every
315
389
  // call site it cannot fold, so it is the shape that actually runs per render — a wrapper
316
- // forwarding a className down. Answering it here skips the rest-array allocation and the
317
- // loop below. \`b\` covers the whole non-string tail \`cx\` ignores (null/undefined/booleans),
318
- // 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.
319
392
  if (n === 2 && typeof a === 'string') {
320
- if (typeof b === 'string') return a ? (b ? a + ' ' + b : a) : b
393
+ if (typeof b === 'string') return a ? (b ? cxCached(a, b) : a) : b
321
394
  if (b == null || b === false || b === true) return a
322
395
  }
323
396
 
324
397
  let str = ''
325
- // \`arguments\` rather than a rest parameter: this is the variadic tail, and allocating an
326
- // 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.
327
400
  for (let i = 0; i < n; i++) {
328
401
  const arg = arguments[i]
329
402
  if (!arg) continue
330
- // Arrays are part of the declared type, so this branch has to handle them.
331
- // Returning '' for \`cx(['a', 'b'])\` would be a lie.
332
403
  const part = Array.isArray(arg) ? cx(...arg) : typeof arg === 'string' ? arg : ''
333
404
  if (!part) continue
334
405
  str && (str += ' ')
335
406
  str += part
407
+ parts++
336
408
  }
337
- 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
338
413
  }
414
+ `;
415
+ function generateCx(ctx) {
416
+ return {
417
+ js: outdent$1`
418
+ ${ctx.file.import("splitProps", "../helpers")}
419
+
420
+ ${cxFunction(ctx.utility.prefix)}
339
421
 
340
422
  // Each node is [miss, undefined, [key, child, key, child, ...]]. Children are either
341
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.63.0",
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.63.0",
42
- "@bamboocss/is-valid-prop": "^1.63.0",
43
- "@bamboocss/logger": "1.63.0",
44
- "@bamboocss/shared": "1.63.0",
45
- "@bamboocss/token-dictionary": "1.63.0",
46
- "@bamboocss/types": "1.63.0"
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"