@bamboocss/generator 1.28.0 → 1.29.0

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/dist/index.cjs CHANGED
@@ -1373,15 +1373,35 @@ function generateSvaFn(ctx) {
1373
1373
  }
1374
1374
  //#endregion
1375
1375
  //#region src/artifacts/js/token.ts
1376
+ /**
1377
+ * `token()` hands back the variable reference for every token, and `token.value()` the
1378
+ * resolved literal.
1379
+ *
1380
+ * It used to be the other way round, decided per token: a base token resolved to its literal
1381
+ * and a virtual or conditional one to its `var()`. That made the return kind a property of
1382
+ * the *theme* rather than of the call, so adding `_dark` to a token silently changed what
1383
+ * every caller received — same call, same path, a colour before and a variable after, with
1384
+ * both typed `string` and nothing to catch it.
1385
+ *
1386
+ * Always-a-reference is the predictable half and the one that keeps working when a theme
1387
+ * switches, so it takes the short name. The literal is still reachable, but has to be asked
1388
+ * for — which is also the honest signal, since it is the form that stops responding to
1389
+ * conditions.
1390
+ *
1391
+ * `value` keeps the old per-token split rather than becoming `token.value` for everything:
1392
+ * a virtual or conditional token has no single literal to hand back, so its `var()` is still
1393
+ * the only truthful answer.
1394
+ */
1376
1395
  function generateTokenJs(ctx) {
1377
1396
  const { tokens } = ctx;
1378
1397
  const map = /* @__PURE__ */ new Map();
1379
1398
  tokens.allTokens.forEach((token) => {
1380
1399
  const { varRef, isVirtual } = token.extensions;
1381
- const value = isVirtual || token.extensions.condition !== "base" ? varRef : token.value;
1400
+ const variable = tokens.view.getVar(token.name) ?? varRef;
1401
+ const value = tokens.view.get(token.name) ?? (isVirtual || token.extensions.condition !== "base" ? varRef : token.value);
1382
1402
  map.set(token.name, {
1383
1403
  value,
1384
- variable: varRef
1404
+ variable
1385
1405
  });
1386
1406
  });
1387
1407
  const obj = Object.fromEntries(map);
@@ -1389,22 +1409,37 @@ function generateTokenJs(ctx) {
1389
1409
  js: outdent.default`
1390
1410
  const tokens = ${JSON.stringify(obj, null, 2)}
1391
1411
 
1412
+ // \`??\`, not \`||\`. The fallback is for a path that names no token, and \`||\` also swallows a
1413
+ // token whose value is legitimately falsy — \`zIndex: { base: { value: 0 } }\` returned the
1414
+ // fallback instead of 0. Nothing in the default preset has one, so this only ever bit a
1415
+ // custom theme.
1392
1416
  export function token(path, fallback) {
1393
- return tokens[path]?.value || fallback
1417
+ return tokens[path]?.variable ?? fallback
1394
1418
  }
1395
1419
 
1396
- function tokenVar(path, fallback) {
1397
- return tokens[path]?.variable || fallback
1420
+ function tokenValue(path, fallback) {
1421
+ return tokens[path]?.value ?? fallback
1398
1422
  }
1399
1423
 
1400
- token.var = tokenVar
1424
+ // \`token.var\` predates \`token()\` returning the reference itself. Kept as the same
1425
+ // function rather than removed, so the spelling that was correct before still is.
1426
+ token.var = token
1427
+ token.value = tokenValue
1401
1428
  `,
1402
1429
  dts: outdent.default`
1403
1430
  ${ctx.file.importType("Token", "./tokens")}
1404
1431
 
1405
1432
  export declare const token: {
1433
+ /** The css variable reference — \`var(--colors-red-300)\`. Stays correct across themes. */
1406
1434
  (path: Token, fallback?: string): string
1435
+ /** Alias of \`token()\`, kept for compatibility. */
1407
1436
  var: (path: Token, fallback?: string) => string
1437
+ /**
1438
+ * The resolved literal — \`#fca5a5\`. Use where css variables cannot be resolved, such as
1439
+ * canvas or a charting library. A conditional token has no single literal and still
1440
+ * returns its \`var()\`.
1441
+ */
1442
+ value: (path: Token, fallback?: string) => string
1408
1443
  }
1409
1444
 
1410
1445
  ${ctx.file.exportTypeStar("./tokens")}
@@ -3602,7 +3637,7 @@ const generateTokenExamples = (token) => {
3602
3637
  const fullTokenName = token.name;
3603
3638
  const functionExamples = [`css({ ${prop}: '${tokenName}' })`];
3604
3639
  const tokenFunctionExamples = [`token('${fullTokenName}')`];
3605
- if (token.extensions.varRef) tokenFunctionExamples.push(`token.var('${fullTokenName}')`);
3640
+ tokenFunctionExamples.push(`token.value('${fullTokenName}')`);
3606
3641
  return {
3607
3642
  functionExamples,
3608
3643
  tokenFunctionExamples
@@ -3872,30 +3907,36 @@ var Generator = class extends _bamboocss_core.Context {
3872
3907
  return names;
3873
3908
  };
3874
3909
  /**
3875
- * Tokens whose javascript value is a `var()` reference rather than a literal.
3876
- * `token('colors.text')` hands those to the caller as a reference, so the declaration
3877
- * has to survive whether or not the generated css mentions it. Ordinary tokens resolve
3878
- * to a literal in javascript and need no such exemption.
3910
+ * The token declarations held open so a runtime `token()` can answer for any path.
3911
+ *
3912
+ * `token()` hands javascript the *variable reference* for every token, so a path the build
3913
+ * cannot resolve could name any of them and every declaration has to survive. That is a
3914
+ * blunt instrument, and deliberately so: the alternative failure is a `var()` with no
3915
+ * declaration behind it, which resolves to the guaranteed-invalid value and inherits
3916
+ * rather than falling back — silently wrong, which is worse than visibly large.
3879
3917
  *
3880
- * The two cases mirror `generateTokenJs`, which is what decides the value javascript
3881
- * actually receives:
3918
+ * It used to be narrower, because `token()` used to return a *literal* for a plain token
3919
+ * and only a `var()` for virtual, conditional and negative ones. That split is gone, and
3920
+ * narrowing this to match it would now strand exactly the base tokens the old split made
3921
+ * safe.
3882
3922
  *
3883
- * - A virtual token, or one carrying a condition, is handed its own `varRef`.
3884
- * - A negative token is handed `calc(var(--x) * -1)`, so it is a reference too — but to
3885
- * the *positive* token's declaration. Its own var is never declared, so the name has
3886
- * to come out of the value.
3923
+ * So the gate below carries the whole saving. `styled-system/tokens` is generated into the
3924
+ * project, so nothing outside it can import them -- if no file under `include` reaches for
3925
+ * a token from javascript, no caller exists to serve and the declarations are as prunable
3926
+ * as any other.
3927
+ *
3928
+ * That gate is all-or-nothing per project, which is the coarse part worth fixing next: a
3929
+ * project whose token calls all resolve to string literals needs none of this, because
3930
+ * `collectTokenReferences` already kept those paths by name. Deciding that needs the
3931
+ * reference accounting the gate does not do yet -- see `tokensReachableFromJs`.
3887
3932
  */
3888
3933
  getAlwaysKeptTokenVars = (tokensReachableFromJs) => {
3889
3934
  const names = /* @__PURE__ */ new Set();
3890
3935
  if (!tokensReachableFromJs) return names;
3891
3936
  this.tokens.allTokens.forEach((token) => {
3892
- const { isVirtual, isNegative, condition, var: varName } = token.extensions;
3893
- if (isVirtual || condition !== "base") {
3894
- if (varName) names.add(varName.startsWith("--") ? varName : `--${varName}`);
3895
- return;
3896
- }
3897
- if (!isNegative) return;
3898
- for (const name of (0, _bamboocss_shared.cssVarRefs)(token.value)) names.add(name);
3937
+ const { var: varName } = token.extensions;
3938
+ if (varName) names.add(varName.startsWith("--") ? varName : `--${varName}`);
3939
+ if (typeof token.value === "string") for (const name of (0, _bamboocss_shared.cssVarRefs)(token.value)) names.add(name);
3899
3940
  });
3900
3941
  return names;
3901
3942
  };
package/dist/index.d.cts CHANGED
@@ -93,18 +93,28 @@ declare class Generator extends Context {
93
93
  */
94
94
  private getThemeTokenVars;
95
95
  /**
96
- * Tokens whose javascript value is a `var()` reference rather than a literal.
97
- * `token('colors.text')` hands those to the caller as a reference, so the declaration
98
- * has to survive whether or not the generated css mentions it. Ordinary tokens resolve
99
- * to a literal in javascript and need no such exemption.
96
+ * The token declarations held open so a runtime `token()` can answer for any path.
100
97
  *
101
- * The two cases mirror `generateTokenJs`, which is what decides the value javascript
102
- * actually receives:
98
+ * `token()` hands javascript the *variable reference* for every token, so a path the build
99
+ * cannot resolve could name any of them and every declaration has to survive. That is a
100
+ * blunt instrument, and deliberately so: the alternative failure is a `var()` with no
101
+ * declaration behind it, which resolves to the guaranteed-invalid value and inherits
102
+ * rather than falling back — silently wrong, which is worse than visibly large.
103
103
  *
104
- * - A virtual token, or one carrying a condition, is handed its own `varRef`.
105
- * - A negative token is handed `calc(var(--x) * -1)`, so it is a reference too — but to
106
- * the *positive* token's declaration. Its own var is never declared, so the name has
107
- * to come out of the value.
104
+ * It used to be narrower, because `token()` used to return a *literal* for a plain token
105
+ * and only a `var()` for virtual, conditional and negative ones. That split is gone, and
106
+ * narrowing this to match it would now strand exactly the base tokens the old split made
107
+ * safe.
108
+ *
109
+ * So the gate below carries the whole saving. `styled-system/tokens` is generated into the
110
+ * project, so nothing outside it can import them -- if no file under `include` reaches for
111
+ * a token from javascript, no caller exists to serve and the declarations are as prunable
112
+ * as any other.
113
+ *
114
+ * That gate is all-or-nothing per project, which is the coarse part worth fixing next: a
115
+ * project whose token calls all resolve to string literals needs none of this, because
116
+ * `collectTokenReferences` already kept those paths by name. Deciding that needs the
117
+ * reference accounting the gate does not do yet -- see `tokensReachableFromJs`.
108
118
  */
109
119
  private getAlwaysKeptTokenVars;
110
120
  getParserCss: (decoder: StyleDecoder) => string;
package/dist/index.d.mts CHANGED
@@ -93,18 +93,28 @@ declare class Generator extends Context {
93
93
  */
94
94
  private getThemeTokenVars;
95
95
  /**
96
- * Tokens whose javascript value is a `var()` reference rather than a literal.
97
- * `token('colors.text')` hands those to the caller as a reference, so the declaration
98
- * has to survive whether or not the generated css mentions it. Ordinary tokens resolve
99
- * to a literal in javascript and need no such exemption.
96
+ * The token declarations held open so a runtime `token()` can answer for any path.
100
97
  *
101
- * The two cases mirror `generateTokenJs`, which is what decides the value javascript
102
- * actually receives:
98
+ * `token()` hands javascript the *variable reference* for every token, so a path the build
99
+ * cannot resolve could name any of them and every declaration has to survive. That is a
100
+ * blunt instrument, and deliberately so: the alternative failure is a `var()` with no
101
+ * declaration behind it, which resolves to the guaranteed-invalid value and inherits
102
+ * rather than falling back — silently wrong, which is worse than visibly large.
103
103
  *
104
- * - A virtual token, or one carrying a condition, is handed its own `varRef`.
105
- * - A negative token is handed `calc(var(--x) * -1)`, so it is a reference too — but to
106
- * the *positive* token's declaration. Its own var is never declared, so the name has
107
- * to come out of the value.
104
+ * It used to be narrower, because `token()` used to return a *literal* for a plain token
105
+ * and only a `var()` for virtual, conditional and negative ones. That split is gone, and
106
+ * narrowing this to match it would now strand exactly the base tokens the old split made
107
+ * safe.
108
+ *
109
+ * So the gate below carries the whole saving. `styled-system/tokens` is generated into the
110
+ * project, so nothing outside it can import them -- if no file under `include` reaches for
111
+ * a token from javascript, no caller exists to serve and the declarations are as prunable
112
+ * as any other.
113
+ *
114
+ * That gate is all-or-nothing per project, which is the coarse part worth fixing next: a
115
+ * project whose token calls all resolve to string literals needs none of this, because
116
+ * `collectTokenReferences` already kept those paths by name. Deciding that needs the
117
+ * reference accounting the gate does not do yet -- see `tokensReachableFromJs`.
108
118
  */
109
119
  private getAlwaysKeptTokenVars;
110
120
  getParserCss: (decoder: StyleDecoder) => string;
package/dist/index.mjs CHANGED
@@ -1347,15 +1347,35 @@ function generateSvaFn(ctx) {
1347
1347
  }
1348
1348
  //#endregion
1349
1349
  //#region src/artifacts/js/token.ts
1350
+ /**
1351
+ * `token()` hands back the variable reference for every token, and `token.value()` the
1352
+ * resolved literal.
1353
+ *
1354
+ * It used to be the other way round, decided per token: a base token resolved to its literal
1355
+ * and a virtual or conditional one to its `var()`. That made the return kind a property of
1356
+ * the *theme* rather than of the call, so adding `_dark` to a token silently changed what
1357
+ * every caller received — same call, same path, a colour before and a variable after, with
1358
+ * both typed `string` and nothing to catch it.
1359
+ *
1360
+ * Always-a-reference is the predictable half and the one that keeps working when a theme
1361
+ * switches, so it takes the short name. The literal is still reachable, but has to be asked
1362
+ * for — which is also the honest signal, since it is the form that stops responding to
1363
+ * conditions.
1364
+ *
1365
+ * `value` keeps the old per-token split rather than becoming `token.value` for everything:
1366
+ * a virtual or conditional token has no single literal to hand back, so its `var()` is still
1367
+ * the only truthful answer.
1368
+ */
1350
1369
  function generateTokenJs(ctx) {
1351
1370
  const { tokens } = ctx;
1352
1371
  const map = /* @__PURE__ */ new Map();
1353
1372
  tokens.allTokens.forEach((token) => {
1354
1373
  const { varRef, isVirtual } = token.extensions;
1355
- const value = isVirtual || token.extensions.condition !== "base" ? varRef : token.value;
1374
+ const variable = tokens.view.getVar(token.name) ?? varRef;
1375
+ const value = tokens.view.get(token.name) ?? (isVirtual || token.extensions.condition !== "base" ? varRef : token.value);
1356
1376
  map.set(token.name, {
1357
1377
  value,
1358
- variable: varRef
1378
+ variable
1359
1379
  });
1360
1380
  });
1361
1381
  const obj = Object.fromEntries(map);
@@ -1363,22 +1383,37 @@ function generateTokenJs(ctx) {
1363
1383
  js: outdent$1`
1364
1384
  const tokens = ${JSON.stringify(obj, null, 2)}
1365
1385
 
1386
+ // \`??\`, not \`||\`. The fallback is for a path that names no token, and \`||\` also swallows a
1387
+ // token whose value is legitimately falsy — \`zIndex: { base: { value: 0 } }\` returned the
1388
+ // fallback instead of 0. Nothing in the default preset has one, so this only ever bit a
1389
+ // custom theme.
1366
1390
  export function token(path, fallback) {
1367
- return tokens[path]?.value || fallback
1391
+ return tokens[path]?.variable ?? fallback
1368
1392
  }
1369
1393
 
1370
- function tokenVar(path, fallback) {
1371
- return tokens[path]?.variable || fallback
1394
+ function tokenValue(path, fallback) {
1395
+ return tokens[path]?.value ?? fallback
1372
1396
  }
1373
1397
 
1374
- token.var = tokenVar
1398
+ // \`token.var\` predates \`token()\` returning the reference itself. Kept as the same
1399
+ // function rather than removed, so the spelling that was correct before still is.
1400
+ token.var = token
1401
+ token.value = tokenValue
1375
1402
  `,
1376
1403
  dts: outdent$1`
1377
1404
  ${ctx.file.importType("Token", "./tokens")}
1378
1405
 
1379
1406
  export declare const token: {
1407
+ /** The css variable reference — \`var(--colors-red-300)\`. Stays correct across themes. */
1380
1408
  (path: Token, fallback?: string): string
1409
+ /** Alias of \`token()\`, kept for compatibility. */
1381
1410
  var: (path: Token, fallback?: string) => string
1411
+ /**
1412
+ * The resolved literal — \`#fca5a5\`. Use where css variables cannot be resolved, such as
1413
+ * canvas or a charting library. A conditional token has no single literal and still
1414
+ * returns its \`var()\`.
1415
+ */
1416
+ value: (path: Token, fallback?: string) => string
1382
1417
  }
1383
1418
 
1384
1419
  ${ctx.file.exportTypeStar("./tokens")}
@@ -3576,7 +3611,7 @@ const generateTokenExamples = (token) => {
3576
3611
  const fullTokenName = token.name;
3577
3612
  const functionExamples = [`css({ ${prop}: '${tokenName}' })`];
3578
3613
  const tokenFunctionExamples = [`token('${fullTokenName}')`];
3579
- if (token.extensions.varRef) tokenFunctionExamples.push(`token.var('${fullTokenName}')`);
3614
+ tokenFunctionExamples.push(`token.value('${fullTokenName}')`);
3580
3615
  return {
3581
3616
  functionExamples,
3582
3617
  tokenFunctionExamples
@@ -3846,30 +3881,36 @@ var Generator = class extends Context {
3846
3881
  return names;
3847
3882
  };
3848
3883
  /**
3849
- * Tokens whose javascript value is a `var()` reference rather than a literal.
3850
- * `token('colors.text')` hands those to the caller as a reference, so the declaration
3851
- * has to survive whether or not the generated css mentions it. Ordinary tokens resolve
3852
- * to a literal in javascript and need no such exemption.
3884
+ * The token declarations held open so a runtime `token()` can answer for any path.
3885
+ *
3886
+ * `token()` hands javascript the *variable reference* for every token, so a path the build
3887
+ * cannot resolve could name any of them and every declaration has to survive. That is a
3888
+ * blunt instrument, and deliberately so: the alternative failure is a `var()` with no
3889
+ * declaration behind it, which resolves to the guaranteed-invalid value and inherits
3890
+ * rather than falling back — silently wrong, which is worse than visibly large.
3853
3891
  *
3854
- * The two cases mirror `generateTokenJs`, which is what decides the value javascript
3855
- * actually receives:
3892
+ * It used to be narrower, because `token()` used to return a *literal* for a plain token
3893
+ * and only a `var()` for virtual, conditional and negative ones. That split is gone, and
3894
+ * narrowing this to match it would now strand exactly the base tokens the old split made
3895
+ * safe.
3856
3896
  *
3857
- * - A virtual token, or one carrying a condition, is handed its own `varRef`.
3858
- * - A negative token is handed `calc(var(--x) * -1)`, so it is a reference too — but to
3859
- * the *positive* token's declaration. Its own var is never declared, so the name has
3860
- * to come out of the value.
3897
+ * So the gate below carries the whole saving. `styled-system/tokens` is generated into the
3898
+ * project, so nothing outside it can import them -- if no file under `include` reaches for
3899
+ * a token from javascript, no caller exists to serve and the declarations are as prunable
3900
+ * as any other.
3901
+ *
3902
+ * That gate is all-or-nothing per project, which is the coarse part worth fixing next: a
3903
+ * project whose token calls all resolve to string literals needs none of this, because
3904
+ * `collectTokenReferences` already kept those paths by name. Deciding that needs the
3905
+ * reference accounting the gate does not do yet -- see `tokensReachableFromJs`.
3861
3906
  */
3862
3907
  getAlwaysKeptTokenVars = (tokensReachableFromJs) => {
3863
3908
  const names = /* @__PURE__ */ new Set();
3864
3909
  if (!tokensReachableFromJs) return names;
3865
3910
  this.tokens.allTokens.forEach((token) => {
3866
- const { isVirtual, isNegative, condition, var: varName } = token.extensions;
3867
- if (isVirtual || condition !== "base") {
3868
- if (varName) names.add(varName.startsWith("--") ? varName : `--${varName}`);
3869
- return;
3870
- }
3871
- if (!isNegative) return;
3872
- for (const name of cssVarRefs(token.value)) names.add(name);
3911
+ const { var: varName } = token.extensions;
3912
+ if (varName) names.add(varName.startsWith("--") ? varName : `--${varName}`);
3913
+ if (typeof token.value === "string") for (const name of cssVarRefs(token.value)) names.add(name);
3873
3914
  });
3874
3915
  return names;
3875
3916
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bamboocss/generator",
3
- "version": "1.28.0",
3
+ "version": "1.29.0",
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.28.0",
42
- "@bamboocss/is-valid-prop": "^1.28.0",
43
- "@bamboocss/logger": "1.28.0",
44
- "@bamboocss/shared": "1.28.0",
45
- "@bamboocss/token-dictionary": "1.28.0",
46
- "@bamboocss/types": "1.28.0"
41
+ "@bamboocss/core": "1.29.0",
42
+ "@bamboocss/is-valid-prop": "^1.29.0",
43
+ "@bamboocss/logger": "1.29.0",
44
+ "@bamboocss/shared": "1.29.0",
45
+ "@bamboocss/token-dictionary": "1.29.0",
46
+ "@bamboocss/types": "1.29.0"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@types/pluralize": "0.0.33"