Skip to content

Commit

Permalink
feat: allow onSuccess callback in tsup config file (#657)
Browse files Browse the repository at this point in the history
Co-authored-by: EGOIST <0x142857@gmail.com>
  • Loading branch information
9oelM and egoist committed Jul 25, 2022
1 parent 135ff39 commit 2087907
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
10 changes: 10 additions & 0 deletions docs/README.md
Expand Up @@ -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.
Expand Down
29 changes: 22 additions & 7 deletions src/index.ts
Expand Up @@ -159,6 +159,7 @@ export async function build(_options: Options) {
esbuildOptions: undefined,
plugins: undefined,
treeshake: undefined,
onSuccess: undefined,
outExtension: undefined,
},
})
Expand All @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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',
})
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions test/index.test.ts
Expand Up @@ -443,6 +443,7 @@ test('svelte: typescript support', async () => {
expect(output).toContain('// Component.svelte')
})


test('onSuccess', async () => {
const { logs } = await run(
getTestName(),
Expand All @@ -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(),
Expand Down

0 comments on commit 2087907

Please sign in to comment.