mirror of
https://github.com/egoist/tsup.git
synced 2025-12-08 20:35:58 +00:00
feat: allow to extend esbuild options via tsup options
This commit is contained in:
parent
12e1c8d50b
commit
1f85660e46
@ -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:
|
||||
|
||||
11
src/index.ts
11
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({
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user