From 449d9e0ceddcbfe96a899189fb2fdb3d74d8db4e Mon Sep 17 00:00:00 2001 From: Tmk Date: Fri, 12 Nov 2021 16:04:12 +0800 Subject: [PATCH] feat(config): allow conditional config (#452) Co-authored-by: EGOIST <0x142857@gmail.com> --- docs/README.md | 41 ++++++++++++++++++++-------------------- src/cli-main.ts | 2 +- src/esbuild/postcss.ts | 2 +- src/index.ts | 18 ++++++++++++++---- src/options.ts | 2 +- src/rollup.ts | 4 ++-- src/rollup/ts-resolve.ts | 2 +- 7 files changed, 41 insertions(+), 30 deletions(-) diff --git a/docs/README.md b/docs/README.md index d4a2255..bcc4b58 100644 --- a/docs/README.md +++ b/docs/README.md @@ -68,34 +68,35 @@ You can use any of these files: [Check out all available options](https://github.com/egoist/tsup/blob/master/src/options.ts). -#### TypeScript +#### TypeScript / JavaScript ```ts -// tsup.config.ts -import type { Options } from 'tsup' -export const tsup: Options = { +import { defineConfig } from 'tsup' + +export default defineConfig({ splitting: false, sourcemap: true, clean: true, entryPoints: ['src/index.ts'], -} +}) ``` -#### JavaScript +#### Conditional config -```js -// tsup.config.cjs -/** - * @type {import("tsup").Options} - */ -module.exports = { - splitting: false, - sourcemap: true, - clean: true, - entryPoints: ['src/index.ts'], -} +If the config needs to be conditionally determined based on CLI flags, it can export a function instead: + +```ts +import { defineConfig } from 'tsup' + +export default defineConfig((options) => { + return { + minify: !options.watch, + } +}) ``` +The `options` here is derived from CLI flags. + #### package.json ```json @@ -290,14 +291,14 @@ The file outputs as `metafile-{format}.json`, e.g. `tsup --format cjs,esm` will Use `esbuildPlugins` and `esbuildOptions` respectively in `tsup.config.ts`: ```ts -import { Options } from 'tsup' +import { defineConfig } from 'tsup' -export const tsup: Options = { +export default defineConfig({ esbuildPlugins: [YourPlugin], esbuildOptions(options, context) { options.define.foo = '"bar"' }, -} +}) ``` The `context` argument for `esbuildOptions`: diff --git a/src/cli-main.ts b/src/cli-main.ts index ce4bdf8..ce9fc40 100644 --- a/src/cli-main.ts +++ b/src/cli-main.ts @@ -73,7 +73,7 @@ export async function main(options: Options = {}) { .option('--clean', 'Clean output directory') .option( '--silent', - 'Supress non-error logs (excluding "onSuccess" process output)' + 'Suppress non-error logs (excluding "onSuccess" process output)' ) .option('--pure ', 'Mark specific expressions as pure') .option('--metafile', 'Emit esbuild metafile (a JSON file)') diff --git a/src/esbuild/postcss.ts b/src/esbuild/postcss.ts index 804b51a..2f07cf1 100644 --- a/src/esbuild/postcss.ts +++ b/src/esbuild/postcss.ts @@ -66,7 +66,7 @@ export const postcssPlugin = ({ } } - // Tranform CSS + // Transform CSS const result = await postcss ?.default(plugins) .process(contents, { ...options, from: args.path }) diff --git a/src/index.ts b/src/index.ts index ce8593f..e62fb57 100644 --- a/src/index.ts +++ b/src/index.ts @@ -54,7 +54,14 @@ const getOutputExtensionMap = ( return map } -export const defineConfig = (options: Options) => options +export const defineConfig = ( + options: + | Options + | (( + /** The options derived from CLI flags */ + overrideOptions: Options + ) => Options) +) => options export async function runEsbuild( options: NormalizedOptions, @@ -239,7 +246,7 @@ export async function runEsbuild( ) } } - // Workaound to enable code splitting for cjs format + // Workaround to enable code splitting for cjs format // Manually transform esm to cjs // TODO: remove this once esbuild supports code splitting for cjs natively if (splitting && format === 'cjs') { @@ -282,7 +289,7 @@ const killProcess = ({ }) const normalizeOptions = async ( - optionsFromConfigFile: Options, + optionsFromConfigFile: Options | undefined, optionsOverride: Options ) => { const options: Buildable = { @@ -344,7 +351,10 @@ const normalizeOptions = async ( export async function build(_options: Options) { const config = await loadTsupConfig(process.cwd()) - const options = await normalizeOptions(config.data, _options) + const configData = + typeof config.data === 'function' ? config.data(_options) : config.data + + const options = await normalizeOptions(configData, _options) log('CLI', 'info', `tsup v${version}`) diff --git a/src/options.ts b/src/options.ts index ab01ddf..9df7e54 100644 --- a/src/options.ts +++ b/src/options.ts @@ -69,7 +69,7 @@ export type Options = { esbuildPlugins?: EsbuildPlugin[] esbuildOptions?: (options: BuildOptions, context: { format: Format }) => void /** - * Supress non-error logs (excluding "onSuccess" process output) + * Suppress non-error logs (excluding "onSuccess" process output) */ silent?: boolean /** diff --git a/src/rollup.ts b/src/rollup.ts index 771a48b..9a2bf52 100644 --- a/src/rollup.ts +++ b/src/rollup.ts @@ -49,10 +49,10 @@ const findLowestCommonAncestor = (filepaths: string[]) => { // See #316 const toObjectEntry = (entry: string[]) => { entry = entry.map((e) => e.replace(/\\/g, '/')) - const ancester = findLowestCommonAncestor(entry) + const ancestor = findLowestCommonAncestor(entry) return entry.reduce((result, item) => { const key = item - .replace(ancester, '') + .replace(ancestor, '') .replace(/^\//, '') .replace(/\.[a-z]+$/, '') return { diff --git a/src/rollup/ts-resolve.ts b/src/rollup/ts-resolve.ts index 5773f9c..2e1b82a 100644 --- a/src/rollup/ts-resolve.ts +++ b/src/rollup/ts-resolve.ts @@ -52,7 +52,7 @@ export const tsResolvePlugin: PluginImpl = ({ if (!shouldResolve) return null } - // Skip absolut path + // Skip absolute path if (path.isAbsolute(source)) return null const basedir = importer ? path.dirname(importer) : process.cwd()