Skip to content

refactor: resolvePlugins by combining resolveFarmPlugins and resolveAsyncPlugins #1274

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fair-buttons-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@farmfe/core': minor
Copy link
Member

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 rerun npx changeset.

---

New Function resolvePlugin created which combine the functionality of resolveFarmPlugin and resloveAsyncPlugins.
4 changes: 2 additions & 2 deletions packages/core/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
resolveAsyncPlugins,
resolveConfigHook,
resolveConfigResolvedHook,
resolveFarmPlugins
resolvePlugins
} from '../plugin/index.js';
import {
bindingPath,
Expand Down Expand Up @@ -158,7 +158,7 @@ export async function resolveConfig(
config: rawConfig
};

const { jsPlugins, rustPlugins } = await resolveFarmPlugins(userConfig);
const { jsPlugins, rustPlugins } = await resolvePlugins(userConfig);

const rawJsPlugins = (await resolveAsyncPlugins(jsPlugins || [])).filter(
Boolean
Expand Down
41 changes: 21 additions & 20 deletions packages/core/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,27 @@ import merge from '../utils/merge.js';
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[]> {
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The 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 {
Expand All @@ -20,7 +39,6 @@ export async function resolveFarmPlugins(config: UserConfig) {
}

const rustPlugins = [];

const jsPlugins: JsPlugin[] = [];

for (const plugin of plugins) {
Expand Down Expand Up @@ -56,23 +74,6 @@ export async function resolveFarmPlugins(config: UserConfig) {
};
}

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