rolldown-plugin-dts 0.9.1 → 0.9.3

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/dist/index.d.ts CHANGED
@@ -6,17 +6,15 @@ import { Plugin } from "rolldown";
6
6
  declare function createFakeJsPlugin({
7
7
  dtsInput,
8
8
  sourcemap
9
- }: OptionsResolved): Plugin;
9
+ }: Pick<OptionsResolved, "dtsInput" | "sourcemap">): Plugin;
10
10
 
11
11
  //#endregion
12
12
  //#region src/generate.d.ts
13
13
  declare function createGeneratePlugin({
14
- tsconfig,
15
14
  compilerOptions,
16
15
  isolatedDeclarations,
17
- resolve,
18
16
  emitDtsOnly
19
- }: OptionsResolved): Plugin;
17
+ }: Pick<OptionsResolved, "compilerOptions" | "isolatedDeclarations" | "emitDtsOnly">): Plugin;
20
18
 
21
19
  //#endregion
22
20
  //#region src/index.d.ts
package/dist/index.js CHANGED
@@ -1,35 +1,15 @@
1
1
  import path from "node:path";
2
2
  import process from "node:process";
3
+ import Debug from "debug";
3
4
  import { getTsconfig, parseTsconfig } from "get-tsconfig";
4
5
  import _generate from "@babel/generator";
5
6
  import { parse } from "@babel/parser";
6
7
  import * as t from "@babel/types";
7
8
  import { isDeclarationType, isTypeOf } from "ast-kit";
8
- import { createResolver } from "dts-resolver";
9
9
  import { isolatedDeclaration } from "oxc-transform";
10
10
  import { createRequire } from "node:module";
11
- import Debug from "debug";
12
-
13
- //#region src/utils/filename.ts
14
- const RE_JS = /\.([cm]?)jsx?$/;
15
- const RE_TS = /\.([cm]?)tsx?$/;
16
- const RE_DTS = /\.d\.([cm]?)ts$/;
17
- const RE_DTS_MAP = /\.d\.([cm]?)ts\.map$/;
18
- const RE_NODE_MODULES = /[\\/]node_modules[\\/]/;
19
- function filename_js_to_dts(id) {
20
- return id.replace(RE_JS, ".d.$1ts");
21
- }
22
- function filename_ts_to_dts(id) {
23
- return id.replace(RE_TS, ".d.$1ts");
24
- }
25
- function filename_dts_to(id, ext) {
26
- return id.replace(RE_DTS, `.$1${ext}`);
27
- }
28
- function isRelative(id) {
29
- return path.isAbsolute(id) || id[0] === ".";
30
- }
11
+ import { createResolver } from "dts-resolver";
31
12
 
32
- //#endregion
33
13
  //#region src/dts-input.ts
34
14
  function createDtsInputPlugin() {
35
15
  return {
@@ -51,16 +31,6 @@ function createDtsInputPlugin() {
51
31
  return "[name].d.ts";
52
32
  }
53
33
  };
54
- },
55
- resolveId: {
56
- order: "pre",
57
- handler(id, importer, options) {
58
- if (options.isEntry) return;
59
- if (RE_NODE_MODULES.test(id) || !isRelative(id)) return {
60
- id,
61
- external: true
62
- };
63
- }
64
34
  }
65
35
  };
66
36
  }
@@ -252,6 +222,26 @@ function walk(ast, { enter, leave }) {
252
222
  return instance.visit(ast, null);
253
223
  }
254
224
 
225
+ //#endregion
226
+ //#region src/utils/filename.ts
227
+ const RE_JS = /\.([cm]?)jsx?$/;
228
+ const RE_TS = /\.([cm]?)tsx?$/;
229
+ const RE_DTS = /\.d\.([cm]?)ts$/;
230
+ const RE_DTS_MAP = /\.d\.([cm]?)ts\.map$/;
231
+ const RE_NODE_MODULES = /[\\/]node_modules[\\/]/;
232
+ function filename_js_to_dts(id) {
233
+ return id.replace(RE_JS, ".d.$1ts");
234
+ }
235
+ function filename_ts_to_dts(id) {
236
+ return id.replace(RE_TS, ".d.$1ts");
237
+ }
238
+ function filename_dts_to(id, ext) {
239
+ return id.replace(RE_DTS, `.$1${ext}`);
240
+ }
241
+ function isRelative(id) {
242
+ return path.isAbsolute(id) || id[0] === ".";
243
+ }
244
+
255
245
  //#endregion
