-
-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: resolvePlugins by combining resolveFarmPlugins and resolveAsyncPlugins #1274
base: main
Are you sure you want to change the base?
Changes from 5 commits
7939894
eeb740e
4377354
4b82e59
426bf8c
27afcdb
6e3d7a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@farmfe/core': minor | ||
--- | ||
|
||
New Function resolvePlugin created which combine the functionality of resolveFarmPlugin and resloveAsyncPlugins. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,27 @@ | |
export * from './js/index.js'; | ||
export * from './rust/index.js'; | ||
|
||
export async function resolveFarmPlugins(config: UserConfig) { | ||
const plugins = config.plugins ?? []; | ||
export async function resolveAsyncPlugins<T>(arr: T[]): Promise<T[]> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method seems to just move the position, and we should put the important method at the top of the code |
||
return arr.reduce<Promise<T[]>>(async (acc, current) => { | ||
const flattenedAcc = await acc; | ||
|
||
if (current instanceof Promise) { | ||
const resolvedElement = await current; | ||
return flattenedAcc.concat(resolvedElement); | ||
} else if (Array.isArray(current)) { | ||
const flattenedArray = await resolveAsyncPlugins(current); | ||
return flattenedAcc.concat(flattenedArray); | ||
} else { | ||
return flattenedAcc.concat(current); | ||
} | ||
}, Promise.resolve([])); | ||
} | ||
|
||
export async function resolvePlugins(config: UserConfig) { | ||
let plugins = config.plugins ?? []; | ||
|
||
// First, resolve any promises and flatten the array | ||
plugins = await resolveAsyncPlugins(plugins); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should first judge whether there is a plugins. if (!plugins.length) {
return {
rustPlugins: [],
jsPlugins: []
};
} This code should be put in front. |
||
|
||
if (!plugins.length) { | ||
return { | ||
|
@@ -20,7 +39,6 @@ | |
} | ||
|
||
const rustPlugins = []; | ||
|
||
const jsPlugins: JsPlugin[] = []; | ||
|
||
for (const plugin of plugins) { | ||
|
@@ -56,23 +74,6 @@ | |
}; | ||
} | ||
|
||
// resolve promise plugins | ||
export async function resolveAsyncPlugins<T>(arr: T[]): Promise<T[]> { | ||
return arr.reduce<Promise<T[]>>(async (acc, current) => { | ||
const flattenedAcc = await acc; | ||
|
||
if (current instanceof Promise) { | ||
const resolvedElement = await current; | ||
return flattenedAcc.concat(resolvedElement); | ||
} else if (Array.isArray(current)) { | ||
const flattenedArray = await resolveAsyncPlugins(current); | ||
return flattenedAcc.concat(flattenedArray); | ||
} else { | ||
return flattenedAcc.concat(current); | ||
} | ||
}, Promise.resolve([])); | ||
} | ||
|
||
export async function resolveConfigHook( | ||
config: UserConfig, | ||
plugins: JsPlugin[] | ||
|
@@ -144,6 +145,6 @@ | |
export function getSortedPluginHooks( | ||
plugins: JsPlugin[], | ||
hookName: keyof JsPlugin | ||
): any { | ||
return plugins.map((p: JsPlugin) => p[hookName]).filter(Boolean); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should only release the
patch
version of@ farmfe/ core
. You need to rerunnpx changeset
.