Skip to content

Commit 38d794b

Browse files
committed
fix(): add jsx.d.ts
1 parent 28cf9e6 commit 38d794b

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

packages/yo/src/plopfile.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,62 @@ export default function (
314314
path: "bricks/{{pkgName}}/docs/{{>lastTagName}}.md",
315315
templateFile: "templates/brick.md.hbs",
316316
},
317+
async function updateJsxDts(answers) {
318+
const jsxDtsFile = path.join(
319+
bricksDir,
320+
answers.pkgName,
321+
"src/jsx.d.ts"
322+
);
323+
/** @type {string} */
324+
let jsxDtsContent;
325+
if (!existsSync(jsxDtsFile)) {
326+
// jsxDtsContent = `import type { DetailedHTMLProps, HTMLAttributes } from "react"`;
327+
const jsxDtsTemplateFile = path.join(
328+
__dirname,
329+
"templates/bricks/src/jsx.d.ts.hbs"
330+
);
331+
jsxDtsContent = await readFile(jsxDtsTemplateFile, "utf-8");
332+
} else {
333+
jsxDtsContent = await readFile(jsxDtsFile, "utf-8");
334+
}
335+
const tagName = plop.renderString(
336+
"{{getTagName brickType pkgName brickName false false}}",
337+
answers
338+
);
339+
const className = plop.renderString(
340+
"{{pascalCase (getTagName brickType pkgName brickName true true)}}",
341+
answers
342+
);
343+
const propName = `${className}Props`;
344+
const importStatement = `import type { ${className}, ${propName} } from "./${answers.brickName}";`;
345+
const definitionProp = ` "${tagName}": DetailedHTMLProps<HTMLAttributes<${className}>, ${className}> & ${propName};`;
346+
347+
/** @type {[RegExp, string][]} */
348+
const replacementPatterns = [
349+
[
350+
/(\r?\n)(\r?\n)?(declare global )/,
351+
`$1${importStatement}$1$2$3`,
352+
],
353+
[
354+
// eslint-disable-next-line no-regex-spaces
355+
/(\r?\n)( \}\r?\n \}\r?\n\}\r?\n)$/,
356+
`$1${definitionProp}$1$2`,
357+
],
358+
];
359+
360+
for (const [pattern, replacement] of replacementPatterns) {
361+
const newJsxDts = jsxDtsContent.replace(pattern, replacement);
362+
if (newJsxDts === jsxDtsContent) {
363+
throw new Error(
364+
`Failed to add definition in jsx.d.ts for ${tagName}.`
365+
);
366+
}
367+
jsxDtsContent = newJsxDts;
368+
}
369+
370+
await writeFile(jsxDtsFile, jsxDtsContent);
371+
return `Updated jsx.d.ts to include ${tagName} definition.`;
372+
},
317373
async function modifyCommonBricksJson(answers) {
318374
if (answers.brickType === "common") {
319375
const realBrickName = `eo-${answers.brickName}`;

packages/yo/src/templates/bricks/package.json.hbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
"dist-types",
1212
"docs"
1313
],
14+
"types": "dist-types/jsx.d.ts",
1415
"exports": {
1516
"./package.json": "./package.json",
1617
"./dist/bricks.json": "./dist/bricks.json",
1718
"./dist/manifest.json": "./dist/manifest.json",
19+
".": "./dist-types/jsx.d.ts",
1820
"./*": {
1921
"types": "./dist-types/*/index.d.ts"
2022
}
@@ -23,7 +25,7 @@
2325
"start": "cross-env NODE_ENV=development build-next-bricks --watch",
2426
"build": "npm run build:main && npm run build:types",
2527
"build:main": "cross-env NODE_ENV=production build-next-bricks",
26-
"build:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist-types --project tsconfig.json",
28+
"build:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist-types --project tsconfig.json && copy-jsx-d-ts",
2729
"build:manifest": "cross-env NODE_ENV=production build-next-bricks --manifest-only",
2830
"test": "cross-env NODE_ENV='test' test-next",
2931
"test:ci": "cross-env NODE_ENV='test' CI=true test-next",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { DetailedHTMLProps, HTMLAttributes } from "react";
2+
3+
declare global {
4+
namespace JSX {
5+
interface IntrinsicElements {
6+
// Extend with brick definitions here
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)