rolldown-plugin-dts 0.13.6 → 0.13.8

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 CHANGED
@@ -6,7 +6,7 @@ A Rolldown plugin to generate and bundle dts files.
6
6
 
7
7
  ## Install
8
8
 
9
- ⚠️ Requires `rolldown@1.0.0-beta.9` or later.
9
+ Requires **`rolldown@1.0.0-beta.9`** or later.
10
10
 
11
11
  ```bash
12
12
  npm i rolldown-plugin-dts
@@ -31,90 +31,132 @@ You can find an example in [here](./rolldown.config.ts).
31
31
 
32
32
  ## Options
33
33
 
34
- ```ts
35
- export interface Options {
36
- /**
37
- * The directory in which the plugin will search for the `tsconfig.json` file.
38
- */
39
- cwd?: string
40
-
41
- /**
42
- * Set to `true` if your entry files are `.d.ts` files instead of `.ts` files.
43
- *
44
- * When enabled, the plugin will skip generating a `.d.ts` file for the entry point.
45
- */
46
- dtsInput?: boolean
47
-
48
- /**
49
- * If `true`, the plugin will emit only `.d.ts` files and remove all other output chunks.
50
- *
51
- * This is especially useful when generating `.d.ts` files for the CommonJS format as part of a separate build step.
52
- */
53
- emitDtsOnly?: boolean
54
-
55
- /**
56
- * The path to the `tsconfig.json` file.
57
- *
58
- * If set to `false`, the plugin will ignore any `tsconfig.json` file.
59
- * You can still specify `compilerOptions` directly in the options.
60
- *
61
- * @default 'tsconfig.json'
62
- */
63
- tsconfig?: string | boolean
64
-
65
- /**
66
- * Pass a raw `tsconfig.json` object directly to the plugin.
67
- *
68
- * @see https://www.typescriptlang.org/tsconfig
69
- */
70
- tsconfigRaw?: Omit<TsConfigJson, 'compilerOptions'>
71
-
72
- /**
73
- * Override the `compilerOptions` specified in `tsconfig.json`.
74
- *
75
- * @see https://www.typescriptlang.org/tsconfig/#compilerOptions
76
- */
77
- compilerOptions?: TsConfigJson.CompilerOptions
78
-
79
- /**
80
- * If `true`, the plugin will generate `.d.ts` files using Oxc,
81
- * which is significantly faster than the TypeScript compiler.
82
- *
83
- * This option is automatically enabled when `isolatedDeclarations` in `compilerOptions` is set to `true`.
84
- */
85
- isolatedDeclarations?:
86
- | boolean
87
- | Omit<IsolatedDeclarationsOptions, 'sourcemap'>
88
-
89
- /**
90
- * If `true`, the plugin will generate declaration maps (`.d.ts.map`) for `.d.ts` files.
91
- */
92
- sourcemap?: boolean
93
-
94
- /**
95
- * Resolve external types used in `.d.ts` files from `node_modules`.
96
- */
97
- resolve?: boolean | (string | RegExp)[]
98
-
99
- /**
100
- * If `true`, the plugin will generate `.d.ts` files using `vue-tsc`.
101
- */
102
- vue?: boolean
103
-
104
- /**
105
- * If `true`, the plugin will launch a separate process for `tsc` or `vue-tsc`.
106
- * This enables processing multiple projects in parallel.
107
- */
108
- parallel?: boolean
109
-
110
- /**
111
- * If `true`, the plugin will prepare all files listed in `tsconfig.json` for `tsc` or `vue-tsc`.
112
- *
113
- * This is especially useful when you have a single `tsconfig.json` for multiple projects in a monorepo.
114
- */
115
- eager?: boolean
116
- }
117
- ```
34
+ Configuration options for the plugin.
35
+ Certainly! Here’s your documentation with simplified section titles (just the option name, no type annotation):
36
+
37
+ ---
38
+
39
+ ### cwd
40
+
41
+ The directory in which the plugin will search for the `tsconfig.json` file.
42
+
43
+ ---
44
+
45
+ ### dtsInput
46
+
47
+ Set to `true` if your entry files are `.d.ts` files instead of `.ts` files.
48
+
49
+ When enabled, the plugin will skip generating a `.d.ts` file for the entry point.
50
+
51
+ ---
52
+
53
+ ### emitDtsOnly
54
+
55
+ If `true`, the plugin will emit only `.d.ts` files and remove all other output chunks.
56
+
57
+ This is especially useful when generating `.d.ts` files for the CommonJS format as part of a separate build step.
58
+
59
+ ---
60
+
61
+ ### tsconfig
62
+
63
+ The path to the `tsconfig.json` file.
64
+
65
+ - If set to `false`, the plugin will ignore any `tsconfig.json` file.
66
+ - You can still specify `compilerOptions` directly in the options.
67
+
68
+ **Default:** `'tsconfig.json'`
69
+
70
+ ---
71
+
72
+ ### tsconfigRaw
73
+
74
+ Pass a raw `tsconfig.json` object directly to the plugin.
75
+
76
+ See: [TypeScript tsconfig documentation](https://www.typescriptlang.org/tsconfig)
77
+
78
+ ---
79
+
80
+ ### incremental
81
+
82
+ Controls how project references and incremental builds are handled:
83
+
84
+ - If your `tsconfig.json` uses [`references`](https://www.typescriptlang.org/tsconfig/#references), the plugin will use [`tsc -b`](https://www.typescriptlang.org/docs/handbook/project-references.html#build-mode-for-typescript) to build the project and all referenced projects before emitting `.d.ts` files.
85
+ - If `incremental` is `true`, all built files (including [`.tsbuildinfo`](https://www.typescriptlang.org/tsconfig/#tsBuildInfoFile)) will be written to disk, similar to running `tsc -b` in your project.
86
+ - If `incremental` is `false`, built files are kept in memory, minimizing disk usage.
87
+
88
+ Enabling this option can speed up builds by caching previous results, which is helpful for large projects with multiple references.
89
+
90
+ **Default:** `true` if your `tsconfig` has [`incremental`](https://www.typescriptlang.org/tsconfig/#incremental) or [`tsBuildInfoFile`](https://www.typescriptlang.org/tsconfig/#tsBuildInfoFile) enabled.
91
+
92
+ > [!NOTE]
93
+ > This option is only used when [`isolatedDeclarations`](#isolateddeclarations) is `false`.
94
+
95
+ ---
96
+
97
+ ### compilerOptions
98
+
99
+ Override the `compilerOptions` specified in `tsconfig.json`.
100
+
101
+ See: [TypeScript compilerOptions documentation](https://www.typescriptlang.org/tsconfig/#compilerOptions)
102
+
103
+ ---
104
+
105
+ ### isolatedDeclarations
106
+
107
+ If `true`, the plugin will generate `.d.ts` files using [Oxc](https://oxc.rs/docs/guide/usage/transformer.html), which is significantly faster than the TypeScript compiler.
108
+
109
+ This option is automatically enabled when `isolatedDeclarations` in `compilerOptions` is set to `true`.
110
+
111
+ ---
112
+
113
+ ### sourcemap
114
+
115
+ If `true`, the plugin will generate declaration maps (`.d.ts.map`) for `.d.ts` files.
116
+
117
+ ---
118
+
119
+ ### resolve
120
+
121
+ Resolve external types used in `.d.ts` files from `node_modules`.
122
+
123
+ - If `true`, all external types are resolved.
124
+ - If an array, only types matching the provided strings or regular expressions are resolved.
125
+
126
+ ---
127
+
128
+ ### vue
129
+
130
+ If `true`, the plugin will generate `.d.ts` files using `vue-tsc`.
131
+
132
+ ---
133
+
134
+ ### parallel
135
+
136
+ If `true`, the plugin will launch a separate process for `tsc` or `vue-tsc`, enabling parallel processing of multiple projects.
137
+
138
+ ---
139
+
140
+ ### eager
141
+
142
+ If `true`, the plugin will prepare all files listed in `tsconfig.json` for `tsc` or `vue-tsc`.
143
+
144
+ This is especially useful when you have a single `tsconfig.json` for multiple projects in a monorepo.
145
+
146
+ ---
147
+
148
+ ### tsgo
149
+
150
+ **[Experimental]** Enables DTS generation using [`tsgo`](https://github.com/microsoft/typescript-go).
151
+
152
+ - To use this option, ensure that `@typescript/native-preview` is installed as a dependency.
153
+ - Set to `true` to enable `tsgo` for the current project.
154
+ - If a string is provided, it should be the root path of your source files.
155
+
156
+ > [!WARNING]
157
+ > This option is experimental and not yet recommended for production environments.
158
+
159
+ ---
118
160
 
119
161
  ## Differences from `rollup-plugin-dts`
120
162
 
@@ -0,0 +1,25 @@
1
+ import path from "node:path";
2
+
3
+ //#region src/filename.ts
4
+ const RE_JS = /\.([cm]?)jsx?$/;
5
+ const RE_TS = /\.([cm]?)tsx?$/;
6
+ const RE_DTS = /\.d\.([cm]?)ts$/;
7
+ const RE_DTS_MAP = /\.d\.([cm]?)ts\.map$/;
8
+ const RE_NODE_MODULES = /[\\/]node_modules[\\/]/;
9
+ const RE_CSS = /\.css$/;
10
+ const RE_VUE = /\.vue$/;
11
+ function filename_js_to_dts(id) {
12
+ return id.replace(RE_JS, ".d.$1ts");
13
+ }
14
+ function filename_ts_to_dts(id) {
15
+ return id.replace(RE_VUE, ".vue.ts").replace(RE_TS, ".d.$1ts");
16
+ }
17
+ function filename_dts_to(id, ext) {
18
+ return id.replace(RE_DTS, `.$1${ext}`);
19
+ }
20
+ function isRelative(id) {
21
+ return path.isAbsolute(id) || id[0] === ".";
22
+ }
23
+
24
+ //#endregion
25
+ export { RE_CSS, RE_DTS, RE_DTS_MAP, RE_JS, RE_NODE_MODULES, RE_TS, RE_VUE, filename_dts_to, filename_js_to_dts, filename_ts_to_dts, isRelative };
@@ -0,0 +1,14 @@
1
+ //#region src/filename.d.ts
2
+ declare const RE_JS: RegExp;
3
+ declare const RE_TS: RegExp;
4
+ declare const RE_DTS: RegExp;
5
+ declare const RE_DTS_MAP: RegExp;
6
+ declare const RE_NODE_MODULES: RegExp;
7
+ declare const RE_CSS: RegExp;
8
+ declare const RE_VUE: RegExp;
9
+ declare function filename_js_to_dts(id: string): string;
10
+ declare function filename_ts_to_dts(id: string): string;
11
+ declare function filename_dts_to(id: string, ext: "js" | "ts"): string;
12
+ declare function isRelative(id: string): boolean;
13
+ //#endregion
14
+ export { RE_CSS, RE_DTS, RE_DTS_MAP, RE_JS, RE_NODE_MODULES, RE_TS, RE_VUE, filename_dts_to, filename_js_to_dts, filename_ts_to_dts, isRelative };
@@ -0,0 +1,2 @@
1
+ import { RE_CSS, RE_DTS, RE_DTS_MAP, RE_JS, RE_NODE_MODULES, RE_TS, RE_VUE, filename_dts_to, filename_js_to_dts, filename_ts_to_dts, isRelative } from "./filename-TbnZq0n4.js";
2
+ export { RE_CSS, RE_DTS, RE_DTS_MAP, RE_JS, RE_NODE_MODULES, RE_TS, RE_VUE, filename_dts_to, filename_js_to_dts, filename_ts_to_dts, isRelative };
@@ -0,0 +1,3 @@
1
+ import { RE_CSS, RE_DTS, RE_DTS_MAP, RE_JS, RE_NODE_MODULES, RE_TS, RE_VUE, filename_dts_to, filename_js_to_dts, filename_ts_to_dts, isRelative } from "./filename-Dpr2dKWZ.js";
2
+
3
+ export { RE_CSS, RE_DTS, RE_DTS_MAP, RE_JS, RE_NODE_MODULES, RE_TS, RE_VUE, filename_dts_to, filename_js_to_dts, filename_ts_to_dts, isRelative };
package/dist/index.d.ts CHANGED
@@ -1,34 +1,9 @@
1
- import { TsConfigJson } from "get-tsconfig";
1
+ import { RE_CSS, RE_DTS, RE_DTS_MAP, RE_JS, RE_NODE_MODULES, RE_TS, RE_VUE } from "./filename-TbnZq0n4.js";
2
2
  import { IsolatedDeclarationsOptions } from "rolldown/experimental";
3
+ import { TsConfigJson } from "get-tsconfig";
3
4
  import { Plugin } from "rolldown";
4
5
 
5
- //#region src/fake-js.d.ts
6
- declare function createFakeJsPlugin({
7
- dtsInput,
8
- sourcemap
9
- }: Pick<OptionsResolved, "dtsInput" | "sourcemap">): Plugin;
10
- //#endregion
11
- //#region src/generate.d.ts
12
- declare function createGeneratePlugin({
13
- tsconfigRaw,
14
- tsconfigDir,
15
- isolatedDeclarations,
16
- emitDtsOnly,
17
- vue,
18
- parallel,
19
- eager
20
- }: Pick<OptionsResolved, "tsconfigRaw" | "tsconfigDir" | "isolatedDeclarations" | "emitDtsOnly" | "vue" | "parallel" | "eager">): Plugin;
21
- //#endregion
22
- //#region src/utils/filename.d.ts
23
- declare const RE_JS: RegExp;
24
- declare const RE_TS: RegExp;
25
- declare const RE_DTS: RegExp;
26
- declare const RE_DTS_MAP: RegExp;
27
- declare const RE_NODE_MODULES: RegExp;
28
- declare const RE_CSS: RegExp;
29
- declare const RE_VUE: RegExp;
30
- //#endregion
31
- //#region src/index.d.ts
6
+ //#region src/options.d.ts
32
7
  interface Options {
33
8
  /**
34
9
  * The directory in which the plugin will search for the `tsconfig.json` file.
@@ -62,6 +37,36 @@ interface Options {
62
37
  */
63
38
  tsconfigRaw?: Omit<TsConfigJson, "compilerOptions">;
64
39
  /**
40
+ * If your tsconfig.json has
41
+ * [`references`](https://www.typescriptlang.org/tsconfig/#references) option,
42
+ * `rolldown-plugin-dts` will use [`tsc
43
+ * -b`](https://www.typescriptlang.org/docs/handbook/project-references.html#build-mode-for-typescript)
44
+ * to build the project and all referenced projects before emitting `.d.ts`
45
+ * files.
46
+ *
47
+ * In such case, if this option is `true`, `rolldown-plugin-dts` will write
48
+ * down all built files into your disk, including
49
+ * [`.tsbuildinfo`](https://www.typescriptlang.org/tsconfig/#tsBuildInfoFile)
50
+ * and other built files. This is equivalent to running `tsc -b` in your
51
+ * project.
52
+ *
53
+ * Otherwise, if this option is `false`, `rolldown-plugin-dts` will write
54
+ * built files only into memory and leave a small footprint in your disk.
55
+ *
56
+ * Enabling this option will decrease the build time by caching previous build
57
+ * results. This is helpful when you have a large project with multiple
58
+ * referenced projects.
59
+ *
60
+ * By default, `incremental` is `true` if your tsconfig has
61
+ * [`incremental`](https://www.typescriptlang.org/tsconfig/#incremental) or
62
+ * [`tsBuildInfoFile`](https://www.typescriptlang.org/tsconfig/#tsBuildInfoFile)
63
+ * enabled.
64
+ *
65
+ * This option is only used when {@link Options.isolatedDeclarations} is
66
+ * `false`.
67
+ */
68
+ incremental?: boolean;
69
+ /**
65
70
  * Override the `compilerOptions` specified in `tsconfig.json`.
66
71
  *
67
72
  * @see https://www.typescriptlang.org/tsconfig/#compilerOptions
@@ -97,18 +102,25 @@ interface Options {
97
102
  * This is especially useful when you have a single `tsconfig.json` for multiple projects in a monorepo.
98
103
  */
99
104
  eager?: boolean;
105
+ /**
106
+ * **[Experimental]** Enables DTS generation using `tsgo`.
107
+ *
108
+ * To use this option, make sure `@typescript/native-preview` is installed as a dependency.
109
+ *
110
+ * **Note:** This option is not yet recommended for production environments.
111
+ */
112
+ tsgo?: boolean | string;
100
113
  }
101
114
  type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
102
115
  type OptionsResolved = Overwrite<Required<Omit<Options, "compilerOptions">>, {
103
116
  tsconfig: string | undefined;
104
117
  isolatedDeclarations: IsolatedDeclarationsOptions | false;
105
118
  tsconfigRaw: TsConfigJson;
106
- tsconfigDir: string;
107
119
  }>;
108
- declare function dts(options?: Options): Plugin[];
109
120
  declare function resolveOptions({
110
121
  cwd,
111
122
  tsconfig,
123
+ incremental,
112
124
  compilerOptions,
113
125
  tsconfigRaw: overriddenTsconfigRaw,
114
126
  isolatedDeclarations,
@@ -118,7 +130,31 @@ declare function resolveOptions({
118
130
  resolve,
119
131
  vue,
120
132
  parallel,
121
- eager
133
+ eager,
134
+ tsgo
122
135
  }: Options): OptionsResolved;
123
136
  //#endregion
124
- export { Options, OptionsResolved, RE_CSS, RE_DTS, RE_DTS_MAP, RE_JS, RE_NODE_MODULES, RE_TS, RE_VUE, createFakeJsPlugin, createGeneratePlugin, dts, resolveOptions };
137
+ //#region src/fake-js.d.ts
138
+ declare function createFakeJsPlugin({
139
+ dtsInput,
140
+ sourcemap
141
+ }: Pick<OptionsResolved, "dtsInput" | "sourcemap">): Plugin;
142
+ //#endregion
143
+ //#region src/generate.d.ts
144
+ declare function createGeneratePlugin({
145
+ tsconfig,
146
+ tsconfigRaw,
147
+ incremental,
148
+ cwd,
149
+ isolatedDeclarations,
150
+ emitDtsOnly,
151
+ vue,
152
+ parallel,
153
+ eager,
154
+ tsgo
155
+ }: Pick<OptionsResolved, "cwd" | "tsconfig" | "tsconfigRaw" | "incremental" | "isolatedDeclarations" | "emitDtsOnly" | "vue" | "parallel" | "eager" | "tsgo">): Plugin;
156
+ //#endregion
157
+ //#region src/index.d.ts
158
+ declare function dts(options?: Options): Plugin[];
159
+ //#endregion
160
+ export { Options, RE_CSS, RE_DTS, RE_DTS_MAP, RE_JS, RE_NODE_MODULES, RE_TS, RE_VUE, createFakeJsPlugin, createGeneratePlugin, dts, resolveOptions };
package/dist/index.js CHANGED
@@ -1,14 +1,18 @@
1
+ import { RE_CSS, RE_DTS, RE_DTS_MAP, RE_JS, RE_NODE_MODULES, RE_TS, RE_VUE, filename_dts_to, filename_js_to_dts, filename_ts_to_dts } from "./filename-Dpr2dKWZ.js";
1
2
  import path from "node:path";
2
- import process from "node:process";
3
3
  import Debug from "debug";
4
- import { getTsconfig, parseTsconfig } from "get-tsconfig";
5
4
  import _generate from "@babel/generator";
6
5
  import { parse } from "@babel/parser";
7
6
  import * as t from "@babel/types";
8
7
  import { isDeclarationType, isTypeOf } from "ast-kit";
9
- import { fork } from "node:child_process";
8
+ import { fork, spawn } from "node:child_process";
9
+ import { existsSync } from "node:fs";
10
+ import { mkdtemp, readFile, rm } from "node:fs/promises";
11
+ import { tmpdir } from "node:os";
10
12
  import { createBirpc } from "birpc";
11
13
  import { ResolverFactory, isolatedDeclaration } from "rolldown/experimental";
14
+ import process from "node:process";
15
+ import { getTsconfig, parseTsconfig } from "get-tsconfig";
12
16
  import { createResolver } from "dts-resolver";
13
17
 
14
18
  //#region src/dts-input.ts
@@ -223,33 +227,14 @@ function walk(ast, { enter, leave }) {
223
227
  return instance.visit(ast, null);
224
228
  }
225
229
 
226
- //#endregion
227
- //#region src/utils/filename.ts
228
- const RE_JS = /\.([cm]?)jsx?$/;
229
- const RE_TS = /\.([cm]?)tsx?$/;
230
- const RE_DTS = /\.d\.([cm]?)ts$/;
231
- const RE_DTS_MAP = /\.d\.([cm]?)ts\.map$/;
232
- const RE_NODE_MODULES = /[\\/]node_modules[\\/]/;
233
- const RE_CSS = /\.css$/;
234
- const RE_VUE = /\.vue$/;
235
- function filename_js_to_dts(id) {
236
- return id.replace(RE_JS, ".d.$1ts");
237
- }
238
- function filename_ts_to_dts(id) {
239
- return id.replace(RE_VUE, ".vue.ts").replace(RE_TS, ".d.$1ts");
240
- }
241
- function filename_dts_to(id, ext) {
242
- return id.replace(RE_DTS, `.$1${ext}`);
243
- }
244
-
245
230
  //#endregion
246
231
  //#region src/fake-js.ts
247
232
  const generate = _generate.default || _generate;
248
233
  function createFakeJsPlugin({ dtsInput, sourcemap }) {
249
234
  let symbolIdx = 0;
250
235
  let identifierIdx = 0;
251
- const symbolMap = new Map();
252
- const commentsMap = new Map();
236
+ const symbolMap = /* @__PURE__ */ new Map();
237
+ const commentsMap = /* @__PURE__ */ new Map();
253
238
  return {
254
239
  name: "rolldown-plugin-dts:fake-js",
255
240
  outputOptions(options) {
@@ -288,7 +273,7 @@ function createFakeJsPlugin({ dtsInput, sourcemap }) {
288
273
  commentsMap.set(id, directives);
289
274
  }
290
275
  const appendStmts = [];
291
- const namespaceStmts = new Map();
276
+ const namespaceStmts = /* @__PURE__ */ new Map();
292
277
  for (const [i, stmt] of program.body.entries()) {
293
278
  const setStmt = (node) => program.body[i] = node;
294
279
  if (rewriteImportExport(stmt, setStmt)) continue;
@@ -401,8 +386,8 @@ function createFakeJsPlugin({ dtsInput, sourcemap }) {
401
386
  return inheritNodeComments(node, original.decl);
402
387
  }).filter((node) => !!node);
403
388
  if (program.body.length === 0) return "export { };";
404
- const comments = new Set();
405
- const commentsValue = new Set();
389
+ const comments = /* @__PURE__ */ new Set();
390
+ const commentsValue = /* @__PURE__ */ new Set();
406
391
  for (const id of chunk.moduleIds) {
407
392
  const preserveComments = commentsMap.get(id);
408
393
  if (preserveComments) {
@@ -438,8 +423,8 @@ function createFakeJsPlugin({ dtsInput, sourcemap }) {
438
423
  return symbolMap.get(symbolId);
439
424
  }
440
425
  function collectDependencies(node, namespaceStmts) {
441
- const deps = new Set();
442
- const seen = new Set();
426
+ const deps = /* @__PURE__ */ new Set();
427
+ const seen = /* @__PURE__ */ new Set();
443
428
  walk(node, { leave(node$1) {
444
429
  if (node$1.type === "ExportNamedDeclaration") {
445
430
  for (const specifier of node$1.specifiers) if (specifier.type === "ExportSpecifier") addDependency(specifier.local);
@@ -528,8 +513,8 @@ function patchImportSource(node) {
528
513
  }
529
514
  }
530
515
  function patchTsNamespace(nodes) {
531
- const emptyObjectAssignments = new Map();
532
- const removed = new Set();
516
+ const emptyObjectAssignments = /* @__PURE__ */ new Map();
517
+ const removed = /* @__PURE__ */ new Set();
533
518
  for (const [i, node] of nodes.entries()) {
534
519
  if (node.type === "VariableDeclaration" && node.declarations.length === 1 && node.declarations[0].id.type === "Identifier" && node.declarations[0].init?.type === "ObjectExpression" && node.declarations[0].init.properties.length === 0) emptyObjectAssignments.set(node.declarations[0].id.name, node);
535
520
  if (node.type !== "ExpressionStatement" || node.expression.type !== "CallExpression" || node.expression.callee.type !== "Identifier" || !node.expression.callee.name.startsWith("__export")) continue;
@@ -626,8 +611,13 @@ function inheritNodeComments(oldNode, newNode) {
626
611
  //#region src/generate.ts
627
612
  const debug$1 = Debug("rolldown-plugin-dts:generate");
628
613
  const WORKER_URL = "./utils/tsc-worker.js";
629
- function createGeneratePlugin({ tsconfigRaw, tsconfigDir, isolatedDeclarations, emitDtsOnly, vue, parallel, eager }) {
630
- const dtsMap = new Map();
614
+ const spawnAsync = (...args) => new Promise((resolve, reject) => {
615
+ const child = spawn(...args);
616
+ child.on("close", () => resolve());
617
+ child.on("error", (error) => reject(error));
618
+ });
619
+ function createGeneratePlugin({ tsconfig, tsconfigRaw, incremental, cwd, isolatedDeclarations, emitDtsOnly, vue, parallel, eager, tsgo }) {
620
+ const dtsMap = /* @__PURE__ */ new Map();
631
621
  /**
632
622
  * A map of input id to output file name
633
623
  *
@@ -637,11 +627,12 @@ function createGeneratePlugin({ tsconfigRaw, tsconfigDir, isolatedDeclarations,
637
627
  * ['/absolute/path/to/src/source_file.ts', 'dist/foo/index'],
638
628
  * ])
639
629
  */
640
- const inputAliasMap = new Map();
630
+ const inputAliasMap = /* @__PURE__ */ new Map();
641
631
  let childProcess;
642
632
  let rpc;
643
633
  let tscEmit;
644
- if (parallel) {
634
+ let tsgoDist;
635
+ if (!tsgo && parallel) {
645
636
  childProcess = fork(new URL(WORKER_URL, import.meta.url), { stdio: "inherit" });
646
637
  rpc = createBirpc({}, {
647
638
  post: (data) => childProcess.send(data),
@@ -651,7 +642,21 @@ function createGeneratePlugin({ tsconfigRaw, tsconfigDir, isolatedDeclarations,
651
642
  return {
652
643
  name: "rolldown-plugin-dts:generate",
653
644
  async buildStart(options) {
654
- if (!parallel && (!isolatedDeclarations || vue)) ({tscEmit} = await import("./tsc-BKvojRml.js"));
645
+ if (tsgo) {
646
+ const tsgoPkg = import.meta.resolve("@typescript/native-preview/package.json");
647
+ const { default: getExePath } = await import(new URL("./lib/getExePath.js", tsgoPkg).href);
648
+ const tsgo$1 = getExePath();
649
+ tsgoDist = await mkdtemp(path.join(tmpdir(), "rolldown-plugin-dts-"));
650
+ await spawnAsync(tsgo$1, [
651
+ "--noEmit",
652
+ "false",
653
+ "--declaration",
654
+ "--emitDeclarationOnly",
655
+ ...tsconfig ? ["-p", tsconfig] : [],
656
+ "--outDir",
657
+ tsgoDist
658
+ ], { stdio: "inherit" });
659
+ } else if (!parallel && (!isolatedDeclarations || vue)) ({tscEmit} = await import("./tsc-sXJZixaR.js"));
655
660
  if (!Array.isArray(options.input)) for (const [name, id] of Object.entries(options.input)) {
656
661
  debug$1("resolving input alias %s -> %s", name, id);
657
662
  let resolved = await this.resolve(id);
@@ -716,7 +721,12 @@ function createGeneratePlugin({ tsconfigRaw, tsconfigDir, isolatedDeclarations,
716
721
  let dtsCode;
717
722
  let map;
718
723
  debug$1("generate dts %s from %s", dtsId, id);
719
- if (isolatedDeclarations && !RE_VUE.test(id)) {
724
+ if (tsgo) {
725
+ if (RE_VUE.test(id)) throw new Error("tsgo does not support Vue files.");
726
+ const dtsPath = path.resolve(tsgoDist, path.relative(path.resolve(typeof tsgo === "string" ? tsgo : "src"), filename_ts_to_dts(id)));
727
+ if (existsSync(dtsPath)) dtsCode = await readFile(dtsPath, "utf8");
728
+ else throw new Error(`tsgo did not generate dts file for ${id}, please check your tsconfig.`);
729
+ } else if (isolatedDeclarations && !RE_VUE.test(id)) {
720
730
  const result = isolatedDeclaration(id, code, isolatedDeclarations);
721
731
  if (result.errors.length) {
722
732
  const [error] = result.errors;
@@ -733,8 +743,10 @@ function createGeneratePlugin({ tsconfigRaw, tsconfigDir, isolatedDeclarations,
733
743
  } else {
734
744
  const entries = eager ? void 0 : Array.from(dtsMap.values()).filter((v) => v.isEntry).map((v) => v.id);
735
745
  const options = {
746
+ tsconfig,
736
747
  tsconfigRaw,
737
- tsconfigDir,
748
+ incremental,
749
+ cwd,
738
750
  entries,
739
751
  id,
740
752
  vue
@@ -756,12 +768,71 @@ function createGeneratePlugin({ tsconfigRaw, tsconfigDir, isolatedDeclarations,
756
768
  generateBundle: emitDtsOnly ? (options, bundle) => {
757
769
  for (const fileName of Object.keys(bundle)) if (bundle[fileName].type === "chunk" && !RE_DTS.test(fileName) && !RE_DTS_MAP.test(fileName)) delete bundle[fileName];
758
770
  } : void 0,
759
- buildEnd() {
771
+ async buildEnd() {
760
772
  childProcess?.kill();
773
+ if (tsgoDist) {
774
+ await rm(tsgoDist, {
775
+ recursive: true,
776
+ force: true
777
+ }).catch(() => {});
778
+ tsgoDist = void 0;
779
+ }
761
780
  }
762
781
  };
763
782
  }
764
783
 
784
+ //#endregion
785
+ //#region src/options.ts
786
+ let warnedTsgo = false;
787
+ function resolveOptions({ cwd = process.cwd(), tsconfig, incremental = false, compilerOptions = {}, tsconfigRaw: overriddenTsconfigRaw = {}, isolatedDeclarations, sourcemap, dtsInput = false, emitDtsOnly = false, resolve = false, vue = false, parallel = false, eager = false, tsgo = false }) {
788
+ let resolvedTsconfig;
789
+ if (tsconfig === true || tsconfig == null) {
790
+ const { config, path: path$1 } = getTsconfig(cwd) || {};
791
+ tsconfig = path$1;
792
+ resolvedTsconfig = config;
793
+ } else if (typeof tsconfig === "string") {
794
+ tsconfig = path.resolve(cwd || process.cwd(), tsconfig);
795
+ resolvedTsconfig = parseTsconfig(tsconfig);
796
+ } else tsconfig = void 0;
797
+ compilerOptions = {
798
+ ...resolvedTsconfig?.compilerOptions,
799
+ ...compilerOptions
800
+ };
801
+ incremental ||= compilerOptions.incremental || !!compilerOptions.tsBuildInfoFile;
802
+ sourcemap ??= !!compilerOptions.declarationMap;
803
+ compilerOptions.declarationMap = sourcemap;
804
+ const tsconfigRaw = {
805
+ ...resolvedTsconfig,
806
+ ...overriddenTsconfigRaw,
807
+ compilerOptions
808
+ };
809
+ if (isolatedDeclarations == null) isolatedDeclarations = !!compilerOptions?.isolatedDeclarations;
810
+ if (isolatedDeclarations === true) isolatedDeclarations = {};
811
+ if (isolatedDeclarations) {
812
+ isolatedDeclarations.stripInternal ??= !!compilerOptions?.stripInternal;
813
+ isolatedDeclarations.sourcemap = !!compilerOptions.declarationMap;
814
+ }
815
+ if (tsgo && !warnedTsgo) {
816
+ console.warn("The `tsgo` option is experimental and may change in the future.");
817
+ warnedTsgo = true;
818
+ }
819
+ return {
820
+ cwd,
821
+ tsconfig,
822
+ tsconfigRaw,
823
+ incremental,
824
+ isolatedDeclarations,
825
+ sourcemap,
826
+ dtsInput,
827
+ emitDtsOnly,
828
+ resolve,
829
+ vue,
830
+ parallel,
831
+ eager,
832
+ tsgo
833
+ };
834
+ }
835
+
765
836
  //#endregion
766
837
  //#region src/resolve.ts
767
838
  function createDtsResolvePlugin({ tsconfig, resolve }) {
@@ -822,49 +893,6 @@ function dts(options = {}) {
822
893
  plugins.push(createDtsResolvePlugin(resolved), createFakeJsPlugin(resolved));
823
894
  return plugins;
824
895
  }
825
- function resolveOptions({ cwd = process.cwd(), tsconfig, compilerOptions = {}, tsconfigRaw: overriddenTsconfigRaw = {}, isolatedDeclarations, sourcemap, dtsInput = false, emitDtsOnly = false, resolve = false, vue = false, parallel = false, eager = false }) {
826
- let resolvedTsconfig;
827
- if (tsconfig === true || tsconfig == null) {
828
- const { config, path: path$1 } = getTsconfig(cwd) || {};
829
- tsconfig = path$1;
830
- resolvedTsconfig = config;
831
- } else if (typeof tsconfig === "string") {
832
- tsconfig = path.resolve(cwd || process.cwd(), tsconfig);
833
- resolvedTsconfig = parseTsconfig(tsconfig);
834
- } else tsconfig = void 0;
835
- compilerOptions = {
836
- ...resolvedTsconfig?.compilerOptions,
837
- ...compilerOptions
838
- };
839
- sourcemap ??= !!compilerOptions.declarationMap;
840
- compilerOptions.declarationMap = sourcemap;
841
- const tsconfigRaw = {
842
- ...resolvedTsconfig,
843
- ...overriddenTsconfigRaw,
844
- compilerOptions
845
- };
846
- const tsconfigDir = tsconfig ? path.dirname(tsconfig) : cwd;
847
- if (isolatedDeclarations == null) isolatedDeclarations = !!compilerOptions?.isolatedDeclarations;
848
- if (isolatedDeclarations === true) isolatedDeclarations = {};
849
- if (isolatedDeclarations) {
850
- isolatedDeclarations.stripInternal ??= !!compilerOptions?.stripInternal;
851
- isolatedDeclarations.sourcemap = !!compilerOptions.declarationMap;
852
- }
853
- return {
854
- cwd,
855
- tsconfig,
856
- tsconfigDir,
857
- tsconfigRaw,
858
- isolatedDeclarations,
859
- sourcemap,
860
- dtsInput,
861
- emitDtsOnly,
862
- resolve,
863
- vue,
864
- parallel,
865
- eager
866
- };
867
- }
868
896
 
869
897
  //#endregion
870
898
  export { RE_CSS, RE_DTS, RE_DTS_MAP, RE_JS, RE_NODE_MODULES, RE_TS, RE_VUE, createFakeJsPlugin, createGeneratePlugin, dts, resolveOptions };
@@ -1,7 +1,52 @@
1
1
  import { createRequire } from "node:module";
2
+ import path from "node:path";
2
3
  import Debug from "debug";
3
4
  import ts from "typescript";
4
5
 
6
+ //#region src/utils/tsc-system.ts
7
+ const debug$2 = Debug("rolldown-plugin-dts:tsc-system");
8
+ const files = /* @__PURE__ */ new Map();
9
+ const fsSystem = {
10
+ ...ts.sys,
11
+ write(message) {
12
+ debug$2(message);
13
+ },
14
+ resolvePath(path$1) {
15
+ if (files.has(path$1)) return path$1;
16
+ return ts.sys.resolvePath(path$1);
17
+ },
18
+ directoryExists(directory) {
19
+ if (Array.from(files.keys()).some((path$1) => path$1.startsWith(directory))) return true;
20
+ return ts.sys.directoryExists(directory);
21
+ },
22
+ fileExists(fileName) {
23
+ if (files.has(fileName)) return true;
24
+ return ts.sys.fileExists(fileName);
25
+ },
26
+ readFile(fileName, ...args) {
27
+ if (files.has(fileName)) return files.get(fileName);
28
+ return ts.sys.readFile(fileName, ...args);
29
+ },
30
+ writeFile(path$1, data, ...args) {
31
+ files.set(path$1, data);
32
+ ts.sys.writeFile(path$1, data, ...args);
33
+ },
34
+ deleteFile(fileName, ...args) {
35
+ files.delete(fileName);
36
+ ts.sys.deleteFile?.(fileName, ...args);
37
+ }
38
+ };
39
+ const memorySystem = {
40
+ ...fsSystem,
41
+ writeFile(path$1, data) {
42
+ files.set(path$1, data);
43
+ },
44
+ deleteFile(fileName) {
45
+ files.delete(fileName);
46
+ }
47
+ };
48
+
49
+ //#endregion
5
50
  //#region src/utils/vue.ts
6
51
  const debug$1 = Debug("rolldown-plugin-dts:vue");
7
52
  let createVueProgram;
@@ -74,14 +119,38 @@ function createOrGetTsModule(options) {
74
119
  programs.push(module.program);
75
120
  return module;
76
121
  }
77
- function createTsProgram({ entries, id, tsconfigRaw, tsconfigDir, vue }) {
78
- const parsedCmd = ts.parseJsonConfigFileContent(tsconfigRaw, ts.sys, tsconfigDir);
122
+ /**
123
+ * Build the root project and all its dependencies projects.
124
+ * This is designed for a project (e.g. tsconfig.json) that has "references" to
125
+ * other composite projects (e.g., tsconfig.node.json and tsconfig.app.json).
126
+ * If `incremental` is `true`, the build result will be cached in the
127
+ * `.tsbuildinfo` file so that the next time the project is built (without
128
+ * changes) the build will be super fast. If `incremental` is `false`, the
129
+ * `.tsbuildinfo` file will only be written to the memory.
130
+ */
131
+ function buildSolution(tsconfig, incremental) {
132
+ debug(`building projects for ${tsconfig} with incremental: ${incremental}`);
133
+ const system = incremental ? fsSystem : memorySystem;
134
+ const host = ts.createSolutionBuilderHost(system);
135
+ const builder = ts.createSolutionBuilder(host, [tsconfig], {
136
+ force: !incremental,
137
+ verbose: true
138
+ });
139
+ const exitStatus = builder.build();
140
+ debug(`built solution for ${tsconfig} with exit status ${exitStatus}`);
141
+ }
142
+ function createTsProgram({ entries, id, tsconfig, tsconfigRaw, incremental, vue, cwd }) {
143
+ const parsedCmd = ts.parseJsonConfigFileContent(tsconfigRaw, fsSystem, tsconfig ? path.dirname(tsconfig) : cwd);
144
+ if (tsconfig && parsedCmd.projectReferences?.length) buildSolution(tsconfig, incremental);
79
145
  const compilerOptions = {
80
146
  ...defaultCompilerOptions,
81
147
  ...parsedCmd.options
82
148
  };
83
- const rootNames = [...new Set([id, ...entries || parsedCmd.fileNames].map((f) => ts.sys.resolvePath(f)))];
149
+ const rootNames = [...new Set([id, ...entries || parsedCmd.fileNames].map((f) => fsSystem.resolvePath(f)))];
84
150
  const host = ts.createCompilerHost(compilerOptions, true);
151
+ host.readFile = fsSystem.readFile;
152
+ host.fileExists = fsSystem.fileExists;
153
+ host.directoryExists = fsSystem.directoryExists;
85
154
  const createProgram = vue ? createVueProgramFactory(ts) : ts.createProgram;
86
155
  const program = createProgram({
87
156
  rootNames,
@@ -90,15 +159,26 @@ function createTsProgram({ entries, id, tsconfigRaw, tsconfigDir, vue }) {
90
159
  projectReferences: parsedCmd.projectReferences
91
160
  });
92
161
  const sourceFile = program.getSourceFile(id);
93
- if (!sourceFile) throw new Error(`Source file not found: ${id}`);
162
+ if (!sourceFile) {
163
+ debug(`source file not found in program: ${id}`);
164
+ if (!fsSystem.fileExists(id)) {
165
+ debug(`File ${id} does not exist on disk.`);
166
+ throw new Error(`Source file not found: ${id}`);
167
+ } else {
168
+ debug(`File ${id} exists on disk.`);
169
+ throw new Error(`Unable to load file ${id} from the program. This seems like a bug of rolldown-plugin-dts. Please report this issue to https://github.com/sxzz/rolldown-plugin-dts/issues`);
170
+ }
171
+ }
94
172
  return {
95
173
  program,
96
174
  file: sourceFile
97
175
  };
