Skip to content

Commit be8a4b0

Browse files
authored
fix: standardize logging messages (#843)
1 parent a9b6f04 commit be8a4b0

File tree

9 files changed

+54
-33
lines changed

9 files changed

+54
-33
lines changed

packages/core/__mocks__/rslog.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
logger: {
33
warn: () => {},
4+
override: () => {},
45
},
56
};

packages/core/src/cli/restart.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ const beforeRestart = async ({
6464

6565
if (filePath) {
6666
const filename = path.basename(filePath);
67-
logger.info(`Restart because ${color.yellow(filename)} is changed.\n`);
67+
logger.info(`restart because ${color.yellow(filename)} is changed.\n`);
6868
} else {
69-
logger.info('Restarting...\n');
69+
logger.info('restarting...\n');
7070
}
7171

7272
for (const cleaner of cleaners) {

packages/core/src/config.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ const composeExternalsWarnConfig = (
282282
if (contextInfo.issuer && dependencyType === 'commonjs') {
283283
matchUserExternals(externals, request, _callback);
284284
if (shouldWarn) {
285-
logger.warn(composeModuleImportWarn(request));
285+
logger.warn(composeModuleImportWarn(request, contextInfo.issuer));
286286
}
287287
}
288288
callback();
@@ -324,7 +324,7 @@ export const composeAutoExternalConfig = (options: {
324324

325325
if (!pkgJson) {
326326
logger.warn(
327-
'autoExternal configuration will not be applied due to read package.json failed',
327+
'The `autoExternal` configuration will not be applied due to read package.json failed',
328328
);
329329
return {};
330330
}
@@ -782,8 +782,11 @@ const composeShimsConfig = (
782782
return { rsbuildConfig, enabledShims };
783783
};
784784

785-
export const composeModuleImportWarn = (request: string): string => {
786-
return `The externalized commonjs request ${color.green(`"${request}"`)} will use ${color.blue('"module"')} external type in ESM format. If you want to specify other external type, consider setting the request and type with ${color.blue('"output.externals"')}.`;
785+
export const composeModuleImportWarn = (
786+
request: string,
787+
issuer: string,
788+
): string => {
789+
return `The externalized commonjs request ${color.green(`"${request}"`)} from ${color.green(issuer)} will use ${color.blue('"module"')} external type in ESM format. If you want to specify other external type, consider setting the request and type with ${color.blue('"output.externals"')}.`;
787790
};
788791

789792
const composeExternalsConfig = (

packages/core/src/utils/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const getDefaultExtension = (options: {
2424

2525
if (!pkgJson) {
2626
logger.warn(
27-
'autoExtension configuration will not be applied due to read package.json failed',
27+
'The `autoExtension` configuration will not be applied due to read package.json failed',
2828
);
2929
return {
3030
jsExtension,

packages/core/src/utils/helper.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ export const readPackageJson = (rootPath: string): undefined | PkgJson => {
117117
const pkgJsonPath = path.join(rootPath, './package.json');
118118

119119
if (!fs.existsSync(pkgJsonPath)) {
120-
logger.warn(`package.json does not exist in the ${rootPath} directory`);
120+
logger.warn(
121+
`The \`package.json\` file does not exist in the ${rootPath} directory`,
122+
);
121123
return;
122124
}
123125

packages/core/src/utils/logger.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
1+
/**
2+
* Logging message case convention:
3+
*
4+
* Info, ready, success and debug messages:
5+
* - Start with lowercase
6+
* - Example: "info build started..."
7+
*
8+
* Errors and warnings:
9+
* - Start with uppercase
10+
* - Example: "error Failed to build"
11+
*
12+
* This convention helps distinguish between normal operations
13+
* and important alerts that require attention.
14+
*/
115
import { type Logger, logger } from 'rslog';
216
import { color } from './helper';
317

4-
// setup the logger level
5-
if (process.env.DEBUG) {
6-
logger.level = 'verbose';
7-
}
8-
918
export const isDebug = (): boolean => {
1019
if (!process.env.DEBUG) {
1120
return false;
1221
}
1322

14-
logger.level = 'verbose'; // support `process.env.DEBUG` in e2e
1523
const values = process.env.DEBUG.toLocaleLowerCase().split(',');
16-
return ['rslib', 'rsbuild', 'builder', '*'].some((key) =>
17-
values.includes(key),
18-
);
24+
return ['rslib', 'rs*', 'rstack', '*'].some((key) => values.includes(key));
1925
};
2026

21-
function getTime() {
27+
// setup the logger level
28+
if (isDebug()) {
29+
logger.level = 'verbose';
30+
}
31+
32+
function getTime(): string {
2233
const now = new Date();
2334
const hours = String(now.getHours()).padStart(2, '0');
2435
const minutes = String(now.getMinutes()).padStart(2, '0');
@@ -27,13 +38,15 @@ function getTime() {
2738
return `${hours}:${minutes}:${seconds}`;
2839
}
2940

30-
export const debug = (message: string | (() => string)): void => {
31-
if (isDebug()) {
32-
const result = typeof message === 'string' ? message : message();
41+
logger.override({
42+
debug: (message, ...args) => {
43+
if (logger.level !== 'verbose') {
44+
return;
45+
}
3346
const time = color.gray(`${getTime()}`);
34-
logger.debug(`${time} ${result}`);
35-
}
36-
};
47+
console.log(` ${color.green('rslib')} ${time} ${message}`, ...args);
48+
},
49+
});
3750

3851
export { logger };
3952
export type { Logger };

packages/plugin-dts/src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ export async function emptyDir(dir: string): Promise<void> {
9696
});
9797
}
9898
} catch (err) {
99-
logger.debug(`Failed to empty dir: ${dir}`);
100-
logger.debug(err);
99+
logger.warn(`Failed to empty dir: ${dir}`);
100+
logger.warn(err);
101101
}
102102
}
103103

@@ -332,7 +332,7 @@ export async function redirectDtsImports(
332332

333333
code.overwrite(start, end, normalizedRedirectImportPath);
334334
} catch (err) {
335-
logger.debug(err);
335+
logger.warn(err);
336336
}
337337
}
338338

tests/integration/auto-external/index.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,26 @@ test('should get warn when use require in ESM', async () => {
9191

9292
const shouldWarn = ['react', 'e2', 'e3', 'e5', 'e6', 'e7'];
9393
const shouldNotWarn = ['e1', 'e4', 'e8', 'lodash/add', 'lodash/drop'];
94+
const issuer = join(fixturePath, 'src/index.ts');
9495

9596
for (const item of shouldWarn) {
9697
expect(entries.esm).toContain(
9798
`import * as __WEBPACK_EXTERNAL_MODULE_${item}__ from "${item}"`,
9899
);
99100
}
100101

101-
for (const item of shouldWarn) {
102+
for (const request of shouldWarn) {
102103
expect(
103104
logStrings.some((l) =>
104-
l.includes(stripAnsi(composeModuleImportWarn(item))),
105+
l.includes(stripAnsi(composeModuleImportWarn(request, issuer))),
105106
),
106107
).toBe(true);
107108
}
108109

109-
for (const item of shouldNotWarn) {
110+
for (const request of shouldNotWarn) {
110111
expect(
111112
logStrings.some((l) =>
112-
l.includes(stripAnsi(composeModuleImportWarn(item))),
113+
l.includes(stripAnsi(composeModuleImportWarn(request, issuer))),
113114
),
114115
).toBe(false);
115116
}

tests/integration/externals/index.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ test('should get warn when use require in ESM', async () => {
4040
const fixturePath = join(__dirname, 'module-import-warn');
4141
const { entries } = await buildAndGetResults({ fixturePath });
4242
const logStrings = logs.map((log) => stripAnsi(log));
43+
const issuer = join(fixturePath, 'src/index.ts');
4344

4445
for (const external of [
4546
'import * as __WEBPACK_EXTERNAL_MODULE_bar__ from "bar";',
@@ -51,15 +52,15 @@ test('should get warn when use require in ESM', async () => {
5152
for (const external of ['foo', 'bar', 'qux']) {
5253
expect(
5354
logStrings.some((l) =>
54-
l.includes(stripAnsi(composeModuleImportWarn(external))),
55+
l.includes(stripAnsi(composeModuleImportWarn(external, issuer))),
5556
),
5657
).toBe(true);
5758
}
5859

5960
for (const external of ['./baz', 'quxx']) {
6061
expect(
6162
logStrings.some((l) =>
62-
l.includes(stripAnsi(composeModuleImportWarn(external))),
63+
l.includes(stripAnsi(composeModuleImportWarn(external, issuer))),
6364
),
6465
).toBe(false);
6566
}

0 commit comments

Comments
 (0)