rolldown-plugin-dts 0.9.1 → 0.9.2
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 +2 -4
- package/dist/index.js +93 -107
- package/package.json +1 -1
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
|
|
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$1 = Debug("rolldown-plugin-dts:tsc");
|
|
645
635
|
let ts;
|
|
646
636
|
let formatHost;
|
|
647
637
|
function initTs() {
|
|
648
|
-
debug("loading typescript");
|
|
638
|
+
debug$1("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$1(`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$1(`create program for module: ${id}`);
|
|
683
673
|
const module = createTsProgram(compilerOptions, dtsMap, id);
|
|
684
|
-
debug(`created program for module: ${id}`);
|
|
674
|
+
debug$1(`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$1.enabled && !RE_NODE_MODULES.test(fileName)) debug$1(`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$1.enabled && !RE_NODE_MODULES.test(fileName)) debug$1(`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$1(`emit dts sourcemap: ${fileName}`);
|
|
726
716
|
map = JSON.parse(code);
|
|
727
717
|
} else {
|
|
728
|
-
debug(`emit dts: ${fileName}`);
|
|
718
|
+
debug$1(`emit dts: ${fileName}`);
|
|
729
719
|
dtsCode = code;
|
|
730
720
|
}
|
|
731
721
|
},
|
|
@@ -749,8 +739,7 @@ function getTsModule(dtsMap, tsId) {
|
|
|
749
739
|
|
|
750
740
|
//#endregion
|
|
751
741
|
//#region src/generate.ts
|
|
752
|
-
|
|
753
|
-
function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarations, resolve = false, emitDtsOnly = false }) {
|
|
742
|
+
function createGeneratePlugin({ compilerOptions = {}, isolatedDeclarations, emitDtsOnly = false }) {
|
|
754
743
|
const dtsMap = new Map();
|
|
755
744
|
/**
|
|
756
745
|
* A map of input id to output file name
|
|
@@ -763,7 +752,6 @@ function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarat
|
|
|
763
752
|
*/
|
|
764
753
|
const inputAliasMap = new Map();
|
|
765
754
|
let programs = [];
|
|
766
|
-
const resolver = createResolver({ tsconfig: tsconfig ? tsconfig : void 0 });
|
|
767
755
|
if (!isolatedDeclarations) initTs();
|
|
768
756
|
return {
|
|
769
757
|
name: "rolldown-plugin-dts:generate",
|
|
@@ -784,6 +772,9 @@ function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarat
|
|
|
784
772
|
}
|
|
785
773
|
};
|
|
786
774
|
},
|
|
775
|
+
resolveId(id) {
|
|
776
|
+
if (dtsMap.has(id)) return { id };
|
|
777
|
+
},
|
|
787
778
|
transform: {
|
|
788
779
|
order: "pre",
|
|
789
780
|
filter: { id: {
|
|
@@ -810,68 +801,6 @@ function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarat
|
|
|
810
801
|
if (emitDtsOnly) return "export { }";
|
|
811
802
|
}
|
|
812
803
|
},
|
|
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
804
|
load: {
|
|
876
805
|
filter: { id: {
|
|
877
806
|
include: [RE_DTS],
|
|
@@ -917,14 +846,71 @@ function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarat
|
|
|
917
846
|
};
|
|
918
847
|
}
|
|
919
848
|
|
|
849
|
+
//#endregion
|
|
850
|
+
//#region src/resolve.ts
|
|
851
|
+
const meta = { dtsFile: true };
|
|
852
|
+
function createDtsResolvePlugin({ tsconfig, resolve }) {
|
|
853
|
+
const resolver = createResolver({ tsconfig: tsconfig ? tsconfig : void 0 });
|
|
854
|
+
return {
|
|
855
|
+
name: "rolldown-plugin-dts:resolve",
|
|
856
|
+
resolveId: {
|
|
857
|
+
order: "pre",
|
|
858
|
+
async handler(id, importer, options) {
|
|
859
|
+
if (!importer || !RE_DTS.test(importer) && !this.getModuleInfo(importer)?.meta.dtsFile) return;
|
|
860
|
+
if (RE_NODE_MODULES.test(importer)) {
|
|
861
|
+
const resolution$1 = resolver(id, importer);
|
|
862
|
+
if (resolution$1) return {
|
|
863
|
+
id: resolution$1,
|
|
864
|
+
meta
|
|
865
|
+
};
|
|
866
|
+
}
|
|
867
|
+
const tsImporter = filename_dts_to(importer, "ts");
|
|
868
|
+
let resolution = await this.resolve(id, tsImporter, options);
|
|
869
|
+
if (!resolution && !id.endsWith(".d")) resolution = await this.resolve(`${id}.d`, tsImporter, options);
|
|
870
|
+
if (RE_NODE_MODULES.test(resolution?.id || id) || !isRelative(resolution?.id || id)) {
|
|
871
|
+
let shouldResolve;
|
|
872
|
+
if (typeof resolve === "boolean") shouldResolve = resolve;
|
|
873
|
+
else shouldResolve = resolve.some((pattern) => typeof pattern === "string" ? id === pattern : pattern.test(id));
|
|
874
|
+
if (shouldResolve) {
|
|
875
|
+
const resolution$1 = resolver(id, importer);
|
|
876
|
+
if (resolution$1) return {
|
|
877
|
+
id: resolution$1,
|
|
878
|
+
meta
|
|
879
|
+
};
|
|
880
|
+
} else return {
|
|
881
|
+
id,
|
|
882
|
+
external: true,
|
|
883
|
+
meta
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
if (!resolution || resolution.external) return resolution;
|
|
887
|
+
if (RE_JS.test(resolution.id)) {
|
|
888
|
+
resolution = await this.resolve(filename_js_to_dts(resolution.id), importer, options);
|
|
889
|
+
if (!resolution) return;
|
|
890
|
+
} else if (RE_TS.test(resolution.id) && !RE_DTS.test(resolution.id)) {
|
|
891
|
+
await this.load(resolution);
|
|
892
|
+
resolution.id = filename_ts_to_dts(resolution.id);
|
|
893
|
+
}
|
|
894
|
+
if (RE_DTS.test(resolution.id)) return {
|
|
895
|
+
...resolution,
|
|
896
|
+
meta
|
|
897
|
+
};
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
|
|
920
903
|
//#endregion
|
|
921
904
|
//#region src/index.ts
|
|
905
|
+
const debug = Debug("rolldown-plugin-dts:options");
|
|
922
906
|
function dts(options = {}) {
|
|
907
|
+
debug("resolving dts options");
|
|
923
908
|
const resolved = resolveOptions(options);
|
|
909
|
+
debug("resolved dts options %o", resolved);
|
|
924
910
|
const plugins = [];
|
|
925
911
|
if (options.dtsInput) plugins.push(createDtsInputPlugin());
|
|
926
912
|
else plugins.push(createGeneratePlugin(resolved));
|
|
927
|
-
plugins.push(createFakeJsPlugin(resolved));
|
|
913
|
+
plugins.push(createDtsResolvePlugin(resolved), createFakeJsPlugin(resolved));
|
|
928
914
|
return plugins;
|
|
929
915
|
}
|
|
930
916
|
function resolveOptions({ cwd = process.cwd(), tsconfig, compilerOptions = {}, isolatedDeclarations, sourcemap, dtsInput = false, emitDtsOnly = false, resolve = false }) {
|