98
176
  }
99
177
  function tscEmit(tscOptions) {
178
+ debug(`running tscEmit ${tscOptions.id}`);
100
179
  const module = createOrGetTsModule(tscOptions);
101
180
  const { program, file } = module;
181
+ debug(`got source file: ${file.fileName}`);
102
182
  let dtsCode;
103
183
  let map;
104
184
  const { emitSkipped, diagnostics } = program.emit(file, (fileName, code) => {
@@ -111,6 +191,10 @@ function tscEmit(tscOptions) {
111
191
  }
112
192
  }, void 0, true, void 0, true);
113
193
  if (emitSkipped && diagnostics.length) return { error: ts.formatDiagnostics(diagnostics, formatHost) };
194
+ if (!dtsCode && file.isDeclarationFile) {
195
+ debug("nothing was emitted. fallback to sourceFile text.");
196
+ dtsCode = file.getFullText();
197
+ }
114
198
  return {
115
199
  code: dtsCode,
116
200
  map
@@ -0,0 +1,3 @@
1
+ import { tscEmit } from "./tsc-BjgfmUbK.js";
2
+
3
+ export { tscEmit };
@@ -1,18 +1,21 @@
1
1
  import { TsConfigJson } from "get-tsconfig";
2
2
  import ts from "typescript";
3
+ import { SourceMapInput } from "rolldown";
3
4
 
4
5
  //#region src/utils/tsc.d.ts
5
6
 
6
7
  interface TscOptions {
8
+ tsconfig?: string;
7
9
  tsconfigRaw: TsConfigJson;
8
- tsconfigDir: string;
10
+ cwd: string;
11
+ incremental: boolean;
9
12
  entries?: string[];
10
13
  id: string;
11
14
  vue?: boolean;
12
15
  }
13
16
  interface TscResult {
14
17
  code?: string;
15
- map?: any;
18
+ map?: SourceMapInput;
16
19
  error?: string;
17
20
  }
18
21
  declare function tscEmit(tscOptions: TscOptions): TscResult;
@@ -1,6 +1,6 @@
1
- import { tscEmit } from "../tsc-yr4fe8f_.js";
2
- import process from "node:process";
1
+ import { tscEmit } from "../tsc-BjgfmUbK.js";
3
2
  import { createBirpc } from "birpc";
3
+ import process from "node:process";
4
4
 
5
5
  //#region src/utils/tsc-worker.ts
6
6
  const functions = { tscEmit };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rolldown-plugin-dts",
3
- "version": "0.13.6",
3
+ "version": "0.13.8",
4
4
  "description": "A Rolldown plugin to bundle dts files",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -22,17 +22,22 @@
22
22
  "types": "./dist/index.d.ts",
23
23
  "exports": {
24
24
  ".": "./dist/index.js",
25
+ "./filename": "./dist/filename.js",
25
26
  "./package.json": "./package.json"
26
27
  },
27
28
  "publishConfig": {
28
29
  "access": "public"
29
30
  },
30
31
  "peerDependencies": {
32
+ "@typescript/native-preview": ">=7.0.0-dev.20250601.1",
31
33
  "rolldown": "^1.0.0-beta.9",
32
34
  "typescript": "^5.0.0",
33
35
  "vue-tsc": "~2.2.0"
34
36
  },
35
37
  "peerDependenciesMeta": {
38
+ "@typescript/native-preview": {
39
+ "optional": true
40
+ },
36
41
  "typescript": {
37
42
  "optional": true
38
43
  },
@@ -41,42 +46,46 @@
41
46
  }
42
47
  },
43
48
  "dependencies": {
44
- "@babel/generator": "^7.27.3",
45
- "@babel/parser": "^7.27.3",
49
+ "@babel/generator": "^7.27.5",
50
+ "@babel/parser": "^7.27.5",
46
51
  "@babel/types": "^7.27.3",
47
- "ast-kit": "^2.0.0",
52
+ "ast-kit": "^2.1.0",
48
53
  "birpc": "^2.3.0",
49
54
  "debug": "^4.4.1",
50
- "dts-resolver": "^2.0.1",
55
+ "dts-resolver": "^2.1.1",
51
56
  "get-tsconfig": "^4.10.1"
52
57
  },
53
58
  "devDependencies": {
54
- "@sxzz/eslint-config": "^7.0.1",
59
+ "@sxzz/eslint-config": "^7.0.2",
55
60
  "@sxzz/prettier-config": "^2.2.1",
56
61
  "@sxzz/test-utils": "^0.5.6",
57
62
  "@types/babel__generator": "^7.27.0",
58
63
  "@types/debug": "^4.1.12",
59
- "@types/node": "^22.15.21",
64
+ "@types/node": "^22.15.29",
65
+ "@typescript/native-preview": "7.0.0-dev.20250604.1",
60
66
  "@volar/typescript": "^2.4.14",
61
67
  "@vue/language-core": "^2.2.10",
62
68
  "bumpp": "^10.1.1",
63
69
  "diff": "^8.0.2",
64
- "eslint": "^9.27.0",
70
+ "eslint": "^9.28.0",
65
71
  "estree-walker": "^3.0.3",
66
72
  "prettier": "^3.5.3",
67
- "rolldown": "1.0.0-beta.9",
73
+ "rolldown": "1.0.0-beta.11-commit.f051675",
68
74
  "rollup-plugin-dts": "^6.2.1",
69
75
  "tinyglobby": "^0.2.14",
70
- "tsdown": "^0.12.3",
76
+ "tsdown": "^0.12.6",
71
77
  "tsx": "^4.19.4",
72
78
  "typescript": "^5.8.3",
73
- "vitest": "^3.1.4",
74
- "vue": "^3.5.15",
79
+ "vitest": "^3.2.1",
80
+ "vue": "^3.5.16",
75
81
  "vue-tsc": "^2.2.10"
76
82
  },
77
83
  "engines": {
78
84
  "node": ">=20.18.0"
79
85
  },
86
+ "resolutions": {
87
+ "rolldown": "1.0.0-beta.11-commit.f051675"
88
+ },
80
89
  "prettier": "@sxzz/prettier-config",
81
90
  "scripts": {
82
91
  "lint": "eslint --cache .",
@@ -1,3 +0,0 @@
1
- import { tscEmit } from "./tsc-yr4fe8f_.js";
2
-
3
- export { tscEmit };