@samitouri / QOS-React-2 / commits / dc603bb2fa

[babel] Add isDev flag and put logging behind it

Sathya Gunasekaran committed May 17, 2023 at 18:38 UTC dc603bb2fa505fc458ad5078aab7fb465c506f3f
5 files changed +24 -13
compiler/forget/packages/snap/src/compiler-worker.ts
+1
@@ -124,6 +124,7 @@ export async function compile(
124 logger: null,
125 gating,
126 panicOnBailout,
127 + isDev: true,
128 }).code;
129 } catch (e) {
130 error = e;
compiler/forget/src/Babel/BabelPlugin.ts
+18 -13
@@ -109,7 +109,9 @@ export default function ReactForgetBabelPlugin(
109 ) {
110 throw err;
111 } else {
112 - console.log(formatErrorsForConsole(err, pass.filename ?? null));
112 + if (pass.opts.isDev) {
113 + log(err, pass.filename ?? null);
114 + }
115 }
116 } finally {
117 // We are generating a new FunctionDeclaration node, so we must skip over it or this
@@ -150,7 +152,9 @@ export default function ReactForgetBabelPlugin(
152 if (options.panicOnBailout || error.isCritical()) {
153 throw error;
154 } else {
153 - console.log(formatErrorsForConsole(error, pass.filename));
155 + if (pass.opts.isDev) {
156 + log(error, pass.filename);
157 + }
158 }
159 return;
160 }
@@ -219,7 +223,9 @@ export default function ReactForgetBabelPlugin(
223 if (options.panicOnBailout || error.isCritical()) {
224 throw error;
225 } else {
222 - console.log(formatErrorsForConsole(error, pass.filename ?? null));
226 + if (options.isDev) {
227 + log(error, pass.filename ?? null);
228 + }
229 }
230 }
231
@@ -329,17 +335,16 @@ function shouldCompile(
335 return true;
336 }
337
332 -function formatErrorsForConsole(
333 - error: CompilerError,
334 - filename: string | null
335 -): string {
338 +function log(error: CompilerError, filename: string | null): void {
339 const filenameStr = filename ? `in ${filename}` : "";
337 - return error.details
338 - .map(
339 - (e) =>
340 - `[ReactForget] Skipping compilation of component ${filenameStr}: ${e.printErrorMessage()}`
341 - )
342 - .join("\n");
340 + console.log(
341 + error.details
342 + .map(
343 + (e) =>
344 + `[ReactForget] Skipping compilation of component ${filenameStr}: ${e.printErrorMessage()}`
345 + )
346 + .join("\n")
347 + );
348 }
349
350 function makeError(
compiler/forget/src/Babel/PluginOptions.ts
+3
@@ -51,6 +51,8 @@ export type PluginOptions = {
51 gating: GatingOptions | null;
52
53 panicOnBailout: boolean;
54 +
55 + isDev: boolean;
56 };
57
58 export type Logger = {
@@ -63,6 +65,7 @@ export const defaultOptions: PluginOptions = {
65 environment: null,
66 logger: null,
67 gating: null,
68 + isDev: false,
69 } as const;
70
71 export function parsePluginOptions(obj: unknown): PluginOptions {
compiler/forget/src/__tests__/compiler-test.ts
+1
@@ -63,6 +63,7 @@ describe("React Forget", () => {
63 logger: null,
64 gating: options.gating,
65 panicOnBailout: options.panicOnBailout,
66 + isDev: true,
67 }).code,
68 });
69 } catch (e) {
compiler/forget/src/__tests__/test-utils/generateTestsFromFixtures.ts
+1
@@ -141,6 +141,7 @@ export default function generateTestsFromFixtures(
141 gating,
142 language: parseLanguage(input),
143 panicOnBailout,
144 + isDev: true,
145 });
146 } else {
147 receivedOutput = "<<input deleted>>";