rolldown-plugin-dts 0.6.0 → 0.7.1

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
@@ -29,7 +29,7 @@ You can find a real demo in [here](./rolldown.config.ts).
29
29
 
30
30
  ## Options
31
31
 
32
- ````ts
32
+ ```ts
33
33
  interface Options {
34
34
  /**
35
35
  * When entries are `.d.ts` files (instead of `.ts` files), this option should be set to `true`.
@@ -47,32 +47,23 @@ interface Options {
47
47
 
48
48
  /**
49
49
  * The `compilerOptions` for the TypeScript compiler.
50
+ * The default value will be inferred from the `tsconfig.json` file.
50
51
  *
51
52
  * @see https://www.typescriptlang.org/docs/handbook/compiler-options.html
52
53
  */
53
- compilerOptions?: CompilerOptions
54
+ compilerOptions?: TsConfigJson.CompilerOptions
54
55
  /**
55
56
  * When `true`, the plugin will generate `.d.ts` files using `oxc-transform`,
56
57
  * which is blazingly faster than `typescript` compiler.
57
58
  *
58
- * This option is enabled when `isolatedDeclaration` in `tsconfig.json` is set to `true`.
59
+ * This option is enabled when `isolatedDeclarations` in `compilerOptions` is set to `true`.
59
60
  */
60
61
  isolatedDeclaration?: boolean | Omit<IsolatedDeclarationsOptions, 'sourcemap'>
61
- /**
62
- * dts file name alias `{ [filename]: path }`
63
- *
64
- * @example
65
- * ```ts
66
- * inputAlias: {
67
- * 'foo.d.ts': 'foo/index.d.ts',
68
- * }
69
- */
70
- inputAlias?: Record<string, string>
71
62
 
72
63
  /** Resolve external types used in dts files from `node_modules` */
73
64
  resolve?: boolean | (string | RegExp)[]
74
65
  }
75
- ````
66
+ ```
76
67
 
77
68
  ## Differences from `rollup-plugin-dts`
78
69
 
package/dist/index.d.ts CHANGED
@@ -1,13 +1,13 @@
1
+ import { TsConfigJson } from "get-tsconfig";
1
2
  import { IsolatedDeclarationsOptions } from "oxc-transform";
2
3
  import { Plugin } from "rolldown";
3
- import { CompilerOptions } from "typescript";
4
4
 
5
5
  //#region src/fake-js.d.ts
6
6
  declare function createFakeJsPlugin({ dtsInput }: Pick<Options, "dtsInput">): Plugin;
7
7
 
8
8
  //#endregion
9
9
  //#region src/generate.d.ts
10
- declare function createGeneratePlugin({ compilerOptions, isolatedDeclaration, inputAlias, resolve, emitDtsOnly }: Pick<Options, "isolatedDeclaration" | "inputAlias" | "resolve" | "emitDtsOnly" | "compilerOptions">): Plugin;
10
+ declare function createGeneratePlugin({ compilerOptions, isolatedDeclaration, resolve, emitDtsOnly }: Pick<Options, "isolatedDeclaration" | "resolve" | "emitDtsOnly" | "compilerOptions">): Plugin;
11
11
 
12
12
  //#endregion
13
13
  //#region src/index.d.ts
@@ -26,27 +26,18 @@ interface Options {
26
26
  emitDtsOnly?: boolean;
27
27
  /**
28
28
  * The `compilerOptions` for the TypeScript compiler.
29
+ * The default value will be inferred from the `tsconfig.json` file.
29
30
  *
30
31
  * @see https://www.typescriptlang.org/docs/handbook/compiler-options.html
31
32
  */
32
- compilerOptions?: CompilerOptions;
33
+ compilerOptions?: TsConfigJson.CompilerOptions;
33
34
  /**
34
35
  * When `true`, the plugin will generate `.d.ts` files using `oxc-transform`,
35
36
  * which is blazingly faster than `typescript` compiler.
36
37
  *
37
- * This option is enabled when `isolatedDeclaration` in `tsconfig.json` is set to `true`.
38
+ * This option is enabled when `isolatedDeclarations` in `compilerOptions` is set to `true`.
38
39
  */
39
40
  isolatedDeclaration?: boolean | Omit<IsolatedDeclarationsOptions, "sourcemap">;
40
- /**
41
- * dts file name alias `{ [filename]: path }`
42
- *
43
- * @example
44
- * ```ts
45
- * inputAlias: {
46
- * 'foo.d.ts': 'foo/index.d.ts',
47
- * }
48
- */
49
- inputAlias?: Record<string, string>;
50
41
  /** Resolve external types used in dts files from `node_modules` */
