• Stars
    star
    313
  • Rank 133,082 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 1 year ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

WORK IN PROGRESS DO NOT USE

dts-buddy

A tool for creating .d.ts bundles.

Why?

If you're creating a package with subpackages (i.e. you can import from my-lib but also my-lib/subpackage), correctly exposing types to your users in a way that works everywhere is difficult difficult lemon difficult.

The TypeScript team recommends a variety of strategies but they all involve adding a bunch of otherwise useless files to your package.

One thing that works everywhere is declare module — if you expose a file like this...

declare module 'my-lib' {
  /**
   * Add two numbers
   */
  export function add(a: number, b: number): number;
}

declare module 'my-lib/subpackage' {
  /**
   * Multiply two numbers
   */
  export function multiply(a: number, b: number): number;
}

...then everyone will get autocompletion and typechecking for those functions. For bonus points, it should include a .d.ts.map file that allows 'go to definition' to take you to the original source. (This rules out hand-authoring the file, which you shouldn't be doing anyway.)

There are other benefits to this approach — you end up with smaller packages, and TypeScript has less work to do on startup, making everything quicker for your users.

Unfortunately, I couldn't find a tool for generating .d.ts bundles, at least not one that worked. dts-buddy aims to fill the gap.

But really, why?

For SvelteKit, where for a long time we hand-authored an ambient.d.ts file containing declare module blocks for subpackages, and also had an index.d.ts file for the main types that had to duplicate the definitions of certain functions. Every time we changed anything, we had to update things in multiple places, and contributing to the codebase was unnecessarily difficult.

An extra dimension is that we have virtual modules like $app/environment, which can't be expressed using any of the techniques suggested by the TypeScript team.

dts-buddy means we can automate generation of all our type definitions, using the source as the, well, source of truth.

How do I use it?

Pick a place to write your .d.ts file to — e.g. types/index.d.ts — then add it to your package.json both as the top-level types property and the types value of each entry in your exports map. Add a prepublishOnly step to your scripts:

{
  "name": "my-lib",
  "version": "1.0.0",
  "type": "module",
+  "types": "./types/index.d.ts",
  "files": [
    "src",
+    "types"
  ],
  "exports": {
    ".": {
+      "types": "./types/index.d.ts",
      "import": "./src/index.js"
    },
    "./subpackage": {
+      "types": "./types/index.d.ts",
      "import": "./src/subpackage.js"
    }
  },
  "scripts": {
+    "prepublishOnly": "dts-buddy"
  }
}

dts-buddy will infer the entry points and the output location from your package.json.

In some cases you may need to specify the entry points and output location manually (for example, you want to use a .d.ts file that re-exports from your .js file as an entry point), in which case:

dts-buddy types/index.d.ts -m my-lib:src/index.js -m my-lib/subpackage:src/subpackage.js

You can also use the JavaScript API directly:

// scripts/generate-dts-bundle.js
import { createBundle } from 'dts-buddy';

await createBundle({
  project: 'tsconfig.json',
  output: 'types/index.d.ts',
  modules: {
    'my-lib': 'src/index.js',
    'my-lib/subpackage': 'src/subpackage.js'
  }
});

Note that the result will also be treeshaken — your .d.ts bundle will only include public types.

License

MIT

More Repositories

1

degit

Straightforward project scaffolding
JavaScript
6,905
star
2

ramjet

Morph DOM elements from one state to another with smooth animations and transitions
JavaScript
5,454
star
3

magic-string

Manipulate strings like a wizard
JavaScript
2,304
star
4

devalue

Gets the job done when JSON.stringify can't
JavaScript
2,085
star
5

pancake

Experimental charting library for Svelte
JavaScript
1,285
star
6

shimport

Use JavaScript modules in all browsers, including dynamic imports
JavaScript
1,238
star
7

svelte-cubed

Svelte ❤️ Three
Svelte
1,191
star
8

butternut

The fast, future-friendly minifier
JavaScript
1,176
star
9

agadoo

Check whether a package is tree-shakeable
JavaScript
535
star
10

headless-qr

A simple, modern QR code library
JavaScript
411
star
11

estree-walker

Traverse an ESTree-compliant AST
JavaScript
385
star
12

code-red

Experimental toolkit for writing x-to-JavaScript compilers
JavaScript
332
star
13

react-svelte

Use Svelte components inside a React app
JavaScript
302
star
14

sorcery

Resolve a chain of sourcemaps back to the original source, like magic
JavaScript
289
star
15

packd

Rollup as a service (with a little help from Browserify)
JavaScript
255
star
16

sveltekit-on-the-edge

SvelteKit, running on the edge
Svelte
217
star
17

simulant

Simulated DOM events for automated testing
JavaScript
202
star
18

svelte-knobby

Svelte
201
star
19

phonograph

🔊 Stream large audio files without the dreaded 'DOMException: play() can only be initiated by a user gesture' error.
TypeScript
199
star
20

svg-parser

JavaScript
196
star
21

svelte-undo

A small utility for managing an undo stack
JavaScript
193
star
22

svelte-gl

Just an idea. For now.
191
star
23

vlq

Generate, and decode, base64 VLQ mappings for sourcemaps and other uses
JavaScript
188
star
24

roadtrip

Client-side routing library. It's about the journey, not just the destination
JavaScript
155
star
25

object-cull

Create a copy of an object with just the bits you actually need
TypeScript
144
star
26

sveltesnaps

Svelte
143
star
27

sander

Promise-based power tool for common filesystem tasks
JavaScript
117
star
28

snowpack-svelte-ssr

JavaScript
116
star
29

svelte-workshop

The website for the Svelte workshop
JavaScript
115
star
30

sveltekit-movies-demo

Movies demo
Svelte
114
star
31

svelte-split-pane

A <SplitPane> component
Svelte
107
star
32

ematchi

Svelte
103
star
33

yootils

Stuff I often need. WIP
JavaScript
103
star
34

Points

Another Pointer Events polyfill
JavaScript
96
star
35

svelte-template-electron

A template for building Electron apps with Svelte
TypeScript
91
star
36

boxxy

Layout manager for web apps.
JavaScript
87
star
37

periscopic

Utility for analyzing scopes belonging to an ESTree-compliant AST
JavaScript
85
star
38

svelte-gl-boxes

a quick Svelte GL demo
HTML
76
star
39

lit-node

Self-documenting Node scripts through literate programming
JavaScript
76
star
40

svelteflix

Svelte
75
star
41

stacking-order

Determine which of two elements is in front of the other
JavaScript
71
star
42

sourcemap-codec

Encode/decode sourcemap mappings
JavaScript
69
star
43

the-answer

The answer to the question of life, the universe and everything
JavaScript
67
star
44

bundler-comparison

A quick test to see how various bundlers compare when bundling the Lodash source code.
JavaScript
66
star
45

svelte-gl-demo

A very basic Svelte GL scene
JavaScript
62
star
46

tape-modern

Minimum viable testing framework
TypeScript
62
star
47

sevenup

Tool for making and loading sprites
JavaScript
61
star
48

zimmerframe

A tool for walking
JavaScript
60
star
49

golden-fleece

Parse and manipulate JSON5 strings
TypeScript
59
star
50

port-authority

Utilities for dealing with ports in Node apps
JavaScript
55
star
51

rollup-svelte-code-splitting

demo repo
JavaScript
50
star
52

svelte-d3-arc-demo

Using Svelte and D3 together to create lightweight interactive visualisations with SSR
JavaScript
49
star
53

begin-svelte-app

Begin app
JavaScript
45
star
54

Statesman

The JavaScript state management library
JavaScript
44
star
55

esrap

Parse in reverse
JavaScript
44
star
56

node-console-group

console.group() for node.js
JavaScript
41
star
57

sveltekit-pdf-demo

Using SvelteKit to generate PDFs dynamically
TypeScript
40
star
58

svelte-three-demo

bare bones demo of svelte-three
JavaScript
38
star
59

locate-character

JavaScript
37
star
60

birdland

The weather report
HTML
35
star
61

svelte-ssr-bundle

Demo of using Svelte and Rollup to create a SSR bundle
JavaScript
35
star
62

generated-types

An example of generated types
TypeScript
32
star
63

svelte-preprocessor-demo

JavaScript
31
star
64

spelunk

Traverse a folder in node, turning its contents into an object for easy consumption
JavaScript
30
star
65

cameoparison-starter

Workshop repo for https://cameoparison.netlify.app
JavaScript
25
star
66

typescript-lib

Project template for a TypeScript library
JavaScript
24
star
67

is-reference

Determine whether an AST node is a reference
JavaScript
22
star
68

ractive-dbmonster

An adaptation of Ember's 'dbmonster' demo of Glimmer
JavaScript
22
star
69

deepClone

Utility for cloning objects and arrays so you can manipulate them without borking your original data
JavaScript
18
star
70

magic-viewbox

Library for making draggable, zoomable, interactive SVG viewboxes
TypeScript
17
star
71

sapper-ws-chat

playing around with websockets
JavaScript
17
star
72

Neo

A matrix manipulation library that tries to explain what the hell's going on
JavaScript
16
star
73

svelte-google-maps

demo of using Svelte with Google Maps
HTML
15
star
74

terser-playground

A playground for seeing how Terser minifies JavaScript
HTML
15
star
75

oscars-data

Svelte
15
star
76

smooth-keyframes

Smoothly interpolate keyframes
TypeScript
14
star
77

transition-this

Svelte
14
star
78

sql

Opinionated wrapper around mysql2
JavaScript
14
star
79

tippex

Find and erase strings and comments in JavaScript code
JavaScript
13
star
80

namey-mcnameface

Generate short but memorable random names for stuff
JavaScript
13
star
81

viewbox

Pan and zoom the contents of SVG elements, and translate between coordinate systems
JavaScript
11
star
82

geotile

Split GeoJSON data up into vector tiles
JavaScript
10
star
83

Ractive-TodoMVC

Example TodoMVC implementation using Ractive.js
CSS
10
star
84

ffmpeg-wasm-demo

JavaScript
10
star
85

pathologist

JavaScript
9
star
86

modulepreload-demo

JavaScript
9
star
87

gurgle

A stream library
JavaScript
9
star
88

birdland-starter

Starter repo for birdland
JavaScript
9
star
89

fowl-play

A demo of SvelteKit's `read` function
JavaScript
9
star
90

svelte-gl-stress-test

JavaScript
8
star
91

superjson-and-devalue

quick script to compare superjson and devalue
JavaScript
7
star
92

sensor

sensor.js - DOM events that don't suck
JavaScript
7
star
93

auto-import-repro

JavaScript
7
star
94

vite-env-reload

HTML
6
star
95

vite-safe-modules-repro

JavaScript
6
star
96

d3-modules

JavaScript
6
star
97

vite-dynamic-import-repro

Repro for https://github.com/sveltejs/kit/issues/8516
JavaScript
6
star
98

svelte-accessors-demo

Using accessors to get and set Svelte component data
JavaScript
6
star
99

simple-css-parser

Parse CSS into JSON
CSS
6
star
100

stackblur

Fork of StackBlur (http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html), used in canvg
JavaScript
6
star