From ad2629a00d71923f11fce94ecd0943763d2ea8e4 Mon Sep 17 00:00:00 2001 From: L <6723574+louisgv@users.noreply.github.com> Date: Tue, 24 May 2022 22:14:45 -0700 Subject: [PATCH] feat(minor): allow tsup config function to be asynchronous (#627) * Allow tsup config function to be asynchronous * update types Co-authored-by: EGOIST --- src/index.ts | 8 +++++--- src/plugin.ts | 3 +-- src/utils.ts | 2 ++ 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 441a91d1..65cf10d4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import path from 'path' import fs from 'fs' import { Worker } from 'worker_threads' import type { Buildable, MarkRequired } from 'ts-essentials' -import { removeFiles, debouncePromise, slash } from './utils' +import { removeFiles, debouncePromise, slash, MaybePromise } from './utils' import { loadTsupConfig } from './load' import glob from 'globby' import { loadTsConfig } from 'bundle-require' @@ -39,7 +39,7 @@ export const defineConfig = ( | (( /** The options derived from CLI flags */ overrideOptions: Options - ) => Options | Options[]) + ) => MaybePromise) ) => options const killProcess = ({ @@ -126,7 +126,9 @@ export async function build(_options: Options) { _options.config === false ? {} : await loadTsupConfig(process.cwd()) const configData = - typeof config.data === 'function' ? config.data(_options) : config.data + typeof config.data === 'function' + ? await config.data(_options) + : config.data await Promise.all( [...(Array.isArray(configData) ? configData : [configData])].map( diff --git a/src/plugin.ts b/src/plugin.ts index 8ee0fe6a..ad8f0b95 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -4,6 +4,7 @@ import { SourceMapConsumer, SourceMapGenerator, RawSourceMap } from 'source-map' import { Format, NormalizedOptions } from '.' import { outputFile } from './fs' import { Logger } from './log' +import { MaybePromise } from './utils' export type ChunkInfo = { type: 'chunk' @@ -22,8 +23,6 @@ export type AssetInfo = { contents: Uint8Array } -type MaybePromise = T | Promise - export type RenderChunk = ( this: PluginContext, code: string, diff --git a/src/utils.ts b/src/utils.ts index 2fcc94a0..9b9ec271 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,6 +3,8 @@ import glob from 'globby' import resolveFrom from 'resolve-from' import strip from 'strip-json-comments' +export type MaybePromise = T | Promise + export type External = | string | RegExp