51
42
  resolve?: boolean | (string | RegExp)[];
52
43
  }
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { MagicStringAST } from "magic-string-ast";
2
2
  import { parseSync } from "oxc-parser";
3
- import path, { basename, extname } from "node:path";
3
+ import path from "node:path";
4
+ import process from "node:process";
4
5
  import { createResolver } from "dts-resolver";
5
6
  import { getTsconfig } from "get-tsconfig";
6
7
  import { isolatedDeclaration } from "oxc-transform";
@@ -171,7 +172,7 @@ function getIdentifierRange(node, offset = 0) {
171
172
  //#endregion
172
173
  //#region src/utils/filename.ts
173
174
  const RE_JS = /\.([cm]?)js$/;
174
- const RE_TS = /\.([cm]?)ts$/;
175
+ const RE_TS = /\.([cm]?)tsx?$/;
175
176
  const RE_DTS = /\.d\.([cm]?)ts$/;
176
177
  const RE_NODE_MODULES = /node_modules/;
177
178
  function filename_js_to_dts(id) {
@@ -373,17 +374,18 @@ function collectDependencies(s, node, getIdentifierIndex) {
373
374
  if (node$1.computed && isReferenceId(node$1.key)) addDependency(node$1.key);
374
375
  if ("value" in node$1 && isReferenceId(node$1.value)) addDependency(node$1.value);
375
376
  } else if (node$1.type === "TSTypeReference") addDependency(node$1.typeName);
376
- else if (node$1.type === "TSTypeQuery") addDependency(node$1.exprName);
377
- else if (node$1.type === "TSImportType") {
377
+ else if (node$1.type === "TSTypeQuery") {
378
+ if (node$1.exprName.type !== "TSImportType") addDependency(node$1.exprName);
379
+ } else if (node$1.type === "TSImportType") {
378
380
  if (node$1.argument.type !== "TSLiteralType" || node$1.argument.literal.type !== "Literal" || typeof node$1.argument.literal.value !== "string") return;
379
381
  const source = node$1.argument.literal.value;
380
- const imported = node$1.qualifier && s.sliceNode(node$1.qualifier);
381
- const local = importNamespace(s, source, imported, getIdentifierIndex);
382
+ const imported = node$1.qualifier && resolveTSTypeName(node$1.qualifier);
383
+ const local = importNamespace(s, source, imported && s.sliceNode(imported), getIdentifierIndex);
382
384
  addDependency({
383
385
  type: "Identifier",
384
386
  name: local,
385
- start: node$1.start + (node$1.isTypeOf ? 7 : 0),
386
- end: node$1.qualifier ? node$1.qualifier.end : node$1.end
387
+ start: node$1.start,
388
+ end: imported ? imported.end : node$1.end
387
389
  });
388
390
  }
389
391
  } });
@@ -393,6 +395,10 @@ function collectDependencies(s, node, getIdentifierIndex) {
393
395
  deps.add(node$1);
394
396
  }
395
397
  }
398
+ function resolveTSTypeName(node) {
399
+ if (node.type === "Identifier") return node;
400
+ return resolveTSTypeName(node.left);
401
+ }
396
402
  function isReferenceId(node) {
397
403
  return !!node && (node.type === "Identifier" || node.type === "MemberExpression");
398
404
  }
@@ -509,10 +515,10 @@ function createOrGetTsModule(programs, compilerOptions, id, code, isEntry) {
509
515
  }
