From 4ecd409ebae317f0e7f90a21274a2d111da18d75 Mon Sep 17 00:00:00 2001 From: Tmk Date: Sun, 7 Nov 2021 13:00:07 +0800 Subject: [PATCH 1/4] feat(config): pass override options to defineConfig --- src/cli-main.ts | 2 +- src/esbuild/postcss.ts | 2 +- src/index.ts | 10 ++++++---- src/options.ts | 2 +- src/rollup.ts | 4 ++-- src/rollup/ts-resolve.ts | 2 +- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/cli-main.ts b/src/cli-main.ts index ce4bdf89..ce9fc402 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 804b51ac..2f07cf14 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 ce8593fa..b84c0d41 100644 --- a/src/index.ts +++ b/src/index.ts @@ -54,7 +54,7 @@ const getOutputExtensionMap = ( return map } -export const defineConfig = (options: Options) => options +export const defineConfig = (options: Options | ((overrideOptions: Partial) => Options)) => options export async function runEsbuild( options: NormalizedOptions, @@ -239,7 +239,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 +282,7 @@ const killProcess = ({ }) const normalizeOptions = async ( - optionsFromConfigFile: Options, + optionsFromConfigFile: Options | undefined, optionsOverride: Options ) => { const options: Buildable = { @@ -344,7 +344,9 @@ 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 ab01ddfc..9df7e547 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 771a48b3..9a2bf521 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 5773f9c1..2e1b82a8 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() From 982c86aaaf721a2d5e469f32d556e0031d58adfa Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Fri, 12 Nov 2021 15:50:29 +0800 Subject: [PATCH 2/4] Options is already Partial --- src/index.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index b84c0d41..e62fb57b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -54,7 +54,14 @@ const getOutputExtensionMap = ( return map } -export const defineConfig = (options: Options | ((overrideOptions: Partial) => Options)) => options +export const defineConfig = ( + options: + | Options + | (( + /** The options derived from CLI flags */ + overrideOptions: Options + ) => Options) +) => options export async function runEsbuild( options: NormalizedOptions, @@ -344,7 +351,8 @@ const normalizeOptions = async ( export async function build(_options: Options) { const config = await loadTsupConfig(process.cwd()) - const configData = typeof config.data === 'function' ? config.data(_options) : config.data; + const configData = + typeof config.data === 'function' ? config.data(_options) : config.data const options = await normalizeOptions(configData, _options) From 3917392f157ef4118d59953df223c579623f811d Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Fri, 12 Nov 2021 15:59:35 +0800 Subject: [PATCH 3/4] update docs --- docs/README.md | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/docs/README.md b/docs/README.md index d4a2255f..36e7e02b 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 conditional 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`: From 4298926dd8789cc621c3c98866cab4c67341505b Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Fri, 12 Nov 2021 16:01:30 +0800 Subject: [PATCH 4/4] tweaks --- docs/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 36e7e02b..bcc4b585 100644 --- a/docs/README.md +++ b/docs/README.md @@ -81,9 +81,9 @@ export default defineConfig({ }) ``` -### Conditional config +#### Conditional config -If the config needs to conditional determined based on CLI flags, it can export a function instead: +If the config needs to be conditionally determined based on CLI flags, it can export a function instead: ```ts import { defineConfig } from 'tsup'