Skip to content

Commit 1747da1

Browse files
committed
feat: support multiple option for plugin API.
Since the namesake plugin can only be executed once by default, if your plugin does need to be executed multiple times, you need to set 'multiple' to true explicitly.
1 parent dd9bea3 commit 1747da1

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

__mocks__/vuepress-plugin-b.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
module.exports = {}
1+
module.exports = {
2+
multiple: true
3+
}

packages/@vuepress/core/__test__/plugin-api/Plugin.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
jest.mock('vuepress-plugin-a')
2+
jest.mock('vuepress-plugin-b')
23
jest.mock('@org/vuepress-plugin-a')
34

45
const Plugin = require('../../lib/plugin-api/index')
@@ -68,4 +69,19 @@ describe('Plugin', () => {
6869
// using the last one
6970
expect(plugin.enabledPlugins[0].$$options).toBe(pluginOptions3)
7071
})
72+
73+
test('a "multuple" plugin can be applied multuple times.', () => {
74+
const pluginOptions1 = { a: 1 }
75+
const pluginOptions2 = { b: 1 }
76+
const pluginsConfig = [
77+
['b', pluginOptions1],
78+
['b', pluginOptions2]
79+
]
80+
const plugin = new Plugin()
81+
plugin.useByPluginsConfig(pluginsConfig)
82+
expect(plugin.enabledPlugins).toHaveLength(2)
83+
// using the last one
84+
expect(plugin.enabledPlugins[0].$$options).toBe(pluginOptions1)
85+
expect(plugin.enabledPlugins[1].$$options).toBe(pluginOptions2)
86+
})
7187
})

packages/@vuepress/core/lib/plugin-api/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ module.exports = class Plugin {
3737
return this
3838
}
3939
plugin = hydratePlugin(plugin, pluginOptions, this._pluginContext, this)
40-
const duplicateIndex = this._pluginQuene.findIndex(({ name }) => name === plugin.name)
41-
if (duplicateIndex !== -1) {
42-
this._pluginQuene.splice(duplicateIndex, 1)
40+
if (plugin.multiple !== true) {
41+
const duplicateIndex = this._pluginQuene.findIndex(({ name }) => name === plugin.name)
42+
if (duplicateIndex !== -1) {
43+
this._pluginQuene.splice(duplicateIndex, 1)
44+
}
4345
}
4446
this._pluginQuene.push(plugin)
45-
4647
return this
4748
}
4849

0 commit comments

Comments
 (0)