Skip to content

Commit

Permalink
Add the normalizeExperimentalDtsOptions utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Oct 18, 2024
1 parent e590f63 commit 95b52c5
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 55 deletions.
105 changes: 104 additions & 1 deletion src/api-extractor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path'
import { glob } from 'tinyglobby'
import { handleError } from './errors'
import { loadPkg } from './load'
import { createLogger } from './log'
Expand All @@ -8,8 +9,13 @@ import {
getApiExtractor,
removeFiles,
toAbsolutePath,
toObjectEntry,
} from './utils'
import type { Format, NormalizedOptions } from './options'
import type {
Format,
NormalizedExperimentalDtsConfig,
NormalizedOptions,
} from './options'
import type {
ExtractorResult,
IConfigFile,
Expand Down Expand Up @@ -183,3 +189,100 @@ export async function runDtsRollup(
logger.error('dts', 'Build error')
}
}

/**
* Normalizes the
* {@linkcode NormalizedExperimentalDtsConfig | experimental DTS options}
* by resolving entry paths and merging the provided
* TypeScript configuration options.
*
* @param options - The options containing entry points and experimental DTS configuration.
* @param tsconfig - The loaded TypeScript configuration data.
* @returns The normalized experimental DTS configuration.
*
* @internal
*/
export const normalizeExperimentalDtsOptions = async (
options: Partial<NormalizedOptions>,
tsconfig: any,
) => {
if (options.entry == null) {
return
}

const experimentalDtsEntry = options.experimentalDts?.entry || options.entry

/**
* Resolves the entry paths for the experimental DTS configuration.
* If the entry is a string or array of strings,
* it uses {@linkcode glob | tinyglobby's glob function} to resolve
* the potential glob patterns. If it's an `object`, it directly uses
* the provided entry object.
*
* @example
*
* ```ts
* import { defineConfig } from 'tsup'
*
* export default defineConfig({
* entry: { index: 'src/index.ts' },
* format: ['esm', 'cjs'],
* experimentalDts: { entry: 'src/**\/*.ts' },
* // experimentalDts: { entry: 'src/**\/*.ts' }
* // becomes experimentalDts: { entry: { index: 'src/index.ts', types: 'src/types.ts } }
* })
* ```
*/
const resolvedEntryPaths =
typeof experimentalDtsEntry === 'string' ||
Array.isArray(experimentalDtsEntry)
? await glob(experimentalDtsEntry)
: experimentalDtsEntry

// Fallback to `options.entry` if we end up with an empty object.
const experimentalDtsObjectEntry =
Object.keys(toObjectEntry(resolvedEntryPaths)).length === 0
? toObjectEntry(options.entry)
: toObjectEntry(resolvedEntryPaths)

const normalizedExperimentalDtsOptions: NormalizedExperimentalDtsConfig = {
compilerOptions: {
...(tsconfig.data.compilerOptions || {}),
...(options.experimentalDts?.compilerOptions || {}),
},

entry: experimentalDtsObjectEntry,
}

return normalizedExperimentalDtsOptions
}

