Skip to content

Commit

Permalink
enhance: avoid writing tsconfig.json when customzing tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jan 12, 2025
1 parent 8edd082 commit 9ec13bf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
18 changes: 14 additions & 4 deletions src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fs from 'fs'
import { resolve } from 'path'
import { createOutputState } from './plugins/output-state-plugin'
import {
fileExists,
getPackageMeta,
getSourcePathFromExportPath,
isTypescriptFile,
Expand All @@ -17,6 +18,7 @@ import type { BuildContext } from './types'
import {
TypescriptOptions,
resolveTsConfig,
resolveTsConfigPath,
writeDefaultTsconfig,
} from './typescript'
import { collectEntriesFromParsedExports } from './entries'
Expand Down Expand Up @@ -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 || {},
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 5 additions & 2 deletions src/lib/memoize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type CacheKeyResolver = string | ((...args: any[]) => string)

const memoize = <T extends (...args: any[]) => any>(
const createMemoize = <T extends (...args: any[]) => any>(
fn: T,
cacheKey?: CacheKeyResolver, // if you need specify a cache key
cacheArg?: Map<string, ReturnType<T>>,
Expand All @@ -24,5 +24,8 @@ const memoize = <T extends (...args: any[]) => any>(

export const memoizeByKey = <T extends (...args: any[]) => any>(fn: T) => {
const cache = new Map<string, ReturnType<T>>()
return (cacheKey?: CacheKeyResolver) => memoize(fn, cacheKey, cache)
return (cacheKey?: CacheKeyResolver) => createMemoize(fn, cacheKey, cache)
}

export const memoize = <T extends (...args: any[]) => any>(fn: T) =>
createMemoize(fn)
21 changes: 15 additions & 6 deletions src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/integration/tsconfig-override/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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 () => {
Expand Down

0 comments on commit 9ec13bf

Please sign in to comment.