Skip to content

Commit 89d1bcb

Browse files
committed
build!: esm-only dist
1 parent 1c3670e commit 89d1bcb

File tree

3 files changed

+853
-1002
lines changed

3 files changed

+853
-1002
lines changed

README.md

Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,108 +10,113 @@ Awaitable hooks system.
1010

1111
## Install
1212

13-
Using yarn:
14-
15-
```bash
16-
yarn add hookable
17-
```
18-
19-
Using npm:
20-
2113
```bash
22-
npm install hookable
14+
npx nypm i hookable
2315
```
2416

2517
## Usage
2618

2719
**Method A: Create a hookable instance:**
2820

2921
```js
30-
import { createHooks } from 'hookable'
22+
import { createHooks } from "hookable";
3123

3224
// Create a hookable instance
33-
const hooks = createHooks()
25+
const hooks = createHooks();
3426

3527
// Hook on 'hello'
36-
hooks.hook('hello', () => { console.log('Hello World' )})
28+
hooks.hook("hello", () => {
29+
console.log("Hello World");
30+
});
3731

3832
// Call 'hello' hook
39-
hooks.callHook('hello')
33+
hooks.callHook("hello");
4034
```
4135

4236
**Method B: Extend your base class from Hookable:**
4337

4438
```js
45-
import { Hookable } from 'hookable'
39+
import { Hookable } from "hookable";
4640

4741
export default class FooLib extends Hookable {
4842
constructor() {
4943
// Call to parent to initialize
50-
super()
44+
super();
5145
// Initialize Hookable with custom logger
5246
// super(consola)
5347
}
5448

5549
async someFunction() {
5650
// Call and wait for `hook1` hooks (if any) sequential
57-
await this.callHook('hook1')
51+
await this.callHook("hook1");
5852
}
5953
}
6054
```
6155

6256
**Inside plugins, register for any hook:**
6357

6458
```js
65-
const lib = new FooLib()
59+
const lib = new FooLib();
6660

6761
// Register a handler for `hook2`
68-
lib.hook('hook2', async () => { /* ... */ })
62+
lib.hook("hook2", async () => {
63+
/* ... */
64+
});
6965

7066
// Register multiply handlers at once
7167
lib.addHooks({
72-
hook1: async () => { /* ... */ },
73-
hook2: [ /* can be also an array */ ]
74-
})
68+
hook1: async () => {
69+
/* ... */
70+
},
71+
hook2: [
72+
/* can be also an array */
73+
],
74+
});
7575
```
7676

7777
**Unregistering hooks:**
7878

7979
```js
80-
const lib = new FooLib()
80+
const lib = new FooLib();
8181

82-
const hook0 = async () => { /* ... */ }
83-
const hook1 = async () => { /* ... */ }
84-
const hook2 = async () => { /* ... */ }
82+
const hook0 = async () => {
83+
/* ... */
84+
};
85+
const hook1 = async () => {
86+
/* ... */
87+
};
88+
const hook2 = async () => {
89+
/* ... */
90+
};
8591

8692
// The hook() method returns an "unregister" function
87-
const unregisterHook0 = lib.hook('hook0', hook0)
88-
const unregisterHooks1and2 = lib.addHooks({ hook1, hook2 })
93+
const unregisterHook0 = lib.hook("hook0", hook0);
94+
const unregisterHooks1and2 = lib.addHooks({ hook1, hook2 });
8995

9096
/* ... */
9197

92-
unregisterHook0()
93-
unregisterHooks1and2()
98+
unregisterHook0();
99+
unregisterHooks1and2();
94100

95101
// or
96102

97-
lib.removeHooks({ hook0, hook1 })
98-
lib.removeHook('hook2', hook2)
103+
lib.removeHooks({ hook0, hook1 });
104+
lib.removeHook("hook2", hook2);
99105
```
100106

101107
**Triggering a hook handler once:**
102108

103109
```js
104-
const lib = new FooLib()
110+
const lib = new FooLib();
105111

106-
const unregister = lib.hook('hook0', async () => {
112+
const unregister = lib.hook("hook0", async () => {
107113
// Unregister as soon as the hook is executed
108-
unregister()
114+
unregister();
109115

110116
/* ... */
111-
})
117+
});
112118
```
113119

114-
115120
## Hookable class
116121

117122
### `constructor()`
@@ -138,10 +143,9 @@ Example:
138143
hookable.addHooks({
139144
test: {
140145
before: () => {},
141-
after: () => {}
142-
}
143-
})
144-
146+
after: () => {},
147+
},
148+
});
145149
```
146150

147151
This registers `test:before` and `test:after` hooks at bulk.
@@ -157,6 +161,7 @@ Used by class itself to **sequentially** call handlers of a specific hook.
157161
If you need custom control over how hooks are called, you can provide a custom function that will receive an array of handlers of a specific hook.
158162

159163
`callerFn` if a callback function that accepts two arguments, `hooks` and `args`:
164+
160165
- `hooks`: Array of user hooks to be called
161166
- `args`: Array of arguments that should be passed each time calling a hook
162167

@@ -179,19 +184,21 @@ Remove multiple hook handlers.
179184
Example:
180185

181186
```js
182-
const handler = async () => { /* ... */ }
187+
const handler = async () => {
188+
/* ... */
189+
};
183190

184-
hookable.hook('test:before', handler)
185-
hookable.addHooks({ test: { after: handler } })
191+
hookable.hook("test:before", handler);
192+
hookable.addHooks({ test: { after: handler } });
186193

187194
// ...
188195

