Skip to content

Commit

Permalink
fix: tapEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy committed Jul 23, 2024
1 parent 6f9d807 commit e8418ba
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 171 deletions.
16 changes: 10 additions & 6 deletions packages/compat/webpack/src/webpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ async function modifyWebpackChain(
): Promise<RspackChain> {
logger.debug('modify webpack chain');

const [modifiedChain] = await context.hooks.modifyWebpackChain.call(
utils.environment.name,
)(chain, utils);
const [modifiedChain] =
await context.hooks.modifyWebpackChain.callInEnvironment({
environment: utils.environment.name,
args: [chain, utils],
});

if (utils.environment.config.tools?.webpackChain) {
for (const item of castArray(utils.environment.config.tools.webpackChain)) {
Expand All @@ -47,9 +49,11 @@ async function modifyWebpackConfig(
utils: ModifyWebpackConfigUtils,
): Promise<WebpackConfig> {
logger.debug('modify webpack config');
let [modifiedConfig] = await context.hooks.modifyWebpackConfig.call(
utils.environment.name,
)(webpackConfig, utils);
let [modifiedConfig] =
await context.hooks.modifyWebpackConfig.callInEnvironment({
environment: utils.environment.name,
args: [webpackConfig, utils],
});

if (utils.environment.config.tools?.webpack) {
modifiedConfig = reduceConfigsWithContext({
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/configChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ export async function modifyBundlerChain(

const bundlerChain = getBundlerChain();

const [modifiedBundlerChain] = await context.hooks.modifyBundlerChain.call(
utils.environment.name,
)(bundlerChain, utils);
const [modifiedBundlerChain] =
await context.hooks.modifyBundlerChain.callInEnvironment({
environment: utils.environment.name,
args: [bundlerChain, utils],
});

if (utils.environment.config.tools?.bundlerChain) {
for (const item of castArray(utils.environment.config.tools.bundlerChain)) {
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/createRsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ async function applyDefaultPlugins(
import('./plugins/asset').then(({ pluginAsset }) => pluginAsset()),
import('./plugins/html').then(({ pluginHtml }) =>
pluginHtml((environment: string) => async (...args) => {
const result = await context.hooks.modifyHTMLTags.call(environment)(
...args,
);
const result = await context.hooks.modifyHTMLTags.callInEnvironment({
environment,
args,
});
return result[0];
}),
),
Expand Down
115 changes: 61 additions & 54 deletions packages/core/src/initHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { isPluginMatchEnvironment } from './pluginManager';
import type {
AsyncHook,
EnvironmentAsyncHook,
EnvironmentMeta,
HookDescriptor,
ModifyBundlerChainFn,
ModifyEnvironmentConfigFn,
Expand All @@ -29,68 +28,77 @@ export function createEnvironmentAsyncHook<
Callback extends (...args: any[]) => any,
>(): EnvironmentAsyncHook<Callback> {
type Hook = {
environmentMeta?: EnvironmentMeta;
environment?: string;
handler: Callback;
};
const preGroup: Hook[] = [];
const postGroup: Hook[] = [];
const defaultGroup: Hook[] = [];

const tap =
(environmentMeta?: EnvironmentMeta) =>
(cb: Callback | HookDescriptor<Callback>) => {
if (isFunction(cb)) {
defaultGroup.push({
environmentMeta,
handler: cb,
});
} else if (cb.order === 'pre') {
preGroup.push({
environmentMeta,
handler: cb.handler,
});
} else if (cb.order === 'post') {
postGroup.push({
environmentMeta,
handler: cb.handler,
});
} else {
defaultGroup.push({
environmentMeta,
handler: cb.handler,
});
const tapEnvironment = ({
environment,
handler: cb,
}: {
environment?: string;
handler: Callback | HookDescriptor<Callback>;
}) => {
if (isFunction(cb)) {
defaultGroup.push({
environment,
handler: cb,
});
} else if (cb.order === 'pre') {
preGroup.push({
environment,
handler: cb.handler,
});
} else if (cb.order === 'post') {
postGroup.push({
environment,
handler: cb.handler,
});
} else {
defaultGroup.push({
environment,
handler: cb.handler,
});
}
};

const callInEnvironment = async ({
environment,
args: params,
}: {
environment?: string;
args: Parameters<Callback>;
}) => {
const callbacks = [...preGroup, ...defaultGroup, ...postGroup];

for (const callback of callbacks) {
// If this callback is not a global callback, the environment info should match
if (
callback.environment &&
environment &&
!isPluginMatchEnvironment(callback.environment, environment)
) {
continue;
}
};

const call =
(environment?: string) =>
async (...args: Parameters<Callback>) => {
const params = args.slice(0) as Parameters<Callback>;
const callbacks = [...preGroup, ...defaultGroup, ...postGroup];

for (const callback of callbacks) {
// If this callback is not a global callback, the environment info should match
if (
callback.environmentMeta &&
environment &&
!isPluginMatchEnvironment(callback.environmentMeta, environment)
) {
continue;
}

const result = await callback.handler(...params);

if (result !== undefined) {
params[0] = result;
}

const result = await callback.handler(...params);

if (result !== undefined) {
params[0] = result;
}
}

return params;
};
return params;
};

return {
tap,
call,
tapEnvironment,
tap: (handler: Callback | HookDescriptor<Callback>) =>
tapEnvironment({ handler }),
callInEnvironment,
};
}

Expand All @@ -113,8 +121,7 @@ export function createAsyncHook<
}
};

const call = async (...args: Parameters<Callback>) => {
const params = args.slice(0) as Parameters<Callback>;
const call = async (...params: Parameters<Callback>) => {
const callbacks = [...preGroup, ...defaultGroup, ...postGroup];

for (const callback of callbacks) {
Expand Down
Loading

0 comments on commit e8418ba

Please sign in to comment.