rolldown-plugin-dts 0.9.0 → 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/README.md +10 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +105 -109
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,6 +31,11 @@ You can find a real demo in [here](./rolldown.config.ts).
|
|
|
31
31
|
|
|
32
32
|
```ts
|
|
33
33
|
interface Options {
|
|
34
|
+
/**
|
|
35
|
+
* The directory where the the plugin will look for the `tsconfig.json` file.
|
|
36
|
+
*/
|
|
37
|
+
cwd?: string
|
|
38
|
+
|
|
34
39
|
/**
|
|
35
40
|
* When entries are `.d.ts` files (instead of `.ts` files), this option should be set to `true`.
|
|
36
41
|
*
|
|
@@ -72,6 +77,11 @@ interface Options {
|
|
|
72
77
|
| boolean
|
|
73
78
|
| Omit<IsolatedDeclarationsOptions, 'sourcemap'>
|
|
74
79
|
|
|
80
|
+
/**
|
|
81
|
+
* When `true`, the plugin will generate declaration maps for `.d.ts` files.
|
|
82
|
+
*/
|
|
83
|
+
sourcemap?: boolean
|
|
84
|
+
|
|
75
85
|
/** Resolve external types used in dts files from `node_modules` */
|
|
76
86
|
resolve?: boolean | (string | RegExp)[]
|
|
77
87
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,21 +6,22 @@ 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
|
|
23
21
|
interface Options {
|
|
22
|
+
/**
|
|
23
|
+
* The directory where the the plugin will look for the `tsconfig.json` file.
|
|
24
|
+
*/
|
|
24
25
|
cwd?: string;
|
|
25
26
|
/**
|
|
26
27
|
* When entries are `.d.ts` files (instead of `.ts` files), this option should be set to `true`.
|
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);
|
|
@@ -717,11 +707,17 @@ function createTsProgram(compilerOptions, dtsMap, id) {
|
|
|
717
707
|
function tscEmit(module) {
|
|
718
708
|
const { program, file } = module;
|
|
719
709
|
let dtsCode;
|
|
710
|
+
let map;
|
|
720
711
|
const { emitSkipped, diagnostics } = program.emit(
|
|
721
712
|
file,
|
|
722
|
-
(
|
|
723
|
-
|
|
724
|
-
|
|
713
|
+
(fileName, code) => {
|
|
714
|
+
if (fileName.endsWith(".map")) {
|
|
715
|
+
debug$1(`emit dts sourcemap: ${fileName}`);
|
|
716
|
+
map = JSON.parse(code);
|
|
717
|
+
} else {
|
|
718
|
+
debug$1(`emit dts: ${fileName}`);
|
|
719
|
+
dtsCode = code;
|
|
720
|
+
}
|
|
725
721
|
},
|
|
726
722
|
void 0,
|
|
727
723
|
true,
|
|
@@ -730,7 +726,10 @@ function tscEmit(module) {
|
|
|
730
726
|
true
|
|
731
727
|
);
|
|
732
728
|
if (emitSkipped && diagnostics.length) return { error: ts.formatDiagnostics(diagnostics, formatHost) };
|
|
733
|
-
return {
|
|
729
|
+
return {
|
|
730
|
+
code: dtsCode,
|
|
731
|
+
map
|
|
732
|
+
};
|
|
734
733
|
}
|
|
735
734
|
function getTsModule(dtsMap, tsId) {
|
|
736
735
|
const module = Array.from(dtsMap.values()).find((dts$1) => dts$1.id === tsId);
|
|
@@ -740,8 +739,7 @@ function getTsModule(dtsMap, tsId) {
|
|
|
740
739
|
|
|
741
740
|
//#endregion
|
|
742
741
|
//#region src/generate.ts
|
|
743
|
-
|
|
744
|
-
function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarations, resolve = false, emitDtsOnly = false }) {
|
|
742
|
+
function createGeneratePlugin({ compilerOptions = {}, isolatedDeclarations, emitDtsOnly = false }) {
|
|
745
743
|
const dtsMap = new Map();
|
|
746
744
|
/**
|
|
747
745
|
* A map of input id to output file name
|
|
@@ -754,7 +752,6 @@ function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarat
|
|
|
754
752
|
*/
|
|
755
753
|
const inputAliasMap = new Map();
|
|
756
754
|
let programs = [];
|
|
757
|
-
const resolver = createResolver({ tsconfig: tsconfig ? tsconfig : void 0 });
|
|
758
755
|
if (!isolatedDeclarations) initTs();
|
|
759
756
|
return {
|
|
760
757
|
name: "rolldown-plugin-dts:generate",
|
|
@@ -775,6 +772,9 @@ function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarat
|
|
|
775
772
|
}
|
|
776
773
|
};
|
|
777
774
|
},
|
|
775
|
+
resolveId(id) {
|
|
776
|
+
if (dtsMap.has(id)) return { id };
|
|
777
|
+
},
|
|
778
778
|
transform: {
|
|
779
779
|
order: "pre",
|
|
780
780
|
filter: { id: {
|
|
@@ -801,68 +801,6 @@ function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarat
|
|
|
801
801
|
if (emitDtsOnly) return "export { }";
|
|
802
802
|
}
|
|
803
803
|
},
|
|
804
|
-
resolveId: {
|
|
805
|
-
order: "pre",
|
|
806
|
-
async handler(id, importer, options) {
|
|
807
|
-
if (dtsMap.has(id)) return {
|
|
808
|
-
id,
|
|
809
|
-
meta
|
|
810
|
-
};
|
|
811
|
-
if (!importer || !this.getModuleInfo(importer)?.meta.dtsFile) return;
|
|
812
|
-
if (RE_DTS.test(id)) {
|
|
813
|
-
const resolution$1 = await this.resolve(id, importer, options);
|
|
814
|
-
if (!resolution$1) return;
|
|
815
|
-
return {
|
|
816
|
-
...resolution$1,
|
|
817
|
-
meta
|
|
818
|
-
};
|
|
819
|
-
}
|
|
820
|
-
if (RE_NODE_MODULES.test(importer)) {
|
|
821
|
-
const resolution$1 = resolver(id, importer);
|
|
822
|
-
if (resolution$1) return {
|
|
823
|
-
id: resolution$1,
|
|
824
|
-
meta
|
|
825
|
-
};
|
|
826
|
-
}
|
|
827
|
-
const tsImporter = filename_dts_to(importer, "ts");
|
|
828
|
-
let resolution = await this.resolve(id, tsImporter);
|
|
829
|
-
if (!resolution && !id.endsWith(".d")) resolution = await this.resolve(`${id}.d`, tsImporter);
|
|
830
|
-
if (RE_NODE_MODULES.test(resolution?.id || id) || !isRelative(resolution?.id || id)) {
|
|
831
|
-
let shouldResolve;
|
|
832
|
-
if (typeof resolve === "boolean") shouldResolve = resolve;
|
|
833
|
-
else shouldResolve = resolve.some((pattern) => typeof pattern === "string" ? id === pattern : pattern.test(id));
|
|
834
|
-
if (shouldResolve) {
|
|
835
|
-
const resolution$1 = resolver(id, importer);
|
|
836
|
-
if (resolution$1) return {
|
|
837
|
-
id: resolution$1,
|
|
838
|
-
meta
|
|
839
|
-
};
|
|
840
|
-
} else return {
|
|
841
|
-
id,
|
|
842
|
-
external: true,
|
|
843
|
-
meta
|
|
844
|
-
};
|
|
845
|
-
}
|
|
846
|
-
if (!resolution || resolution.external) return resolution;
|
|
847
|
-
let dtsId;
|
|
848
|
-
if (RE_JS.test(resolution.id)) {
|
|
849
|
-
resolution = await this.resolve(filename_js_to_dts(resolution.id), importer, { skipSelf: false });
|
|
850
|
-
if (!resolution) return;
|
|
851
|
-
dtsId = resolution.id;
|
|
852
|
-
} else {
|
|
853
|
-
dtsId = RE_DTS.test(resolution.id) ? resolution.id : filename_ts_to_dts(resolution.id);
|
|
854
|
-
if (dtsMap.has(dtsId)) return {
|
|
855
|
-
id: dtsId,
|
|
856
|
-
meta
|
|
857
|
-
};
|
|
858
|
-
}
|
|
859
|
-
await this.load(resolution);
|
|
860
|
-
if (RE_DTS.test(resolution.id) || dtsMap.has(dtsId)) return {
|
|
861
|
-
id: dtsId,
|
|
862
|
-
meta
|
|
863
|
-
};
|
|
864
|
-
}
|
|
865
|
-
},
|
|
866
804
|
load: {
|
|
867
805
|
filter: { id: {
|
|
868
806
|
include: [RE_DTS],
|
|
@@ -889,6 +827,7 @@ function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarat
|
|
|
889
827
|
const result = tscEmit(module);
|
|
890
828
|
if (result.error) return this.error(result.error);
|
|
891
829
|
dtsCode = result.code;
|
|
830
|
+
map = result.map;
|
|
892
831
|
}
|
|
893
832
|
if (!dtsCode) return this.error(new Error(`Failed to generate dts for ${id}`));
|
|
894
833
|
return {
|
|
@@ -907,14 +846,71 @@ function createGeneratePlugin({ tsconfig, compilerOptions = {}, isolatedDeclarat
|
|
|
907
846
|
};
|
|
908
847
|
}
|
|
909
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
|
+
|
|
910
903
|
//#endregion
|
|
911
904
|
//#region src/index.ts
|
|
905
|
+
const debug = Debug("rolldown-plugin-dts:options");
|
|
912
906
|
function dts(options = {}) {
|
|
907
|
+
debug("resolving dts options");
|
|
913
908
|
const resolved = resolveOptions(options);
|
|
909
|
+
debug("resolved dts options %o", resolved);
|
|
914
910
|
const plugins = [];
|
|
915
911
|
if (options.dtsInput) plugins.push(createDtsInputPlugin());
|
|
916
912
|
else plugins.push(createGeneratePlugin(resolved));
|
|
917
|
-
plugins.push(createFakeJsPlugin(resolved));
|
|
913
|
+
plugins.push(createDtsResolvePlugin(resolved), createFakeJsPlugin(resolved));
|
|
918
914
|
return plugins;
|
|
919
915
|
}
|
|
920
916
|
function resolveOptions({ cwd = process.cwd(), tsconfig, compilerOptions = {}, isolatedDeclarations, sourcemap, dtsInput = false, emitDtsOnly = false, resolve = false }) {
|