From 0667de8f41e6b4d6bf4646a504a57d6bef2cc809 Mon Sep 17 00:00:00 2001 From: Arno Gourdol Date: Wed, 15 Feb 2023 17:22:08 -0800 Subject: [PATCH] feat: always display console errors; include asserts and other console log in debug builds only --- scripts/build.mjs | 7 ++++++- src/addons/auto-render.ts | 5 ++++- src/core/fonts.ts | 2 +- src/editor-mathfield/mathfield-private.ts | 4 ++-- src/editor-model/model-private.ts | 2 +- src/editor/speech-read-aloud.ts | 18 ++++++++++++------ src/editor/speech.ts | 14 +++++++++----- src/editor/virtual-keyboard-utils.ts | 15 +++++++++++---- src/public/mathfield-element.ts | 4 ++-- src/public/mathlive-ssr.ts | 2 +- 10 files changed, 49 insertions(+), 24 deletions(-) diff --git a/scripts/build.mjs b/scripts/build.mjs index 61c45422c..ca21b285c 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -22,7 +22,6 @@ const BUILD_OPTIONS = { SDK_VERSION: JSON.stringify(SDK_VERSION), GIT_VERSION: JSON.stringify(process.env.GIT_VERSION || '?.?.?'), }, - drop: ['debugger', 'console'], plugins: [less({ compress: true })], loader: { '.ts': 'ts' }, sourcemap: !PRODUCTION, @@ -50,6 +49,8 @@ build({ build({ ...BUILD_OPTIONS, + drop: ['debugger'], + pure: ['console.assert', 'console.log'], entryPoints: ['./src/mathlive.ts'], outfile: './dist/mathlive.min.mjs', format: 'esm', @@ -59,6 +60,8 @@ build({ build({ ...BUILD_OPTIONS, entryPoints: ['./src/mathlive.ts'], + drop: ['debugger'], + pure: ['console.assert', 'console.log'], outfile: './dist/mathlive.min.js', format: 'iife', globalName: 'MathLive', @@ -68,6 +71,8 @@ build({ build({ ...BUILD_OPTIONS, entryPoints: ['./src/public/mathlive-ssr.ts'], + drop: ['debugger'], + pure: ['console.assert', 'console.log'], outfile: './dist/mathlive-ssr.min.mjs', format: 'esm', minify: true, diff --git a/src/addons/auto-render.ts b/src/addons/auto-render.ts index 4ef41358e..0057c7ecd 100755 --- a/src/addons/auto-render.ts +++ b/src/addons/auto-render.ts @@ -241,7 +241,10 @@ function createMathMLNode( ''; span.innerHTML = options.createHTML ? options.createHTML(html) : html; } catch (error: unknown) { - console.error("Could not convert '" + latex + "' to MathML with ", error); + console.error( + "MathLive: Could not convert '" + latex + "' to MathML with ", + error + ); span.textContent = latex; } diff --git a/src/core/fonts.ts b/src/core/fonts.ts index 6f6784fdd..a3f478340 100644 --- a/src/core/fonts.ts +++ b/src/core/fonts.ts @@ -103,7 +103,7 @@ export async function loadFonts(fontsDirectory?: string): Promise { loadedFonts.forEach((font) => document.fonts.add(font)); } catch (error: unknown) { console.error( - `The mathlive fonts could not be loaded from "${fontsFolder}"`, + `The MathLive fonts could not be loaded from "${fontsFolder}"`, { cause: error } ); document.body.classList.add('ML__fonts-did-not-load'); diff --git a/src/editor-mathfield/mathfield-private.ts b/src/editor-mathfield/mathfield-private.ts index b74bc2da4..5655ee3d5 100644 --- a/src/editor-mathfield/mathfield-private.ts +++ b/src/editor-mathfield/mathfield-private.ts @@ -645,7 +645,7 @@ export class MathfieldPrivate implements GlobalContext, Mathfield { if (ComputeEngineCtor) this._computeEngine = new ComputeEngineCtor(); else { console.error( - 'The CortexJS Compute Engine library is not available.\nLoad the library, for example with:\nimport "https://unpkg.com/@cortex-js/compute-engine?module"' + 'MathLive: The CortexJS Compute Engine library is not available.\nLoad the library, for example with:\nimport "https://unpkg.com/@cortex-js/compute-engine?module"' ); } if (this._computeEngine && this.options.decimalSeparator === ',') @@ -680,7 +680,7 @@ export class MathfieldPrivate implements GlobalContext, Mathfield { if (errors.length > 0) { console.error( - 'invalid keybindings for current keyboard layout', + 'MathLive: Invalid keybindings for current keyboard layout', errors ); } diff --git a/src/editor-model/model-private.ts b/src/editor-model/model-private.ts index b79b65f59..f133b4463 100644 --- a/src/editor-model/model-private.ts +++ b/src/editor-model/model-private.ts @@ -437,7 +437,7 @@ export class ModelPrivate implements Model { if (format === 'ascii-math') return atomToAsciiMath(atom); - console.warn('Unknown format :', format); + console.error('MathLive: Unknown format :', format); return ''; } diff --git a/src/editor/speech-read-aloud.ts b/src/editor/speech-read-aloud.ts index cff6bf265..4a2dd5dca 100644 --- a/src/editor/speech-read-aloud.ts +++ b/src/editor/speech-read-aloud.ts @@ -55,14 +55,16 @@ export function defaultReadAloudHook( config ??= globalMathLive().config; if (config.speechEngine !== 'amazon') { - console.warn('Use Amazon TTS Engine for synchronized highlighting'); + console.error( + 'MathLive: Use Amazon TTS Engine for synchronized highlighting' + ); if (config.speakHook) config.speakHook(text, config); return; } if (!globalThis.AWS) { - console.warn( - 'AWS SDK not loaded. See https://www.npmjs.com/package/aws-sdk' + console.error( + 'MathLive: AWS SDK not loaded. See https://www.npmjs.com/package/aws-sdk' ); return; } @@ -83,7 +85,11 @@ export function defaultReadAloudHook( // Request the mark points polly.synthesizeSpeech(parameters, (err, data) => { if (err) { - console.warn('polly.synthesizeSpeech() error:', err, err.stack); + console.error( + 'MathLive: polly.synthesizeSpeech() error:', + err, + err.stack + ); return; } @@ -109,8 +115,8 @@ export function defaultReadAloudHook( parameters.SpeechMarkTypes = []; polly.synthesizeSpeech(parameters, (err, data) => { if (err) { - console.warn( - 'polly.synthesizeSpeech(', + console.error( + 'MathLive: polly.synthesizeSpeech(', text, ') error:', err, diff --git a/src/editor/speech.ts b/src/editor/speech.ts index f20c5e4ff..4f7fa1f33 100644 --- a/src/editor/speech.ts +++ b/src/editor/speech.ts @@ -194,8 +194,8 @@ export function defaultSpeakHook( window.speechSynthesis.speak(utterance); } else if (config.speechEngine === 'amazon') { if (!('AWS' in window)) { - console.warn( - 'AWS SDK not loaded. See https://www.npmjs.com/package/aws-sdk' + console.error( + 'MathLive: AWS SDK not loaded. See https://www.npmjs.com/package/aws-sdk' ); } else { const polly = new globalThis.AWS.Polly({ apiVersion: '2016-06-10' }); @@ -225,7 +225,11 @@ export function defaultSpeakHook( // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Polly.html#synthesizeSpeech-property polly.synthesizeSpeech(parameters, (err, data) => { if (err) - console.warn('polly.synthesizeSpeech() error:', err, err.stack); + console.error( + 'MathLive: polly.synthesizeSpeech() error:', + err, + err.stack + ); // Announce('plonk'); else if (data?.AudioStream) { const uInt8Array = new Uint8Array(data.AudioStream); @@ -242,8 +246,8 @@ export function defaultSpeakHook( // Can call AWS.Request() on the result of synthesizeSpeech() } } else if (config.speechEngine === 'google') { - console.warn( - 'The Google speech engine is not supported yet. Please come again.' + console.error( + 'MathLive: The Google speech engine is not supported yet. Please come again.' ); // @todo: implement support for Google Text-to-Speech API, // using config.speechEngineToken, config.speechEngineVoice and diff --git a/src/editor/virtual-keyboard-utils.ts b/src/editor/virtual-keyboard-utils.ts index 70f07c9a0..6337e7676 100644 --- a/src/editor/virtual-keyboard-utils.ts +++ b/src/editor/virtual-keyboard-utils.ts @@ -1244,7 +1244,7 @@ function makeKeyboardToolbar( }; for (const keyboard of keyboardList) { if (!keyboards[keyboard]) { - console.error('Unknown virtual keyboard "', keyboard, '"'); + console.error('MathLive: Unknown virtual keyboard "', keyboard, '"'); break; } @@ -1561,7 +1561,8 @@ function expandLayerMarkup( let keys = layout[attributes.name] as string; if (!keys) keys = ROWS.qwerty[attributes.name]; - if (!keys) console.warn('Unknown roman keyboard row:', attributes.name); + if (!keys) + console.error('MathLive: Unknown roman keyboard row:', attributes.name); else { for (const c of keys) { let cls: string = attributes.class ?? ''; @@ -1856,7 +1857,9 @@ export function makeKeyboardElement( const keyboardList = keyboardIDs.replace(/\s+/g, ' ').split(' '); for (const keyboardName of keyboardList) { if (!keyboards[keyboardName]) { - console.error('Unknown virtual keyboard "' + keyboardName + '"'); + console.error( + 'MathLive: Unknown virtual keyboard "' + keyboardName + '"' + ); continue; } @@ -1870,7 +1873,11 @@ export function makeKeyboardElement( for (const layerName of keyboardLayers) { if (!layers[layerName]) { - console.error('Unknown virtual keyboard layer: "', layerName, '"'); + console.error( + 'MathLive: Unknown virtual keyboard layer: "', + layerName, + '"' + ); break; } diff --git a/src/public/mathfield-element.ts b/src/public/mathfield-element.ts index d17f6d5d6..63f7d2c2f 100644 --- a/src/public/mathfield-element.ts +++ b/src/public/mathfield-element.ts @@ -777,7 +777,7 @@ import 'https://unpkg.com/@cortex-js/compute-engine?module'; if (!this._mathfield) return undefined; if (!globalThis[Symbol.for('io.cortexjs.compute-engine')]) { console.error( - 'The CortexJS Compute Engine library is not available.\nLoad the library, for example with:\nimport "https://unpkg.com/@cortex-js/compute-engine?module"' + 'MathLive: The CortexJS Compute Engine library is not available.\nLoad the library, for example with:\nimport "https://unpkg.com/@cortex-js/compute-engine?module"' ); } return this._mathfield.expression; @@ -790,7 +790,7 @@ import 'https://unpkg.com/@cortex-js/compute-engine?module'; if (!globalThis[Symbol.for('io.cortexjs.compute-engine')]) { console.error( - 'The CortexJS Compute Engine library is not available.\nLoad the library, for example with:\nimport "https://unpkg.com/@cortex-js/compute-engine?module"' + 'MathLive: The CortexJS Compute Engine library is not available.\nLoad the library, for example with:\nimport "https://unpkg.com/@cortex-js/compute-engine?module"' ); } } diff --git a/src/public/mathlive-ssr.ts b/src/public/mathlive-ssr.ts index 39ca44a7d..8110bb028 100644 --- a/src/public/mathlive-ssr.ts +++ b/src/public/mathlive-ssr.ts @@ -195,7 +195,7 @@ export function serializeMathJsonToLatex(json: Expression): string { if (ComputeEngineCtor) gComputeEngine = new ComputeEngineCtor(); else { console.error( - 'The CortexJS Compute Engine library is not available.\nLoad the library, for example with:\nimport "https://unpkg.com/@cortex-js/compute-engine?module"' + 'MathLive: The CortexJS Compute Engine library is not available.\nLoad the library, for example with:\nimport "https://unpkg.com/@cortex-js/compute-engine?module"' ); } }