Skip to content

Commit

Permalink
Only suppress warnings in Vite dev mode on client
Browse files Browse the repository at this point in the history
  • Loading branch information
myieye committed Nov 28, 2023
1 parent 545e822 commit c9a1670
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Parser.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export let ordered = false
export let renderers
supressWarnings();
supressWarnings(renderers);
</script>

{#if !type}
Expand Down
35 changes: 27 additions & 8 deletions src/supress-warnings.js
Original file line number Diff line number Diff line change
@@ -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
})
});
}

0 comments on commit c9a1670

Please sign in to comment.