-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathpreact.ts
55 lines (50 loc) · 1.63 KB
/
preact.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { join } from "path";
import {
OutputTargetCustom,
OutputTargetDist,
OutputTargetDistTypes,
} from "@stencil/core/internal/stencil-public-compiler";
export const generatePreactTypes: OutputTargetCustom["generator"] = async (
config,
compilerCtx,
buildCtx,
): Promise<void> => {
const { typesDir } = config.outputTargets.find(({ type }) => type === "dist" || type === "dist-types") as
| OutputTargetDist
| OutputTargetDistTypes;
const outputPath = join(typesDir, "preact.d.ts");
const types = buildCtx.components.map(getType).join("\n");
await compilerCtx.fs.writeFile(outputPath, getTemplate(types));
};
function getTemplate(types: string): string {
return `
import { JSXInternal } from "preact/src/jsx";
import { JSX } from "./components";
declare module "preact/src/jsx" {
namespace JSXInternal {
interface IntrinsicElements {
${types};
}
}
}
`;
}
function getType({ events, tagName, componentClassName }): string {
const className = `Calcite${componentClassName}`;
if (!events?.length) {
return `
"${tagName}": JSX.${className} & JSXInternal.HTMLAttributes<HTML${className}Element>`;
} else {
const stencilEvents = events.map(({ name }) => `"on${capitalize(name)}"`).join(" | ");
const preactEvents = events
.map(({ name }) => `"on${name}"?: (event: CustomEvent<any>) => void;`)
.join("\n ");
return `
"${tagName}": Omit<JSX.${className}, ${stencilEvents}> & JSXInternal.HTMLAttributes<HTML${className}Element> & {
${preactEvents}
}`;
}
}
function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}