Skip to content

Commit 9c26ab0

Browse files
authored
feat: improve deprecation warning messages (#12524)
* feat: improve deprecation warning messages * fix: comment * fix: proxy
1 parent da9254b commit 9c26ab0

File tree

6 files changed

+58
-64
lines changed

6 files changed

+58
-64
lines changed

packages/rspack-test-tools/src/helper/legacy/deprecationTracking.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,18 @@
66

77
"use strict";
88

9-
const util = require("util");
10-
119
let interception = undefined;
1210

13-
const originalDeprecate = util.deprecate;
14-
util.deprecate = (fn, message, code) => {
15-
const original = originalDeprecate(fn, message, code);
11+
const originalWarn = console.warn;
1612

17-
return function (...args) {
18-
if (interception) {
19-
interception.set(`${code}: ${message}`, {
20-
code,
21-
message,
22-
stack: new Error(message).stack
23-
});
24-
return fn.apply(this, args);
25-
} else {
26-
return original.apply(this, args);
27-
}
28-
};
13+
console.warn = (message, ...args) => {
14+
if (interception && typeof message === 'string' && message.includes('[Rspack Deprecation]')) {
15+
interception.set(message, {
16+
message,
17+
stack: new Error(message).stack
18+
});
19+
}
20+
return originalWarn.apply(console, [message, ...args]);
2921
};
3022

3123
exports.start = handler => {

packages/rspack/src/builtin-plugin/lazy-compilation/middleware.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { IncomingMessage, ServerResponse } from "node:http";
22
import { type Compiler, MultiCompiler } from "../..";
33
import type { LazyCompilationOptions } from "../../config";
44
import type { MiddlewareHandler } from "../../config/devServer";
5+
import { deprecate } from "../../util";
56
import { BuiltinLazyCompilationPlugin } from "./lazyCompilation";
67

78
export const LAZY_COMPILATION_PREFIX = "/lazy-compilation-using-";
@@ -57,28 +58,25 @@ export const lazyCompilationMiddleware = (
5758
const middlewareByCompiler: Map<string, MiddlewareHandler> = new Map();
5859

5960
let i = 0;
60-
let isReportDeprecatedWarned = false;
61-
let isReportRepeatWarned = false;
61+
6262
for (const c of compiler.compilers) {
6363
if (c.options.experiments.lazyCompilation) {
6464
if (c.name) {
65-
console.warn(
65+
deprecate(
6666
`The 'experiments.lazyCompilation' option in compiler named '${c.name}' is deprecated, please use the Configuration top level 'lazyCompilation' instead.`
6767
);
68-
} else if (!isReportDeprecatedWarned) {
69-
console.warn(DEPRECATED_LAZY_COMPILATION_OPTIONS_WARN);
70-
isReportDeprecatedWarned = true;
68+
} else {
69+
deprecate(DEPRECATED_LAZY_COMPILATION_OPTIONS_WARN);
7170
}
7271
}
7372

7473
if (c.options.lazyCompilation && c.options.experiments.lazyCompilation) {
7574
if (c.name) {
76-
console.warn(
75+
deprecate(
7776
`The top-level 'lazyCompilation' option in compiler named '${c.name}' will override the 'experiments.lazyCompilation' option.`
7877
);
79-
} else if (!isReportRepeatWarned) {
80-
console.warn(REPEAT_LAZY_COMPILATION_OPTIONS_WARN);
81-
isReportRepeatWarned = true;
78+
} else {
79+
deprecate(REPEAT_LAZY_COMPILATION_OPTIONS_WARN);
8280
}
8381
}
8482

@@ -125,9 +123,9 @@ export const lazyCompilationMiddleware = (
125123
}
126124

127125
if (compiler.options.experiments.lazyCompilation) {
128-
console.warn(DEPRECATED_LAZY_COMPILATION_OPTIONS_WARN);
126+
deprecate(DEPRECATED_LAZY_COMPILATION_OPTIONS_WARN);
129127
if (compiler.options.lazyCompilation) {
130-
console.warn(REPEAT_LAZY_COMPILATION_OPTIONS_WARN);
128+
deprecate(REPEAT_LAZY_COMPILATION_OPTIONS_WARN);
131129
}
132130
}
133131

packages/rspack/src/config/normalization.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
*/
1010

1111
import path from "node:path";
12-
import util from "node:util";
1312
import type { HttpUriPluginOptions } from "../builtin-plugin";
1413
import type { Compilation } from "../Compilation";
1514
import type WebpackError from "../lib/WebpackError";
15+
import { deprecate } from "../util";
1616
import type {
1717
Amd,
1818
AssetModuleFilename,
@@ -153,10 +153,9 @@ export const getNormalizedRspackOptions = (
153153
: getNormalizedEntryStatic(config.entry),
154154
output: nestedConfig(config.output, output => {
155155
if ("cssHeadDataCompression" in output) {
156-
util.deprecate(
157-
() => {},
156+
deprecate(
158157
"cssHeadDataCompression is not used now, see https://github.com/web-infra-dev/rspack/pull/8534, this option could be removed in the future"
159-
)();
158+
);
160159
}
161160

162161
const { library } = output;
@@ -349,40 +348,34 @@ export const getNormalizedRspackOptions = (
349348
plugins: nestedArray(config.plugins, p => [...p]),
350349
experiments: nestedConfig(config.experiments, experiments => {
351350
if (experiments.layers) {
352-
util.deprecate(
353-
() => {},
354-
"`experiments.layers` config has been deprecated and will be removed in Rspack v2.0. Feature layers will be always enabled. Please remove this option from your Rspack configuration."
355-
)();
351+
deprecate(
352+
"`experiments.layers` config is deprecated and will be removed in Rspack v2.0. Feature layers will always be enabled. Remove this option from your Rspack configuration."
353+
);
356354
}
357355
if (experiments.topLevelAwait === false) {
358-
util.deprecate(
359-
() => {},
360-
"`experiments.topLevelAwait` config has been deprecated and will be removed in Rspack v2.0. Top-level await will be always enabled. Please remove this option from your Rspack configuration."
361-
)();
356+
deprecate(
357+
"`experiments.topLevelAwait` config is deprecated and will be removed in Rspack v2.0. Top-level await will always be enabled. Remove this option from your Rspack configuration."
358+
);
362359
}
363360
if (experiments.lazyBarrel) {
364-
util.deprecate(
365-
() => {},
366-
"`experiments.lazyBarrel` config has been deprecated and will be removed in Rspack v2.0. Lazy barrel is already stable and enabled by default. Please remove this option from your Rspack configuration."
367-
)();
361+
deprecate(
362+
"`experiments.lazyBarrel` config is deprecated and will be removed in Rspack v2.0. Lazy barrel is already stable and enabled by default. Remove this option from your Rspack configuration."
363+
);
368364
}
369365
if (experiments.inlineConst) {
370-
util.deprecate(
371-
() => {},
372-
"`experiments.inlineConst` config has been deprecated and will be removed in Rspack v2.0. Inline Const is already stable and enabled by default. Please remove this option from your Rspack configuration."
373-
)();
366+
deprecate(
367+
"`experiments.inlineConst` config is deprecated and will be removed in Rspack v2.0. Inline Const is already stable and enabled by default. Remove this option from your Rspack configuration."
368+
);
374369
}
375370
if (experiments.inlineEnum) {
376-
util.deprecate(
377-
() => {},
378-
"`experiments.inlineEnum` config has been deprecated and will be removed in Rspack v2.0. Inline Enum is already stable. Please remove this option from your Rspack configuration."
379-
)();
371+
deprecate(
372+
"`experiments.inlineEnum` config is deprecated and will be removed in Rspack v2.0. Inline Enum is already stable. Remove this option from your Rspack configuration."
373+
);
380374
}
381375
if (experiments.typeReexportsPresence) {
382-
util.deprecate(
383-
() => {},
384-
"`experiments.typeReexportsPresence` config has been deprecated and will be removed in Rspack v2.0. typeReexportsPresence is already stable. Please remove this option from your Rspack configuration."
385-
)();
376+
deprecate(
377+
"`experiments.typeReexportsPresence` config is deprecated and will be removed in Rspack v2.0. typeReexportsPresence is already stable. Remove this option from your Rspack configuration."
378+
);
386379
}
387380
return {
388381
...experiments,

packages/rspack/src/config/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,6 +2789,7 @@ export type Experiments = {
27892789
outputModule?: boolean;
27902790
/**
27912791
* Enable top-level await.
2792+
* @deprecated This option is deprecated, top-level await is enabled by default.
27922793
* @default true
27932794
*/
27942795
topLevelAwait?: boolean;

packages/rspack/src/rspack.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* Copyright (c) JS Foundation and other contributors
88
* https://github.com/webpack/webpack/blob/main/LICENSE
99
*/
10-
import util from "node:util";
1110
import type { Callback } from "@rspack/lite-tapable";
1211
import { Compiler } from "./Compiler";
1312
import {
@@ -26,7 +25,7 @@ import MultiStats from "./MultiStats";
2625
import NodeEnvironmentPlugin from "./node/NodeEnvironmentPlugin";
2726
import { RspackOptionsApply } from "./rspackOptionsApply";
2827
import { Stats } from "./Stats";
29-
import { isNil } from "./util";
28+
import { deprecate, isNil } from "./util";
3029
import { validateRspackConfig } from "./util/validateConfig";
3130

3231
function createMultiCompiler(options: MultiRspackOptions): MultiCompiler {
@@ -153,10 +152,9 @@ function rspack(
153152
} else {
154153
const { compiler, watch } = create();
155154
if (watch) {
156-
util.deprecate(
157-
() => {},
155+
deprecate(
158156
"A 'callback' argument needs to be provided to the 'rspack(options, callback)' function when the 'watch' option is set. There is no way to handle the 'watch' option without a callback."
159-
)();
157+
);
160158
}
161159
return compiler;
162160
}

packages/rspack/src/util/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,21 @@ export function stringifyLoaderObject(o: LoaderObject): string {
6060
}
6161

6262
export const unsupported = (name: string, issue?: string) => {
63-
let s = `${name} is not supported by rspack.`;
63+
let s = `${name} is not supported by Rspack.`;
6464
if (issue) {
65-
s += ` Please refer to issue ${issue} for more information.`;
65+
s += ` Refer to issue ${issue} for more information.`;
6666
}
6767
throw new Error(s);
6868
};
69+
70+
const warnedMessages = new Set<string>();
71+
72+
export function deprecate(message: string): void {
73+
// ensure we only warn once per message
74+
if (warnedMessages.has(message)) {
75+
return;
76+
}
77+
78+
warnedMessages.add(message);
79+
console.warn(`[Rspack Deprecation] ${message}`);
80+
}

0 commit comments

Comments
 (0)