Skip to content

Commit

Permalink
feat: add modifyEnvironmentConfig hook (#2707)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy authored Jun 26, 2024
1 parent e1d7e38 commit f2483ac
Show file tree
Hide file tree
Showing 9 changed files with 552 additions and 19 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/initHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
AsyncHook,
HookDescriptor,
ModifyBundlerChainFn,
ModifyEnvironmentConfigFn,
ModifyHTMLTagsFn,
ModifyRsbuildConfigFn,
ModifyRspackConfigFn,
Expand Down Expand Up @@ -80,6 +81,7 @@ export function initHooks() {
modifyWebpackChain: createAsyncHook<ModifyWebpackChainFn>(),
modifyWebpackConfig: createAsyncHook<ModifyWebpackConfigFn>(),
modifyRsbuildConfig: createAsyncHook<ModifyRsbuildConfigFn>(),
modifyEnvironmentConfig: createAsyncHook<ModifyEnvironmentConfigFn>(),
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/initPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,6 @@ export function getPluginAPI({
modifyWebpackChain: hooks.modifyWebpackChain.tap,
modifyWebpackConfig: hooks.modifyWebpackConfig.tap,
modifyRsbuildConfig: hooks.modifyRsbuildConfig.tap,
modifyEnvironmentConfig: hooks.modifyEnvironmentConfig.tap,
};
}
78 changes: 64 additions & 14 deletions packages/core/src/provider/initConfigs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type {
InspectConfigOptions,
MergedEnvironmentConfig,
ModifyEnvironmentConfigUtils,
NormalizedEnvironmentConfig,
PluginManager,
RsbuildEntry,
Expand Down Expand Up @@ -33,16 +35,33 @@ async function modifyRsbuildConfig(context: InternalContext) {
logger.debug('modify Rsbuild config done');
}

async function modifyEnvironmentConfig(
context: InternalContext,
config: MergedEnvironmentConfig,
name: string,
) {
logger.debug(`modify Rsbuild environment(${name}) config`);
const [modified] = await context.hooks.modifyEnvironmentConfig.call(config, {
name,
mergeEnvironmentConfig:
mergeRsbuildConfig<MergedEnvironmentConfig> as ModifyEnvironmentConfigUtils['mergeEnvironmentConfig'],
});

logger.debug(`modify Rsbuild environment(${name}) config done`);

return modified;
}

export type InitConfigsOptions = {
context: InternalContext;
pluginManager: PluginManager;
rsbuildOptions: Required<CreateRsbuildOptions>;
};

const normalizeEnvironmentsConfigs = (
const initEnvironmentConfigs = (
normalizedConfig: NormalizedConfig,
rootPath: string,
): Record<string, NormalizedEnvironmentConfig> => {
): Record<string, MergedEnvironmentConfig> => {
let defaultEntry: RsbuildEntry;
const getDefaultEntryWithMemo = () => {
if (!defaultEntry) {
Expand All @@ -52,20 +71,25 @@ const normalizeEnvironmentsConfigs = (
};
const { environments, dev, server, provider, ...rsbuildSharedConfig } =
normalizedConfig;
const { assetPrefix, lazyCompilation } = dev;

if (environments && Object.keys(environments).length) {
return Object.fromEntries(
Object.entries(environments).map(([name, config]) => {
const environmentConfig = {
...mergeRsbuildConfig(
rsbuildSharedConfig,
config as unknown as NormalizedEnvironmentConfig,
),
dev,
server,
const environmentConfig: MergedEnvironmentConfig = {
...(mergeRsbuildConfig(
{
...rsbuildSharedConfig,
dev: {
assetPrefix,
lazyCompilation,
},
} as unknown as MergedEnvironmentConfig,
config as unknown as MergedEnvironmentConfig,
) as unknown as MergedEnvironmentConfig),
};

if (!environmentConfig.source.entry) {
// @ts-expect-error
environmentConfig.source.entry = getDefaultEntryWithMemo();
}

Expand All @@ -81,9 +105,11 @@ const normalizeEnvironmentsConfigs = (
...rsbuildSharedConfig.source,
entry: rsbuildSharedConfig.source.entry ?? getDefaultEntryWithMemo(),
},
dev,
server,
},
dev: {
assetPrefix,
lazyCompilation,
},
} as MergedEnvironmentConfig,
};
};

Expand All @@ -107,11 +133,35 @@ export async function initRsbuildConfig({
await modifyRsbuildConfig(context);
const normalizeBaseConfig = normalizeConfig(context.config);

const environments = normalizeEnvironmentsConfigs(
const environments: Record<string, NormalizedEnvironmentConfig> = {};

const mergedEnvironments = initEnvironmentConfigs(
normalizeBaseConfig,
context.rootPath,
);

const {
dev: { assetPrefix, lazyCompilation, ...rsbuildSharedDev },
server,
} = normalizeBaseConfig;

for (const [name, config] of Object.entries(mergedEnvironments)) {
const environmentConfig = await modifyEnvironmentConfig(
context,
config,
name,
);

environments[name] = {
...environmentConfig,
dev: {
...environmentConfig.dev,
...rsbuildSharedDev,
},
server,
};
}

context.normalizedConfig = {
...normalizeBaseConfig,
environments,
Expand Down
Loading

0 comments on commit f2483ac

Please sign in to comment.