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

feat: add modifyEnvironmentConfig hook #2707

Merged
merged 5 commits into from
Jun 26, 2024
Merged
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
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
Loading