[babel] Add isDev flag and put logging behind it

This commit is contained in:
Sathya Gunasekaran
2023-05-17 18:38:08 +01:00
parent 2a80ba544e
commit dc603bb2fa
5 changed files with 24 additions and 13 deletions
@@ -124,6 +124,7 @@ export async function compile(
logger: null,
gating,
panicOnBailout,
isDev: true,
}).code;
} catch (e) {
error = e;
+18 -13
View File
@@ -109,7 +109,9 @@ export default function ReactForgetBabelPlugin(
) {
throw err;
} else {
console.log(formatErrorsForConsole(err, pass.filename ?? null));
if (pass.opts.isDev) {
log(err, pass.filename ?? null);
}
}
} finally {
// We are generating a new FunctionDeclaration node, so we must skip over it or this
@@ -150,7 +152,9 @@ export default function ReactForgetBabelPlugin(
if (options.panicOnBailout || error.isCritical()) {
throw error;
} else {
console.log(formatErrorsForConsole(error, pass.filename));
if (pass.opts.isDev) {
log(error, pass.filename);
}
}
return;
}
@@ -219,7 +223,9 @@ export default function ReactForgetBabelPlugin(
if (options.panicOnBailout || error.isCritical()) {
throw error;
} else {
console.log(formatErrorsForConsole(error, pass.filename ?? null));
if (options.isDev) {
log(error, pass.filename ?? null);
}
}
}
@@ -329,17 +335,16 @@ function shouldCompile(
return true;
}
function formatErrorsForConsole(
error: CompilerError,
filename: string | null
): string {
function log(error: CompilerError, filename: string | null): void {
const filenameStr = filename ? `in ${filename}` : "";
return error.details
.map(
(e) =>
`[ReactForget] Skipping compilation of component ${filenameStr}: ${e.printErrorMessage()}`
)
.join("\n");
console.log(
error.details
.map(
(e) =>
`[ReactForget] Skipping compilation of component ${filenameStr}: ${e.printErrorMessage()}`
)
.join("\n")
);
}
function makeError(
@@ -51,6 +51,8 @@ export type PluginOptions = {
gating: GatingOptions | null;
panicOnBailout: boolean;
isDev: boolean;
};
export type Logger = {
@@ -63,6 +65,7 @@ export const defaultOptions: PluginOptions = {
environment: null,
logger: null,
gating: null,
isDev: false,
} as const;
export function parsePluginOptions(obj: unknown): PluginOptions {
@@ -63,6 +63,7 @@ describe("React Forget", () => {
logger: null,
gating: options.gating,
panicOnBailout: options.panicOnBailout,
isDev: true,
}).code,
});
} catch (e) {
@@ -141,6 +141,7 @@ export default function generateTestsFromFixtures(
gating,
language: parseLanguage(input),
panicOnBailout,
isDev: true,
});
} else {
receivedOutput = "<<input deleted>>";