Skip to content

Commit 624b8e4

Browse files
committed
fix: dont mangle passed tsconfig on bundle & cleanup on error
1 parent e08feb0 commit 624b8e4

File tree

3 files changed

+73
-23
lines changed

3 files changed

+73
-23
lines changed

src/lib/generateBundle.ts

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function generateBundle(
2929
entryPoints: string[],
3030
compilerOptions: ts.CompilerOptions,
3131
tsconfigPath?: string,
32-
originalConfig: any = {},
32+
originalConfig?: any,
3333
) {
3434
const commonOutDir = getHighestCommonDirectory(entryPoints);
3535

@@ -39,46 +39,60 @@ export function generateBundle(
3939
const postbundleOutDir = resolve(compilerOptions.declarationDir!, "..");
4040

4141
let shouldDeleteTsConfig = false;
42-
if (!tsconfigPath) {
43-
const tempid = randomBytes(20).toString("hex");
42+
if (!tsconfigPath && originalConfig) {
43+
const tempid = randomBytes(6).toString("hex");
4444

4545
tsconfigPath = resolve(process.cwd(), `tsconfig.${tempid}.json`);
4646

47+
console.log(originalConfig)
4748
writeFileSync(
4849
tsconfigPath,
4950
JSON.stringify({
5051
...originalConfig,
51-
compilerOptions,
52+
compilerOptions: {
53+
...originalConfig.compilerOptions,
54+
declaration: true,
55+
emitDeclarationOnly: true,
56+
declarationDir: postbundleOutDir,
57+
},
5258
include: entryPoints,
5359
}),
5460
);
5561

5662
shouldDeleteTsConfig = true;
5763
}
5864

59-
const bundles = generateDtsBundle(
60-
relativeDeclarationPaths.map((path) => ({
61-
filePath: resolve(compilerOptions.declarationDir!, path),
62-
})),
63-
{
64-
preferredConfigPath: tsconfigPath,
65-
},
66-
);
65+
try {
66+
const bundles = generateDtsBundle(
67+
relativeDeclarationPaths.map((path) => ({
68+
filePath: resolve(compilerOptions.declarationDir!, path),
69+
})),
70+
{
71+
preferredConfigPath: tsconfigPath,
72+
},
73+
);
6774

68-
for (let i = 0; i < bundles.length; i++) {
69-
const bundle = bundles[i];
70-
const originalPath = relativeDeclarationPaths[i];
75+
for (let i = 0; i < bundles.length; i++) {
76+
const bundle = bundles[i];
77+
const originalPath = relativeDeclarationPaths[i];
7178

72-
const outputPath = resolve(postbundleOutDir, originalPath);
79+
const outputPath = resolve(postbundleOutDir, originalPath);
7380

74-
writeFileSync(outputPath, bundle);
75-
}
81+
writeFileSync(outputPath, bundle);
82+
}
7683

77-
if (compilerOptions.declarationDir!.endsWith("dts-prebundle")) {
78-
rmSync(compilerOptions.declarationDir!, { recursive: true });
79-
}
84+
if (compilerOptions.declarationDir!.endsWith("dts-prebundle")) {
85+
rmSync(compilerOptions.declarationDir!, { recursive: true });
86+
}
87+
88+
if (shouldDeleteTsConfig && tsconfigPath) {
89+
rmSync(tsconfigPath);
90+
}
91+
} catch (e) {
92+
if (shouldDeleteTsConfig && tsconfigPath) {
93+
rmSync(tsconfigPath);
94+
}
8095

81-
if (shouldDeleteTsConfig) {
82-
rmSync(tsconfigPath);
96+
throw e;
8397
}
8498
}

tests/bundle/__snapshots__/bundle.test.ts.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,13 @@ export declare function c(): string;
1919
export {};
2020
"
2121
`;
22+
23+
exports[`Pass tsconfig as object 1`] = `
24+
"// Generated by dts-bundle-generator v9.5.1
25+
26+
export declare function a(): string;
27+
export declare function b(): string;
28+
29+
export {};
30+
"
31+
`;

tests/bundle/bundle.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { distDir, readOutputFile } from "../_utils";
33
import { expect, test } from "bun:test";
44
import { build } from "esbuild";
55
import { resolve } from "path";
6+
import {TsConfigJson} from "type-fest";
67

78
test("Basic config", async () => {
89
const tsconfig = resolve(__dirname, "./tsconfig.json");
@@ -21,3 +22,28 @@ test("Basic config", async () => {
2122
expect(readOutputFile("bundle")).toMatchSnapshot();
2223
expect(readOutputFile("secondBundle")).toMatchSnapshot();
2324
});
25+
26+
test.only("Pass tsconfig as object", async () => {
27+
const tsconfig: TsConfigJson = {
28+
compilerOptions: {
29+
emitDeclarationOnly: true,
30+
allowImportingTsExtensions: true,
31+
"target": "es6",
32+
"module": "commonjs",
33+
"lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
34+
}
35+
}
36+
37+
await build({
38+
plugins: [dtsPlugin({ tsconfig, experimentalBundling: true })],
39+
entryPoints: [
40+
resolve(__dirname, "./inputs/bundle.ts"),
41+
resolve(__dirname, "./inputs/secondBundle.ts"),
42+
],
43+
outdir: distDir,
44+
tsconfigRaw: tsconfig,
45+
bundle: true,
46+
});
47+
48+
expect(readOutputFile("bundle")).toMatchSnapshot();
49+
});

0 commit comments

Comments
 (0)