rolldown-plugin-dts 0.7.7 → 0.7.9
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.js +58 -47
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { MagicStringAST } from "magic-string-ast";
|
|
2
2
|
import { parseSync } from "oxc-parser";
|
|
3
|
+
import path from "node:path";
|
|
3
4
|
import { createResolver } from "dts-resolver";
|
|
4
5
|
import { getTsconfig, parseTsconfig } from "get-tsconfig";
|
|
5
6
|
import { isolatedDeclaration } from "oxc-transform";
|
|
@@ -182,6 +183,9 @@ function filename_ts_to_dts(id) {
|
|
|
182
183
|
function filename_dts_to(id, ext) {
|
|
183
184
|
return id.replace(RE_DTS, `.$1${ext}`);
|
|
184
185
|
}
|
|
186
|
+
function isRelative(id) {
|
|
187
|
+
return path.isAbsolute(id) || id[0] === ".";
|
|
188
|
+
}
|
|
185
189
|
|
|
186
190
|
//#endregion
|
|
187
191
|
//#region src/utils/magic-string.ts
|
|
@@ -490,26 +494,25 @@ const defaultCompilerOptions = {
|
|
|
490
494
|
target: 99,
|
|
491
495
|
resolveJsonModule: true
|
|
492
496
|
};
|
|
493
|
-
function createOrGetTsModule(programs, compilerOptions, id,
|
|
494
|
-
const
|
|
495
|
-
if (isEntry) return program.getRootFileNames().includes(id);
|
|
496
|
-
return program.getSourceFile(id);
|
|
497
|
+
function createOrGetTsModule(programs, compilerOptions, id, isEntry, dtsMap) {
|
|
498
|
+
const program = programs.find((program$1) => {
|
|
499
|
+
if (isEntry) return program$1.getRootFileNames().includes(id);
|
|
500
|
+
return program$1.getSourceFile(id);
|
|
497
501
|
});
|
|
498
|
-
if (
|
|
499
|
-
const sourceFile =
|
|
502
|
+
if (program) {
|
|
503
|
+
const sourceFile = program.getSourceFile(id);
|
|
500
504
|
if (sourceFile) return {
|
|
501
|
-
program
|
|
505
|
+
program,
|
|
502
506
|
file: sourceFile
|
|
503
507
|
};
|
|
504
508
|
}
|
|
505
509
|
debug(`create program for module: ${id}`);
|
|
506
|
-
const module = createTsProgram(compilerOptions,
|
|
510
|
+
const module = createTsProgram(compilerOptions, dtsMap, id);
|
|
507
511
|
debug(`created program for module: ${id}`);
|
|
508
512
|
programs.push(module.program);
|
|
509
513
|
return module;
|
|
510
514
|
}
|
|
511
|
-
function createTsProgram(compilerOptions,
|
|
512
|
-
const files = new Map([[id, code]]);
|
|
515
|
+
function createTsProgram(compilerOptions, dtsMap, id) {
|
|
513
516
|
const overrideCompilerOptions = ts.convertCompilerOptionsFromJson(compilerOptions, ".").options;
|
|
514
517
|
const options = {
|
|
515
518
|
...defaultCompilerOptions,
|
|
@@ -518,26 +521,26 @@ function createTsProgram(compilerOptions, id, code) {
|
|
|
518
521
|
const host = ts.createCompilerHost(options, true);
|
|
519
522
|
const { readFile: _readFile, fileExists: _fileExists } = host;
|
|
520
523
|
host.fileExists = (fileName) => {
|
|
521
|
-
|
|
524
|
+
const module = getTsModule(dtsMap, fileName);
|
|
525
|
+
if (module) return true;
|
|
522
526
|
return _fileExists(fileName);
|
|
523
527
|
};
|
|
524
528
|
host.readFile = (fileName) => {
|
|
525
|
-
|
|
529
|
+
const module = getTsModule(dtsMap, fileName);
|
|
530
|
+
if (module) return module.code;
|
|
526
531
|
return _readFile(fileName);
|
|
527
532
|
};
|
|
528
|
-
const
|
|
533
|
+
const entries = Array.from(dtsMap.values()).filter((v) => v.isEntry).map((v) => v.id);
|
|
534
|
+
const program = ts.createProgram(Array.from(new Set([id, ...entries])), options, host);
|
|
529
535
|
const sourceFile = program.getSourceFile(id);
|
|
530
536
|
if (!sourceFile) throw new Error(`Source file not found: ${id}`);
|
|
531
537
|
return {
|
|
532
|
-
program
|
|
533
|
-
program,
|
|
534
|
-
files
|
|
535
|
-
},
|
|
538
|
+
program,
|
|
536
539
|
file: sourceFile
|
|
537
540
|
};
|
|
538
541
|
}
|
|
539
542
|
function tscEmit(module) {
|
|
540
|
-
const { program
|
|
543
|
+
const { program, file } = module;
|
|
541
544
|
let dtsCode;
|
|
542
545
|
const { emitSkipped, diagnostics } = program.emit(
|
|
543
546
|
file,
|
|
@@ -554,6 +557,11 @@ function tscEmit(module) {
|
|
|
554
557
|
if (emitSkipped && diagnostics.length) return { error: ts.formatDiagnostics(diagnostics, formatHost) };
|
|
555
558
|
return { code: dtsCode };
|
|
556
559
|
}
|
|
560
|
+
function getTsModule(dtsMap, tsId) {
|
|
561
|
+
const module = Array.from(dtsMap.values()).find((dts$1) => dts$1.id === tsId);
|
|
562
|
+
if (!module) return;
|
|
563
|
+
return module;
|
|
564
|
+
}
|
|
557
565
|
|
|
558
566
|
//#endregion
|
|
559
567
|
//#region src/generate.ts
|
|
@@ -562,8 +570,8 @@ function createGeneratePlugin({ tsconfig, compilerOptions, isolatedDeclaration:
|
|
|
562
570
|
const dtsMap = new Map();
|
|
563
571
|
function resolveOptions(cwd) {
|
|
564
572
|
if (tsconfig === true || tsconfig == null) {
|
|
565
|
-
const { config, path } = getTsconfig(cwd) || {};
|
|
566
|
-
tsconfig = path;
|
|
573
|
+
const { config, path: path$1 } = getTsconfig(cwd) || {};
|
|
574
|
+
tsconfig = path$1;
|
|
567
575
|
compilerOptions = {
|
|
568
576
|
...config?.compilerOptions,
|
|
569
577
|
...compilerOptions
|
|
@@ -620,30 +628,13 @@ function createGeneratePlugin({ tsconfig, compilerOptions, isolatedDeclaration:
|
|
|
620
628
|
exclude: [RE_DTS, RE_NODE_MODULES]
|
|
621
629
|
} },
|
|
622
630
|
handler(code, id) {
|
|
623
|
-
let dtsCode;
|
|
624
631
|
const mod = this.getModuleInfo(id);
|
|
625
|
-
const isEntry = mod?.isEntry;
|
|
626
|
-
if (isolatedDeclaration$1) {
|
|
627
|
-
const result = isolatedDeclaration(id, code, isolatedDeclaration$1 === true ? {} : isolatedDeclaration$1);
|
|
628
|
-
if (result.errors.length) {
|
|
629
|
-
const [error] = result.errors;
|
|
630
|
-
return this.error({
|
|
631
|
-
message: error.message,
|
|
632
|
-
frame: error.codeframe
|
|
633
|
-
});
|
|
634
|
-
}
|
|
635
|
-
dtsCode = result.code;
|
|
636
|
-
} else {
|
|
637
|
-
const module = createOrGetTsModule(programs, compilerOptions, id, code, isEntry);
|
|
638
|
-
const result = tscEmit(module);
|
|
639
|
-
if (result.error) return this.error(result.error);
|
|
640
|
-
dtsCode = result.code;
|
|
641
|
-
}
|
|
642
|
-
if (!dtsCode) return this.error(new Error(`Failed to generate dts for ${id}`));
|
|
632
|
+
const isEntry = !!mod?.isEntry;
|
|
643
633
|
const dtsId = filename_ts_to_dts(id);
|
|
644
634
|
dtsMap.set(dtsId, {
|
|
645
|
-
code
|
|
646
|
-
|
|
635
|
+
code,
|
|
636
|
+
id,
|
|
637
|
+
isEntry
|
|
647
638
|
});
|
|
648
639
|
if (isEntry) {
|
|
649
640
|
const name = inputAliasMap.get(id);
|
|
@@ -671,8 +662,8 @@ function createGeneratePlugin({ tsconfig, compilerOptions, isolatedDeclaration:
|
|
|
671
662
|
};
|
|
672
663
|
}
|
|
673
664
|
let resolution = await this.resolve(id, filename_dts_to(importer, "ts"));
|
|
674
|
-
if (!resolution
|
|
675
|
-
if (RE_NODE_MODULES.test(resolution.id)) {
|
|
665
|
+
if (!resolution) return;
|
|
666
|
+
if (RE_NODE_MODULES.test(resolution.id) || !isRelative(resolution.id)) {
|
|
676
667
|
let shouldResolve;
|
|
677
668
|
if (typeof resolve === "boolean") shouldResolve = resolve;
|
|
678
669
|
else shouldResolve = resolve.some((pattern) => typeof pattern === "string" ? id === pattern : pattern.test(id));
|
|
@@ -687,7 +678,7 @@ function createGeneratePlugin({ tsconfig, compilerOptions, isolatedDeclaration:
|
|
|
687
678
|
external: true,
|
|
688
679
|
meta
|
|
689
680
|
};
|
|
690
|
-
}
|
|
681
|
+
} else if (resolution.external) return resolution;
|
|
691
682
|
let dtsId;
|
|
692
683
|
if (RE_JS.test(resolution.id)) {
|
|
693
684
|
resolution = await this.resolve(filename_js_to_dts(resolution.id), importer, { skipSelf: false });
|
|
@@ -711,9 +702,29 @@ function createGeneratePlugin({ tsconfig, compilerOptions, isolatedDeclaration:
|
|
|
711
702
|
include: [RE_DTS],
|
|
712
703
|
exclude: [RE_NODE_MODULES]
|
|
713
704
|
} },
|
|
714
|
-
handler(
|
|
715
|
-
if (dtsMap.has(
|
|
716
|
-
|
|
705
|
+
handler(dtsId) {
|
|
706
|
+
if (!dtsMap.has(dtsId)) return;
|
|
707
|
+
const { code, id, isEntry } = dtsMap.get(dtsId);
|
|
708
|
+
let dtsCode;
|
|
709
|
+
if (isolatedDeclaration$1) {
|
|
710
|
+
const result = isolatedDeclaration(id, code, isolatedDeclaration$1 === true ? {} : isolatedDeclaration$1);
|
|
711
|
+
if (result.errors.length) {
|
|
712
|
+
const [error] = result.errors;
|
|
713
|
+
return this.error({
|
|
714
|
+
message: error.message,
|
|
715
|
+
frame: error.codeframe
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
dtsCode = result.code;
|
|
719
|
+
} else {
|
|
720
|
+
const module = createOrGetTsModule(programs, compilerOptions, id, isEntry, dtsMap);
|
|
721
|
+
const result = tscEmit(module);
|
|
722
|
+
if (result.error) return this.error(result.error);
|
|
723
|
+
dtsCode = result.code;
|
|
724
|
+
}
|
|
725
|
+
if (!dtsCode) return this.error(new Error(`Failed to generate dts for ${id}`));
|
|
726
|
+
return {
|
|
727
|
+
code: dtsCode,
|
|
717
728
|
moduleSideEffects: false
|
|
718
729
|
};
|
|
719
730
|
}
|