rolldown-plugin-dts 0.27.2 → 0.27.4

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
@@ -56,6 +56,22 @@ Configuration options for the plugin.
56
56
 
57
57
  ### General Options
58
58
 
59
+ #### `generator`
60
+
61
+ Specifies which generator to use for `.d.ts` generation.
62
+
63
+ - `'tsc'`: Uses the TypeScript 5.x or 6.x compiler. Compatible with all TypeScript features.
64
+ - `'oxc'`: Uses [Oxc](https://oxc.rs/docs/guide/usage/transformer.html)'s isolated declaration generator, which is significantly faster than the TypeScript compiler. Only compatible with TypeScript features that do not require type checking.
65
+ - `'tsgo'`: **[Experimental]** Uses the TypeScript Go compiler ([`tsgo`](https://github.com/microsoft/typescript-go)). May not support all TypeScript features.
66
+
67
+ **Default:** `'tsc'`, unless `isolatedDeclarations` is enabled in `compilerOptions`, in which case it defaults to `'oxc'`. If TypeScript 7.0 is installed, it defaults to `'tsgo'`.
68
+
69
+ ```ts
70
+ dts({
71
+ generator: 'oxc',
72
+ })
73
+ ```
74
+
59
75
  #### `entry`
60
76
 
61
77
  Glob pattern(s) to filter which files get `.d.ts` generation.
@@ -140,7 +156,7 @@ Indicates whether the generated `.d.ts` files have side effects.
140
156
  ### `tsc` Options
141
157
 
142
158
  > [!NOTE]
143
- > These options are only applicable when `oxc` and `tsgo` are not enabled.
159
+ > These options are only applicable when the `generator` is `'tsc'`.
144
160
 
145
161
  #### `build`
146
162
 
@@ -201,24 +217,37 @@ Enabled by default when `allowJs` in compilerOptions is `true`.
201
217
 
202
218
  ### Oxc
203
219
 
220
+ > [!NOTE]
221
+ > These options are only applicable when the `generator` is `'oxc'`. Set `generator: 'oxc'` to enable the Oxc generator, or leave it unset when `isolatedDeclarations` in `compilerOptions` is `true`.
222
+
204
223
  #### `oxc`
205
224
 
206
- 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.
225
+ Options passed to Oxc's isolated declaration generator.
207
226
 
208
- This option is automatically enabled when `isolatedDeclarations` in `compilerOptions` is set to `true`.
227
+ See: [IsolatedDeclarationsOptions](https://oxc.rs/docs/guide/usage/transformer.html)
209
228
 
210
229
  ### TypeScript Go
211
230
 
212
231
  > [!WARNING]
213
232
  > TypeScript 7.0 does not yet have a stable API and is experimental. This feature is not yet recommended for production environments, and some options will be unavailable.
214
233
 
234
+ > [!NOTE]
235
+ > These options are only applicable when the `generator` is `'tsgo'`. Set `generator: 'tsgo'` to enable the TypeScript Go generator, or leave it unset — it is used automatically when the native TypeScript compiler (v7+) is installed as the `typescript` package. Otherwise, ensure that `@typescript/native-preview` is installed as a dependency.
236
+ >
237
+ > `tsconfigRaw` and `compilerOptions` options are ignored when this generator is used.
238
+
215
239
  #### `tsgo`
216
240
 
217
- **[Experimental]** Enables DTS generation using [`tsgo`](https://github.com/microsoft/typescript-go).
241
+ **[Experimental]** Options for the [`tsgo`](https://github.com/microsoft/typescript-go) generator.
218
242
 
219
- This is automatically enabled when the native TypeScript compiler (v7+) is installed as the `typescript` package. Otherwise, ensure that `@typescript/native-preview` is installed as a dependency.
243
+ - `path`: Custom path to the `tsgo` binary (e.g., when managed by Nix).
220
244
 
221
- `tsconfigRaw` and `compilerOptions` options will be ignored when this option is enabled.
245
+ ```ts
246
+ dts({
247
+ generator: 'tsgo',
248
+ tsgo: { path: '/path/to/tsgo' },
249
+ })
250
+ ```
222
251
 
223
252
  ## Code Splitting Support
224
253
 
package/dist/index.d.mts CHANGED
@@ -3,6 +3,28 @@ import { IsolatedDeclarationsOptions } from "rolldown/experimental";
3
3
  import { TsconfigJson } from "get-tsconfig";
4
4
  //#region src/options.d.ts
5
5
  interface GeneralOptions {
6
+ /**
7
+ * The generator used to produce `.d.ts` files.
8
+ *
9
+ * - `'tsc'`: The TypeScript 5.x/6.x compiler. Supports all TypeScript features.
10
+ * - `'oxc'`: {@link https://oxc.rs Oxc}'s isolated declaration generator. Much
11
+ * faster than `tsc`, but only supports code that satisfies
12
+ * [`isolatedDeclarations`](https://www.typescriptlang.org/tsconfig/#isolatedDeclarations).
13
+ * - `'tsgo'`: **[Experimental]** The TypeScript Go compiler
14
+ * ({@link https://github.com/microsoft/typescript-go tsgo}). May not support
15
+ * all TypeScript features yet.
16
+ *
17
+ * When unset, the generator is inferred:
18
+ * - `'oxc'` if {@link Options.oxc oxc} options are provided or
19
+ * `isolatedDeclarations` is enabled in `compilerOptions`.
20
+ * - `'tsgo'` if TypeScript 7.0 (or `@typescript/native-preview`) is installed,
21
+ * or {@link Options.tsgo tsgo} options are provided.
22
+ * - `'tsc'` otherwise, and always when {@link TscOptions.vue vue} or
23
+ * {@link TscOptions.tsMacro tsMacro} is enabled.
24
+ *
25
+ * @default 'tsc'
26
+ */
27
+ generator?: "tsc" | "oxc" | "tsgo";
6
28
  /**
7
29
  * Glob pattern(s) to filter which entry files get `.d.ts` generation.
8
30
  *
@@ -199,7 +221,6 @@ interface Options extends GeneralOptions, TscOptions {
199
221
  tsgo?: boolean | TsgoOptions;
200
222
  }
201
223
  interface TsgoOptions {
202
- enabled?: boolean;
203
224
  /**
204
225
  * Custom path to the `tsgo` binary.
205
226
  */
@@ -209,17 +230,17 @@ type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
209
230
  type OptionsResolved = Overwrite<Required<Omit<Options, "compilerOptions">>, {
210
231
  entry?: string[];
211
232
  tsconfig?: string;
212
- oxc: IsolatedDeclarationsOptions | false;
233
+ oxc: IsolatedDeclarationsOptions;
213
234
  tsconfigRaw: TsconfigJson;
214
- tsgo: Omit<TsgoOptions, "enabled"> | false;
235
+ tsgo: TsgoOptions;
215
236
  }>;
216
- declare function resolveOptions({ entry, cwd, dtsInput, emitDtsOnly, tsconfig, tsconfigRaw: overriddenTsconfigRaw, compilerOptions, sourcemap, resolver, cjsDefault, sideEffects, build, incremental, vue, tsMacro, parallel, eager, newContext, emitJs, oxc, tsgo }: Options): OptionsResolved;
237
+ declare function resolveOptions({ generator, entry, cwd, dtsInput, emitDtsOnly, tsconfig, tsconfigRaw: overriddenTsconfigRaw, compilerOptions, sourcemap, resolver, cjsDefault, sideEffects, build, incremental, vue, tsMacro, parallel, eager, newContext, emitJs, oxc, tsgo }: Options): OptionsResolved;
217
238
  //#endregion
218
239
  //#region src/fake-js.d.ts
219
240
  declare function createFakeJsPlugin({ sourcemap, cjsDefault, sideEffects }: Pick<OptionsResolved, "sourcemap" | "cjsDefault" | "sideEffects">): Plugin;
220
241
  //#endregion
221
242
  //#region src/generate.d.ts
222
- declare function createGeneratePlugin({ entry, tsconfig, tsconfigRaw, build, incremental, cwd, oxc, emitDtsOnly, vue, tsMacro, parallel, eager, tsgo, newContext, emitJs, sourcemap }: Pick<OptionsResolved, "entry" | "cwd" | "tsconfig" | "tsconfigRaw" | "build" | "incremental" | "oxc" | "emitDtsOnly" | "vue" | "tsMacro" | "parallel" | "eager" | "tsgo" | "newContext" | "emitJs" | "sourcemap">): Plugin;
243
+ declare function createGeneratePlugin({ generator, entry, tsconfig, tsconfigRaw, build, incremental, cwd, oxc, emitDtsOnly, vue, tsMacro, parallel, eager, tsgo, newContext, emitJs, sourcemap }: Pick<OptionsResolved, "generator" | "entry" | "cwd" | "tsconfig" | "tsconfigRaw" | "build" | "incremental" | "oxc" | "emitDtsOnly" | "vue" | "tsMacro" | "parallel" | "eager" | "tsgo" | "newContext" | "emitJs" | "sourcemap">): Plugin;
223
244
  //#endregion
224
245
  //#region src/index.d.ts
225
246
  declare function dts(options?: Options): Plugin[];
package/dist/index.mjs CHANGED
@@ -767,7 +767,7 @@ function getIdentifierIndex(identifierMap, name) {
767
767
  //#endregion
768
768
  //#region src/tsgo.ts
769
769
  const require = createRequire(import.meta.url);
770
- const debug$3 = createDebug("rolldown-plugin-dts:tsgo");
770
+ const debug$4 = createDebug("rolldown-plugin-dts:tsgo");
771
771
  function getTypeScriptMajor() {
772
772
  try {
773
773
  const { version } = require("typescript/package.json");
@@ -777,7 +777,7 @@ function getTypeScriptMajor() {
777
777
  return;
778
778
  }
779
779
  }
780
- function isTsgo() {
780
+ function isTS7Installed() {
781
781
  const major = getTypeScriptMajor();
782
782
  return major != null && major >= 7;
783
783
  }
@@ -787,23 +787,23 @@ const spawnAsync = (...args) => new Promise((resolve, reject) => {
787
787
  child.on("error", (error) => reject(error));
788
788
  });
789
789
  async function getTsgoPathFromNodeModules() {
790
- const pkgName = isTsgo() ? "typescript" : "@typescript/native-preview";
790
+ const pkgName = isTS7Installed() ? "typescript" : "@typescript/native-preview";
791
791
  const tsgoPkg = import.meta.resolve(`${pkgName}/package.json`);
792
792
  const { default: getExePath } = await import(new URL("lib/getExePath.js", tsgoPkg).href);
793
793
  return getExePath();
794
794
  }
795
795
  async function runTsgo(rootDir, tsconfig, sourcemap, tsgoPath) {
796
- debug$3("[tsgo] rootDir", rootDir);
796
+ debug$4("[tsgo] rootDir", rootDir);
797
797
  let tsgo;
798
798
  if (tsgoPath) {
799
799
  tsgo = tsgoPath;
800
- debug$3("[tsgo] using custom path", tsgo);
800
+ debug$4("[tsgo] using custom path", tsgo);
801
801
  } else {
802
802
  tsgo = await getTsgoPathFromNodeModules();
803
- debug$3("[tsgo] using tsgo from node_modules", tsgo);
803
+ debug$4("[tsgo] using tsgo from node_modules", tsgo);
804
804
  }
805
805
  const tsgoDist = await mkdtemp(path.join(tmpdir(), "rolldown-plugin-dts-"));
806
- debug$3("[tsgo] tsgoDist", tsgoDist);
806
+ debug$4("[tsgo] tsgoDist", tsgoDist);
807
807
  const args = [
808
808
  "--noEmit",
809
809
  "false",
@@ -817,14 +817,14 @@ async function runTsgo(rootDir, tsconfig, sourcemap, tsgoPath) {
817
817
  "--noCheck",
818
818
  ...sourcemap ? ["--declarationMap"] : []
819
819
  ];
820
- debug$3("[tsgo] args %o", args);
820
+ debug$4("[tsgo] args %o", args);
821
821
  await spawnAsync(tsgo, args, { stdio: "inherit" });
822
822
  return {
823
823
  path: tsgoDist,
824
824
  async dispose() {
825
- if (debug$3.enabled) debug$3("[tsgo] skip cleanup of tsgoDist", tsgoDist);
825
+ if (debug$4.enabled) debug$4("[tsgo] skip cleanup of tsgoDist", tsgoDist);
826
826
  else {
827
- debug$3("[tsgo] disposing tsgoDist", tsgoDist);
827
+ debug$4("[tsgo] disposing tsgoDist", tsgoDist);
828
828
  await rm(tsgoDist, {
829
829
  recursive: true,
830
830
  force: true
@@ -835,9 +835,9 @@ async function runTsgo(rootDir, tsconfig, sourcemap, tsgoPath) {
835
835
  }
836
836
  //#endregion
837
837
  //#region src/generate.ts
838
- const debug$2 = createDebug("rolldown-plugin-dts:generate");
838
+ const debug$3 = createDebug("rolldown-plugin-dts:generate");
839
839
  const WORKER_URL = "./tsc-worker.mjs";
840
- function createGeneratePlugin({ entry, tsconfig, tsconfigRaw, build, incremental, cwd, oxc, emitDtsOnly, vue, tsMacro, parallel, eager, tsgo, newContext, emitJs, sourcemap }) {
840
+ function createGeneratePlugin({ generator, entry, tsconfig, tsconfigRaw, build, incremental, cwd, oxc, emitDtsOnly, vue, tsMacro, parallel, eager, tsgo, newContext, emitJs, sourcemap }) {
841
841
  const entryIncludes = entry?.filter((p) => p[0] !== "!");
842
842
  const entryIgnores = entry?.filter((p) => p[0] === "!").map((p) => p.slice(1));
843
843
  const entryMatcher = entry ? (file) => entryIncludes.some((p) => path.matchesGlob(file, p)) && !entryIgnores.some((p) => path.matchesGlob(file, p)) : void 0;
@@ -860,18 +860,18 @@ function createGeneratePlugin({ entry, tsconfig, tsconfigRaw, build, incremental
860
860
  return {
861
861
  name: "rolldown-plugin-dts:generate",
862
862
  async buildStart(options) {
863
- if (tsgo) tsgoContext = await runTsgo(rootDir, tsconfig, sourcemap, tsgo.path);
864
- else if (!oxc) if (parallel) tscWorker = createTscWorker();
863
+ if (generator === "tsgo") tsgoContext = await runTsgo(rootDir, tsconfig, sourcemap, tsgo.path);
864
+ else if (generator === "tsc") if (parallel) tscWorker = createTscWorker();
865
865
  else {
866
866
  tscModule = await import("./tsc.mjs");
867
867
  if (newContext) tscContext = createContext();
868
868
  }
869
869
  if (!Array.isArray(options.input)) for (const [name, id] of Object.entries(options.input)) {
870
- debug$2("resolving input alias %s -> %s", name, id);
870
+ debug$3("resolving input alias %s -> %s", name, id);
871
871
  let resolved = await this.resolve(id);
872
872
  if (!id.startsWith("./")) resolved ||= await this.resolve(`./${id}`);
873
873
  const resolvedId = resolved?.id || id;
874
- debug$2("resolved input alias %s -> %s", id, resolvedId);
874
+ debug$3("resolved input alias %s -> %s", id, resolvedId);
875
875
  inputAliasMap.set(resolvedId, name);
876
876
  }
877
877
  },
@@ -891,7 +891,7 @@ function createGeneratePlugin({ entry, tsconfig, tsconfigRaw, build, incremental
891
891
  },
892
892
  resolveId(id) {
893
893
  if (dtsMap.has(id)) {
894
- debug$2("resolve dts id %s", id);
894
+ debug$3("resolve dts id %s", id);
895
895
  return { id };
896
896
  }
897
897
  },
@@ -922,7 +922,7 @@ function createGeneratePlugin({ entry, tsconfig, tsconfigRaw, build, incremental
922
922
  isEntry,
923
923
  jsFile
924
924
  });
925
- debug$2("register dts source: %s", id);
925
+ debug$3("register dts source: %s", id);
926
926
  if (isEntry) {
927
927
  const name = inputAliasMap.get(id);
928
928
  this.emitFile({
@@ -948,17 +948,17 @@ function createGeneratePlugin({ entry, tsconfig, tsconfigRaw, build, incremental
948
948
  if (!module) return;
949
949
  const { code, id, jsFile } = module;
950
950
  if (jsFile && await access(dtsId).then(() => true).catch(() => false)) {
951
- debug$2("dts file already exists for %s, skipping generation", id);
951
+ debug$3("dts file already exists for %s, skipping generation", id);
952
952
  return;
953
953
  }
954
954
  let dtsCode;
955
955
  let map;
956
- debug$2("generate dts %s from %s", dtsId, id);
957
- if (tsgo) {
956
+ debug$3("generate dts %s from %s", dtsId, id);
957
+ if (generator === "tsgo") {
958
958
  if (RE_VUE.test(id)) throw new Error("tsgo does not support Vue files.");
959
959
  const dtsPath = path.resolve(tsgoContext.path, path.relative(path.resolve(rootDir), filename_to_dts(id)));
960
960
  if (!existsSync(dtsPath)) {
961
- debug$2("[tsgo]", dtsPath, "is missing");
961
+ debug$3("[tsgo]", dtsPath, "is missing");
962
962
  throw new Error(`tsgo did not generate dts file for ${id}, please check your tsconfig.`);
963
963
  }
964
964
  dtsCode = await readFile(dtsPath, "utf8");
@@ -970,7 +970,7 @@ function createGeneratePlugin({ entry, tsconfig, tsconfigRaw, build, incremental
970
970
  sources: [id]
971
971
  };
972
972
  }
973
- } else if (oxc && !RE_VUE.test(id)) {
973
+ } else if (generator === "oxc" && !RE_VUE.test(id)) {
974
974
  const result = isolatedDeclarationSync(id, code, oxc);
975
975
  if (result.errors.length) {
976
976
  const [error] = result.errors;
@@ -1108,12 +1108,9 @@ function collectJsonExports(code) {
1108
1108
  }
1109
1109
  //#endregion
1110
1110
  //#region src/options.ts
1111
+ const debug$2 = createDebug("rolldown-plugin-dts:options");
1111
1112
  let warnedTsgo = false;
1112
- function resolveOptions({ entry, cwd = process.cwd(), dtsInput = false, emitDtsOnly = false, tsconfig, tsconfigRaw: overriddenTsconfigRaw = {}, compilerOptions = {}, sourcemap, resolver = "oxc", cjsDefault = false, sideEffects = false, build = false, incremental = false, vue = false, tsMacro = false, parallel = false, eager = false, newContext = false, emitJs, oxc, tsgo }) {
1113
- if (tsgo == null) tsgo = isTsgo() && !vue && !tsMacro && !oxc;
1114
- if (tsgo === true) tsgo = {};
1115
- else if (typeof tsgo === "object" && tsgo.enabled === false) tsgo = false;
1116
- if (!tsgo && isTsgo()) throw new Error("[rolldown-plugin-dts] TypeScript 7.0 is installed, but the `tsgo` option is disabled. Please enable it to use TypeScript 7.0 features.");
1113
+ function resolveOptions({ generator, entry, cwd = process.cwd(), dtsInput = false, emitDtsOnly = false, tsconfig, tsconfigRaw: overriddenTsconfigRaw = {}, compilerOptions = {}, sourcemap, resolver = "oxc", cjsDefault = false, sideEffects = false, build = false, incremental = false, vue = false, tsMacro = false, parallel = false, eager = false, newContext = false, emitJs, oxc, tsgo }) {
1117
1114
  let resolvedTsconfig;
1118
1115
  if (tsconfig === true || tsconfig == null) {
1119
1116
  const { config, path } = getTsconfig(cwd) || {};
@@ -1135,25 +1132,24 @@ function resolveOptions({ entry, cwd = process.cwd(), dtsInput = false, emitDtsO
1135
1132
  ...overriddenTsconfigRaw,
1136
1133
  compilerOptions
1137
1134
  };
1138
- oxc ??= !!(compilerOptions?.isolatedDeclarations && !vue && !tsgo && !tsMacro);
1139
- if (oxc === true) oxc = {};
1135
+ if (!generator) if (vue || tsMacro) generator = "tsc";
1136
+ else if (tsgo) generator = "tsgo";
1137
+ else if (oxc || compilerOptions?.isolatedDeclarations) generator = "oxc";
1138
+ else if (isTS7Installed()) generator = "tsgo";
1139
+ else generator = "tsc";
1140
+ if (oxc === true || !oxc) oxc = {};
1140
1141
  if (oxc) {
1141
1142
  oxc.stripInternal ??= !!compilerOptions?.stripInternal;
1142
1143
  oxc.sourcemap = !!compilerOptions.declarationMap;
1143
1144
  }
1145
+ if (tsgo === true || !tsgo) tsgo = {};
1144
1146
  emitJs ??= !!(compilerOptions.checkJs || compilerOptions.allowJs);
1145
- if (tsgo) {
1146
- if (vue) throw new Error("[rolldown-plugin-dts] The `tsgo` option is not compatible with the `vue` option. Please disable one of them.");
1147
- if (tsMacro) throw new Error("[rolldown-plugin-dts] The `tsgo` option is not compatible with the `tsMacro` option. Please disable one of them.");
1148
- if (oxc) throw new Error("[rolldown-plugin-dts] The `tsgo` option is not compatible with the `oxc` option. Please disable one of them.");
1149
- }
1150
- if (oxc && vue) throw new Error("[rolldown-plugin-dts] The `oxc` option is not compatible with the `vue` option. Please disable one of them.");
1151
- if (oxc && tsMacro) throw new Error("[rolldown-plugin-dts] The `oxc` option is not compatible with the `tsMacro` option. Please disable one of them.");
1152
- if (tsgo && !warnedTsgo) {
1147
+ if (generator === "tsgo" && !warnedTsgo) {
1153
1148
  console.warn("TypeScript 7.0 does not yet have a stable API and is experimental. Some options will be unavailable.");
1154
1149
  warnedTsgo = true;
1155
1150
  }
1156
- return {
1151
+ const resolved = {
1152
+ generator,
1157
1153
  entry: entry ? Array.isArray(entry) ? entry : [entry] : void 0,
1158
1154
  cwd,
1159
1155
  dtsInput,
@@ -1175,6 +1171,8 @@ function resolveOptions({ entry, cwd = process.cwd(), dtsInput = false, emitDtsO
1175
1171
  oxc,
1176
1172
  tsgo
1177
1173
  };
1174
+ debug$2("Resolved Options: %O", resolved);
1175
+ return resolved;
1178
1176
  }
1179
1177
  //#endregion
1180
1178
  //#region src/resolver.ts
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rolldown-plugin-dts",
3
3
  "type": "module",
4
- "version": "0.27.2",
4
+ "version": "0.27.4",
5
5
  "description": "A Rolldown plugin to generate and bundle dts files.",
6
6
  "author": "Kevin Deng <sxzz@sxzz.moe>",
7
7
  "license": "MIT",