Skip to content

Commit

Permalink
[types] Include re-exported types in top-level folders again.
Browse files Browse the repository at this point in the history
This allows TypeScript to pick up the types without having to author `tsconfig.json`, particularly in VS Code.

I want to get rid of this as soon as possible, as using anything other than the `"exports" field in `package.json` is an anti-pattern. But this is going to provide a much better experience in the meantime.
  • Loading branch information
lgarron committed Mar 17, 2024
1 parent 62da791 commit 8713dc1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ default:
# an error).
.PHONY: build
build: clean build-lib build-bin build-sites
${BUN_RUN} script/build/lib/clean-types.ts
.PHONY: build-lib
build-lib: build-lib-js build-lib-types
.PHONY: build-lib-js
Expand Down
25 changes: 24 additions & 1 deletion script/build/lib/build-lib-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { rename } from "fs/promises";
import { mkdir, rename, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { spawnPromise } from "../../lib/execPromise";
import { packageNames } from "../common/package-info";

Expand Down Expand Up @@ -35,3 +36,25 @@ await spawnPromise("npx", [
"--out-dir",
"dist/lib/cubing",
]);

const TYPESCRIPT_DECLARATION_INDEX = "index.d.ts";

// TODO: remove this once TypeScript resolves types from the `package.json` exports out of the box.
for (const packageName of packageNames) {
await mkdir(packageName);
const indexFileName = join(packageName, TYPESCRIPT_DECLARATION_INDEX);
await writeFile(
indexFileName,
`export type * from ${JSON.stringify(
join(
"..",
"dist",
"lib",
"cubing",
packageName,
TYPESCRIPT_DECLARATION_INDEX,
),
)};`,
"utf-8",
);
}
11 changes: 11 additions & 0 deletions script/build/lib/clean-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { rm, rmdir } from "node:fs/promises";
import { join } from "node:path";
import { packageNames } from "../common/package-info";

const TYPESCRIPT_DECLARATION_INDEX = "index.d.ts";

for (const packageName of packageNames) {
const indexFileName = join(packageName, TYPESCRIPT_DECLARATION_INDEX);
await rm(indexFileName);
await rmdir(packageName);
}

0 comments on commit 8713dc1

Please sign in to comment.