256
246
  //#region src/fake-js.ts
257
247
  const generate = _generate.default || _generate;
@@ -641,11 +631,11 @@ function overwriteNode(node, newNode) {
641
631
 
642
632
  //#endregion
643
633
  //#region src/utils/tsc.ts
644
- const debug = Debug("rolldown-plugin-dts:tsc");
634
+ const debug$2 = Debug("rolldown-plugin-dts:tsc");
645
635
  let ts;
646
636
  let formatHost;
647
637
  function initTs() {
648
- debug("loading typescript");
638
+ debug$2("loading typescript");
649
639
  const require = createRequire(import.meta.url);
650
640
  ts = require("typescript");
651
641
  formatHost = {
@@ -653,7 +643,7 @@ function initTs() {
653
643
  getNewLine: () => ts.sys.newLine,
654
644
  getCanonicalFileName: ts.sys.useCaseSensitiveFileNames ? (f) => f : (f) => f.toLowerCase()
655
645
  };
656
- debug(`loaded typescript: ${ts.version}`);
646
+ debug$2(`loaded typescript: ${ts.version}`);
657
647
  }
658
648
  const defaultCompilerOptions = {
659
649
  declaration: true,
@@ -679,9 +669,9 @@ function createOrGetTsModule(programs, compilerOptions, id, isEntry, dtsMap) {
679
669
  file: sourceFile
680
670
  };
681
671
  }
682
- debug(`create program for module: ${id}`);
672
+ debug$2(`create program for module: ${id}`);
683
673
  const module = createTsProgram(compilerOptions, dtsMap, id);
684
- debug(`created program for module: ${id}`);
674
+ debug$2(`created program for module: ${id}`);
685
675
  programs.push(module.program);
686
676
  return module;
687
677
  }
@@ -696,13 +686,13 @@ function createTsProgram(compilerOptions, dtsMap, id) {
696
686
  host.fileExists = (fileName) => {
697
687
  const module = getTsModule(dtsMap, fileName);
698
688
  if (module) return true;
699
- if (debug.enabled && !RE_NODE_MODULES.test(fileName)) debug(`file exists from fs: ${fileName}`);
689
+ if (debug$2.enabled && !RE_NODE_MODULES.test(fileName)) debug$2(`file exists from fs: ${fileName}`);
700
690
  return _fileExists(fileName);
701
691
  };
702
692
  host.readFile = (fileName) => {
703
693
  const module = getTsModule(dtsMap, fileName);
704
694
  if (module) return module.code;
705
- if (debug.enabled && !RE_NODE_MODULES.test(fileName)) debug(`read file from fs: ${fileName}`);
695
+ if (debug$2.enabled && !RE_NODE_MODULES.test(fileName)) debug$2(`read file from fs: ${fileName}`);
706
696
  return _readFile(fileName);
707
697
  };
708
698
  const entries = Array.from(dtsMap.values()).filter((v) => v.isEntry).map((v) => v.id);
@@ -722,10 +712,10 @@ function tscEmit(module) {
722
712
  file,
723
713
  (fileName, code) => {
724
714
  if (fileName.endsWith(".map")) {
725
- debug(`emit dts sourcemap: ${fileName}`);
715
+ debug$2(`emit dts sourcemap: ${fileName}`);
726
716
  map = JSON.parse(code);
727
717
  } else {
728
- debug(`emit dts: ${fileName}`);
718
+ debug$2(`emit dts: ${fileName}`);
729
719
  dtsCode = code;
730
720
  }
731
721
  },
@@ -749,8 +739,8 @@ function getTsModule(dtsMap, tsId) {
749
739
 
750
740
  //#endregion
751
741
  //#region src/generate.ts
752
- const meta = { dtsFile: true };
753
- function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarations, resolve = false, emitDtsOnly = false }) {
742
+ const debug$1 = Debug("rolldown-plugin-dts:generate");
743
+ function createGeneratePlugin({ compilerOptions = {}, isolatedDeclarations, emitDtsOnly = false }) {
754
744
  const dtsMap = new Map();
755
745
  /**
756
746
  * A map of input id to output file name
@@ -763,15 +753,17 @@ function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarat
763
753
  */
764
754
  const inputAliasMap = new Map();
765
755
  let programs = [];
766
- const resolver = createResolver({ tsconfig: tsconfig ? tsconfig : void 0 });
767
756
  if (!isolatedDeclarations) initTs();
768
757
  return {
769
758
  name: "rolldown-plugin-dts:generate",
770
759
  async buildStart(options) {
771
760
  if (!Array.isArray(options.input)) for (const [name, id] of Object.entries(options.input)) {
772
- let resolved = await this.resolve(id, void 0, { skipSelf: true });
773
- resolved ||= await this.resolve(`./${id}`, void 0, { skipSelf: true });
774
- inputAliasMap.set(resolved?.id || id, name);
761
+ debug$1("resolving input alias %s -> %s", name, id);
762
+ let resolved = await this.resolve(id);
763
+ if (!id.startsWith("./")) resolved ||= await this.resolve(`./${id}`);
764
+ const resolvedId = resolved?.id || id;
765
+ debug$1("resolved input alias %s -> %s", id, resolvedId);
766
+ inputAliasMap.set(resolvedId, name);
775
767
  }
776
768
  },
777
769
  outputOptions(options) {
@@ -784,6 +776,9 @@ function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarat
784
776
  }
785
777
  };
786
778
  },
779
+ resolveId(id) {
780
+ if (dtsMap.has(id)) return { id };
781
+ },
787
782
  transform: {
788
783
  order: "pre",
789
784
  filter: { id: {
@@ -810,68 +805,6 @@ function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarat
810
805
  if (emitDtsOnly) return "export { }";
811
806
  }
812
807
  },
813
- resolveId: {
814
- order: "pre",
815
- async handler(id, importer, options) {
816
- if (dtsMap.has(id)) return {
817
- id,
818
- meta
819
- };
820
- if (!importer || !this.getModuleInfo(importer)?.meta.dtsFile) return;
821
- if (RE_DTS.test(id)) {
822
- const resolution$1 = await this.resolve(id, importer, options);
823
- if (!resolution$1) return;
824
- return {
825
- ...resolution$1,
826
- meta
827
- };
828
- }
829
- if (RE_NODE_MODULES.test(importer)) {
830
- const resolution$1 = resolver(id, importer);
831
- if (resolution$1) return {
832
- id: resolution$1,
833
- meta
834
- };
835
- }
836
- const tsImporter = filename_dts_to(importer, "ts");
837
- let resolution = await this.resolve(id, tsImporter);
838
- if (!resolution && !id.endsWith(".d")) resolution = await this.resolve(`${id}.d`, tsImporter);
839
- if (RE_NODE_MODULES.test(resolution?.id || id) || !isRelative(resolution?.id || id)) {
840
- let shouldResolve;
841
- if (typeof resolve === "boolean") shouldResolve = resolve;
842
- else shouldResolve = resolve.some((pattern) => typeof pattern === "string" ? id === pattern : pattern.test(id));
843
- if (shouldResolve) {
844
- const resolution$1 = resolver(id, importer);
845
- if (resolution$1) return {
846
- id: resolution$1,
847
- meta
848
- };
849
- } else return {
850
- id,
851
- external: true,
852
- meta
853
- };
854
- }
855
- if (!resolution || resolution.external) return resolution;
856
- let dtsId;
857
- if (RE_JS.test(resolution.id)) {
858
- resolution = await this.resolve(filename_js_to_dts(resolution.id), importer, { skipSelf: false });
859
- if (!resolution) return;
860
- dtsId = resolution.id;
861
- } else {
862
- dtsId = RE_DTS.test(resolution.id) ? resolution.id : filename_ts_to_dts(resolution.id);
863
- if (dtsMap.has(dtsId)) return {
864
- id: dtsId,
865
- meta
866
- };
867
- }
868
- await this.load(resolution);
869
- if (RE_DTS.test(resolution.id) || dtsMap.has(dtsId)) return {
870
- id: dtsId,
871
- meta
872
- };
873
- }
874
- },
875
808
  load: {
876
809
  filter: { id: {
877
810
  include: [RE_DTS],
@@ -917,14 +850,70 @@ function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarat
917
850
  };
918
851
  }
919
852
 
853
+ //#endregion
854
+ //#region src/resolve.ts
855
+ const meta = { dtsFile: true };
856
+ function createDtsResolvePlugin({ tsconfig, resolve }) {
857
+ const resolver = createResolver({ tsconfig: tsconfig ? tsconfig : void 0 });
858
+ return {
859
+ name: "rolldown-plugin-dts:resolve",
860
+ resolveId: {
861
+ order: "pre",
862
+ async handler(id, importer, options) {
863
+ if (!importer || !RE_DTS.test(importer) && !this.getModuleInfo(importer)?.meta.dtsFile) return;
864
+ if (RE_NODE_MODULES.test(importer)) {
865
+ const resolution$1 = resolver(id, importer);
866
+ if (resolution$1) return {
867
+ id: resolution$1,
868
+ meta
869
+ };
870
+ }
871
+ let resolution = await this.resolve(id, importer, options);
872
+ if (!resolution && !id.endsWith(".d")) resolution = await this.resolve(`${id}.d`, importer, options);
873
+ if (RE_NODE_MODULES.test(resolution?.id || id) || !isRelative(resolution?.id || id)) {
874
+ let shouldResolve;
875
+ if (typeof resolve === "boolean") shouldResolve = resolve;
876
+ else shouldResolve = resolve.some((pattern) => typeof pattern === "string" ? id === pattern : pattern.test(id));
877
+ if (shouldResolve) {
878
+ const resolution$1 = resolver(id, importer);
879
+ if (resolution$1) return {
880
+ id: resolution$1,
881
+ meta
882
+ };
883
+ } else return {
884
+ id,
885
+ external: true,
886
+ meta
887
+ };
888
+ }
889
+ if (!resolution || resolution.external) return resolution;
890
+ if (RE_JS.test(resolution.id)) {
891
+ resolution = await this.resolve(filename_js_to_dts(resolution.id), importer, options);
892
+ if (!resolution) return;
893
+ } else if (RE_TS.test(resolution.id) && !RE_DTS.test(resolution.id)) {
894
+ if (!this.getModuleInfo(resolution.id)) await this.load(resolution);
895
+ resolution.id = filename_ts_to_dts(resolution.id);
896
+ }
897
+ if (RE_DTS.test(resolution.id)) return {
898
+ ...resolution,
899
+ meta
900
+ };
901
+ }
902
+ }
903
+ };
904
+ }
905
+
920
906
  //#endregion
921
907
  //#region src/index.ts
908
+ const debug = Debug("rolldown-plugin-dts:options");
922
909
  function dts(options = {}) {
910
+ debug("resolving dts options");
923
911
  const resolved = resolveOptions(options);
912
+ debug("resolved dts options %o", resolved);
924
913
  const plugins = [];
925
914
  if (options.dtsInput) plugins.push(createDtsInputPlugin());
926
915
  else plugins.push(createGeneratePlugin(resolved));
927
- plugins.push(createFakeJsPlugin(resolved));
916
+ plugins.push(createDtsResolvePlugin(resolved), createFakeJsPlugin(resolved));
928
917
  return plugins;
929
918
  }
930
919
  function resolveOptions({ cwd = process.cwd(), tsconfig, compilerOptions = {}, isolatedDeclarations, sourcemap, dtsInput = false, emitDtsOnly = false, resolve = false }) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rolldown-plugin-dts",
3
- "version": "0.9.1",
3
+ "version": "0.9.3",
4
4
  "description": "A Rolldown plugin to bundle dts files",
5
5
  "type": "module",
6
6
  "license": "MIT",