Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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 @@
resolveAsyncPlugins,
resolveConfigHook,
resolveConfigResolvedHook,
resolveFarmPlugins
resolvePlugins
} from '../plugin/index.js';
import {
bindingPath,
Expand Down Expand Up @@ -158,7 +158,7 @@
config: rawConfig
};

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

const rawJsPlugins = (await resolveAsyncPlugins(jsPlugins || [])).filter(
Boolean
Expand Down Expand Up @@ -310,7 +310,7 @@
// for node target, we should not define process.env.NODE_ENV
config.output?.targetEnv === 'node'
? {}
: Object.keys(userConfig.env || {}).reduce((env: any, key) => {

Check warning on line 313 in packages/core/src/config/index.ts

View workflow job for this annotation

GitHub Actions / TS Code Lint

Unexpected any. Specify a different type
env[`$__farm_regex:(global(This)?\\.)?process\\.env\\.${key}`] =
JSON.stringify(userConfig.env[key]);
return env;
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 @@
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 @@
}

const rustPlugins = [];

const jsPlugins: JsPlugin[] = [];

for (const plugin of plugins) {
Expand Down Expand Up @@ -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[]
Expand Down Expand Up @@ -144,6 +145,6 @@
export function getSortedPluginHooks(
plugins: JsPlugin[],
hookName: keyof JsPlugin
): any {

Check warning on line 148 in packages/core/src/plugin/index.ts

View workflow job for this annotation

GitHub Actions / TS Code Lint

Unexpected any. Specify a different type
return plugins.map((p: JsPlugin) => p[hookName]).filter(Boolean);
}
Loading