510
516
  function createTsProgram(compilerOptions, id, code) {
511
517
  const files = new Map([[id, code]]);
518
+ const overrideCompilerOptions = ts.convertCompilerOptionsFromJson(compilerOptions, ".").options;
512
519
  const options = {
513
520
  ...defaultCompilerOptions,
514
- ...loadTsconfig(id),
515
- ...compilerOptions
521
+ ...overrideCompilerOptions
516
522
  };
517
523
  const host = ts.createCompilerHost(options, true);
518
524
  const { readFile: _readFile, fileExists: _fileExists } = host;
@@ -524,14 +530,7 @@ function createTsProgram(compilerOptions, id, code) {
524
530
  if (files.has(fileName)) return files.get(fileName);
525
531
  return _readFile(fileName);
526
532
  };
527
- const program = ts.createProgram([id], {
528
- ...compilerOptions,
529
- moduleResolution: ts.ModuleResolutionKind.Node10,
530
- declaration: true,
531
- emitDeclarationOnly: true,
532
- outDir: void 0,
533
- declarationDir: void 0
534
- }, host);
533
+ const program = ts.createProgram([id], options, host);
535
534
  const sourceFile = program.getSourceFile(id);
536
535
  if (!sourceFile) throw new Error(`Source file not found: ${id}`);
537
536
  return {
@@ -560,39 +559,42 @@ function tscEmit(module) {
560
559
  if (emitSkipped && diagnostics.length) return { error: ts.formatDiagnostics(diagnostics, formatHost) };
561
560
  return { code: dtsCode };
562
561
  }
563
- const tsconfigCache = new Map();
564
- function loadTsconfig(id) {
565
- const configPath = ts.findConfigFile(path.dirname(id), ts.sys.fileExists);
566
- if (!configPath) return {};
567
- if (tsconfigCache.has(configPath)) return tsconfigCache.get(configPath);
568
- const { config, error } = ts.readConfigFile(configPath, ts.sys.readFile);
569
- if (error) throw ts.formatDiagnostic(error, formatHost);
570
- const configContents = ts.parseJsonConfigFileContent(config, ts.sys, path.dirname(configPath));
571
- if (configContents.errors.length) throw ts.formatDiagnostics(configContents.errors, formatHost);
572
- tsconfigCache.set(configPath, configContents.options);
573
- return configContents.options;
574
- }
575
562
 
576
563
  //#endregion
577
564
  //#region src/generate.ts
578
565
  const meta = { dtsFile: true };
579
- function createGeneratePlugin({ compilerOptions, isolatedDeclaration: isolatedDeclaration$1, inputAlias, resolve = false, emitDtsOnly = false }) {
566
+ function createGeneratePlugin({ compilerOptions, isolatedDeclaration: isolatedDeclaration$1, resolve = false, emitDtsOnly = false }) {
580
567
  const dtsMap = new Map();
581
- const inputAliasMap = new Map(inputAlias && Object.entries(inputAlias));
568
+ /**
569
+ * A map of input id to output file name
570
+ *
571
+ * @example
572
+ *
573
+ * inputAlias = new Map([
574
+ * ['/absolute/path/to/src/source_file.ts', 'dist/foo/index'],
575
+ * ])
576
+ */
577
+ const inputAliasMap = new Map();
582
578
  const resolver = createResolver();
583
579
  let programs = [];
584
- let inputOption;
585
580
  return {
586
581
  name: "rolldown-plugin-dts:generate",
587
582
  buildStart(options) {
588
- if (isolatedDeclaration$1 == null) {
583
+ if (!compilerOptions) {
589
584
  const { config } = getTsconfig(options.cwd) || {};
590
- if (config?.compilerOptions?.isolatedDeclarations) isolatedDeclaration$1 = { stripInternal: !!config?.compilerOptions.stripInternal };
585
+ compilerOptions = config?.compilerOptions;
591
586
  }
587
+ if (isolatedDeclaration$1 == null) isolatedDeclaration$1 = !!compilerOptions?.isolatedDeclarations;
588
+ if (isolatedDeclaration$1 === true) isolatedDeclaration$1 = {};
589
+ if (isolatedDeclaration$1 && isolatedDeclaration$1.stripInternal == null) isolatedDeclaration$1.stripInternal = !!compilerOptions?.stripInternal;
592
590
  if (!isolatedDeclaration$1) initTs();
593
- },
594
- options({ input }) {
595
- if (isPlainObject(input)) inputOption = { ...input };
591
+ if (!Array.isArray(options.input)) {
592
+ const cwd = options.cwd || process.cwd();
593
+ for (const [fileName, inputFilePath] of Object.entries(options.input)) {
594
+ const id = path.resolve(cwd, inputFilePath);
595
+ inputAliasMap.set(id, fileName);
596
+ }
597
+ }
596
598
  },
597
599
  outputOptions(options) {
598
600
  return {
@@ -637,19 +639,17 @@ function createGeneratePlugin({ compilerOptions, isolatedDeclaration: isolatedDe
637
639
  src: id
638
640
  });
639
641
  if (isEntry) {
640
- let name = basename(dtsId, extname(dtsId));
641
- if (inputAliasMap.has(name)) name = inputAliasMap.get(name);
642
- else if (inputAliasMap.has(dtsId)) name = inputAliasMap.get(dtsId);
642
+ const name = inputAliasMap.get(id);
643
643
  this.emitFile({
644
644
  type: "chunk",
645
645
  id: dtsId,
646
- name
646
+ name: name ? `${name}.d` : void 0
647
647
  });
648
648
  if (emitDtsOnly) return "//";
649
649
  }
650
650
  }
651
651
  },
652
- async resolveId(id, importer, extraOptions) {
652
+ async resolveId(id, importer) {
653
653
  if (dtsMap.has(id)) return {
654
654
  id,
655
655
  meta
@@ -683,16 +683,6 @@ function createGeneratePlugin({ compilerOptions, isolatedDeclaration: isolatedDe
683
683
  id: dtsId,
684
684
  meta
685
685
  };
686
- } else if (extraOptions.isEntry && inputOption) {
687
- const resolution = await this.resolve(id, importer, extraOptions);
688
- if (!resolution) return;
689
- const dtsId = filename_ts_to_dts(resolution.id);
690
- if (inputAliasMap.has(dtsId)) return resolution;
691
- for (const [name, entry] of Object.entries(inputOption)) if (entry === id) {
692
- inputAliasMap.set(dtsId, `${name}.d.ts`);
693
- break;
694
- }
695
- return resolution;
696
686
  }
697
687
  },
698
688
  load: {
@@ -715,11 +705,6 @@ function createGeneratePlugin({ compilerOptions, isolatedDeclaration: isolatedDe
715
705
  }
716
706
  };
717
707
  }
718
- function isPlainObject(data) {
719
- if (typeof data !== "object" || data === null) return false;
720
- const proto = Object.getPrototypeOf(data);
721
- return proto === null || proto === Object.prototype;
722
- }
723
708
 
724
709
  //#endregion
725
710
  //#region src/index.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rolldown-plugin-dts",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "A Rolldown plugin to bundle dts files",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -38,19 +38,19 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "debug": "^4.4.0",
41
- "dts-resolver": "^0.1.0",
41
+ "dts-resolver": "^0.1.1",
42
42
  "get-tsconfig": "^4.10.0",
43
43
  "magic-string-ast": "^0.9.1",
44
- "oxc-parser": "^0.62.0",
45
- "oxc-transform": "^0.62.0"
44
+ "oxc-parser": "^0.64.0",
45
+ "oxc-transform": "^0.64.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@sxzz/eslint-config": "^6.1.1",
49
49
  "@sxzz/prettier-config": "^2.2.1",
50
- "@sxzz/test-utils": "^0.5.4",
50
+ "@sxzz/test-utils": "^0.5.5",
51
51
  "@types/debug": "^4.1.12",
52
52
  "@types/diff": "^7.0.2",
53
- "@types/node": "^22.14.0",
53
+ "@types/node": "^22.14.1",
54
54
  "bumpp": "^10.1.0",
55
55
  "diff": "^7.0.0",
56
56
  "eslint": "^9.24.0",
@@ -58,7 +58,7 @@
58
58
  "prettier": "^3.5.3",
59
59
  "rolldown": "^1.0.0-beta.7",
60
60
  "rollup-plugin-dts": "^6.2.1",
61
- "tsdown": "^0.8.0-beta.1",
61
+ "tsdown": "^0.8.0",
62
62
  "tsx": "^4.19.3",
63
63
  "typescript": "^5.8.3",
64
64
  "vitest": "^3.1.1"