Skip to content

Commit

Permalink
fix: log compiler option (#1009)
Browse files Browse the repository at this point in the history
There was a confusing behavior caused by the strange logic in the compiler
logging. It is possible to turn off the logging only if the `log` option has
been set to a truthy value, which is not equal to "info", because of the
condition `!ctx.options.log || ctx.options.log === 'info'`. However, at the same
time, the type of the `log` option is either `boolean | 'info' | undefined`.
I think that this PR will make the logic of the `log` option clearer.
  • Loading branch information
rmnilin authored May 19, 2024
1 parent 7ff650b commit 746e00f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
3 changes: 1 addition & 2 deletions packages/compiler/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,11 @@ function shouldTransform(
: 0.1;

if (improvement <= threshold) return false;
if (!ctx.options.log || ctx.options.log === 'info') {
if (ctx.options.log === true || ctx.options.log === 'info') {
logImprovement(
name,
improvement,
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
!ctx.options.log || ctx.options.log === 'info',
ctx.options.telemetry,
);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/compiler/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export interface Options extends Omit<CompilerOptions, 'telemetry'> {

// eslint-disable-next-line @typescript-eslint/default-param-last
export const unplugin = createUnplugin((options: Options = {}, meta) => {
if (!options.log) {
options.log = true;
}

const filter = createFilter(
options.filter?.include || DEFAULT_INCLUDE,
options.filter?.exclude || DEFAULT_EXCLUDE,
Expand Down
16 changes: 7 additions & 9 deletions packages/compiler/utils/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export const catchError = (
export const logImprovement = (
component: string,
improvement: number,
stdout: boolean,
telemetry: MillionTelemetry,
): void => {
void telemetry.record({
Expand All @@ -125,14 +124,13 @@ export const logImprovement = (
const improvementFormatted = isFinite(improvement)
? (improvement * 100).toFixed(0)
: '∞';
if (stdout) {
// eslint-disable-next-line no-console
console.log(
`${magenta(' ⚡ ')}${yellow(`<${component}>`)} now renders ${green(
underline(`~${improvementFormatted}%`),
)} faster`,
);
}

// eslint-disable-next-line no-console
console.log(
`${magenta(' ⚡ ')}${yellow(`<${component}>`)} now renders ${green(
underline(`~${improvementFormatted}%`),
)} faster`,
);
};

export const logIgnore = (component: string): void => {
Expand Down

0 comments on commit 746e00f

Please sign in to comment.