Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit f3ec588

Browse files
authored
fix: make filterPlugins work in saber-node.js (#221)
* make filterPlugins work in saber-node.js * tweaks * tweaks * beforePlugins hook is only available in saber-node.js
1 parent a7e2d08 commit f3ec588

File tree

4 files changed

+79
-42
lines changed

4 files changed

+79
-42
lines changed

packages/saber/lib/index.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class Saber {
168168
}
169169

170170
for (const plugin of userPlugins) {
171-
this.applyPlugin(plugin, plugin.options, plugin.__path)
171+
this.applyPlugin(plugin, plugin.options, plugin.location)
172172
}
173173

174174
await this.hooks.afterPlugins.promise()
@@ -214,32 +214,46 @@ class Saber {
214214

215215
getUserPlugins() {
216216
// Plugins that are specified in user config, a.k.a. saber-config.js etc
217-
let plugins =
217+
const plugins =
218218
this.configDir && this.config.plugins
219219
? this.config.plugins.map(p => {
220220
if (typeof p === 'string') {
221221
p = { resolve: p }
222222
}
223223

224-
p.resolve = resolveFrom(this.configDir, p.resolve)
225-
return p
224+
const location = resolveFrom(this.configDir, p.resolve)
225+
226+
const plugin = require(location)
227+
plugin.location = location
228+
plugin.options = p.option
229+
230+
return plugin
226231
})
227232
: []
228233

229-
plugins = plugins.map(({ resolve, options }) => {
230-
const plugin = require(resolve)
231-
plugin.__path = resolve
232-
plugin.options = options
233-
if (plugin.filterPlugins) {
234-
this.hooks.filterPlugins.tap(plugin.name, plugins =>
235-
plugin.filterPlugins(plugins, options)
236-
)
234+
const applyFilterPlugins = plugins => {
235+
const handlers = new Set()
236+
237+
for (const plugin of plugins) {
238+
const { filterPlugins, options } = plugin
239+
if (filterPlugins) {
240+
delete plugin.filterPlugins
241+
handlers.add(plugins => filterPlugins(plugins, options))
242+
}
237243
}
238244

239-
return plugin
240-
})
245+
if (handlers.size > 0) {
246+
for (const handler of handlers) {
247+
plugins = handler(plugins)
248+
}
249+
250+
return applyFilterPlugins(plugins)
251+
}
252+
253+
return plugins
254+
}
241255

242-
return this.hooks.filterPlugins.call(plugins)
256+
return applyFilterPlugins(this.hooks.filterPlugins.call(plugins))
243257
}
244258

245259
resolveCache(...args) {

packages/saber/lib/plugins/extend-node-api.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,39 @@ exports.apply = api => {
2222
updateNodeApi()
2323

2424
const getHookHandler = hookName => nodeApi[hookName] || __noopHandler__
25-
26-
api.hooks.beforePlugins.tapPromise(nodeApiId, () => {
27-
const hookHandler = getHookHandler('beforePlugins')
28-
if (hookHandler.name !== '__noopHandler__') {
29-
log.verbose(() => `beforePlugins ${colors.dim(`(${nodeApiId})`)}`)
25+
const addHook = hookName => {
26+
const hook = api.hooks[hookName]
27+
if (hook) {
28+
const tapType = hook.call ? 'tap' : 'tapPromise'
29+
hook[tapType](nodeApiId, (...args) => {
30+
const hookHandler = getHookHandler(hookName)
31+
if (hookHandler.name !== '__noopHandler__') {
32+
log.verbose(() => `${hookName} ${colors.dim(`(${nodeApiId})`)}`)
33+
}
34+
35+
if (tapType === 'tap') {
36+
return hookHandler.call(api, ...args)
37+
}
38+
39+
return Promise.resolve(hookHandler.call(api, ...args))
40+
})
3041
}
42+
}
3143

32-
return Promise.resolve(hookHandler.call(api))
33-
})
44+
// Hooks that should be added before `afterPlugins` hook
45+
const preHooks = ['beforePlugins', 'filterPlugins']
46+
47+
for (const preHook of preHooks) {
48+
addHook(preHook)
49+
}
3450

3551
api.hooks.afterPlugins.tap(nodeApiId, () => {
3652
for (const hookName of Object.keys(api.hooks)) {
37-
if (hookName === 'beforePlugins') {
53+
if (preHooks.includes(hookName)) {
3854
continue
3955
}
4056

41-
const hook = api.hooks[hookName]
42-
if (hook) {
43-
const tapType = hook.call ? 'tap' : 'tapPromise'
44-
hook[tapType](nodeApiId, (...args) => {
45-
const hookHandler = getHookHandler(hookName)
46-
const result = hookHandler.call(api, ...args)
47-
48-
if (hookHandler.name !== '__noopHandler__') {
49-
log.verbose(() => `${hookName} ${colors.dim(`(${nodeApiId})`)}`)
50-
}
51-
52-
if (tapType === 'tapPromise') {
53-
return Promise.resolve(result)
54-
}
55-
56-
return result
57-
})
58-
}
57+
addHook(hookName)
5958
}
6059
})
6160

website/pages/docs/plugin-api.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,22 @@ A function to invoke.
2323

2424
### filterPlugins
2525

26-
- Type: `(plugins: Plugins[], options: any) => Plugins[]`
26+
- Type: `FilterPlugins`
2727
- Required: `false`
2828

2929
Filter the plugins, you can use it to add or remove plugins.
30+
31+
```ts
32+
type FilterPlugins = (plugins: Plugin[], options: any) => Plugins[]
33+
34+
interface Plugin {
35+
/* Plugin name */
36+
name: string
37+
apply: (api: SaberInstance, options?: any) => void
38+
filterPlugins: FilterPlugins
39+
/* Plugin options */
40+
options?: any
41+
/* The path to the plugin, only used in logs */
42+
location?: string
43+
}
44+
```

website/pages/docs/saber-instance.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,27 @@ Depending on the hook type, `tapAsync` and `tapPromise` may also be available. H
9999

100100
Called to filter plugins.
101101

102+
This hook is __only__ available `saber-node.js`.
103+
102104
```ts
103105
interface Plugin {
106+
/* Plugin name */
104107
name: string
105108
apply: (api: SaberInstance, options?: any) => void
109+
/* Plugin options */
106110
options?: any
111+
/* The path to the plugin, only used in logs */
112+
location?: string
107113
}
108114
```
109115

116+
110117
### `beforePlugins`
111118

112119
- Hook Type: `AsyncSeriesHook`
113120

121+
This hook is __only__ available `saber-node.js`.
122+
114123
Called before loading user plugins.
115124

116125
### `afterPlugins`

0 commit comments

Comments
 (0)