Skip to content

Commit 453a803

Browse files
authored
perf: use Rspack native IgnorePlugin (#2122)
1 parent cb5092b commit 453a803

File tree

7 files changed

+11
-59
lines changed

7 files changed

+11
-59
lines changed

packages/compat/webpack/src/core/webpackConfig.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export async function generateWebpackConfig({
145145
const {
146146
BannerPlugin,
147147
DefinePlugin,
148+
IgnorePlugin,
148149
ProvidePlugin,
149150
HotModuleReplacementPlugin,
150151
} = await import('webpack');
@@ -154,6 +155,7 @@ export async function generateWebpackConfig({
154155
bundler: {
155156
BannerPlugin,
156157
DefinePlugin,
158+
IgnorePlugin,
157159
ProvidePlugin,
158160
HotModuleReplacementPlugin,
159161
},

packages/core/src/plugins/moment.ts

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,17 @@
1-
import type { RspackPluginInstance, Compiler } from '@rspack/core';
21
import type { RsbuildPlugin } from '../types';
32

4-
type IgnorePluginOptions = {
5-
/**
6-
* A RegExp to test the context (directory) against.
7-
*/
8-
contextRegExp?: RegExp;
9-
/**
10-
* A RegExp to test the request against.
11-
*/
12-
resourceRegExp: RegExp;
13-
};
14-
15-
/**
16-
* modified from https://github.com/webpack/webpack/blob/main/lib/IgnorePlugin.js
17-
* MIT License http://www.opensource.org/licenses/mit-license.php
18-
* Author Tobias Koppers @sokra
19-
* TODO: remove this plugin after Rspack provides IgnorePlugin
20-
*/
21-
class IgnorePlugin implements RspackPluginInstance {
22-
options: IgnorePluginOptions;
23-
24-
constructor(options: IgnorePluginOptions) {
25-
this.options = options;
26-
27-
this.checkIgnore = this.checkIgnore.bind(this);
28-
}
29-
30-
checkIgnore(resolveData: any) {
31-
if (
32-
'resourceRegExp' in this.options &&
33-
this.options.resourceRegExp &&
34-
this.options.resourceRegExp.test(resolveData.request)
35-
) {
36-
if ('contextRegExp' in this.options && this.options.contextRegExp) {
37-
// if "contextRegExp" is given,
38-
// both the "resourceRegExp" and "contextRegExp" have to match.
39-
if (this.options.contextRegExp.test(resolveData.context)) {
40-
return false;
41-
}
42-
} else {
43-
return false;
44-
}
45-
}
46-
}
47-
48-
apply(compiler: Compiler) {
49-
compiler.hooks.normalModuleFactory.tap('IgnorePlugin', (nmf) => {
50-
nmf.hooks.beforeResolve.tap('IgnorePlugin', this.checkIgnore);
51-
});
52-
compiler.hooks.contextModuleFactory.tap('IgnorePlugin', (cmf) => {
53-
cmf.hooks.beforeResolve.tap('IgnorePlugin', this.checkIgnore);
54-
});
55-
}
56-
}
57-
583
export const pluginMoment = (): RsbuildPlugin => ({
594
name: 'rsbuild:moment',
605

616
setup(api) {
62-
api.modifyBundlerChain(async (chain) => {
7+
api.modifyBundlerChain(async (chain, { bundler }) => {
638
const config = api.getNormalizedConfig();
649

6510
if (config.performance.removeMomentLocale) {
6611
// Moment.js includes a lots of locale data by default.
6712
// We can using IgnorePlugin to allow the user to opt into importing specific locales.
6813
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
69-
chain.plugin('remove-moment-locale').use(IgnorePlugin, [
14+
chain.plugin('remove-moment-locale').use(bundler.IgnorePlugin, [
7015
{
7116
resourceRegExp: /^\.\/locale$/,
7217
contextRegExp: /moment$/,

packages/core/src/provider/rspackConfig.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export async function generateRspackConfig({
118118
const {
119119
BannerPlugin,
120120
DefinePlugin,
121+
IgnorePlugin,
121122
ProvidePlugin,
122123
HotModuleReplacementPlugin,
123124
} = await import('@rspack/core');
@@ -127,6 +128,7 @@ export async function generateRspackConfig({
127128
bundler: {
128129
BannerPlugin,
129130
DefinePlugin,
131+
IgnorePlugin,
130132
ProvidePlugin,
131133
HotModuleReplacementPlugin,
132134
},

packages/core/src/provider/shared.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import { fse } from '@rsbuild/shared';
1212
import { formatStatsMessages } from '../client/formatStats';
1313
import type { StatsCompilation, StatsValue } from '@rspack/core';
1414

15-
// depend on CSS modules generator config
16-
export const rspackMinVersion = '0.6.0';
15+
// depend on native IgnorePlugin
16+
export const rspackMinVersion = '0.6.2';
1717

1818
const compareSemver = (version1: string, version2: string) => {
1919
const parts1 = version1.split('.').map(Number);

packages/document/docs/en/plugins/dev/hooks.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ type ModifyBundlerChainUtils = {
262262
bundler: {
263263
BannerPlugin: rspack.BannerPlugin;
264264
DefinePlugin: rspack.DefinePlugin;
265+
IgnorePlugin: rspack.IgnorePlugin;
265266
ProvidePlugin: rspack.ProvidePlugin;
266267
HotModuleReplacementPlugin: rspack.HotModuleReplacementPlugin;
267268
};

packages/document/docs/zh/plugins/dev/hooks.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ type ModifyBundlerChainUtils = {
264264
bundler: {
265265
BannerPlugin: rspack.BannerPlugin;
266266
DefinePlugin: rspack.DefinePlugin;
267+
IgnorePlugin: rspack.IgnorePlugin;
267268
ProvidePlugin: rspack.ProvidePlugin;
268269
HotModuleReplacementPlugin: rspack.HotModuleReplacementPlugin;
269270
};

packages/shared/src/types/hooks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export type ModifyBundlerChainUtils = ModifyChainUtils & {
9494
bundler: {
9595
BannerPlugin: PluginInstance;
9696
DefinePlugin: PluginInstance;
97+
IgnorePlugin: PluginInstance;
9798
ProvidePlugin: PluginInstance;
9899
HotModuleReplacementPlugin: PluginInstance;
99100
};

0 commit comments

Comments
 (0)