@kithinji/arcane 1.0.5 → 1.0.7
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/node/index.cjs +30 -13
- package/dist/node/index.cjs.map +3 -3
- package/dist/node/index.mjs +30 -13
- package/dist/node/index.mjs.map +3 -3
- package/dist/types/inline-file/inline.d.ts +1 -2
- package/dist/types/inline-file/inline.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/inline-file/inline.ts +26 -9
package/dist/node/index.cjs
CHANGED
|
@@ -123,20 +123,20 @@ function normalizeValue(prop, value) {
|
|
|
123
123
|
function isPseudoOrMediaKey(key) {
|
|
124
124
|
return key.startsWith(":") || key.startsWith("@") || key.startsWith("&");
|
|
125
125
|
}
|
|
126
|
-
function validateStyleValue(value,
|
|
126
|
+
function validateStyleValue(value, path2) {
|
|
127
127
|
if (value === null) {
|
|
128
128
|
throw new Error(
|
|
129
|
-
`Invalid style value at ${
|
|
129
|
+
`Invalid style value at ${path2}: null is not allowed. Use undefined or omit the property.`
|
|
130
130
|
);
|
|
131
131
|
}
|
|
132
132
|
if (typeof value === "function") {
|
|
133
133
|
throw new Error(
|
|
134
|
-
`Invalid style value at ${
|
|
134
|
+
`Invalid style value at ${path2}: functions must be resolved at build time.`
|
|
135
135
|
);
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
|
-
function processStyleValue(prop, value, cssRules,
|
|
139
|
-
validateStyleValue(value,
|
|
138
|
+
function processStyleValue(prop, value, cssRules, path2 = prop) {
|
|
139
|
+
validateStyleValue(value, path2);
|
|
140
140
|
if (prop.startsWith("--")) {
|
|
141
141
|
const className2 = hashProperty(prop, value, cssRules);
|
|
142
142
|
if (!cssRules.has(className2)) {
|
|
@@ -149,7 +149,7 @@ function processStyleValue(prop, value, cssRules, path = prop) {
|
|
|
149
149
|
if (typeof value === "object" && !Array.isArray(value)) {
|
|
150
150
|
const classes = [];
|
|
151
151
|
for (const [nestedKey, nestedValue] of Object.entries(value)) {
|
|
152
|
-
validateStyleValue(nestedValue, `${
|
|
152
|
+
validateStyleValue(nestedValue, `${path2}.${nestedKey}`);
|
|
153
153
|
if (nestedKey === "default") {
|
|
154
154
|
const normalizedValue2 = normalizeValue(
|
|
155
155
|
prop,
|
|
@@ -186,7 +186,7 @@ function processStyleValue(prop, value, cssRules, path = prop) {
|
|
|
186
186
|
classes.push(className2);
|
|
187
187
|
} else {
|
|
188
188
|
throw new Error(
|
|
189
|
-
`Invalid nested key "${nestedKey}" at ${
|
|
189
|
+
`Invalid nested key "${nestedKey}" at ${path2}. Expected "default", a pseudo-class (":hover"), media query ("@media"), or nesting selector ("&").`
|
|
190
190
|
);
|
|
191
191
|
}
|
|
192
192
|
}
|
|
@@ -415,16 +415,33 @@ function apply$(...c) {
|
|
|
415
415
|
}
|
|
416
416
|
|
|
417
417
|
// src/inline-file/inline.ts
|
|
418
|
-
|
|
418
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
419
|
+
var import_path = __toESM(require("path"), 1);
|
|
420
|
+
function inlineFile$(filePath, context) {
|
|
419
421
|
if (!context) {
|
|
420
422
|
throw new Error(
|
|
421
|
-
"
|
|
423
|
+
"inlineFile$ macro requires MacroContext. Ensure you're using this as a build-time macro."
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
const pathNode = filePath;
|
|
427
|
+
const resolvedPath = context.resolveNodeValue(pathNode);
|
|
428
|
+
if (typeof resolvedPath !== "string") {
|
|
429
|
+
throw new Error(
|
|
430
|
+
`inlineFile$ macro requires a string literal path, got: ${typeof resolvedPath}`
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
const sourceFile = context.sourceFile;
|
|
434
|
+
const sourceDir = import_path.default.dirname(sourceFile.fileName);
|
|
435
|
+
const absolutePath = import_path.default.resolve(sourceDir, resolvedPath);
|
|
436
|
+
let content;
|
|
437
|
+
try {
|
|
438
|
+
content = import_fs.default.readFileSync(absolutePath, "utf-8");
|
|
439
|
+
} catch (error) {
|
|
440
|
+
throw new Error(
|
|
441
|
+
`Failed to read file "${absolutePath}": ${error instanceof Error ? error.message : String(error)}`
|
|
422
442
|
);
|
|
423
443
|
}
|
|
424
|
-
|
|
425
|
-
const value = context.resolveNodeValue(rPath);
|
|
426
|
-
console.log(value);
|
|
427
|
-
return context.factory.createStringLiteral("file.txt");
|
|
444
|
+
return context.factory.createStringLiteral(content);
|
|
428
445
|
}
|
|
429
446
|
// Annotate the CommonJS export names for ESM import in node:
|
|
430
447
|
0 && (module.exports = {
|
package/dist/node/index.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.node.ts", "../../src/style/style.ts", "../../src/inline-file/inline.ts"],
|
|
4
|
-
"sourcesContent": ["export * from \"./style\";\nexport * from \"./inline-file\";\n", "import { MacroContext } from \"@kithinji/pod\";\nimport ts from \"typescript\";\nimport { MapNamespaces } from \"./style_types\";\n\nconst UNITLESS_PROPS = new Set([\n \"z-index\",\n \"opacity\",\n \"flex-grow\",\n \"flex-shrink\",\n \"flex\",\n \"order\",\n \"font-weight\",\n \"line-height\",\n \"zoom\",\n \"column-count\",\n \"animation-iteration-count\",\n \"grid-column\",\n \"grid-row\",\n \"grid-column-start\",\n \"grid-column-end\",\n \"grid-row-start\",\n \"grid-row-end\",\n \"tab-size\",\n \"counter-increment\",\n \"counter-reset\",\n \"orphans\",\n \"widows\",\n]);\n\nconst DEV_MODE = process.env.NODE_ENV === \"development\";\n\nfunction simpleHash(str: string): string {\n let hash = 0;\n for (let i = 0; i < str.length; i++) {\n const char = str.charCodeAt(i);\n hash = (hash << 5) - hash + char;\n hash = hash & hash;\n }\n\n return Math.abs(hash).toString(36).padStart(8, \"0\").slice(0, 8);\n}\n\nfunction hashProperty(\n prop: string,\n value: string | number,\n cssRules: Map<string, string>\n): string {\n const input = `${prop}:${value}`;\n let hash = simpleHash(input);\n let className: string;\n\n if (DEV_MODE) {\n // Add readable hint in development\n const hint = prop\n .replace(/[^a-z]/gi, \"\")\n .slice(0, 3)\n .toLowerCase();\n className = `a-${hint}-${hash}`;\n } else {\n className = `a-${hash}`;\n }\n\n let attempt = 0;\n\n // Handle hash collisions\n while (cssRules.has(className)) {\n const existing = cssRules.get(className)!;\n const kebabProp = toKebabCase(prop);\n const normalizedValue = normalizeValue(prop, value);\n const expectedRule = `.${className} { ${kebabProp}: ${normalizedValue}; }`;\n\n // If the existing rule matches, it's the same style (deduplication)\n if (existing.includes(`${kebabProp}: ${normalizedValue}`)) {\n break;\n }\n\n // Collision detected, rehash\n hash = simpleHash(`${input}-${++attempt}`);\n className = DEV_MODE ? `a-${prop.slice(0, 3)}-${hash}` : `a-${hash}`;\n\n if (attempt > 100) {\n throw new Error(\n `Hash collision limit exceeded for property ${prop}:${value}`\n );\n }\n }\n\n return className;\n}\n\nfunction toKebabCase(str: string): string {\n // Handle vendor prefixes (webkit, moz, ms)\n if (str.match(/^(webkit|moz|ms)[A-Z]/)) {\n return \"-\" + str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);\n }\n\n // Handle CSS custom properties\n if (str.startsWith(\"--\")) {\n return str;\n }\n\n return str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);\n}\n\nfunction normalizeValue(prop: string, value: string | number): string {\n if (typeof value === \"number\") {\n const kebabProp = toKebabCase(prop);\n\n if (UNITLESS_PROPS.has(kebabProp)) {\n return String(value);\n }\n\n return `${value}px`;\n }\n\n return String(value);\n}\n\nfunction isPseudoOrMediaKey(key: string): boolean {\n return key.startsWith(\":\") || key.startsWith(\"@\") || key.startsWith(\"&\");\n}\n\nfunction validateStyleValue(value: any, path: string): void {\n if (value === null) {\n throw new Error(\n `Invalid style value at ${path}: null is not allowed. Use undefined or omit the property.`\n );\n }\n\n if (typeof value === \"function\") {\n throw new Error(\n `Invalid style value at ${path}: functions must be resolved at build time.`\n );\n }\n}\n\nfunction processStyleValue(\n prop: string,\n value: any,\n cssRules: Map<string, string>,\n path: string = prop\n): string {\n validateStyleValue(value, path);\n\n if (prop.startsWith(\"--\")) {\n const className = hashProperty(prop, value, cssRules);\n if (!cssRules.has(className)) {\n const cssRule = `.${className} { ${prop}: ${value}; }`;\n cssRules.set(className, cssRule);\n }\n return className;\n }\n\n const kebabProp = toKebabCase(prop);\n\n // Handle nested objects for pseudo-classes, media queries, etc.\n if (typeof value === \"object\" && !Array.isArray(value)) {\n const classes: string[] = [];\n\n for (const [nestedKey, nestedValue] of Object.entries(value)) {\n validateStyleValue(nestedValue, `${path}.${nestedKey}`);\n\n if (nestedKey === \"default\") {\n const normalizedValue = normalizeValue(\n prop,\n nestedValue as string | number\n );\n const className = hashProperty(prop, normalizedValue, cssRules);\n\n if (!cssRules.has(className)) {\n const cssRule = `.${className} { ${kebabProp}: ${normalizedValue}; }`;\n cssRules.set(className, cssRule);\n }\n\n classes.push(className);\n } else if (isPseudoOrMediaKey(nestedKey)) {\n const normalizedValue = normalizeValue(\n prop,\n nestedValue as string | number\n );\n const className = hashProperty(\n `${prop}${nestedKey}`,\n normalizedValue,\n cssRules\n );\n\n if (!cssRules.has(className)) {\n let cssRule: string;\n\n if (nestedKey.startsWith(\"@\")) {\n // Media query\n cssRule = `${nestedKey} { .${className} { ${kebabProp}: ${normalizedValue}; } }`;\n } else if (nestedKey.startsWith(\"&\")) {\n // Nesting selector (e.g., &:hover, & > div)\n const selector = nestedKey.slice(1);\n cssRule = `.${className}${selector} { ${kebabProp}: ${normalizedValue}; }`;\n } else {\n // Pseudo-class/element (e.g., :hover, ::before)\n cssRule = `.${className}${nestedKey} { ${kebabProp}: ${normalizedValue}; }`;\n }\n\n cssRules.set(className, cssRule);\n }\n\n classes.push(className);\n } else {\n throw new Error(\n `Invalid nested key \"${nestedKey}\" at ${path}. Expected \"default\", a pseudo-class (\":hover\"), media query (\"@media\"), or nesting selector (\"&\").`\n );\n }\n }\n\n return classes.join(\" \");\n }\n\n const normalizedValue = normalizeValue(prop, value);\n const className = hashProperty(prop, normalizedValue, cssRules);\n\n if (!cssRules.has(className)) {\n const cssRule = `.${className} { ${kebabProp}: ${normalizedValue}; }`;\n cssRules.set(className, cssRule);\n }\n\n return className;\n}\n\nfunction processStyleObject(\n styleObj: Record<string, any>,\n cssRules: Map<string, string>\n): Record<string, string> {\n const result: Record<string, string> = {};\n\n for (const [namespace, styles] of Object.entries(styleObj)) {\n if (typeof styles !== \"object\" || Array.isArray(styles)) {\n throw new Error(\n `Invalid style namespace \"${namespace}\": expected an object, got ${typeof styles}`\n );\n }\n\n const classes: string[] = [];\n\n for (const [prop, value] of Object.entries(styles)) {\n if (value === undefined) continue;\n\n const className = processStyleValue(\n prop,\n value,\n cssRules,\n `${namespace}.${prop}`\n );\n\n if (className) {\n classes.push(className);\n }\n }\n\n result[namespace] = classes.filter(Boolean).join(\" \");\n }\n\n return result;\n}\n\nexport function style$<const T extends Record<string, any>>(\n style: T,\n context?: MacroContext\n): MapNamespaces<T> {\n if (!context) {\n throw new Error(\n \"style$ macro requires MacroContext. Ensure you're using this as a build-time macro.\"\n );\n }\n\n const rStyle = style as unknown as ts.Node;\n const value = context.resolveNodeValue(rStyle);\n\n if (value == undefined) {\n throw new Error(\n `Could not resolve style object at build time. ` +\n `Ensure all values are statically analyzable (no runtime expressions, dynamic imports should be inlined).`\n );\n }\n\n if (typeof value !== \"object\" || Array.isArray(value)) {\n throw new Error(\n `style$ expects an object with style namespaces, got ${typeof value}`\n );\n }\n\n const cssRules = new Map<string, string>();\n const classNameMap = processStyleObject(value, cssRules);\n\n context.store.set(\"style_rules\", Array.from(cssRules.values()));\n\n const properties = Object.entries(classNameMap).map(([key, className]) =>\n context.factory.createPropertyAssignment(\n context.factory.createStringLiteral(key),\n context.factory.createStringLiteral(className)\n )\n );\n\n return context.factory.createObjectLiteralExpression(properties, true) as any;\n}\n\ntype Fragment =\n | { kind: \"static\"; value: string }\n | { kind: \"dynamic\"; expr: ts.Expression; stringSafe: boolean };\n\nexport function apply$(...c: any[]) {\n if (c.length < 1) {\n throw new Error(\"apply$ requires at least one argument plus MacroContext\");\n }\n\n const context = c.pop() as MacroContext;\n\n if (!context || !context.factory) {\n throw new Error(\n \"apply$ macro requires MacroContext as the last argument. Ensure you're using this as a build-time macro.\"\n );\n }\n\n const args = c as ts.Expression[];\n\n if (args.length === 0) {\n return context.factory.createObjectLiteralExpression(\n [\n context.factory.createPropertyAssignment(\n context.factory.createIdentifier(\"className\"),\n context.factory.createStringLiteral(\"\")\n ),\n ],\n false\n );\n }\n\n const f = context.factory;\n\n function isTrue(e: ts.Expression): boolean {\n return e.kind === ts.SyntaxKind.TrueKeyword;\n }\n\n function isFalse(e: ts.Expression): boolean {\n return e.kind === ts.SyntaxKind.FalseKeyword;\n }\n\n function isEmptyString(e: ts.Expression): boolean {\n return ts.isStringLiteral(e) && e.text === \"\";\n }\n\n function tryResolveStatic(expr: ts.Expression): string | null {\n // Try to resolve string literals directly\n if (ts.isStringLiteral(expr)) {\n return expr.text;\n }\n\n // Try to resolve property access chains (e.g., classes.button)\n if (!ts.isPropertyAccessExpression(expr)) return null;\n\n const chain: string[] = [];\n let cur: ts.Expression = expr;\n\n while (ts.isPropertyAccessExpression(cur)) {\n chain.unshift(cur.name.text);\n cur = cur.expression;\n }\n\n if (!ts.isIdentifier(cur)) return null;\n chain.unshift(cur.text);\n\n const root = context.resolveIdentifier(f.createIdentifier(chain[0]));\n let value = context.resolveNodeValue(root);\n\n if (value == null) return null;\n\n for (let i = 1; i < chain.length; i++) {\n if (typeof value !== \"object\" || !(chain[i] in value)) {\n return null;\n }\n value = value[chain[i]];\n }\n\n return typeof value === \"string\" ? value : null;\n }\n\n function build(expr: ts.Expression): Fragment {\n // Handle empty strings\n if (isEmptyString(expr)) {\n return { kind: \"static\", value: \"\" };\n }\n\n // Try static resolution first\n const s = tryResolveStatic(expr);\n if (s != null) {\n return { kind: \"static\", value: s };\n }\n\n // Handle: condition && \"class\"\n if (\n ts.isBinaryExpression(expr) &&\n expr.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken\n ) {\n if (isTrue(expr.left)) return build(expr.right);\n if (isFalse(expr.left)) return { kind: \"static\", value: \"\" };\n\n const rs = tryResolveStatic(expr.right);\n if (rs != null) {\n // Optimize to: [\"\", \"class\"][+condition]\n return {\n kind: \"dynamic\",\n stringSafe: true,\n expr: f.createElementAccessExpression(\n f.createArrayLiteralExpression([\n f.createStringLiteral(\"\"),\n f.createStringLiteral(rs),\n ]),\n f.createPrefixUnaryExpression(ts.SyntaxKind.PlusToken, expr.left)\n ),\n };\n }\n\n return { kind: \"dynamic\", expr, stringSafe: false };\n }\n\n // Handle: condition ? \"a\" : \"b\"\n if (ts.isConditionalExpression(expr)) {\n if (isTrue(expr.condition)) return build(expr.whenTrue);\n if (isFalse(expr.condition)) return build(expr.whenFalse);\n\n const t = tryResolveStatic(expr.whenTrue);\n const fv = tryResolveStatic(expr.whenFalse);\n\n if (t != null && fv != null) {\n // Optimize to: [\"b\", \"a\"][+condition]\n return {\n kind: \"dynamic\",\n stringSafe: true,\n expr: f.createElementAccessExpression(\n f.createArrayLiteralExpression([\n f.createStringLiteral(fv),\n f.createStringLiteral(t),\n ]),\n f.createPrefixUnaryExpression(\n ts.SyntaxKind.PlusToken,\n expr.condition\n )\n ),\n };\n }\n\n return { kind: \"dynamic\", expr, stringSafe: false };\n }\n\n return { kind: \"dynamic\", expr, stringSafe: false };\n }\n\n // Build and merge fragments\n const frags: Fragment[] = [];\n\n for (const arg of args) {\n const frag = build(arg);\n\n // Skip empty static values\n if (frag.kind === \"static\" && frag.value === \"\") {\n continue;\n }\n\n const last = frags[frags.length - 1];\n\n // Merge consecutive static fragments\n if (frag.kind === \"static\" && last?.kind === \"static\") {\n last.value += \" \" + frag.value;\n } else {\n frags.push(frag);\n }\n }\n\n // Generate final className expression\n let classExpr: ts.Expression;\n\n if (frags.length === 0) {\n // All classes were empty\n classExpr = f.createStringLiteral(\"\");\n } else if (frags.every((f) => f.kind === \"static\")) {\n // All static - compile to single string\n classExpr = f.createStringLiteral(\n frags\n .map((f) => f.value)\n .join(\" \")\n .trim()\n );\n } else {\n // Mixed static/dynamic - generate array.join(\" \")\n classExpr = f.createCallExpression(\n f.createPropertyAccessExpression(\n f.createArrayLiteralExpression(\n frags.map((frag) => {\n if (frag.kind === \"static\") {\n return f.createStringLiteral(frag.value);\n }\n if (frag.stringSafe) {\n return frag.expr;\n }\n // Wrap unsafe expressions in conditional: expr ? expr : \"\"\n return f.createConditionalExpression(\n frag.expr,\n undefined,\n frag.expr,\n undefined,\n f.createStringLiteral(\"\")\n );\n })\n ),\n \"join\"\n ),\n undefined,\n [f.createStringLiteral(\" \")]\n );\n }\n\n return f.createObjectLiteralExpression(\n [f.createPropertyAssignment(f.createIdentifier(\"className\"), classExpr)],\n false\n );\n}\n", "import { MacroContext } from \"@kithinji/pod\";\nimport ts from \"typescript\";\n\nexport function inlineFile$(path: string, context?: MacroContext) {\n if (!context) {\n throw new Error(\n \"style$ macro requires MacroContext. Ensure you're using this as a build-time macro.\"\n );\n }\n\n const rPath = path as unknown as ts.Node;\n const value = context.resolveNodeValue(rPath);\n\n console.log(value);\n\n // read file\n // escape problematic characters\n\n // return file content as a string\n return context.factory.createStringLiteral(\"file.txt\");\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,wBAAe;AAGf,IAAM,iBAAiB,oBAAI,IAAI;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAED,IAAM,WAAW,QAAQ,IAAI,aAAa;AAE1C,SAAS,WAAW,KAAqB;A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,UAAU,CAAC,WAAW,IAAI,OAAO,YAAY,CAAC,EAAE;AAAA,EAC3E;AAGA,MAAI,IAAI,WAAW,IAAI,GAAG;AACxB,WAAO;AAAA,EACT;AAEA,SAAO,IAAI,QAAQ,UAAU,CAAC,WAAW,IAAI,OAAO,YAAY,CAAC,EAAE;AACrE;AAEA,SAAS,eAAe,MAAc,OAAgC;AACpE,MAAI,OAAO,UAAU,UAAU;AAC7B,UAAM,YAAY,YAAY,IAAI;AAElC,QAAI,eAAe,IAAI,SAAS,GAAG;AACjC,aAAO,OAAO,KAAK;AAAA,IACrB;AAEA,WAAO,GAAG,KAAK;AAAA,EACjB;AAEA,SAAO,OAAO,KAAK;AACrB;AAEA,SAAS,mBAAmB,KAAsB;AAChD,SAAO,IAAI,WAAW,GAAG,KAAK,IAAI,WAAW,GAAG,KAAK,IAAI,WAAW,GAAG;AACzE;AAEA,SAAS,mBAAmB,
|
|
6
|
-
"names": ["className", "normalizedValue", "ts", "f"]
|
|
4
|
+
"sourcesContent": ["export * from \"./style\";\nexport * from \"./inline-file\";\n", "import { MacroContext } from \"@kithinji/pod\";\nimport ts from \"typescript\";\nimport { MapNamespaces } from \"./style_types\";\n\nconst UNITLESS_PROPS = new Set([\n \"z-index\",\n \"opacity\",\n \"flex-grow\",\n \"flex-shrink\",\n \"flex\",\n \"order\",\n \"font-weight\",\n \"line-height\",\n \"zoom\",\n \"column-count\",\n \"animation-iteration-count\",\n \"grid-column\",\n \"grid-row\",\n \"grid-column-start\",\n \"grid-column-end\",\n \"grid-row-start\",\n \"grid-row-end\",\n \"tab-size\",\n \"counter-increment\",\n \"counter-reset\",\n \"orphans\",\n \"widows\",\n]);\n\nconst DEV_MODE = process.env.NODE_ENV === \"development\";\n\nfunction simpleHash(str: string): string {\n let hash = 0;\n for (let i = 0; i < str.length; i++) {\n const char = str.charCodeAt(i);\n hash = (hash << 5) - hash + char;\n hash = hash & hash;\n }\n\n return Math.abs(hash).toString(36).padStart(8, \"0\").slice(0, 8);\n}\n\nfunction hashProperty(\n prop: string,\n value: string | number,\n cssRules: Map<string, string>\n): string {\n const input = `${prop}:${value}`;\n let hash = simpleHash(input);\n let className: string;\n\n if (DEV_MODE) {\n // Add readable hint in development\n const hint = prop\n .replace(/[^a-z]/gi, \"\")\n .slice(0, 3)\n .toLowerCase();\n className = `a-${hint}-${hash}`;\n } else {\n className = `a-${hash}`;\n }\n\n let attempt = 0;\n\n // Handle hash collisions\n while (cssRules.has(className)) {\n const existing = cssRules.get(className)!;\n const kebabProp = toKebabCase(prop);\n const normalizedValue = normalizeValue(prop, value);\n const expectedRule = `.${className} { ${kebabProp}: ${normalizedValue}; }`;\n\n // If the existing rule matches, it's the same style (deduplication)\n if (existing.includes(`${kebabProp}: ${normalizedValue}`)) {\n break;\n }\n\n // Collision detected, rehash\n hash = simpleHash(`${input}-${++attempt}`);\n className = DEV_MODE ? `a-${prop.slice(0, 3)}-${hash}` : `a-${hash}`;\n\n if (attempt > 100) {\n throw new Error(\n `Hash collision limit exceeded for property ${prop}:${value}`\n );\n }\n }\n\n return className;\n}\n\nfunction toKebabCase(str: string): string {\n // Handle vendor prefixes (webkit, moz, ms)\n if (str.match(/^(webkit|moz|ms)[A-Z]/)) {\n return \"-\" + str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);\n }\n\n // Handle CSS custom properties\n if (str.startsWith(\"--\")) {\n return str;\n }\n\n return str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);\n}\n\nfunction normalizeValue(prop: string, value: string | number): string {\n if (typeof value === \"number\") {\n const kebabProp = toKebabCase(prop);\n\n if (UNITLESS_PROPS.has(kebabProp)) {\n return String(value);\n }\n\n return `${value}px`;\n }\n\n return String(value);\n}\n\nfunction isPseudoOrMediaKey(key: string): boolean {\n return key.startsWith(\":\") || key.startsWith(\"@\") || key.startsWith(\"&\");\n}\n\nfunction validateStyleValue(value: any, path: string): void {\n if (value === null) {\n throw new Error(\n `Invalid style value at ${path}: null is not allowed. Use undefined or omit the property.`\n );\n }\n\n if (typeof value === \"function\") {\n throw new Error(\n `Invalid style value at ${path}: functions must be resolved at build time.`\n );\n }\n}\n\nfunction processStyleValue(\n prop: string,\n value: any,\n cssRules: Map<string, string>,\n path: string = prop\n): string {\n validateStyleValue(value, path);\n\n if (prop.startsWith(\"--\")) {\n const className = hashProperty(prop, value, cssRules);\n if (!cssRules.has(className)) {\n const cssRule = `.${className} { ${prop}: ${value}; }`;\n cssRules.set(className, cssRule);\n }\n return className;\n }\n\n const kebabProp = toKebabCase(prop);\n\n // Handle nested objects for pseudo-classes, media queries, etc.\n if (typeof value === \"object\" && !Array.isArray(value)) {\n const classes: string[] = [];\n\n for (const [nestedKey, nestedValue] of Object.entries(value)) {\n validateStyleValue(nestedValue, `${path}.${nestedKey}`);\n\n if (nestedKey === \"default\") {\n const normalizedValue = normalizeValue(\n prop,\n nestedValue as string | number\n );\n const className = hashProperty(prop, normalizedValue, cssRules);\n\n if (!cssRules.has(className)) {\n const cssRule = `.${className} { ${kebabProp}: ${normalizedValue}; }`;\n cssRules.set(className, cssRule);\n }\n\n classes.push(className);\n } else if (isPseudoOrMediaKey(nestedKey)) {\n const normalizedValue = normalizeValue(\n prop,\n nestedValue as string | number\n );\n const className = hashProperty(\n `${prop}${nestedKey}`,\n normalizedValue,\n cssRules\n );\n\n if (!cssRules.has(className)) {\n let cssRule: string;\n\n if (nestedKey.startsWith(\"@\")) {\n // Media query\n cssRule = `${nestedKey} { .${className} { ${kebabProp}: ${normalizedValue}; } }`;\n } else if (nestedKey.startsWith(\"&\")) {\n // Nesting selector (e.g., &:hover, & > div)\n const selector = nestedKey.slice(1);\n cssRule = `.${className}${selector} { ${kebabProp}: ${normalizedValue}; }`;\n } else {\n // Pseudo-class/element (e.g., :hover, ::before)\n cssRule = `.${className}${nestedKey} { ${kebabProp}: ${normalizedValue}; }`;\n }\n\n cssRules.set(className, cssRule);\n }\n\n classes.push(className);\n } else {\n throw new Error(\n `Invalid nested key \"${nestedKey}\" at ${path}. Expected \"default\", a pseudo-class (\":hover\"), media query (\"@media\"), or nesting selector (\"&\").`\n );\n }\n }\n\n return classes.join(\" \");\n }\n\n const normalizedValue = normalizeValue(prop, value);\n const className = hashProperty(prop, normalizedValue, cssRules);\n\n if (!cssRules.has(className)) {\n const cssRule = `.${className} { ${kebabProp}: ${normalizedValue}; }`;\n cssRules.set(className, cssRule);\n }\n\n return className;\n}\n\nfunction processStyleObject(\n styleObj: Record<string, any>,\n cssRules: Map<string, string>\n): Record<string, string> {\n const result: Record<string, string> = {};\n\n for (const [namespace, styles] of Object.entries(styleObj)) {\n if (typeof styles !== \"object\" || Array.isArray(styles)) {\n throw new Error(\n `Invalid style namespace \"${namespace}\": expected an object, got ${typeof styles}`\n );\n }\n\n const classes: string[] = [];\n\n for (const [prop, value] of Object.entries(styles)) {\n if (value === undefined) continue;\n\n const className = processStyleValue(\n prop,\n value,\n cssRules,\n `${namespace}.${prop}`\n );\n\n if (className) {\n classes.push(className);\n }\n }\n\n result[namespace] = classes.filter(Boolean).join(\" \");\n }\n\n return result;\n}\n\nexport function style$<const T extends Record<string, any>>(\n style: T,\n context?: MacroContext\n): MapNamespaces<T> {\n if (!context) {\n throw new Error(\n \"style$ macro requires MacroContext. Ensure you're using this as a build-time macro.\"\n );\n }\n\n const rStyle = style as unknown as ts.Node;\n const value = context.resolveNodeValue(rStyle);\n\n if (value == undefined) {\n throw new Error(\n `Could not resolve style object at build time. ` +\n `Ensure all values are statically analyzable (no runtime expressions, dynamic imports should be inlined).`\n );\n }\n\n if (typeof value !== \"object\" || Array.isArray(value)) {\n throw new Error(\n `style$ expects an object with style namespaces, got ${typeof value}`\n );\n }\n\n const cssRules = new Map<string, string>();\n const classNameMap = processStyleObject(value, cssRules);\n\n context.store.set(\"style_rules\", Array.from(cssRules.values()));\n\n const properties = Object.entries(classNameMap).map(([key, className]) =>\n context.factory.createPropertyAssignment(\n context.factory.createStringLiteral(key),\n context.factory.createStringLiteral(className)\n )\n );\n\n return context.factory.createObjectLiteralExpression(properties, true) as any;\n}\n\ntype Fragment =\n | { kind: \"static\"; value: string }\n | { kind: \"dynamic\"; expr: ts.Expression; stringSafe: boolean };\n\nexport function apply$(...c: any[]) {\n if (c.length < 1) {\n throw new Error(\"apply$ requires at least one argument plus MacroContext\");\n }\n\n const context = c.pop() as MacroContext;\n\n if (!context || !context.factory) {\n throw new Error(\n \"apply$ macro requires MacroContext as the last argument. Ensure you're using this as a build-time macro.\"\n );\n }\n\n const args = c as ts.Expression[];\n\n if (args.length === 0) {\n return context.factory.createObjectLiteralExpression(\n [\n context.factory.createPropertyAssignment(\n context.factory.createIdentifier(\"className\"),\n context.factory.createStringLiteral(\"\")\n ),\n ],\n false\n );\n }\n\n const f = context.factory;\n\n function isTrue(e: ts.Expression): boolean {\n return e.kind === ts.SyntaxKind.TrueKeyword;\n }\n\n function isFalse(e: ts.Expression): boolean {\n return e.kind === ts.SyntaxKind.FalseKeyword;\n }\n\n function isEmptyString(e: ts.Expression): boolean {\n return ts.isStringLiteral(e) && e.text === \"\";\n }\n\n function tryResolveStatic(expr: ts.Expression): string | null {\n // Try to resolve string literals directly\n if (ts.isStringLiteral(expr)) {\n return expr.text;\n }\n\n // Try to resolve property access chains (e.g., classes.button)\n if (!ts.isPropertyAccessExpression(expr)) return null;\n\n const chain: string[] = [];\n let cur: ts.Expression = expr;\n\n while (ts.isPropertyAccessExpression(cur)) {\n chain.unshift(cur.name.text);\n cur = cur.expression;\n }\n\n if (!ts.isIdentifier(cur)) return null;\n chain.unshift(cur.text);\n\n const root = context.resolveIdentifier(f.createIdentifier(chain[0]));\n let value = context.resolveNodeValue(root);\n\n if (value == null) return null;\n\n for (let i = 1; i < chain.length; i++) {\n if (typeof value !== \"object\" || !(chain[i] in value)) {\n return null;\n }\n value = value[chain[i]];\n }\n\n return typeof value === \"string\" ? value : null;\n }\n\n function build(expr: ts.Expression): Fragment {\n // Handle empty strings\n if (isEmptyString(expr)) {\n return { kind: \"static\", value: \"\" };\n }\n\n // Try static resolution first\n const s = tryResolveStatic(expr);\n if (s != null) {\n return { kind: \"static\", value: s };\n }\n\n // Handle: condition && \"class\"\n if (\n ts.isBinaryExpression(expr) &&\n expr.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken\n ) {\n if (isTrue(expr.left)) return build(expr.right);\n if (isFalse(expr.left)) return { kind: \"static\", value: \"\" };\n\n const rs = tryResolveStatic(expr.right);\n if (rs != null) {\n // Optimize to: [\"\", \"class\"][+condition]\n return {\n kind: \"dynamic\",\n stringSafe: true,\n expr: f.createElementAccessExpression(\n f.createArrayLiteralExpression([\n f.createStringLiteral(\"\"),\n f.createStringLiteral(rs),\n ]),\n f.createPrefixUnaryExpression(ts.SyntaxKind.PlusToken, expr.left)\n ),\n };\n }\n\n return { kind: \"dynamic\", expr, stringSafe: false };\n }\n\n // Handle: condition ? \"a\" : \"b\"\n if (ts.isConditionalExpression(expr)) {\n if (isTrue(expr.condition)) return build(expr.whenTrue);\n if (isFalse(expr.condition)) return build(expr.whenFalse);\n\n const t = tryResolveStatic(expr.whenTrue);\n const fv = tryResolveStatic(expr.whenFalse);\n\n if (t != null && fv != null) {\n // Optimize to: [\"b\", \"a\"][+condition]\n return {\n kind: \"dynamic\",\n stringSafe: true,\n expr: f.createElementAccessExpression(\n f.createArrayLiteralExpression([\n f.createStringLiteral(fv),\n f.createStringLiteral(t),\n ]),\n f.createPrefixUnaryExpression(\n ts.SyntaxKind.PlusToken,\n expr.condition\n )\n ),\n };\n }\n\n return { kind: \"dynamic\", expr, stringSafe: false };\n }\n\n return { kind: \"dynamic\", expr, stringSafe: false };\n }\n\n // Build and merge fragments\n const frags: Fragment[] = [];\n\n for (const arg of args) {\n const frag = build(arg);\n\n // Skip empty static values\n if (frag.kind === \"static\" && frag.value === \"\") {\n continue;\n }\n\n const last = frags[frags.length - 1];\n\n // Merge consecutive static fragments\n if (frag.kind === \"static\" && last?.kind === \"static\") {\n last.value += \" \" + frag.value;\n } else {\n frags.push(frag);\n }\n }\n\n // Generate final className expression\n let classExpr: ts.Expression;\n\n if (frags.length === 0) {\n // All classes were empty\n classExpr = f.createStringLiteral(\"\");\n } else if (frags.every((f) => f.kind === \"static\")) {\n // All static - compile to single string\n classExpr = f.createStringLiteral(\n frags\n .map((f) => f.value)\n .join(\" \")\n .trim()\n );\n } else {\n // Mixed static/dynamic - generate array.join(\" \")\n classExpr = f.createCallExpression(\n f.createPropertyAccessExpression(\n f.createArrayLiteralExpression(\n frags.map((frag) => {\n if (frag.kind === \"static\") {\n return f.createStringLiteral(frag.value);\n }\n if (frag.stringSafe) {\n return frag.expr;\n }\n // Wrap unsafe expressions in conditional: expr ? expr : \"\"\n return f.createConditionalExpression(\n frag.expr,\n undefined,\n frag.expr,\n undefined,\n f.createStringLiteral(\"\")\n );\n })\n ),\n \"join\"\n ),\n undefined,\n [f.createStringLiteral(\" \")]\n );\n }\n\n return f.createObjectLiteralExpression(\n [f.createPropertyAssignment(f.createIdentifier(\"className\"), classExpr)],\n false\n );\n}\n", "import { MacroContext } from \"@kithinji/pod\";\nimport ts from \"typescript\";\nimport fs from \"fs\";\nimport path from \"path\";\n\nexport function inlineFile$(filePath: string, context?: MacroContext): string {\n if (!context) {\n throw new Error(\n \"inlineFile$ macro requires MacroContext. Ensure you're using this as a build-time macro.\"\n );\n }\n\n const pathNode = filePath as unknown as ts.Node;\n const resolvedPath = context.resolveNodeValue(pathNode);\n\n if (typeof resolvedPath !== \"string\") {\n throw new Error(\n `inlineFile$ macro requires a string literal path, got: ${typeof resolvedPath}`\n );\n }\n\n const sourceFile = context.sourceFile;\n const sourceDir = path.dirname(sourceFile.fileName);\n const absolutePath = path.resolve(sourceDir, resolvedPath);\n\n let content: string;\n try {\n content = fs.readFileSync(absolutePath, \"utf-8\");\n } catch (error) {\n throw new Error(\n `Failed to read file \"${absolutePath}\": ${\n error instanceof Error ? error.message : String(error)\n }`\n );\n }\n\n return context.factory.createStringLiteral(content) as any;\n}\n"],
|
|
5
|
+
"mappings": ";;;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AAA,MACA,CAAC,EAAE,oBAAoB,GAAG,CAAC;AAAA,IAC7B;AAAA,EACF;AAEA,SAAO,EAAE;AAAA,IACP,CAAC,EAAE,yBAAyB,EAAE,iBAAiB,WAAW,GAAG,SAAS,CAAC;AAAA,IACvE;AAAA,EACF;AACF;;;ACxgBA,gBAAe;AACf,kBAAiB;AAEV,SAAS,YAAY,UAAkB,SAAgC;AAC5E,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,QAAM,WAAW;AACjB,QAAM,eAAe,QAAQ,iBAAiB,QAAQ;AAEtD,MAAI,OAAO,iBAAiB,UAAU;AACpC,UAAM,IAAI;AAAA,MACR,0DAA0D,OAAO,YAAY;AAAA,IAC/E;AAAA,EACF;AAEA,QAAM,aAAa,QAAQ;AAC3B,QAAM,YAAY,YAAAC,QAAK,QAAQ,WAAW,QAAQ;AAClD,QAAM,eAAe,YAAAA,QAAK,QAAQ,WAAW,YAAY;AAEzD,MAAI;AACJ,MAAI;AACF,cAAU,UAAAC,QAAG,aAAa,cAAc,OAAO;AAAA,EACjD,SAAS,OAAO;AACd,UAAM,IAAI;AAAA,MACR,wBAAwB,YAAY,MAClC,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CACvD;AAAA,IACF;AAAA,EACF;AAEA,SAAO,QAAQ,QAAQ,oBAAoB,OAAO;AACpD;",
|
|
6
|
+
"names": ["path", "className", "normalizedValue", "ts", "f", "path", "fs"]
|
|
7
7
|
}
|
package/dist/node/index.mjs
CHANGED
|
@@ -85,20 +85,20 @@ function normalizeValue(prop, value) {
|
|
|
85
85
|
function isPseudoOrMediaKey(key) {
|
|
86
86
|
return key.startsWith(":") || key.startsWith("@") || key.startsWith("&");
|
|
87
87
|
}
|
|
88
|
-
function validateStyleValue(value,
|
|
88
|
+
function validateStyleValue(value, path2) {
|
|
89
89
|
if (value === null) {
|
|
90
90
|
throw new Error(
|
|
91
|
-
`Invalid style value at ${
|
|
91
|
+
`Invalid style value at ${path2}: null is not allowed. Use undefined or omit the property.`
|
|
92
92
|
);
|
|
93
93
|
}
|
|
94
94
|
if (typeof value === "function") {
|
|
95
95
|
throw new Error(
|
|
96
|
-
`Invalid style value at ${
|
|
96
|
+
`Invalid style value at ${path2}: functions must be resolved at build time.`
|
|
97
97
|
);
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
-
function processStyleValue(prop, value, cssRules,
|
|
101
|
-
validateStyleValue(value,
|
|
100
|
+
function processStyleValue(prop, value, cssRules, path2 = prop) {
|
|
101
|
+
validateStyleValue(value, path2);
|
|
102
102
|
if (prop.startsWith("--")) {
|
|
103
103
|
const className2 = hashProperty(prop, value, cssRules);
|
|
104
104
|
if (!cssRules.has(className2)) {
|
|
@@ -111,7 +111,7 @@ function processStyleValue(prop, value, cssRules, path = prop) {
|
|
|
111
111
|
if (typeof value === "object" && !Array.isArray(value)) {
|
|
112
112
|
const classes = [];
|
|
113
113
|
for (const [nestedKey, nestedValue] of Object.entries(value)) {
|
|
114
|
-
validateStyleValue(nestedValue, `${
|
|
114
|
+
validateStyleValue(nestedValue, `${path2}.${nestedKey}`);
|
|
115
115
|
if (nestedKey === "default") {
|
|
116
116
|
const normalizedValue2 = normalizeValue(
|
|
117
117
|
prop,
|
|
@@ -148,7 +148,7 @@ function processStyleValue(prop, value, cssRules, path = prop) {
|
|
|
148
148
|
classes.push(className2);
|
|
149
149
|
} else {
|
|
150
150
|
throw new Error(
|
|
151
|
-
`Invalid nested key "${nestedKey}" at ${
|
|
151
|
+
`Invalid nested key "${nestedKey}" at ${path2}. Expected "default", a pseudo-class (":hover"), media query ("@media"), or nesting selector ("&").`
|
|
152
152
|
);
|
|
153
153
|
}
|
|
154
154
|
}
|
|
@@ -377,16 +377,33 @@ function apply$(...c) {
|
|
|
377
377
|
}
|
|
378
378
|
|
|
379
379
|
// src/inline-file/inline.ts
|
|
380
|
-
|
|
380
|
+
import fs from "fs";
|
|
381
|
+
import path from "path";
|
|
382
|
+
function inlineFile$(filePath, context) {
|
|
381
383
|
if (!context) {
|
|
382
384
|
throw new Error(
|
|
383
|
-
"
|
|
385
|
+
"inlineFile$ macro requires MacroContext. Ensure you're using this as a build-time macro."
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
const pathNode = filePath;
|
|
389
|
+
const resolvedPath = context.resolveNodeValue(pathNode);
|
|
390
|
+
if (typeof resolvedPath !== "string") {
|
|
391
|
+
throw new Error(
|
|
392
|
+
`inlineFile$ macro requires a string literal path, got: ${typeof resolvedPath}`
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
const sourceFile = context.sourceFile;
|
|
396
|
+
const sourceDir = path.dirname(sourceFile.fileName);
|
|
397
|
+
const absolutePath = path.resolve(sourceDir, resolvedPath);
|
|
398
|
+
let content;
|
|
399
|
+
try {
|
|
400
|
+
content = fs.readFileSync(absolutePath, "utf-8");
|
|
401
|
+
} catch (error) {
|
|
402
|
+
throw new Error(
|
|
403
|
+
`Failed to read file "${absolutePath}": ${error instanceof Error ? error.message : String(error)}`
|
|
384
404
|
);
|
|
385
405
|
}
|
|
386
|
-
|
|
387
|
-
const value = context.resolveNodeValue(rPath);
|
|
388
|
-
console.log(value);
|
|
389
|
-
return context.factory.createStringLiteral("file.txt");
|
|
406
|
+
return context.factory.createStringLiteral(content);
|
|
390
407
|
}
|
|
391
408
|
export {
|
|
392
409
|
apply$,
|
package/dist/node/index.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/style/style.ts", "../../src/inline-file/inline.ts"],
|
|
4
|
-
"sourcesContent": ["import { MacroContext } from \"@kithinji/pod\";\nimport ts from \"typescript\";\nimport { MapNamespaces } from \"./style_types\";\n\nconst UNITLESS_PROPS = new Set([\n \"z-index\",\n \"opacity\",\n \"flex-grow\",\n \"flex-shrink\",\n \"flex\",\n \"order\",\n \"font-weight\",\n \"line-height\",\n \"zoom\",\n \"column-count\",\n \"animation-iteration-count\",\n \"grid-column\",\n \"grid-row\",\n \"grid-column-start\",\n \"grid-column-end\",\n \"grid-row-start\",\n \"grid-row-end\",\n \"tab-size\",\n \"counter-increment\",\n \"counter-reset\",\n \"orphans\",\n \"widows\",\n]);\n\nconst DEV_MODE = process.env.NODE_ENV === \"development\";\n\nfunction simpleHash(str: string): string {\n let hash = 0;\n for (let i = 0; i < str.length; i++) {\n const char = str.charCodeAt(i);\n hash = (hash << 5) - hash + char;\n hash = hash & hash;\n }\n\n return Math.abs(hash).toString(36).padStart(8, \"0\").slice(0, 8);\n}\n\nfunction hashProperty(\n prop: string,\n value: string | number,\n cssRules: Map<string, string>\n): string {\n const input = `${prop}:${value}`;\n let hash = simpleHash(input);\n let className: string;\n\n if (DEV_MODE) {\n // Add readable hint in development\n const hint = prop\n .replace(/[^a-z]/gi, \"\")\n .slice(0, 3)\n .toLowerCase();\n className = `a-${hint}-${hash}`;\n } else {\n className = `a-${hash}`;\n }\n\n let attempt = 0;\n\n // Handle hash collisions\n while (cssRules.has(className)) {\n const existing = cssRules.get(className)!;\n const kebabProp = toKebabCase(prop);\n const normalizedValue = normalizeValue(prop, value);\n const expectedRule = `.${className} { ${kebabProp}: ${normalizedValue}; }`;\n\n // If the existing rule matches, it's the same style (deduplication)\n if (existing.includes(`${kebabProp}: ${normalizedValue}`)) {\n break;\n }\n\n // Collision detected, rehash\n hash = simpleHash(`${input}-${++attempt}`);\n className = DEV_MODE ? `a-${prop.slice(0, 3)}-${hash}` : `a-${hash}`;\n\n if (attempt > 100) {\n throw new Error(\n `Hash collision limit exceeded for property ${prop}:${value}`\n );\n }\n }\n\n return className;\n}\n\nfunction toKebabCase(str: string): string {\n // Handle vendor prefixes (webkit, moz, ms)\n if (str.match(/^(webkit|moz|ms)[A-Z]/)) {\n return \"-\" + str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);\n }\n\n // Handle CSS custom properties\n if (str.startsWith(\"--\")) {\n return str;\n }\n\n return str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);\n}\n\nfunction normalizeValue(prop: string, value: string | number): string {\n if (typeof value === \"number\") {\n const kebabProp = toKebabCase(prop);\n\n if (UNITLESS_PROPS.has(kebabProp)) {\n return String(value);\n }\n\n return `${value}px`;\n }\n\n return String(value);\n}\n\nfunction isPseudoOrMediaKey(key: string): boolean {\n return key.startsWith(\":\") || key.startsWith(\"@\") || key.startsWith(\"&\");\n}\n\nfunction validateStyleValue(value: any, path: string): void {\n if (value === null) {\n throw new Error(\n `Invalid style value at ${path}: null is not allowed. Use undefined or omit the property.`\n );\n }\n\n if (typeof value === \"function\") {\n throw new Error(\n `Invalid style value at ${path}: functions must be resolved at build time.`\n );\n }\n}\n\nfunction processStyleValue(\n prop: string,\n value: any,\n cssRules: Map<string, string>,\n path: string = prop\n): string {\n validateStyleValue(value, path);\n\n if (prop.startsWith(\"--\")) {\n const className = hashProperty(prop, value, cssRules);\n if (!cssRules.has(className)) {\n const cssRule = `.${className} { ${prop}: ${value}; }`;\n cssRules.set(className, cssRule);\n }\n return className;\n }\n\n const kebabProp = toKebabCase(prop);\n\n // Handle nested objects for pseudo-classes, media queries, etc.\n if (typeof value === \"object\" && !Array.isArray(value)) {\n const classes: string[] = [];\n\n for (const [nestedKey, nestedValue] of Object.entries(value)) {\n validateStyleValue(nestedValue, `${path}.${nestedKey}`);\n\n if (nestedKey === \"default\") {\n const normalizedValue = normalizeValue(\n prop,\n nestedValue as string | number\n );\n const className = hashProperty(prop, normalizedValue, cssRules);\n\n if (!cssRules.has(className)) {\n const cssRule = `.${className} { ${kebabProp}: ${normalizedValue}; }`;\n cssRules.set(className, cssRule);\n }\n\n classes.push(className);\n } else if (isPseudoOrMediaKey(nestedKey)) {\n const normalizedValue = normalizeValue(\n prop,\n nestedValue as string | number\n );\n const className = hashProperty(\n `${prop}${nestedKey}`,\n normalizedValue,\n cssRules\n );\n\n if (!cssRules.has(className)) {\n let cssRule: string;\n\n if (nestedKey.startsWith(\"@\")) {\n // Media query\n cssRule = `${nestedKey} { .${className} { ${kebabProp}: ${normalizedValue}; } }`;\n } else if (nestedKey.startsWith(\"&\")) {\n // Nesting selector (e.g., &:hover, & > div)\n const selector = nestedKey.slice(1);\n cssRule = `.${className}${selector} { ${kebabProp}: ${normalizedValue}; }`;\n } else {\n // Pseudo-class/element (e.g., :hover, ::before)\n cssRule = `.${className}${nestedKey} { ${kebabProp}: ${normalizedValue}; }`;\n }\n\n cssRules.set(className, cssRule);\n }\n\n classes.push(className);\n } else {\n throw new Error(\n `Invalid nested key \"${nestedKey}\" at ${path}. Expected \"default\", a pseudo-class (\":hover\"), media query (\"@media\"), or nesting selector (\"&\").`\n );\n }\n }\n\n return classes.join(\" \");\n }\n\n const normalizedValue = normalizeValue(prop, value);\n const className = hashProperty(prop, normalizedValue, cssRules);\n\n if (!cssRules.has(className)) {\n const cssRule = `.${className} { ${kebabProp}: ${normalizedValue}; }`;\n cssRules.set(className, cssRule);\n }\n\n return className;\n}\n\nfunction processStyleObject(\n styleObj: Record<string, any>,\n cssRules: Map<string, string>\n): Record<string, string> {\n const result: Record<string, string> = {};\n\n for (const [namespace, styles] of Object.entries(styleObj)) {\n if (typeof styles !== \"object\" || Array.isArray(styles)) {\n throw new Error(\n `Invalid style namespace \"${namespace}\": expected an object, got ${typeof styles}`\n );\n }\n\n const classes: string[] = [];\n\n for (const [prop, value] of Object.entries(styles)) {\n if (value === undefined) continue;\n\n const className = processStyleValue(\n prop,\n value,\n cssRules,\n `${namespace}.${prop}`\n );\n\n if (className) {\n classes.push(className);\n }\n }\n\n result[namespace] = classes.filter(Boolean).join(\" \");\n }\n\n return result;\n}\n\nexport function style$<const T extends Record<string, any>>(\n style: T,\n context?: MacroContext\n): MapNamespaces<T> {\n if (!context) {\n throw new Error(\n \"style$ macro requires MacroContext. Ensure you're using this as a build-time macro.\"\n );\n }\n\n const rStyle = style as unknown as ts.Node;\n const value = context.resolveNodeValue(rStyle);\n\n if (value == undefined) {\n throw new Error(\n `Could not resolve style object at build time. ` +\n `Ensure all values are statically analyzable (no runtime expressions, dynamic imports should be inlined).`\n );\n }\n\n if (typeof value !== \"object\" || Array.isArray(value)) {\n throw new Error(\n `style$ expects an object with style namespaces, got ${typeof value}`\n );\n }\n\n const cssRules = new Map<string, string>();\n const classNameMap = processStyleObject(value, cssRules);\n\n context.store.set(\"style_rules\", Array.from(cssRules.values()));\n\n const properties = Object.entries(classNameMap).map(([key, className]) =>\n context.factory.createPropertyAssignment(\n context.factory.createStringLiteral(key),\n context.factory.createStringLiteral(className)\n )\n );\n\n return context.factory.createObjectLiteralExpression(properties, true) as any;\n}\n\ntype Fragment =\n | { kind: \"static\"; value: string }\n | { kind: \"dynamic\"; expr: ts.Expression; stringSafe: boolean };\n\nexport function apply$(...c: any[]) {\n if (c.length < 1) {\n throw new Error(\"apply$ requires at least one argument plus MacroContext\");\n }\n\n const context = c.pop() as MacroContext;\n\n if (!context || !context.factory) {\n throw new Error(\n \"apply$ macro requires MacroContext as the last argument. Ensure you're using this as a build-time macro.\"\n );\n }\n\n const args = c as ts.Expression[];\n\n if (args.length === 0) {\n return context.factory.createObjectLiteralExpression(\n [\n context.factory.createPropertyAssignment(\n context.factory.createIdentifier(\"className\"),\n context.factory.createStringLiteral(\"\")\n ),\n ],\n false\n );\n }\n\n const f = context.factory;\n\n function isTrue(e: ts.Expression): boolean {\n return e.kind === ts.SyntaxKind.TrueKeyword;\n }\n\n function isFalse(e: ts.Expression): boolean {\n return e.kind === ts.SyntaxKind.FalseKeyword;\n }\n\n function isEmptyString(e: ts.Expression): boolean {\n return ts.isStringLiteral(e) && e.text === \"\";\n }\n\n function tryResolveStatic(expr: ts.Expression): string | null {\n // Try to resolve string literals directly\n if (ts.isStringLiteral(expr)) {\n return expr.text;\n }\n\n // Try to resolve property access chains (e.g., classes.button)\n if (!ts.isPropertyAccessExpression(expr)) return null;\n\n const chain: string[] = [];\n let cur: ts.Expression = expr;\n\n while (ts.isPropertyAccessExpression(cur)) {\n chain.unshift(cur.name.text);\n cur = cur.expression;\n }\n\n if (!ts.isIdentifier(cur)) return null;\n chain.unshift(cur.text);\n\n const root = context.resolveIdentifier(f.createIdentifier(chain[0]));\n let value = context.resolveNodeValue(root);\n\n if (value == null) return null;\n\n for (let i = 1; i < chain.length; i++) {\n if (typeof value !== \"object\" || !(chain[i] in value)) {\n return null;\n }\n value = value[chain[i]];\n }\n\n return typeof value === \"string\" ? value : null;\n }\n\n function build(expr: ts.Expression): Fragment {\n // Handle empty strings\n if (isEmptyString(expr)) {\n return { kind: \"static\", value: \"\" };\n }\n\n // Try static resolution first\n const s = tryResolveStatic(expr);\n if (s != null) {\n return { kind: \"static\", value: s };\n }\n\n // Handle: condition && \"class\"\n if (\n ts.isBinaryExpression(expr) &&\n expr.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken\n ) {\n if (isTrue(expr.left)) return build(expr.right);\n if (isFalse(expr.left)) return { kind: \"static\", value: \"\" };\n\n const rs = tryResolveStatic(expr.right);\n if (rs != null) {\n // Optimize to: [\"\", \"class\"][+condition]\n return {\n kind: \"dynamic\",\n stringSafe: true,\n expr: f.createElementAccessExpression(\n f.createArrayLiteralExpression([\n f.createStringLiteral(\"\"),\n f.createStringLiteral(rs),\n ]),\n f.createPrefixUnaryExpression(ts.SyntaxKind.PlusToken, expr.left)\n ),\n };\n }\n\n return { kind: \"dynamic\", expr, stringSafe: false };\n }\n\n // Handle: condition ? \"a\" : \"b\"\n if (ts.isConditionalExpression(expr)) {\n if (isTrue(expr.condition)) return build(expr.whenTrue);\n if (isFalse(expr.condition)) return build(expr.whenFalse);\n\n const t = tryResolveStatic(expr.whenTrue);\n const fv = tryResolveStatic(expr.whenFalse);\n\n if (t != null && fv != null) {\n // Optimize to: [\"b\", \"a\"][+condition]\n return {\n kind: \"dynamic\",\n stringSafe: true,\n expr: f.createElementAccessExpression(\n f.createArrayLiteralExpression([\n f.createStringLiteral(fv),\n f.createStringLiteral(t),\n ]),\n f.createPrefixUnaryExpression(\n ts.SyntaxKind.PlusToken,\n expr.condition\n )\n ),\n };\n }\n\n return { kind: \"dynamic\", expr, stringSafe: false };\n }\n\n return { kind: \"dynamic\", expr, stringSafe: false };\n }\n\n // Build and merge fragments\n const frags: Fragment[] = [];\n\n for (const arg of args) {\n const frag = build(arg);\n\n // Skip empty static values\n if (frag.kind === \"static\" && frag.value === \"\") {\n continue;\n }\n\n const last = frags[frags.length - 1];\n\n // Merge consecutive static fragments\n if (frag.kind === \"static\" && last?.kind === \"static\") {\n last.value += \" \" + frag.value;\n } else {\n frags.push(frag);\n }\n }\n\n // Generate final className expression\n let classExpr: ts.Expression;\n\n if (frags.length === 0) {\n // All classes were empty\n classExpr = f.createStringLiteral(\"\");\n } else if (frags.every((f) => f.kind === \"static\")) {\n // All static - compile to single string\n classExpr = f.createStringLiteral(\n frags\n .map((f) => f.value)\n .join(\" \")\n .trim()\n );\n } else {\n // Mixed static/dynamic - generate array.join(\" \")\n classExpr = f.createCallExpression(\n f.createPropertyAccessExpression(\n f.createArrayLiteralExpression(\n frags.map((frag) => {\n if (frag.kind === \"static\") {\n return f.createStringLiteral(frag.value);\n }\n if (frag.stringSafe) {\n return frag.expr;\n }\n // Wrap unsafe expressions in conditional: expr ? expr : \"\"\n return f.createConditionalExpression(\n frag.expr,\n undefined,\n frag.expr,\n undefined,\n f.createStringLiteral(\"\")\n );\n })\n ),\n \"join\"\n ),\n undefined,\n [f.createStringLiteral(\" \")]\n );\n }\n\n return f.createObjectLiteralExpression(\n [f.createPropertyAssignment(f.createIdentifier(\"className\"), classExpr)],\n false\n );\n}\n", "import { MacroContext } from \"@kithinji/pod\";\nimport ts from \"typescript\";\n\nexport function inlineFile$(path: string, context?: MacroContext) {\n if (!context) {\n throw new Error(\n \"style$ macro requires MacroContext. Ensure you're using this as a build-time macro.\"\n );\n }\n\n const rPath = path as unknown as ts.Node;\n const value = context.resolveNodeValue(rPath);\n\n console.log(value);\n\n // read file\n // escape problematic characters\n\n //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AAI,SAAS,SAAS,GAAG,SAAS,KAAK,eAAe,EAAE,GAAG;AACzD;AAAA,IACF;AAGA,WAAO,WAAW,GAAG,KAAK,IAAI,EAAE,OAAO,EAAE;AACzC,gBAAY,WAAW,KAAK,KAAK,MAAM,GAAG,CAAC,CAAC,IAAI,IAAI,KAAK,KAAK,IAAI;AAElE,QAAI,UAAU,KAAK;AACjB,YAAM,IAAI;AAAA,QACR,8CAA8C,IAAI,IAAI,KAAK;AAAA,MAC7D;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAEA,SAAS,YAAY,KAAqB;AAExC,MAAI,IAAI,MAAM,uBAAuB,GAAG;AACtC,WAAO,MAAM,IAAI,QAAQ,UAAU,CAAC,WAAW,IAAI,OAAO,YAAY,CAAC,EAAE;AAAA,EAC3E;AAGA,MAAI,IAAI,WAAW,IAAI,GAAG;AACxB,WAAO;AAAA,EACT;AAEA,SAAO,IAAI,QAAQ,UAAU,CAAC,WAAW,IAAI,OAAO,YAAY,CAAC,EAAE;AACrE;AAEA,SAAS,eAAe,MAAc,OAAgC;AACpE,MAAI,OAAO,UAAU,UAAU;AAC7B,UAAM,YAAY,YAAY,IAAI;AAElC,QAAI,eAAe,IAAI,SAAS,GAAG;AACjC,aAAO,OAAO,KAAK;AAAA,IACrB;AAEA,WAAO,GAAG,KAAK;AAAA,EACjB;AAEA,SAAO,OAAO,KAAK;AACrB;AAEA,SAAS,mBAAmB,KAAsB;AAChD,SAAO,IAAI,WAAW,GAAG,KAAK,IAAI,WAAW,GAAG,KAAK,IAAI,WAAW,GAAG;AACzE;AAEA,SAAS,mBAAmB,
|
|
6
|
-
"names": ["className", "normalizedValue", "f"]
|
|
4
|
+
"sourcesContent": ["import { MacroContext } from \"@kithinji/pod\";\nimport ts from \"typescript\";\nimport { MapNamespaces } from \"./style_types\";\n\nconst UNITLESS_PROPS = new Set([\n \"z-index\",\n \"opacity\",\n \"flex-grow\",\n \"flex-shrink\",\n \"flex\",\n \"order\",\n \"font-weight\",\n \"line-height\",\n \"zoom\",\n \"column-count\",\n \"animation-iteration-count\",\n \"grid-column\",\n \"grid-row\",\n \"grid-column-start\",\n \"grid-column-end\",\n \"grid-row-start\",\n \"grid-row-end\",\n \"tab-size\",\n \"counter-increment\",\n \"counter-reset\",\n \"orphans\",\n \"widows\",\n]);\n\nconst DEV_MODE = process.env.NODE_ENV === \"development\";\n\nfunction simpleHash(str: string): string {\n let hash = 0;\n for (let i = 0; i < str.length; i++) {\n const char = str.charCodeAt(i);\n hash = (hash << 5) - hash + char;\n hash = hash & hash;\n }\n\n return Math.abs(hash).toString(36).padStart(8, \"0\").slice(0, 8);\n}\n\nfunction hashProperty(\n prop: string,\n value: string | number,\n cssRules: Map<string, string>\n): string {\n const input = `${prop}:${value}`;\n let hash = simpleHash(input);\n let className: string;\n\n if (DEV_MODE) {\n // Add readable hint in development\n const hint = prop\n .replace(/[^a-z]/gi, \"\")\n .slice(0, 3)\n .toLowerCase();\n className = `a-${hint}-${hash}`;\n } else {\n className = `a-${hash}`;\n }\n\n let attempt = 0;\n\n // Handle hash collisions\n while (cssRules.has(className)) {\n const existing = cssRules.get(className)!;\n const kebabProp = toKebabCase(prop);\n const normalizedValue = normalizeValue(prop, value);\n const expectedRule = `.${className} { ${kebabProp}: ${normalizedValue}; }`;\n\n // If the existing rule matches, it's the same style (deduplication)\n if (existing.includes(`${kebabProp}: ${normalizedValue}`)) {\n break;\n }\n\n // Collision detected, rehash\n hash = simpleHash(`${input}-${++attempt}`);\n className = DEV_MODE ? `a-${prop.slice(0, 3)}-${hash}` : `a-${hash}`;\n\n if (attempt > 100) {\n throw new Error(\n `Hash collision limit exceeded for property ${prop}:${value}`\n );\n }\n }\n\n return className;\n}\n\nfunction toKebabCase(str: string): string {\n // Handle vendor prefixes (webkit, moz, ms)\n if (str.match(/^(webkit|moz|ms)[A-Z]/)) {\n return \"-\" + str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);\n }\n\n // Handle CSS custom properties\n if (str.startsWith(\"--\")) {\n return str;\n }\n\n return str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);\n}\n\nfunction normalizeValue(prop: string, value: string | number): string {\n if (typeof value === \"number\") {\n const kebabProp = toKebabCase(prop);\n\n if (UNITLESS_PROPS.has(kebabProp)) {\n return String(value);\n }\n\n return `${value}px`;\n }\n\n return String(value);\n}\n\nfunction isPseudoOrMediaKey(key: string): boolean {\n return key.startsWith(\":\") || key.startsWith(\"@\") || key.startsWith(\"&\");\n}\n\nfunction validateStyleValue(value: any, path: string): void {\n if (value === null) {\n throw new Error(\n `Invalid style value at ${path}: null is not allowed. Use undefined or omit the property.`\n );\n }\n\n if (typeof value === \"function\") {\n throw new Error(\n `Invalid style value at ${path}: functions must be resolved at build time.`\n );\n }\n}\n\nfunction processStyleValue(\n prop: string,\n value: any,\n cssRules: Map<string, string>,\n path: string = prop\n): string {\n validateStyleValue(value, path);\n\n if (prop.startsWith(\"--\")) {\n const className = hashProperty(prop, value, cssRules);\n if (!cssRules.has(className)) {\n const cssRule = `.${className} { ${prop}: ${value}; }`;\n cssRules.set(className, cssRule);\n }\n return className;\n }\n\n const kebabProp = toKebabCase(prop);\n\n // Handle nested objects for pseudo-classes, media queries, etc.\n if (typeof value === \"object\" && !Array.isArray(value)) {\n const classes: string[] = [];\n\n for (const [nestedKey, nestedValue] of Object.entries(value)) {\n validateStyleValue(nestedValue, `${path}.${nestedKey}`);\n\n if (nestedKey === \"default\") {\n const normalizedValue = normalizeValue(\n prop,\n nestedValue as string | number\n );\n const className = hashProperty(prop, normalizedValue, cssRules);\n\n if (!cssRules.has(className)) {\n const cssRule = `.${className} { ${kebabProp}: ${normalizedValue}; }`;\n cssRules.set(className, cssRule);\n }\n\n classes.push(className);\n } else if (isPseudoOrMediaKey(nestedKey)) {\n const normalizedValue = normalizeValue(\n prop,\n nestedValue as string | number\n );\n const className = hashProperty(\n `${prop}${nestedKey}`,\n normalizedValue,\n cssRules\n );\n\n if (!cssRules.has(className)) {\n let cssRule: string;\n\n if (nestedKey.startsWith(\"@\")) {\n // Media query\n cssRule = `${nestedKey} { .${className} { ${kebabProp}: ${normalizedValue}; } }`;\n } else if (nestedKey.startsWith(\"&\")) {\n // Nesting selector (e.g., &:hover, & > div)\n const selector = nestedKey.slice(1);\n cssRule = `.${className}${selector} { ${kebabProp}: ${normalizedValue}; }`;\n } else {\n // Pseudo-class/element (e.g., :hover, ::before)\n cssRule = `.${className}${nestedKey} { ${kebabProp}: ${normalizedValue}; }`;\n }\n\n cssRules.set(className, cssRule);\n }\n\n classes.push(className);\n } else {\n throw new Error(\n `Invalid nested key \"${nestedKey}\" at ${path}. Expected \"default\", a pseudo-class (\":hover\"), media query (\"@media\"), or nesting selector (\"&\").`\n );\n }\n }\n\n return classes.join(\" \");\n }\n\n const normalizedValue = normalizeValue(prop, value);\n const className = hashProperty(prop, normalizedValue, cssRules);\n\n if (!cssRules.has(className)) {\n const cssRule = `.${className} { ${kebabProp}: ${normalizedValue}; }`;\n cssRules.set(className, cssRule);\n }\n\n return className;\n}\n\nfunction processStyleObject(\n styleObj: Record<string, any>,\n cssRules: Map<string, string>\n): Record<string, string> {\n const result: Record<string, string> = {};\n\n for (const [namespace, styles] of Object.entries(styleObj)) {\n if (typeof styles !== \"object\" || Array.isArray(styles)) {\n throw new Error(\n `Invalid style namespace \"${namespace}\": expected an object, got ${typeof styles}`\n );\n }\n\n const classes: string[] = [];\n\n for (const [prop, value] of Object.entries(styles)) {\n if (value === undefined) continue;\n\n const className = processStyleValue(\n prop,\n value,\n cssRules,\n `${namespace}.${prop}`\n );\n\n if (className) {\n classes.push(className);\n }\n }\n\n result[namespace] = classes.filter(Boolean).join(\" \");\n }\n\n return result;\n}\n\nexport function style$<const T extends Record<string, any>>(\n style: T,\n context?: MacroContext\n): MapNamespaces<T> {\n if (!context) {\n throw new Error(\n \"style$ macro requires MacroContext. Ensure you're using this as a build-time macro.\"\n );\n }\n\n const rStyle = style as unknown as ts.Node;\n const value = context.resolveNodeValue(rStyle);\n\n if (value == undefined) {\n throw new Error(\n `Could not resolve style object at build time. ` +\n `Ensure all values are statically analyzable (no runtime expressions, dynamic imports should be inlined).`\n );\n }\n\n if (typeof value !== \"object\" || Array.isArray(value)) {\n throw new Error(\n `style$ expects an object with style namespaces, got ${typeof value}`\n );\n }\n\n const cssRules = new Map<string, string>();\n const classNameMap = processStyleObject(value, cssRules);\n\n context.store.set(\"style_rules\", Array.from(cssRules.values()));\n\n const properties = Object.entries(classNameMap).map(([key, className]) =>\n context.factory.createPropertyAssignment(\n context.factory.createStringLiteral(key),\n context.factory.createStringLiteral(className)\n )\n );\n\n return context.factory.createObjectLiteralExpression(properties, true) as any;\n}\n\ntype Fragment =\n | { kind: \"static\"; value: string }\n | { kind: \"dynamic\"; expr: ts.Expression; stringSafe: boolean };\n\nexport function apply$(...c: any[]) {\n if (c.length < 1) {\n throw new Error(\"apply$ requires at least one argument plus MacroContext\");\n }\n\n const context = c.pop() as MacroContext;\n\n if (!context || !context.factory) {\n throw new Error(\n \"apply$ macro requires MacroContext as the last argument. Ensure you're using this as a build-time macro.\"\n );\n }\n\n const args = c as ts.Expression[];\n\n if (args.length === 0) {\n return context.factory.createObjectLiteralExpression(\n [\n context.factory.createPropertyAssignment(\n context.factory.createIdentifier(\"className\"),\n context.factory.createStringLiteral(\"\")\n ),\n ],\n false\n );\n }\n\n const f = context.factory;\n\n function isTrue(e: ts.Expression): boolean {\n return e.kind === ts.SyntaxKind.TrueKeyword;\n }\n\n function isFalse(e: ts.Expression): boolean {\n return e.kind === ts.SyntaxKind.FalseKeyword;\n }\n\n function isEmptyString(e: ts.Expression): boolean {\n return ts.isStringLiteral(e) && e.text === \"\";\n }\n\n function tryResolveStatic(expr: ts.Expression): string | null {\n // Try to resolve string literals directly\n if (ts.isStringLiteral(expr)) {\n return expr.text;\n }\n\n // Try to resolve property access chains (e.g., classes.button)\n if (!ts.isPropertyAccessExpression(expr)) return null;\n\n const chain: string[] = [];\n let cur: ts.Expression = expr;\n\n while (ts.isPropertyAccessExpression(cur)) {\n chain.unshift(cur.name.text);\n cur = cur.expression;\n }\n\n if (!ts.isIdentifier(cur)) return null;\n chain.unshift(cur.text);\n\n const root = context.resolveIdentifier(f.createIdentifier(chain[0]));\n let value = context.resolveNodeValue(root);\n\n if (value == null) return null;\n\n for (let i = 1; i < chain.length; i++) {\n if (typeof value !== \"object\" || !(chain[i] in value)) {\n return null;\n }\n value = value[chain[i]];\n }\n\n return typeof value === \"string\" ? value : null;\n }\n\n function build(expr: ts.Expression): Fragment {\n // Handle empty strings\n if (isEmptyString(expr)) {\n return { kind: \"static\", value: \"\" };\n }\n\n // Try static resolution first\n const s = tryResolveStatic(expr);\n if (s != null) {\n return { kind: \"static\", value: s };\n }\n\n // Handle: condition && \"class\"\n if (\n ts.isBinaryExpression(expr) &&\n expr.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken\n ) {\n if (isTrue(expr.left)) return build(expr.right);\n if (isFalse(expr.left)) return { kind: \"static\", value: \"\" };\n\n const rs = tryResolveStatic(expr.right);\n if (rs != null) {\n // Optimize to: [\"\", \"class\"][+condition]\n return {\n kind: \"dynamic\",\n stringSafe: true,\n expr: f.createElementAccessExpression(\n f.createArrayLiteralExpression([\n f.createStringLiteral(\"\"),\n f.createStringLiteral(rs),\n ]),\n f.createPrefixUnaryExpression(ts.SyntaxKind.PlusToken, expr.left)\n ),\n };\n }\n\n return { kind: \"dynamic\", expr, stringSafe: false };\n }\n\n // Handle: condition ? \"a\" : \"b\"\n if (ts.isConditionalExpression(expr)) {\n if (isTrue(expr.condition)) return build(expr.whenTrue);\n if (isFalse(expr.condition)) return build(expr.whenFalse);\n\n const t = tryResolveStatic(expr.whenTrue);\n const fv = tryResolveStatic(expr.whenFalse);\n\n if (t != null && fv != null) {\n // Optimize to: [\"b\", \"a\"][+condition]\n return {\n kind: \"dynamic\",\n stringSafe: true,\n expr: f.createElementAccessExpression(\n f.createArrayLiteralExpression([\n f.createStringLiteral(fv),\n f.createStringLiteral(t),\n ]),\n f.createPrefixUnaryExpression(\n ts.SyntaxKind.PlusToken,\n expr.condition\n )\n ),\n };\n }\n\n return { kind: \"dynamic\", expr, stringSafe: false };\n }\n\n return { kind: \"dynamic\", expr, stringSafe: false };\n }\n\n // Build and merge fragments\n const frags: Fragment[] = [];\n\n for (const arg of args) {\n const frag = build(arg);\n\n // Skip empty static values\n if (frag.kind === \"static\" && frag.value === \"\") {\n continue;\n }\n\n const last = frags[frags.length - 1];\n\n // Merge consecutive static fragments\n if (frag.kind === \"static\" && last?.kind === \"static\") {\n last.value += \" \" + frag.value;\n } else {\n frags.push(frag);\n }\n }\n\n // Generate final className expression\n let classExpr: ts.Expression;\n\n if (frags.length === 0) {\n // All classes were empty\n classExpr = f.createStringLiteral(\"\");\n } else if (frags.every((f) => f.kind === \"static\")) {\n // All static - compile to single string\n classExpr = f.createStringLiteral(\n frags\n .map((f) => f.value)\n .join(\" \")\n .trim()\n );\n } else {\n // Mixed static/dynamic - generate array.join(\" \")\n classExpr = f.createCallExpression(\n f.createPropertyAccessExpression(\n f.createArrayLiteralExpression(\n frags.map((frag) => {\n if (frag.kind === \"static\") {\n return f.createStringLiteral(frag.value);\n }\n if (frag.stringSafe) {\n return frag.expr;\n }\n // Wrap unsafe expressions in conditional: expr ? expr : \"\"\n return f.createConditionalExpression(\n frag.expr,\n undefined,\n frag.expr,\n undefined,\n f.createStringLiteral(\"\")\n );\n })\n ),\n \"join\"\n ),\n undefined,\n [f.createStringLiteral(\" \")]\n );\n }\n\n return f.createObjectLiteralExpression(\n [f.createPropertyAssignment(f.createIdentifier(\"className\"), classExpr)],\n false\n );\n}\n", "import { MacroContext } from \"@kithinji/pod\";\nimport ts from \"typescript\";\nimport fs from \"fs\";\nimport path from \"path\";\n\nexport function inlineFile$(filePath: string, context?: MacroContext): string {\n if (!context) {\n throw new Error(\n \"inlineFile$ macro requires MacroContext. Ensure you're using this as a build-time macro.\"\n );\n }\n\n const pathNode = filePath as unknown as ts.Node;\n const resolvedPath = context.resolveNodeValue(pathNode);\n\n if (typeof resolvedPath !== \"string\") {\n throw new Error(\n `inlineFile$ macro requires a string literal path, got: ${typeof resolvedPath}`\n );\n }\n\n const sourceFile = context.sourceFile;\n const sourceDir = path.dirname(sourceFile.fileName);\n const absolutePath = path.resolve(sourceDir, resolvedPath);\n\n let content: string;\n try {\n content = fs.readFileSync(absolutePath, \"utf-8\");\n } catch (error) {\n throw new Error(\n `Failed to read file \"${absolutePath}\": ${\n error instanceof Error ? error.message : String(error)\n }`\n );\n }\n\n return context.factory.createStringLiteral(content) as any;\n}\n"],
|
|
5
|
+
"mappings": ";AACA,OAAO,QAAQ;AAGf,IAAM,iBAAiB,oBAAI,IAAI;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAED,IAAM,WAAW,QAAQ,IAAI,aAAa;AAE1C,SAAS,WAAW,KAAqB;AACvC,MAAI,OAAO;AACX,WAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK;AACnC,UAAM,OAAO,IAAI,WAAW,CAAC;AAC7B,YAAQ,QAAQ,KAAK,OAAO;AAC5B,WAAO,OAAO;AAAA,EAChB;AAEA,SAAO,KAAK,IAAI,IAAI,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,EAAE,MAAM,GAAG,CAAC;AAChE;AAEA,SAAS,aACP,MACA,OACA,UACQ;AACR,QAAM,QAAQ,GAAG,IAAI,IAAI,KAAK;AAC9B,MAAI,OAAO,WAAW,KAAK;AAC3B,MAAI;AAEJ,MAAI,UAAU;AAEZ,UAAM,OAAO,KACV,QAAQ,YAAY,EAAE,EACtB,MAAM,GAAG,CAAC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K,YAAY;AACnB,qBAAO,KAAK;AAAA,YACd;AAEA,mBAAO,EAAE;AAAA,cACP,KAAK;AAAA,cACL;AAAA,cACA,KAAK;AAAA,cACL;AAAA,cACA,EAAE,oBAAoB,EAAE;AAAA,YAC1B;AAAA,UACF,CAAC;AAAA,QACH;AAAA,QACA;AAAA,MACF;AAAA,MACA;AAAA,MACA,CAAC,EAAE,oBAAoB,GAAG,CAAC;AAAA,IAC7B;AAAA,EACF;AAEA,SAAO,EAAE;AAAA,IACP,CAAC,EAAE,yBAAyB,EAAE,iBAAiB,WAAW,GAAG,SAAS,CAAC;AAAA,IACvE;AAAA,EACF;AACF;;;ACxgBA,OAAO,QAAQ;AACf,OAAO,UAAU;AAEV,SAAS,YAAY,UAAkB,SAAgC;AAC5E,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,QAAM,WAAW;AACjB,QAAM,eAAe,QAAQ,iBAAiB,QAAQ;AAEtD,MAAI,OAAO,iBAAiB,UAAU;AACpC,UAAM,IAAI;AAAA,MACR,0DAA0D,OAAO,YAAY;AAAA,IAC/E;AAAA,EACF;AAEA,QAAM,aAAa,QAAQ;AAC3B,QAAM,YAAY,KAAK,QAAQ,WAAW,QAAQ;AAClD,QAAM,eAAe,KAAK,QAAQ,WAAW,YAAY;AAEzD,MAAI;AACJ,MAAI;AACF,cAAU,GAAG,aAAa,cAAc,OAAO;AAAA,EACjD,SAAS,OAAO;AACd,UAAM,IAAI;AAAA,MACR,wBAAwB,YAAY,MAClC,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CACvD;AAAA,IACF;AAAA,EACF;AAEA,SAAO,QAAQ,QAAQ,oBAAoB,OAAO;AACpD;",
|
|
6
|
+
"names": ["path", "className", "normalizedValue", "f"]
|
|
7
7
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import { MacroContext } from "@kithinji/pod";
|
|
2
|
-
|
|
3
|
-
export declare function inlineFile$(path: string, context?: MacroContext): ts.StringLiteral;
|
|
2
|
+
export declare function inlineFile$(filePath: string, context?: MacroContext): string;
|
|
4
3
|
//# sourceMappingURL=inline.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inline.d.ts","sourceRoot":"","sources":["../../../src/inline-file/inline.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"inline.d.ts","sourceRoot":"","sources":["../../../src/inline-file/inline.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAK7C,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,MAAM,CAgC5E"}
|
package/package.json
CHANGED
|
@@ -1,21 +1,38 @@
|
|
|
1
1
|
import { MacroContext } from "@kithinji/pod";
|
|
2
2
|
import ts from "typescript";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import path from "path";
|
|
3
5
|
|
|
4
|
-
export function inlineFile$(
|
|
6
|
+
export function inlineFile$(filePath: string, context?: MacroContext): string {
|
|
5
7
|
if (!context) {
|
|
6
8
|
throw new Error(
|
|
7
|
-
"
|
|
9
|
+
"inlineFile$ macro requires MacroContext. Ensure you're using this as a build-time macro."
|
|
8
10
|
);
|
|
9
11
|
}
|
|
10
12
|
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
+
const pathNode = filePath as unknown as ts.Node;
|
|
14
|
+
const resolvedPath = context.resolveNodeValue(pathNode);
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
if (typeof resolvedPath !== "string") {
|
|
17
|
+
throw new Error(
|
|
18
|
+
`inlineFile$ macro requires a string literal path, got: ${typeof resolvedPath}`
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const sourceFile = context.sourceFile;
|
|
23
|
+
const sourceDir = path.dirname(sourceFile.fileName);
|
|
24
|
+
const absolutePath = path.resolve(sourceDir, resolvedPath);
|
|
15
25
|
|
|
16
|
-
|
|
17
|
-
|
|
26
|
+
let content: string;
|
|
27
|
+
try {
|
|
28
|
+
content = fs.readFileSync(absolutePath, "utf-8");
|
|
29
|
+
} catch (error) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`Failed to read file "${absolutePath}": ${
|
|
32
|
+
error instanceof Error ? error.message : String(error)
|
|
33
|
+
}`
|
|
34
|
+
);
|
|
35
|
+
}
|
|
18
36
|
|
|
19
|
-
|
|
20
|
-
return context.factory.createStringLiteral("file.txt");
|
|
37
|
+
return context.factory.createStringLiteral(content) as any;
|
|
21
38
|
}
|