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

Add support for async and nested plugins #1272

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
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[]> {
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 {
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