/**
* Normalizes the initial experimental DTS configuration
* into a consistent {@linkcode NormalizedExperimentalDtsConfig | experimentalDts config object}.
*
* This function handles different types of
* {@linkcode NormalizedExperimentalDtsConfig | experimentalDts} inputs:
* - If {@linkcode experimentalDts} is a `boolean`, it returns a default object with an empty entry (`{ entry: {} }`) if `true`, or `undefined` if `false`.
* - If {@linkcode experimentalDts} is a `string`, it returns an object with the string as the `entry` property.
* - If {@linkcode experimentalDts} is already an object ({@linkcode NormalizedExperimentalDtsConfig}), it returns the object as is.
*
* The function focuses specifically on normalizing the **initial** {@linkcode NormalizedExperimentalDtsConfig | experimentalDts configuration}.
*
* @param experimentalDts - The {@linkcode NormalizedExperimentalDtsConfig | experimentalDts} value, which can be a `boolean`, `string`, `object`, or `undefined`.
* @returns A normalized {@linkcode NormalizedExperimentalDtsConfig | experimentalDts config object}, or `undefined` if input was `false` or `undefined`.
*
* @internal
*/
export const normalizeInitialExperimentalDtsOptions = (
experimentalDts:
| boolean
| string
| NormalizedExperimentalDtsConfig
| undefined,
): NormalizedExperimentalDtsConfig | undefined => {
if (typeof experimentalDts === 'boolean')
return experimentalDts ? { entry: {} } : undefined
if (typeof experimentalDts === 'string') return { entry: experimentalDts }
return experimentalDts
}
69 changes: 15 additions & 54 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ import kill from 'tree-kill'
import { version } from '../package.json'
import { PrettyError, handleError } from './errors'
import { getAllDepsHash, loadTsupConfig } from './load'
import {
type MaybePromise,
debouncePromise,
removeFiles,
slash,
toObjectEntry,
} from './utils'
import { type MaybePromise, debouncePromise, removeFiles, slash } from './utils'
import { createLogger, setSilent } from './log'
import { runEsbuild } from './esbuild'
import { shebang } from './plugins/shebang'
Expand All @@ -26,15 +20,13 @@ import { treeShakingPlugin } from './plugins/tree-shaking'
import { copyPublicDir, isInPublicDir } from './lib/public-dir'
import { terserPlugin } from './plugins/terser'
import { runTypeScriptCompiler } from './tsc'
import { runDtsRollup } from './api-extractor'
import {
normalizeExperimentalDtsOptions,
normalizeInitialExperimentalDtsOptions,
runDtsRollup,
} from './api-extractor'
import { cjsInterop } from './plugins/cjs-interop'
import type {
Format,
KILL_SIGNAL,
NormalizedExperimentalDtsConfig,
NormalizedOptions,
Options,
} from './options'
import type { Format, KILL_SIGNAL, NormalizedOptions, Options } from './options'

export type { Format, Options, NormalizedOptions }

Expand Down Expand Up @@ -99,14 +91,9 @@ const normalizeOptions = async (
? { entry: _options.dts }
: _options.dts,

experimentalDts:
typeof _options.experimentalDts === 'boolean'
? _options.experimentalDts
? { entry: {} }
: undefined
: typeof _options.experimentalDts === 'string'
? { entry: _options.experimentalDts }
: _options.experimentalDts,
experimentalDts: normalizeInitialExperimentalDtsOptions(
_options.experimentalDts,
),
}

setSilent(options.silent)
Expand Down Expand Up @@ -154,38 +141,12 @@ const normalizeOptions = async (
}

if (options.experimentalDts) {
const experimentalDtsEntry =
options.experimentalDts.entry || options.entry

const experimentalDtsObjectEntry =
Object.keys(
toObjectEntry(
Array.isArray(experimentalDtsEntry) ||
typeof experimentalDtsEntry === 'string'
? await glob(experimentalDtsEntry)
: experimentalDtsEntry,
),
).length === 0
? toObjectEntry(options.entry)
: toObjectEntry(
Array.isArray(experimentalDtsEntry) ||
typeof experimentalDtsEntry === 'string'
? await glob(experimentalDtsEntry)
: experimentalDtsEntry,
)

const normalizedExperimentalDtsOptions: NormalizedExperimentalDtsConfig =
{
compilerOptions: {
...(tsconfig.data.compilerOptions || {}),
...(options.experimentalDts.compilerOptions || {}),
},

entry: experimentalDtsObjectEntry,
}

options.experimentalDts = normalizedExperimentalDtsOptions
options.experimentalDts = await normalizeExperimentalDtsOptions(
options,
tsconfig,
)
}

if (!options.target) {
options.target = tsconfig.data?.compilerOptions?.target?.toLowerCase()
}
Expand Down

0 comments on commit 95b52c5

Please sign in to comment.