189196
hookable.removeHooks({
190197
test: {
191198
before: handler,
192-
after: handler
193-
}
194-
})
199+
after: handler,
200+
},
201+
});
195202
```
196203

197204
### `removeAllHooks`
@@ -203,38 +210,46 @@ Remove all hook handlers.
203210
Registers a (sync) callback to be called before each hook is being called.
204211

205212
```js
206-
hookable.beforeEach((event) => { console.log(`${event.name} hook is being called with ${event.args}`)})
207-
hookable.hook('test', () => { console.log('running test hook') })
213+
hookable.beforeEach((event) => {
214+
console.log(`${event.name} hook is being called with ${event.args}`);
215+
});
216+
hookable.hook("test", () => {
217+
console.log("running test hook");
218+
});
208219

209220
// test hook is being called with []
210221
// running test hook
211-
await hookable.callHook('test')
222+
await hookable.callHook("test");
212223
```
213224

214225
### `afterEach (syncCallback)`
215226

216227
Registers a (sync) callback to be called after each hook is being called.
217228

218229
```js
219-
hookable.afterEach((event) => { console.log(`${event.name} hook called with ${event.args}`)})
220-
hookable.hook('test', () => { console.log('running test hook') })
230+
hookable.afterEach((event) => {
231+
console.log(`${event.name} hook called with ${event.args}`);
232+
});
233+
hookable.hook("test", () => {
234+
console.log("running test hook");
235+
});
221236

222237
// running test hook
223238
// test hook called with []
224-
await hookable.callHook('test')
239+
await hookable.callHook("test");
225240
```
226241

227242
### `createDebugger`
228243

229244
Automatically logs each hook that is called and how long it takes to run.
230245

231246
```js
232-
const debug = hookable.createDebugger(hooks, { tag: 'something' })
247+
const debug = hookable.createDebugger(hooks, { tag: "something" });
233248

234-
hooks.callHook('some-hook', 'some-arg')
249+
hooks.callHook("some-hook", "some-arg");
235250
// [something] some-hook: 0.21ms
236251

237-
debug.close()
252+
debug.close();
238253
```
239254

240255
## Migration
@@ -244,9 +259,9 @@ debug.close()
244259
- Type checking improved. You can use `Hookable<T>` or `createHooks<T>()` to provide types interface **([c2e1e22](https://github.com/unjs/hookable/commit/c2e1e223d16e7bf87117cd8d72ad3ba211a333d8))**
245260
- We no longer provide an IE11 compatible umd build. Instead, you should use an ESM-aware bundler such as webpack or rollup to transpile if needed.
246261
- Logger param is dropped. We use `console.warn` by default for deprecated hooks.
247-
- Package now uses named exports. You should import `{ Hookable }` instead of `Hookable` or use new `createHooks` util
262+
- Package now uses named exports. You should import `{ Hookable }` instead of `Hookable` or use new `createHooks` util
248263
- `mergeHooks` util is exported standalone. You should replace `Hookable.mergeHooks` and `this.mergeHooks` with new `{ mergeHooks }` export
249-
- In versions < 5.0.0 when using `callHook` if an error happened by one of the hook callbacks, we was handling errors globally and call global `error` hook + `console.error` instead and resolve `callHook` promise! This sometimes makes confusing behavior when we think code worked but it didn't. v5 introduced a breaking change that when a hook throws an error, `callHook` also rejects instead of a global `error` event. This means you should be careful to handle all errors when using `callHook` now.
264+
- In versions < 5.0.0 when using `callHook` if an error happened by one of the hook callbacks, we was handling errors globally and call global `error` hook + `console.error` instead and resolve `callHook` promise! This sometimes makes confusing behavior when we think code worked but it didn't. v5 introduced a breaking change that when a hook throws an error, `callHook` also rejects instead of a global `error` event. This means you should be careful to handle all errors when using `callHook` now.
250265

251266
## Credits
252267

@@ -259,6 +274,7 @@ Thanks to [Joe Paice](https://github.com/RGBboy) for donating [hookable](https:/
259274
MIT - Made with 💖
260275

261276
<!-- Badges -->
277+
262278
[npm-version-src]: https://img.shields.io/npm/v/hookable?style=flat&colorA=18181B&colorB=F0DB4F
263279
[npm-version-href]: https://npmjs.com/package/hookable
264280
[npm-downloads-src]: https://img.shields.io/npm/dm/hookable?style=flat&colorA=18181B&colorB=F0DB4F

package.json

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@
1212
"repository": "unjs/hookable",
1313
"license": "MIT",
1414
"exports": {
15-
"types": "./dist/index.d.ts",
16-
"import": "./dist/index.mjs",
17-
"require": "./dist/index.cjs"
15+
".": "./dist/index.mjs"
1816
},
19-
"main": "./dist/index.cjs",
20-
"module": "./dist/index.mjs",
21-
"types": "./dist/index.d.ts",
17+
"main": "./dist/index.mjs",
18+
"types": "./dist/index.d.mts",
2219
"files": [
2320
"dist"
2421
],
2522
"scripts": {
26-
"build": "unbuild",
23+
"build": "obuild src/index.ts",
2724
"dev": "vitest",
2825
"lint": "eslint --cache . && prettier -c src test",
2926
"lint:fix": "eslint --cache . --fix && prettier -c src test -w",
@@ -39,9 +36,9 @@
3936
"eslint": "^9.38.0",
4037
"eslint-config-unjs": "^0.5.0",
4138
"expect-type": "^1.2.2",
39+
"obuild": "^0.3.0",
4240
"prettier": "^3.6.2",
4341
"typescript": "^5.9.3",
44-
"unbuild": "^3.6.1",
4542
"vite": "^7.1.12",
4643
"vitest": "^4.0.3"
4744
},

0 commit comments

Comments
 (0)