Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only suppress warnings in Vite dev mode and on client #82

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
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
})
});
}
Loading