rolldown-plugin-dts 0.6.0 → 0.7.0

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 `isolatedDeclaration` 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 `isolatedDeclaration` 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";
@@ -509,10 +510,10 @@ function createOrGetTsModule(programs, compilerOptions, id, code, isEntry) {
509
510
  }
510
511
  function createTsProgram(compilerOptions, id, code) {
511
512
  const files = new Map([[id, code]]);
513
+ const overrideCompilerOptions = ts.convertCompilerOptionsFromJson(compilerOptions, ".").options;
512
514
  const options = {
513
515
  ...defaultCompilerOptions,
514
- ...loadTsconfig(id),
515
- ...compilerOptions
516
+ ...overrideCompilerOptions
516
517
  };
517
518
  const host = ts.createCompilerHost(options, true);
518
519
  const { readFile: _readFile, fileExists: _fileExists } = host;
@@ -524,14 +525,7 @@ function createTsProgram(compilerOptions, id, code) {
524
525
  if (files.has(fileName)) return files.get(fileName);
525
526
  return _readFile(fileName);
526
527
  };
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);
528
+ const program = ts.createProgram([id], options, host);
535
529
  const sourceFile = program.getSourceFile(id);
536
530
  if (!sourceFile) throw new Error(`Source file not found: ${id}`);
537
531
  return {
@@ -560,39 +554,42 @@ function tscEmit(module) {
560
554
  if (emitSkipped && diagnostics.length) return { error: ts.formatDiagnostics(diagnostics, formatHost) };
561
555
  return { code: dtsCode };
562
556
  }
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
557
 
576
558
  //#endregion
577
559
  //#region src/generate.ts
578
560
  const meta = { dtsFile: true };
579
- function createGeneratePlugin({ compilerOptions, isolatedDeclaration: isolatedDeclaration$1, inputAlias, resolve = false, emitDtsOnly = false }) {
561
+ function createGeneratePlugin({ compilerOptions, isolatedDeclaration: isolatedDeclaration$1, resolve = false, emitDtsOnly = false }) {
580
562
  const dtsMap = new Map();
581
- const inputAliasMap = new Map(inputAlias && Object.entries(inputAlias));
563
+ /**
564
+ * A map of input id to output file name
565
+ *
566
+ * @example
567
+ *
568
+ * inputAlias = new Map([
569
+ * ['/absolute/path/to/src/source_file.ts', 'dist/foo/index'],
570
+ * ])
571
+ */
572
+ const inputAliasMap = new Map();
582
573
  const resolver = createResolver();
583
574
  let programs = [];
584
- let inputOption;
585
575
  return {
586
576
  name: "rolldown-plugin-dts:generate",
587
577
  buildStart(options) {
588
- if (isolatedDeclaration$1 == null) {
578
+ if (!compilerOptions) {
589
579
  const { config } = getTsconfig(options.cwd) || {};
590
- if (config?.compilerOptions?.isolatedDeclarations) isolatedDeclaration$1 = { stripInternal: !!config?.compilerOptions.stripInternal };
580
+ compilerOptions = config?.compilerOptions;
591
581
  }
582
+ if (isolatedDeclaration$1 == null) isolatedDeclaration$1 = !!compilerOptions?.isolatedDeclarations;
583
+ if (isolatedDeclaration$1 === true) isolatedDeclaration$1 = {};
584
+ if (isolatedDeclaration$1 && isolatedDeclaration$1.stripInternal == null) isolatedDeclaration$1.stripInternal = !!compilerOptions?.stripInternal;
592
585
  if (!isolatedDeclaration$1) initTs();
593
- },
594
- options({ input }) {
595
- if (isPlainObject(input)) inputOption = { ...input };
586
+ if (!Array.isArray(options.input)) {
587
+ const cwd = options.cwd || process.cwd();
588
+ for (const [fileName, inputFilePath] of Object.entries(options.input)) {
589
+ const id = path.resolve(cwd, inputFilePath);
590
+ inputAliasMap.set(id, fileName);
591
+ }
592
+ }
596
593
  },
597
594
  outputOptions(options) {
598
595
  return {
@@ -637,19 +634,17 @@ function createGeneratePlugin({ compilerOptions, isolatedDeclaration: isolatedDe
637
634
  src: id
638
635
  });
639
636
  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);
637
+ const name = inputAliasMap.get(id);
643
638
  this.emitFile({
644
639
  type: "chunk",
645
640
  id: dtsId,
646
- name
641
+ name: name ? `${name}.d` : void 0
647
642
  });
648
643
  if (emitDtsOnly) return "//";
649
644
  }
650
645
  }
651
646
  },
652
- async resolveId(id, importer, extraOptions) {
647
+ async resolveId(id, importer) {
653
648
  if (dtsMap.has(id)) return {
654
649
  id,
655
650
  meta
@@ -683,16 +678,6 @@ function createGeneratePlugin({ compilerOptions, isolatedDeclaration: isolatedDe
683
678
  id: dtsId,
684
679
  meta
685
680
  };
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
681
  }
697
682
  },
698
683
  load: {
@@ -715,11 +700,6 @@ function createGeneratePlugin({ compilerOptions, isolatedDeclaration: isolatedDe
715
700
  }
716
701
  };
717
702
  }
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
703
 
724
704
  //#endregion
725
705
  //#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.0",
4
4
  "description": "A Rolldown plugin to bundle dts files",
5
5
  "type": "module",
6
6
  "license": "MIT",