-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(minor): add bud.before method (#2606)
Add bud.before method, docs, tests. ## Type of change **PATCH: backwards compatible change**
- Loading branch information
1 parent
38e8844
commit 8e19fac
Showing
4 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
--- | ||
title: bud.before | ||
description: Do something before compilation has started | ||
tags: | ||
- facade | ||
- lifecycle | ||
--- | ||
|
||
import ConfigExample from '@site/src/components/example'; | ||
|
||
**bud.before** allows you to execute arbitrary code directly prior to the start of the compilation process. | ||
|
||
## Usage | ||
|
||
Using an asyncronous function: | ||
|
||
<ConfigExample title="bud.config"> | ||
|
||
```ts | ||
bud.before(async bud => { | ||
// Do something before the compilation has completed | ||
}) | ||
``` | ||
|
||
```js | ||
bud.before(async bud => { | ||
// Do something before the compilation has completed | ||
}) | ||
``` | ||
|
||
```yml | ||
before: > | ||
=> async bud => { | ||
// do someting before the compilation has started | ||
} | ||
``` | ||
```json | ||
{ | ||
"before": "=> async bud => { | ||
// do something before the compilation has started | ||
}" | ||
} | ||
``` | ||
|
||
</ConfigExample> | ||
|
||
Using a synchronous function: | ||
|
||
|
||
<ConfigExample title="bud.config"> | ||
|
||
```ts | ||
bud.before(bud => { | ||
// Do something before the compilation has completed | ||
}) | ||
``` | ||
|
||
```js | ||
bud.before(bud => { | ||
// Do something before the compilation has completed | ||
}) | ||
``` | ||
|
||
```yml | ||
before: > | ||
=> bud => { | ||
// do someting before the compilation has started | ||
} | ||
``` | ||
```json | ||
{ | ||
"before": "=> bud => { | ||
// do something before the compilation has started | ||
}" | ||
} | ||
``` | ||
|
||
</ConfigExample> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {Bud} from '@roots/bud-framework' | ||
|
||
declare module '@roots/bud-framework' { | ||
interface Bud { | ||
/** | ||
* Execute a function before compiler has finished | ||
*/ | ||
before: before | ||
} | ||
} | ||
|
||
export interface before { | ||
( | ||
callback: ((app: Bud) => Promise<unknown>) | ((app: Bud) => unknown), | ||
onError?: (error: Error) => unknown, | ||
): Bud | ||
} | ||
|
||
/** | ||
* Execute a function before compiler has finished | ||
*/ | ||
export const before: before = function ( | ||
this: Bud, | ||
fn: ((app: Bud) => any) | ((app: Bud) => Promise<any>), | ||
onError?: (error: Error) => unknown, | ||
): Bud { | ||
this.hooks.action(`compiler.before`, async bud => { | ||
try { | ||
await bud.resolvePromises() | ||
await fn(bud) | ||
} catch (error) { | ||
onError ? onError(error) : bud.catch(error) | ||
} | ||
}) | ||
|
||
return this | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
sources/@roots/bud-framework/test/methods/before/before.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {type Bud, factory} from '@repo/test-kit' | ||
import {beforeAll, describe, expect, it, vi} from 'vitest' | ||
|
||
import {before as subject} from '../../../src/methods/before/index.js' | ||
|
||
describe(`@roots/bud-framework/methods/before`, function () { | ||
let before: subject | ||
let bud: Bud | ||
|
||
beforeAll(async () => { | ||
bud = await factory() | ||
before = subject.bind(bud) | ||
}) | ||
|
||
it(`should be a function`, () => { | ||
expect(before).toBeInstanceOf(Function) | ||
}) | ||
|
||
it(`should return bud`, async () => { | ||
const value = before(async () => {}) | ||
expect(value).toBe(bud) | ||
}) | ||
|
||
it(`should call the callback`, async () => { | ||
const fn = vi.fn(async () => null) | ||
before(fn) | ||
await bud.hooks.fire(`compiler.before`, bud) | ||
expect(fn).toHaveBeenCalled() | ||
}) | ||
|
||
it(`should work with a synchronous callback`, async () => { | ||
const fn = vi.fn() | ||
before(fn) | ||
await bud.hooks.fire(`compiler.before`, bud) | ||
expect(fn).toHaveBeenCalled() | ||
}) | ||
|
||
it(`should call the error handler if supplied`, async () => { | ||
const onError = vi.fn() | ||
before(() => { | ||
throw new Error(`test`) | ||
}, onError) | ||
await bud.hooks.fire(`compiler.before`, bud) | ||
expect(onError).toHaveBeenCalled() | ||
}) | ||
}) |