rolldown-plugin-dts 0.15.5 → 0.15.7

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
@@ -1,6 +1,8 @@
1
- # rolldown-plugin-dts [![npm](https://img.shields.io/npm/v/rolldown-plugin-dts.svg)](https://npmjs.com/package/rolldown-plugin-dts)
1
+ # rolldown-plugin-dts
2
2
 
3
- [![Unit Test](https://github.com/sxzz/rolldown-plugin-dts/actions/workflows/unit-test.yml/badge.svg)](https://github.com/sxzz/rolldown-plugin-dts/actions/workflows/unit-test.yml)
3
+ [![npm version][npm-version-src]][npm-version-href]
4
+ [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
+ [![Unit Test][unit-test-src]][unit-test-href]
4
6
 
5
7
  A Rolldown plugin to generate and bundle dts files.
6
8
 
@@ -86,6 +88,14 @@ Resolve external types used in `.d.ts` files from `node_modules`.
86
88
  - If `true`, all external types are resolved.
87
89
  - If an array, only types matching the provided strings or regular expressions are resolved.
88
90
 
91
+ #### `cjsDefault`
92
+
93
+ Determines how the default export is emitted.
94
+
95
+ If set to `true`, and you are only exporting a single item using `export default ...`,
96
+ the output will use `export = ...` instead of the standard ES module syntax.
97
+ This is useful for compatibility with CommonJS.
98
+
89
99
  ### `tsc` Options
90
100
 
91
101
  > [!NOTE]
@@ -167,7 +177,7 @@ This option is automatically enabled when `isolatedDeclarations` in `compilerOpt
167
177
 
168
178
  To use this option, ensure that `@typescript/native-preview` is installed as a dependency.
169
179
 
170
- `tsconfigRaw` option will be ignored when this option is enabled.
180
+ `tsconfigRaw` and `compilerOptions` options will be ignored when this option is enabled.
171
181
 
172
182
  ## Differences from `rollup-plugin-dts`
173
183
 
@@ -204,3 +214,12 @@ Furthermore, the test suite is authorized by them and distributed under the MIT
204
214
  ## License
205
215
 
206
216
  [MIT](./LICENSE) License © 2025 [三咲智子 Kevin Deng](https://github.com/sxzz)
217
+
218
+ <!-- Badges -->
219
+
220
+ [npm-version-src]: https://img.shields.io/npm/v/rolldown-plugin-dts.svg
221
+ [npm-version-href]: https://npmjs.com/package/rolldown-plugin-dts
222
+ [npm-downloads-src]: https://img.shields.io/npm/dm/rolldown-plugin-dts
223
+ [npm-downloads-href]: https://www.npmcharts.com/compare/rolldown-plugin-dts?interval=30
224
+ [unit-test-src]: https://github.com/sxzz/rolldown-plugin-dts/actions/workflows/unit-test.yml/badge.svg
225
+ [unit-test-href]: https://github.com/sxzz/rolldown-plugin-dts/actions/workflows/unit-test.yml
@@ -1,3 +1,5 @@
1
+ import { ChunkFileNamesFunction, PreRenderedChunk } from "rolldown";
2
+
1
3
  //#region src/filename.d.ts
2
4
  declare const RE_JS: RegExp;
3
5
  declare const RE_TS: RegExp;
@@ -9,5 +11,7 @@ declare const RE_VUE: RegExp;
9
11
  declare function filename_js_to_dts(id: string): string;
10
12
  declare function filename_to_dts(id: string): string;
11
13
  declare function filename_dts_to(id: string, ext: "js" | "ts"): string;
14
+ declare function resolveTemplateFn(fn: string | ChunkFileNamesFunction, chunk: PreRenderedChunk): string;
15
+ declare function replaceTemplateName(template: string, name: string): string;
12
16
  //#endregion
13
- 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_to_dts };
17
+ 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_to_dts, replaceTemplateName, resolveTemplateFn };
@@ -15,6 +15,12 @@ function filename_to_dts(id) {
15
15
  function filename_dts_to(id, ext) {
16
16
  return id.replace(RE_DTS, `.$1${ext}`);
17
17
  }
18
+ function resolveTemplateFn(fn, chunk) {
19
+ return typeof fn === "function" ? fn(chunk) : fn;
20
+ }
21
+ function replaceTemplateName(template, name) {
22
+ return template.replaceAll("[name]", name);
23
+ }
18
24
 
19
25
  //#endregion
20
- 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_to_dts };
26
+ 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_to_dts, replaceTemplateName, resolveTemplateFn };
@@ -1,2 +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_to_dts } from "./filename-DUAyqgqS.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_to_dts };
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_to_dts, replaceTemplateName, resolveTemplateFn } from "./filename-BDKLOOY5.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_to_dts, replaceTemplateName, resolveTemplateFn };
package/dist/filename.js CHANGED
@@ -1,3 +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_to_dts } from "./filename-DtB5setL.js";
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_to_dts, replaceTemplateName, resolveTemplateFn } from "./filename-Dd76wOJT.js";
2
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_to_dts };
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_to_dts, replaceTemplateName, resolveTemplateFn };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { RE_CSS, RE_DTS, RE_DTS_MAP, RE_JS, RE_NODE_MODULES, RE_TS, RE_VUE } from "./filename-DUAyqgqS.js";
1
+ import { RE_CSS, RE_DTS, RE_DTS_MAP, RE_JS, RE_NODE_MODULES, RE_TS, RE_VUE } from "./filename-BDKLOOY5.js";
2
2
  import { IsolatedDeclarationsOptions } from "rolldown/experimental";
3
3
  import { TsConfigJson } from "get-tsconfig";
4
4
  import { Plugin } from "rolldown";
@@ -50,6 +50,14 @@ interface GeneralOptions {
50
50
  * Resolve external types used in `.d.ts` files from `node_modules`.
51
51
  */
52
52
  resolve?: boolean | (string | RegExp)[];
53
+ /**
54
+ * Determines how the default export is emitted.
55
+ *
56
+ * If set to `true`, and you are only exporting a single item using `export default ...`,
57
+ * the output will use `export = ...` instead of the standard ES module syntax.
58
+ * This is useful for compatibility with CommonJS.
59
+ */
60
+ cjsDefault?: boolean;
53
61
  }
54
62
  interface TscOptions {
55
63
  /**
@@ -160,6 +168,7 @@ declare function resolveOptions({
160
168
  compilerOptions,
161
169
  sourcemap,
162
170
  resolve,
171
+ cjsDefault,
163
172
  build,
164
173
  incremental,
165
174
  vue,
@@ -174,8 +183,9 @@ declare function resolveOptions({
174
183
  //#region src/fake-js.d.ts
175
184
  declare function createFakeJsPlugin({
176
185
  dtsInput,
177
- sourcemap
178
- }: Pick<OptionsResolved, "dtsInput" | "sourcemap">): Plugin;
186
+ sourcemap,
187
+ cjsDefault
188
+ }: Pick<OptionsResolved, "dtsInput" | "sourcemap" | "cjsDefault">): Plugin;
179
189
  //#endregion
180
190
  //#region src/generate.d.ts
181
191
  declare function createGeneratePlugin({
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
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_to_dts } from "./filename-DtB5setL.js";
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_to_dts, replaceTemplateName, resolveTemplateFn } from "./filename-Dd76wOJT.js";
2
2
  import { createContext, globalContext, invalidateContextFile } from "./context-BG0dlajy.js";
3
3
  import Debug from "debug";
4
4
  import _generate from "@babel/generator";
@@ -32,6 +32,15 @@ function createDtsInputPlugin() {
32
32
  return {
33
33
  ...options,
34
34
  entryFileNames(chunk) {
35
+ const { entryFileNames } = options;
36
+ if (entryFileNames) {
37
+ const nameTemplate = resolveTemplateFn(entryFileNames, chunk);
38
+ const renderedName = replaceTemplateName(nameTemplate, chunk.name);
39
+ if (RE_DTS.test(renderedName)) return nameTemplate;
40
+ const renderedNameWithD = replaceTemplateName(nameTemplate, `${chunk.name}.d`);
41
+ if (RE_DTS.test(renderedNameWithD)) return renderedNameWithD;
42
+ }
43
+ if (RE_DTS.test(chunk.name)) return chunk.name;
35
44
  if (chunk.name.endsWith(".d")) return "[name].ts";
36
45
  return "[name].d.ts";
37
46
  }
@@ -230,7 +239,7 @@ function walk(ast, { enter, leave }) {
230
239
  //#endregion
231
240
  //#region src/fake-js.ts
232
241
  const generate = _generate.default || _generate;
233
- function createFakeJsPlugin({ dtsInput, sourcemap }) {
242
+ function createFakeJsPlugin({ dtsInput, sourcemap, cjsDefault }) {
234
243
  let symbolIdx = 0;
235
244
  const identifierMap = Object.create(null);
236
245
  const symbolMap = /* @__PURE__ */ new Map();
@@ -240,14 +249,20 @@ function createFakeJsPlugin({ dtsInput, sourcemap }) {
240
249
  name: "rolldown-plugin-dts:fake-js",
241
250
  outputOptions(options) {
242
251
  if (options.format === "cjs" || options.format === "commonjs") throw new Error("[rolldown-plugin-dts] Cannot bundle dts files with `cjs` format.");
252
+ const { chunkFileNames } = options;
243
253
  return {
244
254
  ...options,
245
255
  sourcemap: options.sourcemap || sourcemap,
246
256
  entryFileNames: options.entryFileNames ?? (dtsInput ? "[name].ts" : void 0),
247
257
  chunkFileNames(chunk) {
248
- const original = (typeof options.chunkFileNames === "function" ? options.chunkFileNames(chunk) : options.chunkFileNames) || "[name]-[hash].js";
249
- if (!original.includes(".d") && chunk.name.endsWith(".d")) return filename_js_to_dts(original).replace("[name]", chunk.name.slice(0, -2));
250
- return original;
258
+ const nameTemplate = resolveTemplateFn(chunkFileNames || "[name]-[hash].js", chunk);
259
+ if (chunk.name.endsWith(".d")) {
260
+ const renderedNameWithoutD = filename_js_to_dts(replaceTemplateName(nameTemplate, chunk.name.slice(0, -2)));
261
+ if (RE_DTS.test(renderedNameWithoutD)) return renderedNameWithoutD;
262
+ const renderedName = filename_js_to_dts(replaceTemplateName(nameTemplate, chunk.name));
263
+ if (RE_DTS.test(renderedName)) return renderedName;
264
+ }
265
+ return nameTemplate;
251
266
  }
252
267
  };
253
268
  },
@@ -371,7 +386,8 @@ function createFakeJsPlugin({ dtsInput, sourcemap }) {
371
386
  program.body = patchTsNamespace(program.body);
372
387
  program.body = program.body.map((node) => {
373
388
  if (isHelperImport(node)) return null;
374
- if (patchImportExport(node, typeOnlyIds)) return node;
389
+ const newNode = patchImportExport(node, typeOnlyIds, cjsDefault);
390
+ if (newNode) return newNode;
375
391
  if (node.type !== "VariableDeclaration") return node;
376
392
  const [decl] = node.declarations;
377
393
  if (decl.init?.type !== "ArrayExpression" || !decl.init.elements[0]) return null;
@@ -515,7 +531,7 @@ function isReferenceId(node) {
515
531
  function isHelperImport(node) {
516
532
  return node.type === "ImportDeclaration" && node.specifiers.length === 1 && node.specifiers.every((spec) => spec.type === "ImportSpecifier" && spec.imported.type === "Identifier" && ["__export", "__reExport"].includes(spec.imported.name));
517
533
  }
518
- function patchImportExport(node, typeOnlyIds) {
534
+ function patchImportExport(node, typeOnlyIds, cjsDefault) {
519
535
  if (isTypeOf(node, [
520
536
  "ImportDeclaration",
521
537
  "ExportAllDeclaration",
@@ -528,7 +544,14 @@ function patchImportExport(node, typeOnlyIds) {
528
544
  }
529
545
  if (node.source?.value && RE_DTS.test(node.source.value)) {
530
546
  node.source.value = filename_dts_to(node.source.value, "js");
531
- return true;
547
+ return node;
548
+ }
549
+ if (cjsDefault && node.type === "ExportNamedDeclaration" && !node.source && node.specifiers.length === 1 && node.specifiers[0].type === "ExportSpecifier" && resolveString(node.specifiers[0].exported) === "default") {
550
+ const defaultExport = node.specifiers[0];
551
+ return {
552
+ type: "TSExportAssignment",
553
+ expression: defaultExport.local
554
+ };
532
555
  }
533
556
  }
534
557
  }
@@ -684,10 +707,10 @@ function createGeneratePlugin({ tsconfig, tsconfigRaw, build, incremental, cwd,
684
707
  return {
685
708
  ...options,
686
709
  entryFileNames(chunk) {
687
- const original = (typeof options.entryFileNames === "function" ? options.entryFileNames(chunk) : options.entryFileNames) || "[name].js";
688
- if (!chunk.name.endsWith(".d")) return original;
689
- if (RE_DTS.test(original)) return original.replace("[name]", chunk.name.slice(0, -2));
690
- return original.replace(RE_JS, ".$1ts");
710
+ const { entryFileNames } = options;
711
+ const nameTemplate = (typeof entryFileNames === "function" ? entryFileNames(chunk) : entryFileNames) || "[name].js";
712
+ if (chunk.name.endsWith(".d") && RE_JS.test(nameTemplate)) return nameTemplate.replace(RE_JS, ".$1ts");
713
+ return nameTemplate;
691
714
  }
692
715
  };
693
716
  },
@@ -832,7 +855,7 @@ async function runTsgo(root, tsconfig) {
832
855
  //#endregion
833
856
  //#region src/options.ts
834
857
  let warnedTsgo = false;
835
- function resolveOptions({ cwd = process.cwd(), dtsInput = false, emitDtsOnly = false, tsconfig, tsconfigRaw: overriddenTsconfigRaw = {}, compilerOptions = {}, sourcemap, resolve = false, build = false, incremental = false, vue = false, parallel = false, eager = false, newContext = false, emitJs, oxc, tsgo = false }) {
858
+ function resolveOptions({ cwd = process.cwd(), dtsInput = false, emitDtsOnly = false, tsconfig, tsconfigRaw: overriddenTsconfigRaw = {}, compilerOptions = {}, sourcemap, resolve = false, cjsDefault = false, build = false, incremental = false, vue = false, parallel = false, eager = false, newContext = false, emitJs, oxc, tsgo = false }) {
836
859
  let resolvedTsconfig;
837
860
  if (tsconfig === true || tsconfig == null) {
838
861
  const { config, path: path$1 } = getTsconfig(cwd) || {};
@@ -878,6 +901,7 @@ function resolveOptions({ cwd = process.cwd(), dtsInput = false, emitDtsOnly = f
878
901
  tsconfigRaw,
879
902
  sourcemap,
880
903
  resolve,
904
+ cjsDefault,
881
905
  build,
882
906
  incremental,
883
907
  vue,
@@ -914,7 +938,12 @@ function createDtsResolvePlugin({ tsconfig, resolve }) {
914
938
  let dtsResolution = resolver(id, importer);
915
939
  dtsResolution &&= path.normalize(dtsResolution);
916
940
  if (!dtsResolution || !RE_TS.test(dtsResolution) && !RE_VUE.test(dtsResolution)) {
917
- if (!rolldownResolution || !RE_TS.test(rolldownResolution.id) && !RE_VUE.test(rolldownResolution.id)) return external;
941
+ const unresolved = !rolldownResolution || !RE_TS.test(rolldownResolution.id) && !RE_VUE.test(rolldownResolution.id);
942
+ if (unresolved) {
943
+ const isRelativeOrAbsolute = id.startsWith(".") || path.isAbsolute(id);
944
+ if (isRelativeOrAbsolute) return this.error(`Cannot resolve import '${id}' from '${importer}'`);
945
+ return external;
946
+ }
918
947
  dtsResolution = rolldownResolution.id;
919
948
  }
920
949
  if (RE_NODE_MODULES.test(dtsResolution)) {
@@ -264,7 +264,12 @@ function tscEmit(tscOptions) {
264
264
  dtsCode = code;
265
265
  }
266
266
  }, void 0, true, { afterDeclarations: [stripPrivateFields] }, true);
267
- if (emitSkipped && diagnostics.length) return { error: ts.formatDiagnostics(diagnostics, formatHost) };
267
+ const emitErrors = diagnostics.filter((d) => d.category === ts.DiagnosticCategory.Error);
268
+ if (emitErrors.length > 0) return { error: ts.formatDiagnostics(emitErrors, formatHost) };
269
+ if (emitSkipped) {
270
+ const errors = ts.getPreEmitDiagnostics(program).filter((d) => d.category === ts.DiagnosticCategory.Error);
271
+ if (errors.length > 0) return { error: ts.formatDiagnostics(errors, formatHost) };
272
+ }
268
273
  if (!dtsCode && file.isDeclarationFile) {
269
274
  debug("nothing was emitted. fallback to sourceFile text.");
270
275
  dtsCode = file.getFullText();
@@ -1,5 +1,5 @@
1
1
  import "./context-BG0dlajy.js";
2
- import { tscEmit } from "./tsc-BJW5IMTs.js";
2
+ import { tscEmit } from "./tsc-BLPzoCMq.js";
3
3
  import process from "node:process";
4
4
  import { createBirpc } from "birpc";
5
5
 
@@ -10,4 +10,5 @@ createBirpc(functions, {
10
10
  on: (fn) => process.on("message", fn)
11
11
  });
12
12
 
13
- //#endregion
13
+ //#endregion
14
+ export { };
package/dist/tsc.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import "./context-BG0dlajy.js";
2
- import { tscEmit } from "./tsc-BJW5IMTs.js";
2
+ import { tscEmit } from "./tsc-BLPzoCMq.js";
3
3
 
4
4
  export { tscEmit };
package/package.json CHANGED
@@ -1,8 +1,16 @@
1
1
  {
2
2
  "name": "rolldown-plugin-dts",
3
- "version": "0.15.5",
4
- "description": "A Rolldown plugin to bundle dts files",
3
+ "version": "0.15.7",
4
+ "description": "A Rolldown plugin to generate and bundle dts files.",
5
5
  "type": "module",
6
+ "keywords": [
7
+ "rolldown",
8
+ "plugin",
9
+ "dts",
10
+ "typescript",
11
+ "vue",
12
+ "jsdoc"
13
+ ],
6
14
  "license": "MIT",
7
15
  "homepage": "https://github.com/sxzz/rolldown-plugin-dts#readme",
8
16
  "bugs": {
@@ -49,38 +57,38 @@
49
57
  }
50
58
  },
51
59
  "dependencies": {
52
- "@babel/generator": "^7.28.0",
53
- "@babel/parser": "^7.28.0",
60
+ "@babel/generator": "^7.28.3",
61
+ "@babel/parser": "^7.28.3",
54
62
  "@babel/types": "^7.28.2",
55
- "ast-kit": "^2.1.1",
63
+ "ast-kit": "^2.1.2",
56
64
  "birpc": "^2.5.0",
57
65
  "debug": "^4.4.1",
58
66
  "dts-resolver": "^2.1.1",
59
67
  "get-tsconfig": "^4.10.1"
60
68
  },
61
69
  "devDependencies": {
62
- "@sxzz/eslint-config": "^7.1.2",
63
- "@sxzz/prettier-config": "^2.2.3",
64
- "@sxzz/test-utils": "^0.5.9",
70
+ "@sxzz/eslint-config": "^7.1.4",
71
+ "@sxzz/prettier-config": "^2.2.4",
72
+ "@sxzz/test-utils": "^0.5.10",
65
73
  "@types/babel__generator": "^7.27.0",
66
74
  "@types/debug": "^4.1.12",
67
- "@types/node": "^24.2.0",
68
- "@typescript/native-preview": "7.0.0-dev.20250806.1",
69
- "@volar/typescript": "^2.4.22",
70
- "@vue/language-core": "^3.0.5",
71
- "bumpp": "^10.2.2",
75
+ "@types/node": "^24.3.0",
76
+ "@typescript/native-preview": "7.0.0-dev.20250820.1",
77
+ "@volar/typescript": "^2.4.23",
78
+ "@vue/language-core": "^3.0.6",
79
+ "bumpp": "^10.2.3",
72
80
  "diff": "^8.0.2",
73
- "eslint": "^9.32.0",
81
+ "eslint": "^9.33.0",
74
82
  "estree-walker": "^3.0.3",
75
83
  "prettier": "^3.6.2",
76
- "rolldown": "^1.0.0-beta.31",
77
- "rollup-plugin-dts": "^6.2.1",
84
+ "rolldown": "^1.0.0-beta.33",
85
+ "rollup-plugin-dts": "^6.2.3",
78
86
  "tinyglobby": "^0.2.14",
79
- "tsdown": "^0.13.3",
87
+ "tsdown": "^0.14.1",
80
88
  "typescript": "^5.9.2",
81
89
  "vitest": "^3.2.4",
82
90
  "vue": "^3.5.18",
83
- "vue-tsc": "^3.0.5"
91
+ "vue-tsc": "^3.0.6"
84
92
  },
85
93
  "engines": {
86
94
  "node": ">=20.18.0"
@@ -94,6 +102,6 @@
94
102
  "test": "vitest",
95
103
  "typecheck": "tsc --noEmit",
96
104
  "format": "prettier --cache --write .",
97
- "release": "bumpp && pnpm publish"
105
+ "release": "bumpp && unotp pnpm publish"
98
106
  }
99
107
  }