Skip to content

Commit

Permalink
feat: optimize tsconfig.json loading (#660)
Browse files Browse the repository at this point in the history
* feat: add option to override tsconfig

* fix: resolve tsconfig

* tweaks

Co-authored-by: EGOIST <0x142857@gmail.com>
  • Loading branch information
sxzz and egoist committed Jul 25, 2022
1 parent 2087907 commit 407cec6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/index.ts
@@ -1,7 +1,6 @@
import path from 'path'
import fs from 'fs'
import { Worker } from 'worker_threads'
import type { Buildable, MarkRequired } from 'ts-essentials'
import { removeFiles, debouncePromise, slash, MaybePromise } from './utils'
import { loadTsupConfig } from './load'
import glob from 'globby'
Expand Down Expand Up @@ -53,7 +52,7 @@ const normalizeOptions = async (
...optionsFromConfigFile,
...optionsOverride,
}
const options: Buildable<NormalizedOptions> = {
const options: Partial<NormalizedOptions> = {
target: 'node14',
outDir: 'dist',
..._options,
Expand Down Expand Up @@ -108,6 +107,12 @@ const normalizeOptions = async (
options.tsconfigResolvePaths = tsconfig.data?.compilerOptions?.paths || {}
options.tsconfigDecoratorMetadata =
tsconfig.data?.compilerOptions?.emitDecoratorMetadata
if (options.dts) {
options.dts.compilerOptions = {
...(tsconfig.data.compilerOptions || {}),
...(options.dts.compilerOptions || {}),
}
}
} else if (options.tsconfig) {
throw new PrettyError(`Cannot find tsconfig: ${options.tsconfig}`)
}
Expand Down
5 changes: 5 additions & 0 deletions src/options.ts
Expand Up @@ -29,6 +29,11 @@ export type DtsConfig = {
banner?: string
/** Insert at the bottom */
footer?: string
/**
* Overrides `compilerOptions`
* This option takes higher priority than `compilerOptions` in tsconfig.json
*/
compilerOptions?: any
}

export type BannerOrFooter =
Expand Down
9 changes: 4 additions & 5 deletions src/rollup.ts
Expand Up @@ -15,11 +15,10 @@ import resolveFrom from 'resolve-from'

const logger = createLogger()

const loadCompilerOptions = (tsconfig?: string) => {
if (!tsconfig) return {}
const configFile = ts.readConfigFile(tsconfig, ts.sys.readFile)
const parseCompilerOptions = (compilerOptions?: any) => {
if (!compilerOptions) return {}
const { options } = ts.parseJsonConfigFileContent(
configFile.config,
{ compilerOptions },
ts.sys,
'./'
)
Expand Down Expand Up @@ -83,7 +82,7 @@ const getRollupConfig = async (
): Promise<RollupConfig> => {
setSilent(options.silent)

const compilerOptions = loadCompilerOptions(options.tsconfig)
const compilerOptions = parseCompilerOptions(options.dts?.compilerOptions)

const dtsOptions = options.dts || {}
dtsOptions.entry = dtsOptions.entry || options.entry
Expand Down

0 comments on commit 407cec6

Please sign in to comment.