@bunup/dts 0.11.23 → 0.11.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -53
- package/dist/index.js +6 -13
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,56 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @bunup/dts
|
|
2
2
|
|
|
3
3
|
A blazing-fast `.d.ts` bundler written in Bun, designed to generate and merge TypeScript declarations from an entry point into a single `index.d.ts`, with advanced features like splitting and minification.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
## How It Works
|
|
8
|
-
|
|
9
|
-
Typeroll leverages **Bun's native bundler** under the hood to achieve blazing-fast TypeScript declaration bundling. Since Bun's bundler is designed for JavaScript and outputs JavaScript (not built for types or `.d.ts` files), we employ some clever tricks to make Bun bundle TypeScript declarations while supporting advanced features like code splitting and minification.
|
|
10
|
-
|
|
11
|
-
### The Process
|
|
12
|
-
|
|
13
|
-
**1. Declaration Generation**
|
|
14
|
-
|
|
15
|
-
First, `typeroll` generates `.d.ts` files from TypeScript sources using **isolated declarations** via `oxc-transform`.
|
|
16
|
-
|
|
17
|
-
**2. FakeJS Transformation**
|
|
18
|
-
|
|
19
|
-
Here's where the magic happens: we convert `.d.ts` files into synthetic JavaScript modules ("FakeJS"). Each type declaration becomes a **token array** where:
|
|
20
|
-
- Type content is broken down into individual tokens
|
|
21
|
-
- **Identifiers are preserved as live JavaScript variables** (not strings)
|
|
22
|
-
- The array variable name matches the original type name
|
|
23
|
-
|
|
24
|
-
```javascript
|
|
25
|
-
// Original TypeScript declaration:
|
|
26
|
-
export interface UserProfile extends BaseUser {
|
|
27
|
-
name: string;
|
|
28
|
-
age: number;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Transformed to FakeJS:
|
|
32
|
-
export var UserProfile = [
|
|
33
|
-
"interface", UserProfile, "extends", BaseUser, "{",
|
|
34
|
-
"name", ":", "string", ";",
|
|
35
|
-
"age", ":", "number", ";",
|
|
36
|
-
"}"
|
|
37
|
-
];
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
**3. Bundling with Bun**
|
|
41
|
-
|
|
42
|
-
Bun treats these FakeJS modules as regular JavaScript, enabling it to:
|
|
43
|
-
- **Track usage** of types through variable references
|
|
44
|
-
- Apply **tree-shaking** to remove unused types
|
|
45
|
-
- Perform **code splitting** when shared types are detected
|
|
46
|
-
- Apply **name mangling** during minification
|
|
47
|
-
|
|
48
|
-
The key insight: since identifiers are live variables, when Bun renames a variable during minification, it automatically updates **all references** throughout the bundle.
|
|
49
|
-
|
|
50
|
-
**4. Rehydration**
|
|
51
|
-
|
|
52
|
-
After bundling, we convert the Bun-bundled FakeJS back to clean `.d.ts` declarations. The beauty is that any name mangling applied by Bun is perfectly preserved—if Bun renamed `UserInterface` to `a`, all references are consistently updated.
|
|
53
|
-
|
|
54
|
-
### The Result
|
|
55
|
-
|
|
56
|
-
This round-trip transformation gives us the best of both worlds: the performance and advanced features of Bun's JavaScript bundler applied to TypeScript declarations, resulting in optimized, tree-shaken, and properly split `.d.ts` files without relying on TypeScript's own emit pipeline.
|
|
5
|
+
@bunup/dts powers [Bunup's TypeScript declarations feature](https://bunup.dev/docs/guide/typescript-declarations). Learn more at [bunup.dev](https://bunup.dev/).
|
package/dist/index.js
CHANGED
|
@@ -6,13 +6,6 @@ import { resolveTsImportPath } from "ts-import-resolver";
|
|
|
6
6
|
// packages/dts/src/constants.ts
|
|
7
7
|
var EMPTY_EXPORT = "export {};";
|
|
8
8
|
|
|
9
|
-
// packages/dts/src/errors.ts
|
|
10
|
-
class TyperollError extends Error {
|
|
11
|
-
constructor(message) {
|
|
12
|
-
super(`typeroll: ${message}`);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
9
|
// packages/dts/src/fake-js.ts
|
|
17
10
|
import { parse } from "@babel/parser";
|
|
18
11
|
|
|
@@ -385,11 +378,11 @@ function processTokenElement(element) {
|
|
|
385
378
|
return null;
|
|
386
379
|
}
|
|
387
380
|
function escapeNewlinesAndTabs(text) {
|
|
388
|
-
return text.replace(/\n/g, "
|
|
381
|
+
return text.replace(/\n/g, "__bunup_dts_intermediate_new__line__").replace(/\t/g, "__bunup_dts_intermediate__tab__");
|
|
389
382
|
}
|
|
390
383
|
function unescapeNewlinesAndTabs(text) {
|
|
391
|
-
return text.replace(/
|
|
392
|
-
`).replace(/
|
|
384
|
+
return text.replace(/__bunup_dts_intermediate_new__line__/g, `
|
|
385
|
+
`).replace(/__bunup_dts_intermediate__tab__/g, "\t");
|
|
393
386
|
}
|
|
394
387
|
function handleNamespace(stmt) {
|
|
395
388
|
const expr = stmt.expression;
|
|
@@ -481,7 +474,7 @@ async function generateDts(entrypoints, options = {}) {
|
|
|
481
474
|
const resolvedEntrypoints = await getFilesFromGlobs(nonAbsoluteEntrypoints, cwd);
|
|
482
475
|
const absoluteEntrypoints = entrypoints.filter((entrypoint) => path.isAbsolute(entrypoint));
|
|
483
476
|
if (!filterTypescriptFiles([...resolvedEntrypoints, ...absoluteEntrypoints]).length) {
|
|
484
|
-
throw new
|
|
477
|
+
throw new Error("One or more of the entrypoints you provided do not exist. Please check that each entrypoint points to a valid file.");
|
|
485
478
|
}
|
|
486
479
|
const collectedErrors = [];
|
|
487
480
|
const resolver = createResolver({
|
|
@@ -553,7 +546,7 @@ async function generateDts(entrypoints, options = {}) {
|
|
|
553
546
|
throw: false
|
|
554
547
|
});
|
|
555
548
|
if (!result.success) {
|
|
556
|
-
throw new
|
|
549
|
+
throw new Error(`DTS bundling failed: ${result.logs}`);
|
|
557
550
|
}
|
|
558
551
|
const outputs = result.outputs.filter((output) => output.kind === "chunk" || output.kind === "entry-point");
|
|
559
552
|
const bundledFiles = [];
|
|
@@ -568,7 +561,7 @@ async function generateDts(entrypoints, options = {}) {
|
|
|
568
561
|
continue;
|
|
569
562
|
}
|
|
570
563
|
if (treeshakedDts.errors.length && !treeshakedDts.code) {
|
|
571
|
-
throw new
|
|
564
|
+
throw new Error(`DTS treeshaking failed for ${entrypoint || outputPath}
|
|
572
565
|
|
|
573
566
|
${JSON.stringify(treeshakedDts.errors, null, 2)}`);
|
|
574
567
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bunup/dts",
|
|
3
3
|
"description": "An extremely fast TypeScript declaration file generator and bundler that outputs to a single file.",
|
|
4
|
-
"version": "0.11.
|
|
4
|
+
"version": "0.11.25",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
"types": "./dist/index.d.ts",
|
|
15
15
|
"default": "./dist/index.js"
|
|
16
16
|
}
|
|
17
|
-
}
|
|
17
|
+
},
|
|
18
|
+
"./package.json": "./package.json"
|
|
18
19
|
},
|
|
19
20
|
"dependencies": {
|
|
20
21
|
"@babel/parser": "^7.28.0",
|