diff --git a/src/bundle.ts b/src/bundle.ts index bca3270e..3862ad08 100644 --- a/src/bundle.ts +++ b/src/bundle.ts @@ -8,6 +8,7 @@ import fs from 'fs' import { resolve } from 'path' import { createOutputState } from './plugins/output-state-plugin' import { + fileExists, getPackageMeta, getSourcePathFromExportPath, isTypescriptFile, @@ -17,6 +18,7 @@ import type { BuildContext } from './types' import { TypescriptOptions, resolveTsConfig, + resolveTsConfigPath, writeDefaultTsconfig, } from './typescript' import { collectEntriesFromParsedExports } from './entries' @@ -59,8 +61,10 @@ async function bundle( const inputFile = cliEntryPath const isFromCli = Boolean(cliEntryPath) - let tsConfig = resolveTsConfig(cwd, options.tsconfig) + const tsConfigPath = resolveTsConfigPath(cwd, options.tsconfig) + let tsConfig = resolveTsConfig(cwd, tsConfigPath) let hasTsConfig = Boolean(tsConfig?.tsConfigPath) + const defaultTsOptions: TypescriptOptions = { tsConfigPath: tsConfig?.tsConfigPath, tsCompilerOptions: tsConfig?.tsCompilerOptions || {}, @@ -132,10 +136,16 @@ async function bundle( const hasTypeScriptFiles = Object.values(entries).some((entry) => isTypescriptFile(entry.source), ) + // If there's no tsconfig, create one. if (hasTypeScriptFiles && !hasTsConfig) { - const tsConfigPath = resolve(cwd, 'tsconfig.json') - defaultTsOptions.tsConfigPath = tsConfigPath - await writeDefaultTsconfig(tsConfigPath) + // Check if tsconfig.json exists in the project first. + // If not, create one with default settings. + // Otherwise, use the existing one. + const defaultTsConfigPath = resolve(cwd, 'tsconfig.json') + if (!fileExists(defaultTsConfigPath)) { + defaultTsOptions.tsConfigPath = defaultTsConfigPath + await writeDefaultTsconfig(defaultTsConfigPath) + } hasTsConfig = true } diff --git a/src/lib/memoize.ts b/src/lib/memoize.ts index 40914aaf..64e113d3 100644 --- a/src/lib/memoize.ts +++ b/src/lib/memoize.ts @@ -1,6 +1,6 @@ type CacheKeyResolver = string | ((...args: any[]) => string) -const memoize = any>( +const createMemoize = any>( fn: T, cacheKey?: CacheKeyResolver, // if you need specify a cache key cacheArg?: Map>, @@ -24,5 +24,8 @@ const memoize = any>( export const memoizeByKey = any>(fn: T) => { const cache = new Map>() - return (cacheKey?: CacheKeyResolver) => memoize(fn, cacheKey, cache) + return (cacheKey?: CacheKeyResolver) => createMemoize(fn, cacheKey, cache) } + +export const memoize = any>(fn: T) => + createMemoize(fn) diff --git a/src/typescript.ts b/src/typescript.ts index 3a228477..79ecac21 100644 --- a/src/typescript.ts +++ b/src/typescript.ts @@ -4,7 +4,7 @@ import { promises as fsp } from 'fs' import { Module } from 'module' import pc from 'picocolors' import { exit, fileExists } from './utils' -import { memoizeByKey } from './lib/memoize' +import { memoize } from './lib/memoize' import { DEFAULT_TS_CONFIG } from './constants' import { logger } from './logger' @@ -38,14 +38,23 @@ function resolveTypescript(cwd: string): typeof import('typescript') { return ts } +export const resolveTsConfigPath = memoize( + ( + cwd: string, + tsconfigFileName: string | undefined = 'tsconfig.json', + ): string | undefined => { + let tsConfigPath: string | undefined + tsConfigPath = resolve(cwd, tsconfigFileName) + return fileExists(tsConfigPath) ? tsConfigPath : undefined + }, +) + function resolveTsConfigHandler( cwd: string, - tsconfig = 'tsconfig.json', + tsConfigPath: string | undefined, ): null | TypescriptOptions { let tsCompilerOptions: CompilerOptions = {} - let tsConfigPath: string | undefined - tsConfigPath = resolve(cwd, tsconfig) - if (fileExists(tsConfigPath)) { + if (tsConfigPath) { // Use the original ts handler to avoid memory leak const ts = resolveTypescript(cwd) const basePath = tsConfigPath ? dirname(tsConfigPath) : cwd @@ -64,7 +73,7 @@ function resolveTsConfigHandler( } } -export const resolveTsConfig = memoizeByKey(resolveTsConfigHandler)() +export const resolveTsConfig = memoize(resolveTsConfigHandler) export async function convertCompilerOptions(cwd: string, json: any) { // Use the original ts handler to avoid memory leak diff --git a/test/integration/tsconfig-override/index.test.ts b/test/integration/tsconfig-override/index.test.ts index 102fdc85..d10490f2 100644 --- a/test/integration/tsconfig-override/index.test.ts +++ b/test/integration/tsconfig-override/index.test.ts @@ -4,6 +4,7 @@ describe('integration - tsconfig-override - default', () => { const { distDir } = createJob({ directory: __dirname, }) + it('should use es5 output in build without override', async () => { await assertFilesContent(distDir, { ['index.js']: (content) => { @@ -16,7 +17,7 @@ describe('integration - tsconfig-override - default', () => { describe('integration - tsconfig-override - customized', () => { const { distDir } = createJob({ directory: __dirname, - args: ['--tsconfig', 'tsconfig.override.json'], + args: ['--tsconfig', 'tsconfig.build.json'], }) it('should use es8 output in build', async () => {