Skip to content

Commit

Permalink
feat: use dts-buddy to generate types (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusmb committed Aug 23, 2023
1 parent 089b8eb commit c00ef85
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"lint": "pnpm --filter ./workspace/* lint",
"test": "pnpm --filter ./workspace/* test",
"format": "pnpm --filter ./workspace/* format",
"postinstall": "cd ./workspace/marqua && pnpm build"
"postinstall": "cd ./workspace/marqua && tsc"
},
"packageManager": "pnpm@8.6.11",
"prettier": "mauss/prettier.json",
Expand Down
50 changes: 50 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions workspace/marqua/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.js
*.d.ts
*.map

!test/**/*.js
5 changes: 0 additions & 5 deletions workspace/marqua/.npmignore

This file was deleted.

30 changes: 19 additions & 11 deletions workspace/marqua/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,37 @@
"version": "0.5.2",
"description": "Augmented Markdown Compiler",
"repository": "github:ignatiusmb/marqua",
"license": "MIT",
"author": "Ignatius Bagus",
"type": "module",
"scripts": {
"watch": "pnpm build --watch",
"build": "tsc --declaration",
"format": "prettier -w . --plugin=prettier-plugin-sort-package-json",
"lint": "pnpm lint:code && pnpm lint:style",
"lint:code": "tsc --noEmit",
"lint:style": "prettier -c . --plugin=prettier-plugin-sort-package-json",
"test": "pnpm test:core && pnpm test:apps",
"test:core": "uvu -r tsm src \"(spec\\.ts)\"",
"test:apps": "pnpm build && uvu test/apps",
"prepublish": "pnpm build && pnpm format"
"test:apps": "tsc && uvu test/apps",
"build:types": "tsm scripts/compile-types.ts",
"build:files": "tsc && tsc --project ./src/artisan/tsconfig.json && rm utils.d.ts",
"prepublish": "pnpm build:types && pnpm build:files"
},
"typings": "index.d.ts",
"exports": {
".": "./core/index.js",
"./artisan": "./artisan/index.js",
"./browser": "./browser/index.js",
"./fs": "./fs/index.js",
"./transform": "./transform/index.js",
"./styles/code.css": "./styles/code.css",
".": "./src/core/index.js",
"./artisan": "./src/artisan/index.js",
"./browser": "./src/browser/index.js",
"./fs": "./src/fs/index.js",
"./transform": "./src/transform/index.js",
"./styles/*.css": "./styles/*.css",
"./package.json": "./package.json"
},
"files": [
"artisan/*.d.ts",
"src/**/*.js",
"styles/",
"index.d.ts",
"index.d.ts.map"
],
"engines": {
"node": ">=14.8"
},
Expand All @@ -47,6 +54,7 @@
"devDependencies": {
"@types/markdown-it": "^13.0.0",
"@types/node": "^20.5.1",
"dts-buddy": "^0.1.12",
"prettier": "^3.0.2",
"prettier-plugin-sort-package-json": "^0.1.1",
"tsm": "^2.3.0",
Expand Down
27 changes: 27 additions & 0 deletions workspace/marqua/scripts/compile-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as fs from 'node:fs';
import { createBundle } from 'dts-buddy';
import { exports } from '../package.json';

walk('./src', (path) => path.endsWith('.js') && fs.unlinkSync(path));

await createBundle({
output: 'index.d.ts',
modules: Object.keys(exports).reduce((acc, key) => {
if (key === './artisan') return acc; // TODO: why did this fails
if (key.slice(2).includes('.')) return acc; // skip non-modules
return { ...acc, ['marqua' + key.slice(1)]: exports[key] };
}, {}),
});

// ---- helper functions ----

function walk(entry: string, fn: (pathname: string) => void) {
for (const name of fs.readdirSync(entry)) {
const path = `${entry}/${name}`;
if (fs.statSync(path).isDirectory()) {
walk(path, fn);
} else {
fn(path);
}
}
}
9 changes: 9 additions & 0 deletions workspace/marqua/src/artisan/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": true,
"declarationDir": "../..",
"emitDeclarationOnly": true
},
"include": ["**/*"]
}
8 changes: 2 additions & 6 deletions workspace/marqua/src/fs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ interface Metadata {
table: MarquaTable[];
}

interface Compiled extends Metadata {
content: string;
}

interface HydrateChunk {
breadcrumb: string[];
buffer: Buffer;
parse: typeof parse;
}

export function compile(entry: string): Compiled;
export function compile(entry: string): Metadata & { content: string };
export function compile<Output extends object>(
entry: string,
hydrate?: (chunk: HydrateChunk) => undefined | Output,
Expand All @@ -34,7 +30,7 @@ export function compile<Output extends object>(
const breadcrumb = entry.split(/[/\\]/).reverse();
if (hydrate) return hydrate({ breadcrumb, buffer, parse });
const { content, metadata } = parse(buffer.toString('utf-8'));
return { ...metadata, content } as Compiled;
return { ...metadata, content } as Metadata & { content: string };
});

if (!result /* hydrate returns nothing */) return;
Expand Down
2 changes: 1 addition & 1 deletion workspace/marqua/test/apps/multiple/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import { traverse } from '../../../fs/index.js';
import { traverse } from '../../../src/fs/index.js';
import { readJSON } from '../utils.js';

const basics = {
Expand Down
1 change: 0 additions & 1 deletion workspace/marqua/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": "mauss/tsconfig.json",
"compilerOptions": {
"outDir": ".",
"module": "ES2022",
"target": "ES2019"
},
Expand Down

1 comment on commit c00ef85

@vercel
Copy link

@vercel vercel bot commented on c00ef85 Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.