From c9a1670a62050870b32bef3d7859e9ccae15d28b Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Mon, 27 Nov 2023 13:08:52 +0100 Subject: [PATCH 1/2] Only suppress warnings in Vite dev mode on client --- src/Parser.svelte | 2 +- src/supress-warnings.js | 35 +++++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/Parser.svelte b/src/Parser.svelte index 7a90b9d7..4ceaa379 100644 --- a/src/Parser.svelte +++ b/src/Parser.svelte @@ -8,7 +8,7 @@ export let ordered = false export let renderers - supressWarnings(); + supressWarnings(renderers); {#if !type} diff --git a/src/supress-warnings.js b/src/supress-warnings.js index 057103ab..4c892ea0 100644 --- a/src/supress-warnings.js +++ b/src/supress-warnings.js @@ -1,15 +1,34 @@ import { onMount } from 'svelte' -export function supressWarnings() { - const origWarn = console.warn +export function supressWarnings(renderers) { + // https://vitejs.dev/guide/env-and-mode.html + if (import.meta?.env?.DEV === false) return; // we're suppressing warnings that are only shown in dev mode - console.warn = (message) => { - if (message.includes('unknown prop')) return - if (message.includes('unexpected slot')) return - origWarn(message) - } + const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; + if (!isBrowser) return; // we don't want to change anything on the server, especially, because we only undo the change on the client (in onMount) + + const markdownComponentNames = Object.values(renderers) + .map(r => r?.name) + .filter(r => !!r) + .join('|'); + + const unknownPropsRegex = new RegExp(`<(${markdownComponentNames})(_[\w$]+)?> was created (with unknown|without expected) prop`); + const unexpectedSlotRegex = new RegExp(`<(${markdownComponentNames})(_[\w$]+)?> received an unexpected slot ["']default["']`); + + // Nasty hack to silence harmless warnings the user can do nothing about. SvelteKit does the same thing: + // https://github.com/sveltejs/kit/blob/976a8b80fb4fa9ac2e7938deb3ea248b2d54dfa1/packages/kit/src/runtime/client/client.js#L1557C1-L1571C2 + const origWarn = console.warn; + console.warn = (...args) => { + if ( + args.length === 1 && + (unknownPropsRegex.test(args[0]) || unexpectedSlotRegex.test(args[0])) + ) { + return; + } + origWarn(...args); + }; onMount(() => { console.warn = origWarn - }) + }); } From 4cc88c323ada463faa3eb29d165e38a94e441305 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Tue, 28 Nov 2023 12:10:02 +0100 Subject: [PATCH 2/2] chore: bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fda9d18f..c54945b6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "svelte-markdown", "description": "A markdown renderer for Svelte", - "version": "0.4.0", + "version": "0.4.1", "main": "dist/sveltemarkdown.js", "module": "dist/sveltemarkdown.es.js", "jsnext:main": "dist/sveltemarkdown.es.js",