Skip to content

Commit 7b42984

Browse files
committed
feat($core): flatten return array of functional option
1 parent c4e27a8 commit 7b42984

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

Diff for: packages/@vuepress/core/lib/plugin-api/abstract/AsyncOption.js

+20-12
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,25 @@ class AsyncOption extends Option {
1919
*/
2020

2121
async asyncApply (...args) {
22-
const items = []
23-
for (const { name, value } of this.items) {
22+
const rawItems = this.items
23+
this.items = []
24+
this.appliedItems = this.items
25+
26+
for (const { name, value } of rawItems) {
2427
try {
25-
items.push({
28+
this.add(
2629
name,
27-
value: isFunction(value)
30+
isFunction(value)
2831
? await value(...args)
2932
: value
30-
})
33+
)
3134
} catch (error) {
3235
logger.error(`${chalk.cyan(name)} apply ${chalk.cyan(this.key)} failed.`)
3336
throw error
3437
}
3538
}
36-
this.appliedItems = items
39+
40+
this.items = rawItems
3741
}
3842

3943
/**
@@ -43,23 +47,27 @@ class AsyncOption extends Option {
4347
*/
4448

4549
async parallelApply (...args) {
46-
const items = []
47-
await Promise.all(this.items.map(async ({ name, value }) => {
50+
const rawItems = this.items
51+
this.items = []
52+
this.appliedItems = this.items
53+
54+
await Promise.all(rawItems.map(async ({ name, value }) => {
4855
try {
49-
items.push({
56+
this.add(
5057
name,
51-
value: isFunction(value)
58+
isFunction(value)
5259
? await value(...args)
5360
: value
54-
})
61+
)
5562
} catch (error) {
5663
logger.error(`${chalk.cyan(name)} apply ${chalk.cyan(this.key)} failed.`)
5764
throw error
5865
}
5966
})).catch(error => {
6067
throw error
6168
})
62-
return items
69+
70+
this.items = rawItems
6371
}
6472

6573
/**

Diff for: packages/@vuepress/core/lib/plugin-api/abstract/Option.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,25 @@ class Option {
8484
*/
8585

8686
syncApply (...args) {
87-
const items = []
88-
for (const { name, value } of this.items) {
87+
const rawItems = this.items
88+
this.items = []
89+
this.appliedItems = this.items
90+
91+
for (const { name, value } of rawItems) {
8992
try {
90-
items.push({
93+
this.add(
9194
name,
92-
value: isFunction(value)
95+
isFunction(value)
9396
? value(...args)
9497
: value
95-
})
98+
)
9699
} catch (error) {
97100
logger.error(`${chalk.cyan(name)} apply ${chalk.cyan(this.key)} failed.`)
98101
throw error
99102
}
100103
}
101-
this.appliedItems = items
104+
105+
this.items = rawItems
102106
}
103107

104108
/**

Diff for: packages/@vuepress/core/lib/plugin-api/override/ClientDynamicModulesOption.js

+5-13
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,16 @@ module.exports = class ClientDynamicModulesOption extends AsyncOption {
1414
async apply (ctx) {
1515
await super.asyncApply()
1616

17-
for (const item of this.appliedItems) {
18-
let { value: modules } = item
19-
const { name: pluginName } = item
20-
21-
if (!Array.isArray(modules)) {
22-
modules = [modules]
23-
}
24-
25-
await Promise.all(modules.map(async ({ name, content, dirname = 'dynamic' }) => {
26-
await ctx.writeTemp(
27-
`${dirname}/${name}`,
28-
`
17+
for (const { value, name: pluginName } of this.appliedItems) {
18+
const { name, content, dirname = 'dynamic' } = value
19+
await ctx.writeTemp(
20+
`${dirname}/${name}`,
21+
`
2922
/**
3023
* Generated by "${pluginName}"
3124
*/
3225
${content}\n\n
3326
`.trim())
34-
}))
3527
}
3628
}
3729
}

0 commit comments

Comments
 (0)