Skip to content

Commit

Permalink
fix: write temporary tsconfig when passing only an config object
Browse files Browse the repository at this point in the history
Fixes #17
  • Loading branch information
Floffah committed Oct 11, 2024
1 parent 2a9caad commit 0251cfa
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
38 changes: 37 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test/out.cjs
test/index.d.ts
test/index.d.ts


# Created by https://www.toptal.com/developers/gitignore/api/yarn,node
# Edit at https://www.toptal.com/developers/gitignore?templates=yarn,node
Expand Down Expand Up @@ -135,3 +136,38 @@ tmp/
temp/

# End of https://www.toptal.com/developers/gitignore/api/yarn,node

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### macOS Patch ###
# iCloud generated files
*.icloud

# End of https://www.toptal.com/developers/gitignore/api/macos
29 changes: 29 additions & 0 deletions src/lib/generateBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { generateDtsBundle } from "dts-bundle-generator";
import { rmSync, writeFileSync } from "fs";
import { dirname, sep as pathSeparator, resolve } from "path";
import ts from "typescript";
import { randomBytes } from "crypto";

function getHighestCommonDirectory(paths: string[]): string {
if (paths.length === 0) {
Expand All @@ -28,6 +29,7 @@ export function generateBundle(
entryPoints: string[],
compilerOptions: ts.CompilerOptions,
tsconfigPath?: string,
originalConfig: any = {},
) {
const commonOutDir = getHighestCommonDirectory(entryPoints);

Expand All @@ -36,6 +38,29 @@ export function generateBundle(
);
const postbundleOutDir = resolve(compilerOptions.declarationDir!, "..");

let shouldDeleteTsConfig = false;
if (!tsconfigPath) {
const tempid = randomBytes(20).toString('hex');

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

writeFileSync(
tsconfigPath,
JSON.stringify(
{
...originalConfig,
compilerOptions,
include: entryPoints,
}
),
)

shouldDeleteTsConfig = true
}

const bundles = generateDtsBundle(
relativeDeclarationPaths.map((path) => ({
filePath: resolve(compilerOptions.declarationDir!, path),
Expand All @@ -57,4 +82,8 @@ export function generateBundle(
if (compilerOptions.declarationDir!.endsWith("dts-prebundle")) {
rmSync(compilerOptions.declarationDir!, { recursive: true });
}

if (shouldDeleteTsConfig) {
rmSync(tsconfigPath)
}
}
2 changes: 1 addition & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const dtsPlugin = (opts: DTSPluginOpts = {}) =>
);
}

generateBundle(entryPoints, compilerOptions, configPath);
generateBundle(entryPoints, compilerOptions, configPath, config);
}

if (
Expand Down

0 comments on commit 0251cfa

Please sign in to comment.