Skip to content

Commit

Permalink
feat: allow to access environment context in process assets (#2970)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Jul 21, 2024
1 parent 718343c commit eebe00d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
36 changes: 21 additions & 15 deletions packages/core/src/initPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,26 +209,32 @@ export function getPluginAPI({
const processAssets: ProcessAssetsFn = (descriptor, handler) => {
const name = 'RsbuildProcessAssetsPlugin';

class RsbuildProcessAssetsPlugin {
apply(compiler: Compiler): void {
compiler.hooks.compilation.tap(name, (compilation) => {
compilation.hooks.processAssets.tapPromise(
{
name,
stage: mapProcessAssetsStage(compiler, descriptor.stage),
},
async (assets) => handler({ assets, compiler, compilation }),
);
});
}
}

hooks.modifyBundlerChain.tap((chain, { target }) => {
hooks.modifyBundlerChain.tap((chain, { target, environment }) => {
// filter by targets
if (descriptor.targets && !descriptor.targets.includes(target)) {
return;
}

class RsbuildProcessAssetsPlugin {
apply(compiler: Compiler): void {
compiler.hooks.compilation.tap(name, (compilation) => {
compilation.hooks.processAssets.tapPromise(
{
name,
stage: mapProcessAssetsStage(compiler, descriptor.stage),
},
async (assets) =>
handler({
assets,
compiler,
compilation,
environment,
}),
);
});
}
}

chain
.plugin(`RsbuildProcessAssetsPlugin#${processAssetsId++}`)
.use(RsbuildProcessAssetsPlugin);
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ export type ProcessAssetsHandler = (context: {
assets: Record<string, Rspack.sources.Source>;
compiler: Rspack.Compiler;
compilation: Rspack.Compilation;
/**
* The environment context for current build.
*/
environment: EnvironmentContext;
}) => Promise<void> | void;

export type ProcessAssetsFn = (
Expand Down

0 comments on commit eebe00d

Please sign in to comment.