rolldown-plugin-dts 0.7.8 → 0.7.10
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 +49 -42
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -494,26 +494,25 @@ const defaultCompilerOptions = {
|
|
|
494
494
|
target: 99,
|
|
495
495
|
resolveJsonModule: true
|
|
496
496
|
};
|
|
497
|
-
function createOrGetTsModule(programs, compilerOptions, id,
|
|
498
|
-
const
|
|
499
|
-
if (isEntry) return program.getRootFileNames().includes(id);
|
|
500
|
-
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);
|
|
501
501
|
});
|
|
502
|
-
if (
|
|
503
|
-
const sourceFile =
|
|
502
|
+
if (program) {
|
|
503
|
+
const sourceFile = program.getSourceFile(id);
|
|
504
504
|
if (sourceFile) return {
|
|
505
|
-
program
|
|
505
|
+
program,
|
|
506
506
|
file: sourceFile
|
|
507
507
|
};
|
|
508
508
|
}
|
|
509
509
|
debug(`create program for module: ${id}`);
|
|
510
|
-
const module = createTsProgram(compilerOptions,
|
|
510
|
+
const module = createTsProgram(compilerOptions, dtsMap, id);
|
|
511
511
|
debug(`created program for module: ${id}`);
|
|
512
512
|
programs.push(module.program);
|
|
513
513
|
return module;
|
|
514
514
|
}
|
|
515
|
-
function createTsProgram(compilerOptions,
|
|
516
|
-
const files = new Map([[id, code]]);
|
|
515
|
+
function createTsProgram(compilerOptions, dtsMap, id) {
|
|
517
516
|
const overrideCompilerOptions = ts.convertCompilerOptionsFromJson(compilerOptions, ".").options;
|
|
518
517
|
const options = {
|
|
519
518
|
...defaultCompilerOptions,
|
|
@@ -522,26 +521,26 @@ function createTsProgram(compilerOptions, id, code) {
|
|
|
522
521
|
const host = ts.createCompilerHost(options, true);
|
|
523
522
|
const { readFile: _readFile, fileExists: _fileExists } = host;
|
|
524
523
|
host.fileExists = (fileName) => {
|
|
525
|
-
|
|
524
|
+
const module = getTsModule(dtsMap, fileName);
|
|
525
|
+
if (module) return true;
|
|
526
526
|
return _fileExists(fileName);
|
|
527
527
|
};
|
|
528
528
|
host.readFile = (fileName) => {
|
|
529
|
-
|
|
529
|
+
const module = getTsModule(dtsMap, fileName);
|
|
530
|
+
if (module) return module.code;
|
|
530
531
|
return _readFile(fileName);
|
|
531
532
|
};
|
|
532
|
-
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);
|
|
533
535
|
const sourceFile = program.getSourceFile(id);
|
|
534
536
|
if (!sourceFile) throw new Error(`Source file not found: ${id}`);
|
|
535
537
|
return {
|
|
536
|
-
program
|
|
537
|
-
program,
|
|
538
|
-
files
|
|
539
|
-
},
|
|
538
|
+
program,
|
|
540
539
|
file: sourceFile
|
|
541
540
|
};
|
|
542
541
|
}
|
|
543
542
|
function tscEmit(module) {
|
|
544
|
-
const { program
|
|
543
|
+
const { program, file } = module;
|
|
545
544
|
let dtsCode;
|
|
546
545
|
const { emitSkipped, diagnostics } = program.emit(
|
|
547
546
|
file,
|
|
@@ -558,6 +557,11 @@ function tscEmit(module) {
|
|
|
558
557
|
if (emitSkipped && diagnostics.length) return { error: ts.formatDiagnostics(diagnostics, formatHost) };
|
|
559
558
|
return { code: dtsCode };
|
|
560
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
|
+
}
|
|
561
565
|
|
|
562
566
|
//#endregion
|
|
563
567
|
//#region src/generate.ts
|
|
@@ -624,30 +628,13 @@ function createGeneratePlugin({ tsconfig, compilerOptions, isolatedDeclaration:
|
|
|
624
628
|
exclude: [RE_DTS, RE_NODE_MODULES]
|
|
625
629
|
} },
|
|
626
630
|
handler(code, id) {
|
|
627
|
-
let dtsCode;
|
|
628
631
|
const mod = this.getModuleInfo(id);
|
|
629
|
-
const isEntry = mod?.isEntry;
|
|
630
|
-
if (isolatedDeclaration$1) {
|
|
631
|
-
const result = isolatedDeclaration(id, code, isolatedDeclaration$1 === true ? {} : isolatedDeclaration$1);
|
|
632
|
-
if (result.errors.length) {
|
|
633
|
-
const [error] = result.errors;
|
|
634
|
-
return this.error({
|
|
635
|
-
message: error.message,
|
|
636
|
-
frame: error.codeframe
|
|
637
|
-
});
|
|
638
|
-
}
|
|
639
|
-
dtsCode = result.code;
|
|
640
|
-
} else {
|
|
641
|
-
const module = createOrGetTsModule(programs, compilerOptions, id, code, isEntry);
|
|
642
|
-
const result = tscEmit(module);
|
|
643
|
-
if (result.error) return this.error(result.error);
|
|
644
|
-
dtsCode = result.code;
|
|
645
|
-
}
|
|
646
|
-
if (!dtsCode) return this.error(new Error(`Failed to generate dts for ${id}`));
|
|
632
|
+
const isEntry = !!mod?.isEntry;
|
|
647
633
|
const dtsId = filename_ts_to_dts(id);
|
|
648
634
|
dtsMap.set(dtsId, {
|
|
649
|
-
code
|
|
650
|
-
|
|
635
|
+
code,
|
|
636
|
+
id,
|
|
637
|
+
isEntry
|
|
651
638
|
});
|
|
652
639
|
if (isEntry) {
|
|
653
640
|
const name = inputAliasMap.get(id);
|
|
@@ -715,9 +702,29 @@ function createGeneratePlugin({ tsconfig, compilerOptions, isolatedDeclaration:
|
|
|
715
702
|
include: [RE_DTS],
|
|
716
703
|
exclude: [RE_NODE_MODULES]
|
|
717
704
|
} },
|
|
718
|
-
handler(
|
|
719
|
-
if (dtsMap.has(
|
|
720
|
-
|
|
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,
|
|
721
728
|
moduleSideEffects: false
|
|
722
729
|
};
|
|
723
730
|
}
|