Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/__mocks__/rslog.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
logger: {
warn: () => {},
override: () => {},
},
};
4 changes: 2 additions & 2 deletions packages/core/src/cli/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ const beforeRestart = async ({

if (filePath) {
const filename = path.basename(filePath);
logger.info(`Restart because ${color.yellow(filename)} is changed.\n`);
logger.info(`restart because ${color.yellow(filename)} is changed.\n`);
} else {
logger.info('Restarting...\n');
logger.info('restarting...\n');
}

for (const cleaner of cleaners) {
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ const composeExternalsWarnConfig = (
if (contextInfo.issuer && dependencyType === 'commonjs') {
matchUserExternals(externals, request, _callback);
if (shouldWarn) {
logger.warn(composeModuleImportWarn(request));
logger.warn(composeModuleImportWarn(request, contextInfo.issuer));
}
}
callback();
Expand Down Expand Up @@ -324,7 +324,7 @@ export const composeAutoExternalConfig = (options: {

if (!pkgJson) {
logger.warn(
'autoExternal configuration will not be applied due to read package.json failed',
'The `autoExternal` configuration will not be applied due to read package.json failed',
);
return {};
}
Expand Down Expand Up @@ -782,8 +782,11 @@ const composeShimsConfig = (
return { rsbuildConfig, enabledShims };
};

export const composeModuleImportWarn = (request: string): string => {
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"')}.`;
export const composeModuleImportWarn = (
request: string,
issuer: string,
): string => {
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"')}.`;
};

const composeExternalsConfig = (
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const getDefaultExtension = (options: {

if (!pkgJson) {
logger.warn(
'autoExtension configuration will not be applied due to read package.json failed',
'The `autoExtension` configuration will not be applied due to read package.json failed',
);
return {
jsExtension,
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ export const readPackageJson = (rootPath: string): undefined | PkgJson => {
const pkgJsonPath = path.join(rootPath, './package.json');

if (!fs.existsSync(pkgJsonPath)) {
logger.warn(`package.json does not exist in the ${rootPath} directory`);
logger.warn(
`The \`package.json\` file does not exist in the ${rootPath} directory`,
);
return;
}

Expand Down
45 changes: 29 additions & 16 deletions packages/core/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
/**
* Logging message case convention:
*
* Info, ready, success and debug messages:
* - Start with lowercase
* - Example: "info build started..."
*
* Errors and warnings:
* - Start with uppercase
* - Example: "error Failed to build"
*
* This convention helps distinguish between normal operations
* and important alerts that require attention.
*/
import { type Logger, logger } from 'rslog';
import { color } from './helper';

// setup the logger level
if (process.env.DEBUG) {
logger.level = 'verbose';
}

export const isDebug = (): boolean => {
if (!process.env.DEBUG) {
return false;
}

logger.level = 'verbose'; // support `process.env.DEBUG` in e2e
const values = process.env.DEBUG.toLocaleLowerCase().split(',');
return ['rslib', 'rsbuild', 'builder', '*'].some((key) =>
values.includes(key),
);
return ['rslib', 'rs*', 'rstack', '*'].some((key) => values.includes(key));
};

function getTime() {
// setup the logger level
if (isDebug()) {
logger.level = 'verbose';
}

function getTime(): string {
const now = new Date();
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
Expand All @@ -27,13 +38,15 @@ function getTime() {
return `${hours}:${minutes}:${seconds}`;
}

export const debug = (message: string | (() => string)): void => {
if (isDebug()) {
const result = typeof message === 'string' ? message : message();
logger.override({
debug: (message, ...args) => {
if (logger.level !== 'verbose') {
return;
}
const time = color.gray(`${getTime()}`);
logger.debug(`${time} ${result}`);
}
};
console.log(` ${color.green('rslib')} ${time} ${message}`, ...args);
},
});

export { logger };
export type { Logger };
6 changes: 3 additions & 3 deletions packages/plugin-dts/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ export async function emptyDir(dir: string): Promise<void> {
});
}
} catch (err) {
logger.debug(`Failed to empty dir: ${dir}`);
logger.debug(err);
logger.warn(`Failed to empty dir: ${dir}`);
logger.warn(err);
}
}

Expand Down Expand Up @@ -332,7 +332,7 @@ export async function redirectDtsImports(

code.overwrite(start, end, normalizedRedirectImportPath);
} catch (err) {
logger.debug(err);
logger.warn(err);
}
}

Expand Down
9 changes: 5 additions & 4 deletions tests/integration/auto-external/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,26 @@ test('should get warn when use require in ESM', async () => {

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

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

for (const item of shouldWarn) {
for (const request of shouldWarn) {
expect(
logStrings.some((l) =>
l.includes(stripAnsi(composeModuleImportWarn(item))),
l.includes(stripAnsi(composeModuleImportWarn(request, issuer))),
),
).toBe(true);
}

for (const item of shouldNotWarn) {
for (const request of shouldNotWarn) {
expect(
logStrings.some((l) =>
l.includes(stripAnsi(composeModuleImportWarn(item))),
l.includes(stripAnsi(composeModuleImportWarn(request, issuer))),
),
).toBe(false);
}
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/externals/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ test('should get warn when use require in ESM', async () => {
const fixturePath = join(__dirname, 'module-import-warn');
const { entries } = await buildAndGetResults({ fixturePath });
const logStrings = logs.map((log) => stripAnsi(log));
const issuer = join(fixturePath, 'src/index.ts');

for (const external of [
'import * as __WEBPACK_EXTERNAL_MODULE_bar__ from "bar";',
Expand All @@ -51,15 +52,15 @@ test('should get warn when use require in ESM', async () => {
for (const external of ['foo', 'bar', 'qux']) {
expect(
logStrings.some((l) =>
l.includes(stripAnsi(composeModuleImportWarn(external))),
l.includes(stripAnsi(composeModuleImportWarn(external, issuer))),
),
).toBe(true);
}

for (const external of ['./baz', 'quxx']) {
expect(
logStrings.some((l) =>
l.includes(stripAnsi(composeModuleImportWarn(external))),
l.includes(stripAnsi(composeModuleImportWarn(external, issuer))),
),
).toBe(false);
}
Expand Down
Loading