Skip to content

Commit

Permalink
fix: dont mangle passed tsconfig on bundle & cleanup on error
Browse files Browse the repository at this point in the history
  • Loading branch information
Floffah committed Oct 12, 2024
1 parent e08feb0 commit 624b8e4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 23 deletions.
60 changes: 37 additions & 23 deletions src/lib/generateBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function generateBundle(
entryPoints: string[],
compilerOptions: ts.CompilerOptions,
tsconfigPath?: string,
originalConfig: any = {},
originalConfig?: any,
) {
const commonOutDir = getHighestCommonDirectory(entryPoints);

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

let shouldDeleteTsConfig = false;
if (!tsconfigPath) {
const tempid = randomBytes(20).toString("hex");
if (!tsconfigPath && originalConfig) {
const tempid = randomBytes(6).toString("hex");

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

console.log(originalConfig)
writeFileSync(
tsconfigPath,
JSON.stringify({
...originalConfig,
compilerOptions,
compilerOptions: {
...originalConfig.compilerOptions,
declaration: true,
emitDeclarationOnly: true,
declarationDir: postbundleOutDir,
},
include: entryPoints,
}),
);

shouldDeleteTsConfig = true;
}

const bundles = generateDtsBundle(
relativeDeclarationPaths.map((path) => ({
filePath: resolve(compilerOptions.declarationDir!, path),
})),
{
preferredConfigPath: tsconfigPath,
},
);
try {
const bundles = generateDtsBundle(
relativeDeclarationPaths.map((path) => ({
filePath: resolve(compilerOptions.declarationDir!, path),
})),
{
preferredConfigPath: tsconfigPath,
},
);

for (let i = 0; i < bundles.length; i++) {
const bundle = bundles[i];
const originalPath = relativeDeclarationPaths[i];
for (let i = 0; i < bundles.length; i++) {
const bundle = bundles[i];
const originalPath = relativeDeclarationPaths[i];

const outputPath = resolve(postbundleOutDir, originalPath);
const outputPath = resolve(postbundleOutDir, originalPath);

writeFileSync(outputPath, bundle);
}
writeFileSync(outputPath, bundle);
}

if (compilerOptions.declarationDir!.endsWith("dts-prebundle")) {
rmSync(compilerOptions.declarationDir!, { recursive: true });
}
if (compilerOptions.declarationDir!.endsWith("dts-prebundle")) {
rmSync(compilerOptions.declarationDir!, { recursive: true });
}

if (shouldDeleteTsConfig && tsconfigPath) {
rmSync(tsconfigPath);
}
} catch (e) {
if (shouldDeleteTsConfig && tsconfigPath) {
rmSync(tsconfigPath);
}

if (shouldDeleteTsConfig) {
rmSync(tsconfigPath);
throw e;
}
}
10 changes: 10 additions & 0 deletions tests/bundle/__snapshots__/bundle.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ export declare function c(): string;
export {};
"
`;

exports[`Pass tsconfig as object 1`] = `
"// Generated by dts-bundle-generator v9.5.1
export declare function a(): string;
export declare function b(): string;
export {};
"
`;
26 changes: 26 additions & 0 deletions tests/bundle/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { distDir, readOutputFile } from "../_utils";
import { expect, test } from "bun:test";
import { build } from "esbuild";
import { resolve } from "path";
import {TsConfigJson} from "type-fest";

test("Basic config", async () => {
const tsconfig = resolve(__dirname, "./tsconfig.json");
Expand All @@ -21,3 +22,28 @@ test("Basic config", async () => {
expect(readOutputFile("bundle")).toMatchSnapshot();
expect(readOutputFile("secondBundle")).toMatchSnapshot();
});

test.only("Pass tsconfig as object", async () => {
const tsconfig: TsConfigJson = {
compilerOptions: {
emitDeclarationOnly: true,
allowImportingTsExtensions: true,
"target": "es6",
"module": "commonjs",
"lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
}
}

await build({
plugins: [dtsPlugin({ tsconfig, experimentalBundling: true })],
entryPoints: [
resolve(__dirname, "./inputs/bundle.ts"),
resolve(__dirname, "./inputs/secondBundle.ts"),
],
outdir: distDir,
tsconfigRaw: tsconfig,
bundle: true,
});

expect(readOutputFile("bundle")).toMatchSnapshot();
});

0 comments on commit 624b8e4

Please sign in to comment.