Skip to content

Commit 337adcc

Browse files
authored
fix: processAssets hook filter error (#4671)
1 parent bf9a0fc commit 337adcc

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

e2e/cases/plugin-api/plugin-process-assets-by-environments/index.test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { existsSync } from 'node:fs';
22
import { join } from 'node:path';
33
import { build, rspackOnlyTest } from '@e2e/helper';
44
import { expect } from '@playwright/test';
5+
import type { RsbuildPluginAPI } from '@rsbuild/core';
56

67
rspackOnlyTest(
78
'should allow plugin to process assets by environments',
@@ -14,3 +15,29 @@ rspackOnlyTest(
1415
expect(existsSync(join(rsbuild.distPath, 'server/index.js'))).toBeTruthy();
1516
},
1617
);
18+
19+
rspackOnlyTest('should filter environments correctly', async () => {
20+
const rsbuild = await build({
21+
cwd: __dirname,
22+
plugins: [
23+
{
24+
name: 'my-plugin-node',
25+
setup(api: RsbuildPluginAPI) {
26+
api.processAssets(
27+
{ stage: 'summarize', environments: ['node'] },
28+
({ assets, compilation }) => {
29+
for (const key of Object.keys(assets)) {
30+
if (key.endsWith('.js')) {
31+
compilation.deleteAsset(key);
32+
}
33+
}
34+
},
35+
);
36+
},
37+
},
38+
],
39+
});
40+
41+
expect(existsSync(join(rsbuild.distPath, 'static/index.js'))).toBeFalsy();
42+
expect(existsSync(join(rsbuild.distPath, 'server/index.js'))).toBeFalsy();
43+
});

packages/core/src/initPlugins.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export function initPluginAPI({
179179
pluginEnvironment &&
180180
!isPluginMatchEnvironment(pluginEnvironment, environment.name)
181181
) {
182-
return;
182+
continue;
183183
}
184184

185185
compiler.hooks.compilation.tap(
@@ -213,7 +213,7 @@ export function initPluginAPI({
213213
} of processAssetsFns) {
214214
// filter by targets
215215
if (descriptor.targets && !descriptor.targets.includes(target)) {
216-
return;
216+
continue;
217217
}
218218

219219
// filter by environments
@@ -224,7 +224,7 @@ export function initPluginAPI({
224224
(pluginEnvironment &&
225225
!isPluginMatchEnvironment(pluginEnvironment, environment.name))
226226
) {
227-
return;
227+
continue;
228228
}
229229

230230
compilation.hooks.processAssets.tapPromise(

0 commit comments

Comments
 (0)