Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: export types from barrel file #88

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"@carbon/pictograms": "12.43.0",
"@types/bun": "latest",
"culls": "^0.1.1",
"sveld": "latest",
"svelte": "^5.0.3",
"typescript": "latest"
},
Expand Down
71 changes: 38 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import buildInfo from "@carbon/pictograms/metadata.json" assert { type: "json" };
import { $ } from "bun";
import { ComponentParser } from "sveld";
import type { ParsedExports } from "sveld/lib/parse-exports";
import writeTsDefinitions from "sveld/lib/writer/writer-ts-definitions";
import { devDependencies, name } from "../package.json" assert { type: "json" };
import { template } from "./template";

Expand All @@ -11,56 +8,64 @@ export const buildPictograms = async () => {
await $`rm -rf lib`;
await $`mkdir lib`;

const parser = new ComponentParser();
const components = new Map();
const exports: ParsedExports = {};
let definitions = `import type { SvelteComponentTyped } from "svelte";
import type { SvelteHTMLElements } from "svelte/elements";

let imports = "";
type RestProps = SvelteHTMLElements["svg"];

export interface CarbonPictogramProps extends RestProps {
/**
* Specify the pictogram title.
* @default undefined
*/
title?: string;

[key: \`data-\${string}\`]: any;
}

export declare class CarbonPictogram extends SvelteComponentTyped<
CarbonPictogramProps,
Record<string, any>,
{}
> {}\n\n`;

let libExport = "";

const pictograms: string[] = [];

buildInfo.icons.forEach(async ({ output }) => {
const { moduleName } = output[0];

imports += `export { default as ${moduleName} } from "./${moduleName}.svelte";\n`;
pictograms.push(moduleName);

const source = template(output[0]);
const ts_file_path = `./${moduleName}.svelte.d.ts`;
definitions += `export declare class ${moduleName} extends CarbonPictogram {}\n`;
libExport += `export { default as ${moduleName} } from "./${moduleName}.svelte";\n`;

components.set(moduleName, {
moduleName,
filePath: ts_file_path,
...parser.parseSvelteComponent(source, {
moduleName,
filePath: ts_file_path,
}),
});
const fileName = `lib/${moduleName}.svelte`;

exports[moduleName] = {
source: `./${moduleName}.svelte`,
default: false,
};

await Bun.write(`lib/${moduleName}.svelte`, source);
Bun.write(fileName, template(output[0]));
Bun.write(
fileName + ".d.ts",
`export { ${moduleName} as default } from "./";\n`
);
});

const metadata = `${pictograms.length} pictograms from @carbon/pictograms@${devDependencies["@carbon/pictograms"]}`;
const packageMetadata = `${pictograms.length} pictograms from @carbon/pictograms@${devDependencies["@carbon/pictograms"]}`;

await writeTsDefinitions(components, {
preamble: `// Type definitions for ${name}\n// ${metadata}\n\n`,
exports,
inputDir: "lib",
outDir: "lib",
});
await Bun.write(
"lib/index.d.ts",
`// Type definitions for ${name}
// ${packageMetadata}

await Bun.write("lib/index.js", imports);
${definitions}`
);
await Bun.write("lib/index.js", libExport);
await Bun.write(
"PICTOGRAM_INDEX.md",
`
# Pictogram Index

> ${metadata}
> ${packageMetadata}

## Usage

Expand Down
2 changes: 1 addition & 1 deletion src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export function template({ descriptor }: PictogramOutput) {
{title}
{...attributes}
{...$$restProps}>
${descriptor.content.map((element) => toString(element)).join("")}
${descriptor.content.map(toString).join("")}
</svg>`;
}
2 changes: 1 addition & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test("imports", async () => {
const pictograms = await buildPictograms();
expect(pictograms.length).toEqual(1187);
expect(pictograms).toMatchSnapshot();
}, 30_000);
});

test("template", () => {
const props: PictogramOutput = {
Expand Down
Loading