#!/usr/bin/env node /** * Generate a consumer JSX namespace from `jsx-h.d.ts`. * * `jsx-h.d.ts` is the source of truth (hyperscript: FunctionMaybe values, * FunctionElement children). This script: * 1. optionally compiles that away for the JSX transform (`--compile`) * 2. stamps `JSX.Element` and injects consumer imports (`--element`, `--import`) * * pnpm --filter @solidjs/web jsx-sync # compiled + SolidElement (web) * node scripts/jsx-sync.mjs --input ... --output ... --element "..." --import "..." */ import { execFileSync } from "node:child_process"; import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const here = path.dirname(fileURLToPath(import.meta.url)); const webRoot = path.resolve(here, ".."); const repoRoot = path.resolve(webRoot, "../.."); const prettierBin = path.join(repoRoot, "node_modules/.bin/prettier"); const args = process.argv.slice(2); function readOption(name) { const index = args.indexOf(`--${name}`); if (index === -1) return undefined; const value = args[index + 1]; if (!value || value.startsWith("--")) throw new Error(`Missing value for --${name}`); return value; } const compile = args.includes("--compile"); const input = path.resolve(readOption("input") ?? path.join(webRoot, "jsx/jsx-h.d.ts")); const output = path.resolve(readOption("output") ?? path.join(webRoot, "jsx/jsx.d.ts")); const element = readOption("element"); const imports = args .flatMap((arg, index) => (arg === "--import" ? [args[index + 1]] : [])) .filter(Boolean); if (!element) { throw new Error( [ "Usage: jsx-sync --element [--input jsx-h.d.ts] [--output jsx.d.ts] [--compile] [--import ...]", "", "Example:", ' jsx-sync --compile --element "SolidElement | Node | ArrayElement" --import \'import type { Element as SolidElement } from "solid-js";\'' ].join("\n") ); } function unwrapFunctionMaybe(line) { const marker = "FunctionMaybe<"; let out = ""; let i = 0; while (i < line.length) { const idx = line.indexOf(marker, i); if (idx === -1) { out += line.slice(i); break; } out += line.slice(i, idx); let depth = 1; let j = idx + marker.length; while (j < line.length && depth > 0) { const c = line[j]; if (c === "<") depth++; else if (c === ">") depth--; if (depth > 0) j++; } out += line.slice(idx + marker.length, j); i = j + 1; } return out; } function compileSource(source) { const tmp = path.join(path.dirname(input), "jsx-h.temp.d.ts"); fs.writeFileSync(tmp, source); execFileSync(prettierBin, [tmp, "--write", "--no-editorconfig", "--print-width", "100000"], { cwd: webRoot, stdio: "pipe" }); const lines = fs.readFileSync(tmp, "utf8").split("\n"); fs.unlinkSync(tmp); for (let i = 0; i < lines.length; i++) { const line = lines[i].trim(); if (line.startsWith("type FunctionMaybe") || !line.includes("FunctionMaybe")) continue; lines[i] = unwrapFunctionMaybe(lines[i]); } return lines.join("\n"); } function customize(source) { for (const statement of imports) { source = source.replace(new RegExp(`^${escapeRegExp(statement)}\\n`, "gm"), ""); } const importAnchor = 'import type { PropKey, WidenPropValue } from "./jsx-properties.js";'; if (!source.includes(importAnchor)) { throw new Error(`Unable to find JSX properties import in ${input}`); } source = source.replace(importAnchor, [importAnchor, ...imports].join("\n")); const next = source.replace( /type Element =[\s\S]*?\n \/\/ END - difference between `jsx\.d\.ts` and `jsx-h\.d\.ts`/, `type Element = ${element};\n // END - difference between \`jsx.d.ts\` and \`jsx-h.d.ts\`` ); if (next === source) { throw new Error(`Unable to find JSX.Element declaration block in ${input}`); } return next; } function escapeRegExp(value) { return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } const banner = `/** * THIS FILE IS GENERATED by \`scripts/jsx-sync.mjs\`. * Edit \`jsx-h.d.ts\` and run \`pnpm --filter @solidjs/web jsx-sync\` * (also runs as part of \`pnpm types\`). */ `; let source = fs.readFileSync(input, "utf8"); if (compile) source = compileSource(source); source = customize(source); source = source.replace(/^\/\*\*[\s\S]*?THIS FILE IS GENERATED[\s\S]*?\*\/\s*/, ""); source = banner + source; fs.mkdirSync(path.dirname(output), { recursive: true }); fs.writeFileSync(output, source); execFileSync(prettierBin, [output, "--write"], { cwd: webRoot, stdio: "pipe" });