Skip to content

Commit

Permalink
Refactor resolveFarmPlugins and Update Changeset to major
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepeshKalura committed Apr 30, 2024
1 parent 426bf8c commit 27afcdb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
16 changes: 16 additions & 0 deletions .changeset/sixty-actors-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@farmfe/core': major
---

Description:

This PR introduces a new function, resolvePlugins, which combines the functionality of resolveFarmPlugins and resolveAsyncPlugins. This new function first resolves any promises and flattens the array of plugins, and then processes the resulting plugins as in resolveFarmPlugins. This allows the use of async and nested plugins in the configuration.

Changes:

Added a new function resolvePlugins in src/plugin/index.ts that handles async and nested plugins.
Replaced calls to resolveFarmPlugins with resolvePlugins in the codebase.

BREAKING CHANGE:

This change should not introduce any breaking changes. The new function resolvePlugins is backwards compatible with resolveFarmPlugins.
37 changes: 18 additions & 19 deletions packages/core/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,17 @@ import merge from '../utils/merge.js';
export * from './js/index.js';
export * from './rust/index.js';

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 resolvePlugins(config: UserConfig) {
let plugins = config.plugins ?? [];

// First, resolve any promises and flatten the array
plugins = await resolveAsyncPlugins(plugins);

if (!plugins.length) {
return {
rustPlugins: [],
jsPlugins: []
};
}
// First, resolve any promises and flatten the array
plugins = await resolveAsyncPlugins(plugins);

const rustPlugins = [];
const jsPlugins: JsPlugin[] = [];
Expand Down Expand Up @@ -74,6 +57,22 @@ export async function resolvePlugins(config: UserConfig) {
};
}

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[]
Expand Down

0 comments on commit 27afcdb

Please sign in to comment.