diff --git a/docs/README.md b/docs/README.md index 7a86923..e606c83 100644 --- a/docs/README.md +++ b/docs/README.md @@ -283,6 +283,27 @@ Passing `--metafile` flag to tell esbuild to produce some metadata about the bui The file outputs as `metafile-{format}.json`, e.g. `tsup --format cjs,esm` will generate `metafile-cjs.json` and `metafile-esm.json`. +### Custom esbuild plugin and options + +Use `esbuildPlugins` and `esbuildOptions` respectively in `tsup.config.ts`: + +```ts +import { Options } from 'tsup' + +export const tsup: Options = { + esbuildPlugins: [YourPlugin], + esbuildOptions(options, context) { + options.define.foo = '"bar"' + }, +} +``` + +The `context` argument for `esbuildOptions`: + +- `context.format`: `cjs`, `esm`, `iife` + +See all options [here](https://esbuild.github.io/api/#build-api), and [how to write an esbuild plugin](https://esbuild.github.io/plugins/#using-plugins). + --- For more details: diff --git a/src/index.ts b/src/index.ts index ee5bbff..457a251 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import fs from 'fs' -import path, { dirname, join, extname, basename } from 'path' +import path, { dirname, join, extname } from 'path' import { Worker } from 'worker_threads' import type { InputOption } from 'rollup' import { transform as transformToEs5 } from 'buble' @@ -90,6 +90,7 @@ export type Options = { */ clean?: boolean | string[] esbuildPlugins?: EsbuildPlugin[] + esbuildOptions?: (options: BuildOptions, context: { format: Format }) => void /** * Supress non-error logs (excluding "onSuccess" process output) */ @@ -191,6 +192,14 @@ export async function runEsbuild( sourcemap: options.sourcemap, target: options.target === 'es5' ? 'es2016' : options.target, plugins: [ + { + name: 'modify-options', + setup(build) { + if (options.esbuildOptions) { + options.esbuildOptions(build.initialOptions, { format }) + } + }, + }, // esbuild's `external` option doesn't support RegExp // So here we use a custom plugin to implement it externalPlugin({