Skip to content

Commit 0d3744f

Browse files
authored
refactor: move internal helpers to provider parameters (#3978)
1 parent c15d0b3 commit 0d3744f

19 files changed

+103
-126
lines changed

.changeset/sixty-pets-hide.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@rsbuild/webpack': patch
3+
---
4+
5+
bump

packages/compat/webpack/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"typescript": "^5.6.3"
4646
},
4747
"peerDependencies": {
48-
"@rsbuild/core": "1.x"
48+
"@rsbuild/core": "^1.1.2"
4949
},
5050
"publishConfig": {
5151
"access": "public",

packages/compat/webpack/src/build.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import type { Configuration as WebpackConfig } from 'webpack';
44
import WebpackMultiStats from 'webpack/lib/MultiStats.js';
55
import { createCompiler } from './createCompiler.js';
66
import type { InitConfigsOptions } from './initConfigs.js';
7-
import { registerBuildHook } from './shared.js';
87

98
export const build = async (
109
initOptions: InitConfigsOptions,
1110
{ watch, compiler: customCompiler }: BuildOptions = {},
1211
): Promise<ReturnType<Build>> => {
13-
const { context } = initOptions;
12+
const { helpers, context } = initOptions;
1413

1514
let compiler: Rspack.Compiler | Rspack.MultiCompiler;
1615
let bundlerConfigs: WebpackConfig[] | undefined;
@@ -23,7 +22,7 @@ export const build = async (
2322
bundlerConfigs = result.webpackConfigs;
2423
}
2524

26-
registerBuildHook({
25+
helpers.registerBuildHook({
2726
context,
2827
bundlerConfigs: bundlerConfigs as Rspack.Configuration[],
2928
compiler,

packages/compat/webpack/src/createCompiler.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { type Rspack, logger } from '@rsbuild/core';
22
import WebpackMultiStats from 'webpack/lib/MultiStats.js';
33
import { type InitConfigsOptions, initConfigs } from './initConfigs.js';
4-
import { formatStats, getStatsOptions, registerDevHook } from './shared.js';
54

65
export async function createCompiler(options: InitConfigsOptions) {
76
logger.debug('create compiler');
8-
const { context } = options;
7+
const { helpers, context } = options;
98
const { webpackConfigs } = await initConfigs(options);
109

1110
await context.hooks.onBeforeCreateCompiler.call({
@@ -22,7 +21,7 @@ export async function createCompiler(options: InitConfigsOptions) {
2221
| Rspack.MultiCompiler;
2322

2423
const done = (stats: Rspack.Stats) => {
25-
const statsOptions = getStatsOptions(compiler);
24+
const statsOptions = helpers.getStatsOptions(compiler);
2625
const statsJson = stats.toJson({
2726
children: true,
2827
...(typeof statsOptions === 'string'
@@ -31,7 +30,10 @@ export async function createCompiler(options: InitConfigsOptions) {
3130
...(typeof statsOptions === 'object' ? statsOptions : {}),
3231
});
3332

34-
const { message, level } = formatStats(statsJson, stats.hasErrors());
33+
const { message, level } = helpers.formatStats(
34+
statsJson,
35+
stats.hasErrors(),
36+
);
3537

3638
if (level === 'error') {
3739
logger.error(message);
@@ -46,7 +48,7 @@ export async function createCompiler(options: InitConfigsOptions) {
4648
});
4749

4850
if (context.normalizedConfig?.mode === 'development') {
49-
registerDevHook({
51+
helpers.registerDevHook({
5052
compiler,
5153
context,
5254
bundlerConfigs: webpackConfigs as Rspack.Configuration[],

packages/compat/webpack/src/initConfigs.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
import {
22
type InspectConfigOptions,
3+
type InternalContext,
34
type PluginManager,
45
type ResolvedCreateRsbuildOptions,
6+
type RsbuildProviderHelpers,
57
logger,
68
} from '@rsbuild/core';
79
import { inspectConfig } from './inspectConfig.js';
8-
import { type InternalContext, initRsbuildConfig } from './shared.js';
910
import type { WebpackConfig } from './types.js';
1011
import { generateWebpackConfig } from './webpackConfig.js';
1112

1213
export type InitConfigsOptions = {
1314
context: InternalContext;
1415
pluginManager: PluginManager;
1516
rsbuildOptions: ResolvedCreateRsbuildOptions;
17+
helpers: RsbuildProviderHelpers;
1618
};
1719

1820
export async function initConfigs({
1921
context,
2022
pluginManager,
2123
rsbuildOptions,
24+
helpers,
2225
}: InitConfigsOptions): Promise<{
2326
webpackConfigs: WebpackConfig[];
2427
}> {
25-
const normalizedConfig = await initRsbuildConfig({
28+
const normalizedConfig = await helpers.initRsbuildConfig({
2629
context,
2730
pluginManager,
2831
});
@@ -33,6 +36,7 @@ export async function initConfigs({
3336
target: config.output.target,
3437
context,
3538
environment,
39+
helpers,
3640
}),
3741
),
3842
);
@@ -50,6 +54,7 @@ export async function initConfigs({
5054
inspectOptions,
5155
rsbuildOptions,
5256
bundlerConfigs: webpackConfigs,
57+
helpers,
5358
});
5459
};
5560

packages/compat/webpack/src/inspectConfig.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { isAbsolute, join } from 'node:path';
2-
import type { InspectConfigOptions, InspectConfigResult } from '@rsbuild/core';
2+
import type {
3+
InspectConfigOptions,
4+
InspectConfigResult,
5+
InternalContext,
6+
} from '@rsbuild/core';
37
import { type InitConfigsOptions, initConfigs } from './initConfigs.js';
4-
import {
5-
type InternalContext,
6-
getRsbuildInspectConfig,
7-
outputInspectConfigFiles,
8-
stringifyConfig,
9-
} from './shared.js';
108
import type { WebpackConfig } from './types.js';
119

1210
const getInspectOutputPath = (
@@ -25,6 +23,7 @@ const getInspectOutputPath = (
2523
};
2624

2725
export async function inspectConfig({
26+
helpers,
2827
context,
2928
pluginManager,
3029
rsbuildOptions,
@@ -47,20 +46,21 @@ export async function inspectConfig({
4746
context,
4847
pluginManager,
4948
rsbuildOptions,
49+
helpers,
5050
})
5151
).webpackConfigs;
5252

5353
const rawBundlerConfigs = webpackConfigs.map((config, index) => ({
5454
name: config.name || String(index),
55-
content: stringifyConfig(config, inspectOptions.verbose),
55+
content: helpers.stringifyConfig(config, inspectOptions.verbose),
5656
}));
5757

5858
const {
5959
rsbuildConfig,
6060
rawRsbuildConfig,
6161
environmentConfigs,
6262
rawEnvironmentConfigs,
63-
} = getRsbuildInspectConfig({
63+
} = helpers.getRsbuildInspectConfig({
6464
normalizedConfig: context.normalizedConfig!,
6565
inspectOptions,
6666
pluginManager,
@@ -69,7 +69,7 @@ export async function inspectConfig({
6969
const outputPath = getInspectOutputPath(context, inspectOptions);
7070

7171
if (inspectOptions.writeToDisk) {
72-
await outputInspectConfigFiles({
72+
await helpers.outputInspectConfigFiles({
7373
rawBundlerConfigs,
7474
rawEnvironmentConfigs,
7575
inspectOptions: {

packages/compat/webpack/src/plugin.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'node:fs';
22
import type {
33
ChainIdentifier,
44
RsbuildPlugin,
5+
RsbuildProviderHelpers,
56
RsbuildTarget,
67
Rspack,
78
RspackChain,
@@ -57,7 +58,9 @@ const getMainFields = (chain: RspackChain, target: RsbuildTarget) => {
5758
/**
5859
* Handling differences between Webpack and Rspack
5960
*/
60-
export const pluginAdaptor = (): RsbuildPlugin => ({
61+
export const pluginAdaptor = (
62+
helpers: RsbuildProviderHelpers,
63+
): RsbuildPlugin => ({
6164
name: 'rsbuild-webpack:adaptor',
6265

6366
setup(api) {
@@ -81,6 +84,7 @@ export const pluginAdaptor = (): RsbuildPlugin => ({
8184
chain.plugin(CHAIN_ID.PLUGIN.PROGRESS).use(ProgressPlugin, [
8285
{
8386
id: environment.name,
87+
prettyTime: helpers.prettyTime,
8488
...(progress === true ? {} : progress),
8589
},
8690
]);

packages/compat/webpack/src/progress/ProgressPlugin.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { logger } from '@rsbuild/core';
22
import color from 'picocolors';
33
import webpack from 'webpack';
4-
import { prettyTime } from '../shared.js';
54
import { bus, createFriendlyPercentage } from './helpers/index.js';
65
import { createNonTTYLogger } from './helpers/nonTty.js';
76
import type { Props } from './helpers/types.js';
87

98
export interface ProgressOptions
109
extends Omit<Partial<Props>, 'message' | 'total' | 'current' | 'done'> {
1110
id?: string;
11+
prettyTime: (seconds: number) => string;
1212
}
1313

1414
export class ProgressPlugin extends webpack.ProgressPlugin {
@@ -20,6 +20,8 @@ export class ProgressPlugin extends webpack.ProgressPlugin {
2020

2121
compileTime: string | null = null;
2222

23+
prettyTime: (seconds: number) => string;
24+
2325
constructor(options: ProgressOptions) {
2426
const { id = 'Rsbuild' } = options;
2527

@@ -61,6 +63,7 @@ export class ProgressPlugin extends webpack.ProgressPlugin {
6163
});
6264

6365
this.id = id;
66+
this.prettyTime = options.prettyTime;
6467
}
6568

6669
apply(compiler: webpack.Compiler): void {
@@ -78,7 +81,7 @@ export class ProgressPlugin extends webpack.ProgressPlugin {
7881
this.hasCompileErrors = stat.hasErrors();
7982
const hrtime = process.hrtime(startTime);
8083
const seconds = hrtime[0] + hrtime[1] / 1e9;
81-
this.compileTime = prettyTime(seconds);
84+
this.compileTime = this.prettyTime(seconds);
8285
startTime = null;
8386

8487
if (!this.hasCompileErrors) {

packages/compat/webpack/src/provider.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ import { createCompiler as baseCreateCompiler } from './createCompiler.js';
44
import { initConfigs } from './initConfigs.js';
55
import { inspectConfig } from './inspectConfig.js';
66
import { pluginAdaptor } from './plugin.js';
7-
import { createDevServer, initRsbuildConfig } from './shared.js';
87

98
export const webpackProvider: RsbuildProvider<'webpack'> = async ({
109
context,
1110
pluginManager,
1211
rsbuildOptions,
13-
setCssExtractPlugin,
12+
helpers,
1413
}) => {
1514
const { default: cssExtractPlugin } = await import('mini-css-extract-plugin');
16-
setCssExtractPlugin(cssExtractPlugin);
15+
helpers.setCssExtractPlugin(cssExtractPlugin);
1716

1817
const createCompiler = (async () => {
1918
const result = await baseCreateCompiler({
2019
context,
2120
pluginManager,
2221
rsbuildOptions,
22+
helpers,
2323
});
2424
return result.compiler;
2525
}) as CreateCompiler;
2626

27-
pluginManager.addPlugins([pluginAdaptor()]);
27+
pluginManager.addPlugins([pluginAdaptor(helpers)]);
2828

2929
return {
3030
bundler: 'webpack',
@@ -36,13 +36,17 @@ export const webpackProvider: RsbuildProvider<'webpack'> = async ({
3636
context,
3737
pluginManager,
3838
rsbuildOptions,
39+
helpers,
3940
});
4041
return webpackConfigs;
4142
},
4243

4344
async createDevServer(options) {
44-
const config = await initRsbuildConfig({ context, pluginManager });
45-
return createDevServer(
45+
const config = await helpers.initRsbuildConfig({
46+
context,
47+
pluginManager,
48+
});
49+
return helpers.createDevServer(
4650
{ context, pluginManager, rsbuildOptions },
4751
createCompiler,
4852
config,
@@ -51,11 +55,11 @@ export const webpackProvider: RsbuildProvider<'webpack'> = async ({
5155
},
5256

5357
async startDevServer(options) {
54-
const config = await initRsbuildConfig({
58+
const config = await helpers.initRsbuildConfig({
5559
context,
5660
pluginManager,
5761
});
58-
const server = await createDevServer(
62+
const server = await helpers.createDevServer(
5963
{
6064
context,
6165
pluginManager,
@@ -70,7 +74,10 @@ export const webpackProvider: RsbuildProvider<'webpack'> = async ({
7074
},
7175

7276
async build(options) {
73-
return build({ context, pluginManager, rsbuildOptions }, options);
77+
return build(
78+
{ context, pluginManager, rsbuildOptions, helpers },
79+
options,
80+
);
7481
},
7582

7683
async inspectConfig(inspectOptions) {
@@ -79,6 +86,7 @@ export const webpackProvider: RsbuildProvider<'webpack'> = async ({
7986
pluginManager,
8087
rsbuildOptions,
8188
inspectOptions,
89+
helpers,
8290
});
8391
},
8492
};

packages/compat/webpack/src/shared.ts

-36
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,3 @@
1-
import { __internalHelper } from '@rsbuild/core';
2-
3-
const {
4-
getChainUtils,
5-
initRsbuildConfig,
6-
createDevServer,
7-
formatStats,
8-
getStatsOptions,
9-
stringifyConfig,
10-
outputInspectConfigFiles,
11-
getRsbuildInspectConfig,
12-
chainToConfig,
13-
modifyBundlerChain,
14-
registerDevHook,
15-
registerBuildHook,
16-
prettyTime,
17-
} = __internalHelper;
18-
19-
export {
20-
getChainUtils,
21-
initRsbuildConfig,
22-
createDevServer,
23-
formatStats,
24-
getStatsOptions,
25-
stringifyConfig,
26-
outputInspectConfigFiles,
27-
chainToConfig,
28-
modifyBundlerChain,
29-
registerDevHook,
30-
registerBuildHook,
31-
prettyTime,
32-
getRsbuildInspectConfig,
33-
};
34-
35-
export type InternalContext = __internalHelper.InternalContext;
36-
371
export const castArray = <T>(arr?: T | T[]): T[] => {
382
if (arr === undefined) {
393
return [];

0 commit comments

Comments
 (0)