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.
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.
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
8 changes: 4 additions & 4 deletions packages/core/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
export * from './js/index.js';
export * from './rust/index.js';

export async function resolveFarmPlugins(config: UserConfig) {
const plugins = config.plugins ?? [];
export async function resolvePlugins(config: UserConfig) {
let plugins = config.plugins ?? [];

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

const rustPlugins = [];

const jsPlugins: JsPlugin[] = [];

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

Check warning on line 147 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);
}