@homebound/truss 2.6.1 → 2.8.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/build/index.js +15 -1
- package/build/index.js.map +1 -1
- package/build/plugin/index.js +44 -12
- package/build/plugin/index.js.map +1 -1
- package/build/runtime.js +23 -10
- package/build/runtime.js.map +1 -1
- package/package.json +1 -1
package/build/runtime.js
CHANGED
|
@@ -7,6 +7,8 @@ var TrussDebugInfo = class {
|
|
|
7
7
|
}
|
|
8
8
|
};
|
|
9
9
|
var shouldValidateTrussStyleValues = resolveShouldValidateTrussStyleValues();
|
|
10
|
+
var TRUSS_CSS_CHUNKS = "__trussCssChunks__";
|
|
11
|
+
var trussStyleElement = null;
|
|
10
12
|
function trussProps(...hashes) {
|
|
11
13
|
const merged = {};
|
|
12
14
|
for (const hash of hashes) {
|
|
@@ -60,17 +62,15 @@ function mergeProps(explicitClassName, explicitStyle, ...hashes) {
|
|
|
60
62
|
return result;
|
|
61
63
|
}
|
|
62
64
|
function __injectTrussCSS(cssText) {
|
|
63
|
-
if (typeof document === "undefined") return;
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
document.head.appendChild(style);
|
|
70
|
-
}
|
|
71
|
-
if (!style.textContent?.includes(cssText)) {
|
|
72
|
-
style.textContent = (style.textContent ?? "") + cssText;
|
|
65
|
+
if (typeof document === "undefined" || cssText.length === 0) return;
|
|
66
|
+
const style = getOrCreateTrussStyleElement();
|
|
67
|
+
const injectedChunks = style[TRUSS_CSS_CHUNKS] ??= /* @__PURE__ */ new Set();
|
|
68
|
+
if (injectedChunks.has(cssText) || style.textContent?.includes(cssText)) {
|
|
69
|
+
injectedChunks.add(cssText);
|
|
70
|
+
return;
|
|
73
71
|
}
|
|
72
|
+
injectedChunks.add(cssText);
|
|
73
|
+
style.textContent = (style.textContent ?? "") + cssText;
|
|
74
74
|
}
|
|
75
75
|
function assertValidTrussStyleValue(key, value) {
|
|
76
76
|
if (typeof value === "string") return;
|
|
@@ -101,6 +101,19 @@ function resolveShouldValidateTrussStyleValues() {
|
|
|
101
101
|
}
|
|
102
102
|
return false;
|
|
103
103
|
}
|
|
104
|
+
function getOrCreateTrussStyleElement() {
|
|
105
|
+
const id = "data-truss";
|
|
106
|
+
if (trussStyleElement?.ownerDocument === document && trussStyleElement.isConnected) {
|
|
107
|
+
return trussStyleElement;
|
|
108
|
+
}
|
|
109
|
+
const style = document.querySelector(`style[${id}]`) ?? document.createElement("style");
|
|
110
|
+
if (!style.isConnected) {
|
|
111
|
+
style.setAttribute(id, "");
|
|
112
|
+
document.head.appendChild(style);
|
|
113
|
+
}
|
|
114
|
+
trussStyleElement = style;
|
|
115
|
+
return trussStyleElement;
|
|
116
|
+
}
|
|
104
117
|
export {
|
|
105
118
|
TrussDebugInfo,
|
|
106
119
|
__injectTrussCSS,
|
package/build/runtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/runtime.ts"],"sourcesContent":["/** A compact source label for a Truss CSS expression, used in debug mode. */\nexport class TrussDebugInfo {\n /** I.e. `\"FileName.tsx:line\"` */\n readonly src: string;\n\n constructor(src: string) {\n this.src = src;\n }\n}\n\n/**\n * Space-separated atomic class names, or a variable tuple with class names + CSS variable map.\n *\n * In debug mode, the transform appends a TrussDebugInfo as an extra tuple element:\n * - static with debug: `[classNames, debugInfo]`\n * - variable with debug: `[classNames, vars, debugInfo]`\n */\nexport type TrussStyleValue =\n | string\n | [classNames: string, vars: Record<string, string>]\n | [classNames: string, debugInfo: TrussDebugInfo]\n | [classNames: string, vars: Record<string, string>, debugInfo: TrussDebugInfo];\n\n/** A property-keyed style hash where each key owns one logical CSS property. */\nexport type TrussStyleHash = Record<string, TrussStyleValue>;\n\nconst shouldValidateTrussStyleValues = resolveShouldValidateTrussStyleValues();\n\n/** Merge one or more Truss style hashes into `{ className, style?, data-truss-src? }`. */\nexport function trussProps(\n ...hashes: ReadonlyArray<TrussStyleHash | false | null | undefined>\n): Record<string, unknown> {\n const merged: Record<string, TrussStyleValue> = {};\n\n for (const hash of hashes) {\n if (!hash || typeof hash !== \"object\") continue;\n Object.assign(merged, hash);\n }\n\n const classNames: string[] = [];\n const inlineStyle: Record<string, string> = {};\n const debugSources: string[] = [];\n\n for (const [key, value] of Object.entries(merged)) {\n if (shouldValidateTrussStyleValues) assertValidTrussStyleValue(key, value);\n\n // __marker is a special key — its value is a marker class name, not a CSS property\n if (key === \"__marker\") {\n if (typeof value === \"string\") {\n classNames.push(value);\n }\n continue;\n }\n\n if (typeof value === \"string\") {\n // I.e. \"df\" or \"black blue_h\"\n classNames.push(value);\n continue;\n }\n\n // Tuple: [classNames, varsOrDebug?, maybeDebug?]\n classNames.push(value[0]);\n\n for (let i = 1; i < value.length; i++) {\n const el = value[i];\n if (el instanceof TrussDebugInfo) {\n debugSources.push(el.src);\n } else if (typeof el === \"object\" && el !== null) {\n Object.assign(inlineStyle, el);\n }\n }\n }\n\n const props: Record<string, unknown> = {\n className: classNames.join(\" \"),\n };\n\n if (Object.keys(inlineStyle).length > 0) {\n props.style = inlineStyle;\n }\n\n if (debugSources.length > 0) {\n props[\"data-truss-src\"] = [...new Set(debugSources)].join(\"; \");\n }\n\n return props;\n}\n\n/** Merge explicit className/style with Truss style hashes. */\nexport function mergeProps(\n explicitClassName: string | undefined,\n explicitStyle: Record<string, unknown> | undefined,\n ...hashes: ReadonlyArray<TrussStyleHash | false | null | undefined>\n): Record<string, unknown> {\n const result = trussProps(...hashes);\n\n if (explicitClassName) {\n result.className = `${explicitClassName} ${result.className ?? \"\"}`.trim();\n }\n\n if (explicitStyle) {\n result.style = { ...explicitStyle, ...(result.style as Record<string, unknown> | undefined) };\n }\n\n return result;\n}\n\n/**\n * Inject CSS text into the document for jsdom/test environments.\n *\n * In browser dev mode, CSS is served via the Vite virtual endpoint instead.\n */\nexport function __injectTrussCSS(cssText: string): void {\n if (typeof document === \"undefined\") return;\n\n const
|
|
1
|
+
{"version":3,"sources":["../src/runtime.ts"],"sourcesContent":["/** A compact source label for a Truss CSS expression, used in debug mode. */\nexport class TrussDebugInfo {\n /** I.e. `\"FileName.tsx:line\"` */\n readonly src: string;\n\n constructor(src: string) {\n this.src = src;\n }\n}\n\n/**\n * Space-separated atomic class names, or a variable tuple with class names + CSS variable map.\n *\n * In debug mode, the transform appends a TrussDebugInfo as an extra tuple element:\n * - static with debug: `[classNames, debugInfo]`\n * - variable with debug: `[classNames, vars, debugInfo]`\n */\nexport type TrussStyleValue =\n | string\n | [classNames: string, vars: Record<string, string>]\n | [classNames: string, debugInfo: TrussDebugInfo]\n | [classNames: string, vars: Record<string, string>, debugInfo: TrussDebugInfo];\n\n/** A property-keyed style hash where each key owns one logical CSS property. */\nexport type TrussStyleHash = Record<string, TrussStyleValue>;\n\nconst shouldValidateTrussStyleValues = resolveShouldValidateTrussStyleValues();\nconst TRUSS_CSS_CHUNKS = \"__trussCssChunks__\";\nlet trussStyleElement: TrussStyleElement | null = null;\n\n/** Merge one or more Truss style hashes into `{ className, style?, data-truss-src? }`. */\nexport function trussProps(\n ...hashes: ReadonlyArray<TrussStyleHash | false | null | undefined>\n): Record<string, unknown> {\n const merged: Record<string, TrussStyleValue> = {};\n\n for (const hash of hashes) {\n if (!hash || typeof hash !== \"object\") continue;\n Object.assign(merged, hash);\n }\n\n const classNames: string[] = [];\n const inlineStyle: Record<string, string> = {};\n const debugSources: string[] = [];\n\n for (const [key, value] of Object.entries(merged)) {\n if (shouldValidateTrussStyleValues) assertValidTrussStyleValue(key, value);\n\n // __marker is a special key — its value is a marker class name, not a CSS property\n if (key === \"__marker\") {\n if (typeof value === \"string\") {\n classNames.push(value);\n }\n continue;\n }\n\n if (typeof value === \"string\") {\n // I.e. \"df\" or \"black blue_h\"\n classNames.push(value);\n continue;\n }\n\n // Tuple: [classNames, varsOrDebug?, maybeDebug?]\n classNames.push(value[0]);\n\n for (let i = 1; i < value.length; i++) {\n const el = value[i];\n if (el instanceof TrussDebugInfo) {\n debugSources.push(el.src);\n } else if (typeof el === \"object\" && el !== null) {\n Object.assign(inlineStyle, el);\n }\n }\n }\n\n const props: Record<string, unknown> = {\n className: classNames.join(\" \"),\n };\n\n if (Object.keys(inlineStyle).length > 0) {\n props.style = inlineStyle;\n }\n\n if (debugSources.length > 0) {\n props[\"data-truss-src\"] = [...new Set(debugSources)].join(\"; \");\n }\n\n return props;\n}\n\n/** Merge explicit className/style with Truss style hashes. */\nexport function mergeProps(\n explicitClassName: string | undefined,\n explicitStyle: Record<string, unknown> | undefined,\n ...hashes: ReadonlyArray<TrussStyleHash | false | null | undefined>\n): Record<string, unknown> {\n const result = trussProps(...hashes);\n\n if (explicitClassName) {\n result.className = `${explicitClassName} ${result.className ?? \"\"}`.trim();\n }\n\n if (explicitStyle) {\n result.style = { ...explicitStyle, ...(result.style as Record<string, unknown> | undefined) };\n }\n\n return result;\n}\n\n/**\n * Inject CSS text into the document for jsdom/test environments.\n *\n * In browser dev mode, CSS is served via the Vite virtual endpoint instead.\n */\nexport function __injectTrussCSS(cssText: string): void {\n if (typeof document === \"undefined\" || cssText.length === 0) return;\n\n const style = getOrCreateTrussStyleElement();\n\n // Track exact injected chunks on the style node so repeated execution of the\n // test bootstrap or transformed modules does not append duplicate CSS text.\n const injectedChunks = (style[TRUSS_CSS_CHUNKS] ??= new Set<string>());\n if (injectedChunks.has(cssText) || style.textContent?.includes(cssText)) {\n injectedChunks.add(cssText);\n return;\n }\n\n injectedChunks.add(cssText);\n style.textContent = (style.textContent ?? \"\") + cssText;\n}\n\n/** Fail fast when `trussProps` receives a non-Truss style value. */\nfunction assertValidTrussStyleValue(key: string, value: unknown): asserts value is TrussStyleValue {\n if (typeof value === \"string\") return;\n if (Array.isArray(value) && typeof value[0] === \"string\") {\n for (let i = 1; i < value.length; i++) {\n const el = value[i];\n if (el instanceof TrussDebugInfo) continue;\n if (typeof el === \"object\" && el !== null && !Array.isArray(el)) continue;\n throw new TypeError(invalidTrussStyleValueMessage(key));\n }\n return;\n }\n throw new TypeError(invalidTrussStyleValueMessage(key));\n}\n\nfunction invalidTrussStyleValueMessage(key: string): string {\n return `Invalid Truss style value for \\`${key}\\`. trussProps only accepts generated Truss style hashes; use mergeProps for explicit className/style merging.`;\n}\n\n/** Enable validation in dev/test environments, but skip it in production. */\nfunction resolveShouldValidateTrussStyleValues(): boolean {\n if (typeof process !== \"undefined\" && typeof process.env.NODE_ENV === \"string\") {\n return process.env.NODE_ENV !== \"production\";\n }\n const viteEnv = (import.meta as ImportMeta & { env?: { DEV?: boolean; PROD?: boolean } }).env;\n if (typeof viteEnv?.DEV === \"boolean\") {\n return viteEnv.DEV;\n }\n if (typeof viteEnv?.PROD === \"boolean\") {\n return !viteEnv.PROD;\n }\n return false;\n}\n\nfunction getOrCreateTrussStyleElement(): TrussStyleElement {\n const id = \"data-truss\";\n if (trussStyleElement?.ownerDocument === document && trussStyleElement.isConnected) {\n return trussStyleElement;\n }\n\n const style = (document.querySelector(`style[${id}]`) as TrussStyleElement | null) ?? document.createElement(\"style\");\n if (!style.isConnected) {\n style.setAttribute(id, \"\");\n document.head.appendChild(style);\n }\n trussStyleElement = style;\n return trussStyleElement;\n}\n\ntype TrussStyleElement = HTMLStyleElement & {\n [TRUSS_CSS_CHUNKS]?: Set<string>;\n};\n"],"mappings":";AACO,IAAM,iBAAN,MAAqB;AAAA;AAAA,EAEjB;AAAA,EAET,YAAY,KAAa;AACvB,SAAK,MAAM;AAAA,EACb;AACF;AAkBA,IAAM,iCAAiC,sCAAsC;AAC7E,IAAM,mBAAmB;AACzB,IAAI,oBAA8C;AAG3C,SAAS,cACX,QACsB;AACzB,QAAM,SAA0C,CAAC;AAEjD,aAAW,QAAQ,QAAQ;AACzB,QAAI,CAAC,QAAQ,OAAO,SAAS,SAAU;AACvC,WAAO,OAAO,QAAQ,IAAI;AAAA,EAC5B;AAEA,QAAM,aAAuB,CAAC;AAC9B,QAAM,cAAsC,CAAC;AAC7C,QAA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