Skip to content

Commit 7e277cd

Browse files
committed
feat(plugins-manager): support a wrapPlugin property for adding
1 parent bc61063 commit 7e277cd

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

.changeset/odd-pumpkins-rhyme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'plugins-manager': patch
3+
---
4+
5+
Add a "hidden" feature in addPlugin that if you attach a `wrapPlugin` property to the returning function it will call `wrapPlugin` on the plugin before adding it.

docs/docs/tools/plugins-manager.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,13 @@ addPlugin(MyClass, { otherProp: 'new name' }); // ts error
112112

113113
Many plugin systems require you to either execute a plugin function like in `rollup`.
114114

115-
<!-- prettier-ignore-start -->
116115
```js
117116
import json from '@rollup/plugin-json';
118117

119-
/** @type {import('rocket/cli').RocketCliConfig} */
120-
export default ({
118+
export default /** @type {import('rocket/cli').RocketCliConfig} */ ({
121119
plugins: [json({ preferConst: true })],
122120
});
123121
```
124-
<!-- prettier-ignore-end -->
125122

126123
or add it in a special way like in `eleventy`
127124

@@ -254,6 +251,18 @@ addPlugin(myPlugin, { myFlag: true }); // ts ok
254251
addPlugin(myPlugin, { notExisting: true }); // ts error
255252
```
256253

254+
Note: There is a "hidden" feature in addPlugin that if you attach a `wrapPlugin` property to the returning function it will call `wrapPlugin` on the plugin before adding it.
255+
256+
```js
257+
// example auto wrap rollup plugins for @web/dev-server
258+
import { fromRollup } from '@web/dev-server-rollup';
259+
260+
const userSetupFunctions = [addPlugin(json)].map(mod => {
261+
mod.wrapPlugin = fromRollup;
262+
return mod;
263+
});
264+
```
265+
257266
## Adjusting Plugin Options
258267

259268
Adjusting options means to either

packages/plugins-manager/src/addPlugin.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/ban-ts-comment */
12
/**
23
* @template {import('../types/main').Plugin} T
34
* @param {T} plugin
@@ -12,6 +13,10 @@ export function addPlugin(plugin, options = {}, { how = 'after', location = 'bot
1213
if (plugins === undefined) {
1314
plugins = [];
1415
}
16+
17+
// @ts-ignore
18+
const usePlugin = addPluginFn.wrapPlugin ? addPluginFn.wrapPlugin(plugin) : plugin;
19+
1520
// only add if name is not already in the meta plugin list
1621
if (plugins.findIndex(pluginObj => pluginObj.plugin === plugin) === -1) {
1722
let index = -1;
@@ -40,7 +45,7 @@ export function addPlugin(plugin, options = {}, { how = 'after', location = 'bot
4045
}
4146

4247
plugins.splice(index, 0, {
43-
plugin,
48+
plugin: usePlugin,
4449
options,
4550
});
4651
}

packages/plugins-manager/test-node/addPlugin.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,18 @@ describe('addPlugin', () => {
109109
});
110110
expect(config.plugins).to.deep.equal(['-- newFirst last Plugin --']);
111111
});
112+
113+
it('[advanced] can add a `wrapPlugin` property to the function itself which will call it on the plugin on init', async () => {
114+
function myWrapper(plugin) {
115+
return () => 'wrapped' + plugin();
116+
}
117+
118+
const config = applyPlugins({
119+
setupPlugins: [addPlugin(insertPlugin)].map(mod => {
120+
mod.wrapPlugin = myWrapper;
121+
return mod;
122+
}),
123+
});
124+
expect(config.plugins).to.deep.equal(['wrapped-- first last Plugin --']);
125+
});
112126
});

0 commit comments

Comments
 (0)