mirror of
https://github.com/egoist/tsup.git
synced 2025-12-08 20:35:58 +00:00
feat: allow onSuccess callback in tsup config file (#657)
Co-authored-by: EGOIST <0x142857@gmail.com>
This commit is contained in:
parent
135ff397cd
commit
20879071a9
@ -331,6 +331,16 @@ tsup src/index.ts --watch --onSuccess "node dist/index.js"
|
||||
|
||||
> Warning: You should not use shell scripts, if you need to specify shell scripts you can add it in your "scripts" field and set for example `tsup src/index.ts --watch --onSuccess \"npm run dev\"`
|
||||
|
||||
`onSuccess` can also be a `function` that returns `Promise`. For this to work, you need to use `tsup.config.ts` instead of the cli flag:
|
||||
|
||||
```ts
|
||||
import { defineConfig } from 'tsup'
|
||||
|
||||
export default defineConfig({
|
||||
onSuccess: async () => { ... }
|
||||
})
|
||||
```
|
||||
|
||||
### Minify output
|
||||
|
||||
You can also minify the output, resulting into lower bundle sizes by using the `--minify` flag.
|
||||
|
||||
29
src/index.ts
29
src/index.ts
@ -159,6 +159,7 @@ export async function build(_options: Options) {
|
||||
esbuildOptions: undefined,
|
||||
plugins: undefined,
|
||||
treeshake: undefined,
|
||||
onSuccess: undefined,
|
||||
outExtension: undefined,
|
||||
},
|
||||
})
|
||||
@ -176,16 +177,26 @@ export async function build(_options: Options) {
|
||||
const otherTasks = async () => {
|
||||
if (!options.dts?.only) {
|
||||
let existingOnSuccess: ChildProcess | undefined
|
||||
let existingOnSuccessFnPromise: Promise<any> | undefined
|
||||
/** Files imported by the entry */
|
||||
const buildDependencies: Set<string> = new Set()
|
||||
|
||||
const killPreviousProcess = async () => {
|
||||
const killPreviousProcessOrPromise = async () => {
|
||||
if (existingOnSuccess) {
|
||||
await killProcess({
|
||||
pid: existingOnSuccess.pid,
|
||||
})
|
||||
existingOnSuccess = undefined
|
||||
} else if (existingOnSuccessFnPromise) {
|
||||
await Promise.race([
|
||||
existingOnSuccessFnPromise,
|
||||
// cancel existingOnSuccessFnPromise if it is still running,
|
||||
// using a promise that's been already resolved
|
||||
Promise.resolve(),
|
||||
])
|
||||
}
|
||||
// reset them in all occassions anyway
|
||||
existingOnSuccess = undefined
|
||||
existingOnSuccessFnPromise = undefined
|
||||
}
|
||||
|
||||
const debouncedBuildAll = debouncePromise(
|
||||
@ -197,7 +208,7 @@ export async function build(_options: Options) {
|
||||
)
|
||||
|
||||
const buildAll = async () => {
|
||||
const killPromise = killPreviousProcess()
|
||||
const killPromise = killPreviousProcessOrPromise()
|
||||
// Store previous build dependencies in case the build failed
|
||||
// So we can restore it
|
||||
const previousBuildDependencies = new Set(buildDependencies)
|
||||
@ -244,10 +255,14 @@ export async function build(_options: Options) {
|
||||
])
|
||||
await killPromise
|
||||
if (options.onSuccess) {
|
||||
existingOnSuccess = execa(options.onSuccess, {
|
||||
shell: true,
|
||||
stdio: 'inherit',
|
||||
})
|
||||
if (typeof options.onSuccess === 'function') {
|
||||
existingOnSuccessFnPromise = options.onSuccess()
|
||||
} else {
|
||||
existingOnSuccess = execa(options.onSuccess, {
|
||||
shell: true,
|
||||
stdio: 'inherit',
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -67,7 +67,7 @@ export type Options = {
|
||||
keepNames?: boolean
|
||||
watch?: boolean | string | (string | boolean)[]
|
||||
ignoreWatch?: string[] | string
|
||||
onSuccess?: string
|
||||
onSuccess?: string | ((...params: any[]) => Promise<any>),
|
||||
jsxFactory?: string
|
||||
jsxFragment?: string
|
||||
outDir?: string
|
||||
|
||||
@ -443,6 +443,7 @@ test('svelte: typescript support', async () => {
|
||||
expect(output).toContain('// Component.svelte')
|
||||
})
|
||||
|
||||
|
||||
test('onSuccess', async () => {
|
||||
const { logs } = await run(
|
||||
getTestName(),
|
||||
@ -458,6 +459,30 @@ test('onSuccess', async () => {
|
||||
expect(logs.includes('world')).toEqual(true)
|
||||
})
|
||||
|
||||
test('onSuccess: use a function from config file', async () => {
|
||||
const { logs } = await run(
|
||||
getTestName(),
|
||||
{
|
||||
'input.ts': "console.log('test');",
|
||||
'tsup.config.ts': `
|
||||
export default {
|
||||
onSuccess: async () => {
|
||||
console.log('hello')
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
console.log('world')
|
||||
resolve('')
|
||||
}, 1_000)
|
||||
})
|
||||
}
|
||||
}`
|
||||
},
|
||||
)
|
||||
|
||||
expect(logs.includes('hello')).toEqual(true)
|
||||
expect(logs.includes('world')).toEqual(true)
|
||||
})
|
||||
|
||||
test('custom tsconfig', async () => {
|
||||
await run(
|
||||
getTestName(),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user