Skip to content

feat: custom devtools formatters #13070

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

Closed
wants to merge 6 commits into from
Closed
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
4 changes: 4 additions & 0 deletions packages/svelte/messages/client-warnings/warnings.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

> `%binding%` (%location%) is binding to a non-reactive property

## enable_custom_formatters

> We recommend enabling custom formatters for better results when logging `$state` objects — in your devtools, click the gear icon and check the 'Custom formatters' box

## event_handler_invalid

> %handler% should be a function. Did you mean to %suggestion%?
Expand Down
46 changes: 46 additions & 0 deletions packages/svelte/src/internal/client/dev/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { STATE_SYMBOL } from '../constants.js';
import { VERSION } from '../../../version.js';
import { snapshot } from '../../shared/clone.js';
import * as w from '../warnings.js';

export function install_custom_formatter() {
// Custom formatters are 'supported' in Firefox, but they're worse than useless.
// They're not supported in Firefox. We can maybe tweak this over time
var is_chrome = typeof navigator !== 'undefined' && navigator.userAgent.includes('Chrome');
var custom_formatters_enabled = false;

if (is_chrome) {
// @ts-expect-error
(window.devtoolsFormatters ??= []).push({
/**
* @param {any} object
* @param {any} config
*/
header(object, config) {
custom_formatters_enabled = true;

if (STATE_SYMBOL in object) {
return [
'div',
{},
['span', { style: 'font-weight: bold' }, '$state'],
['object', { object: snapshot(object), config }]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line is why it's not working in FF. The "object" type isn't supported it seems, so we'd have to manually do it with own own styled divs/spans?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Surprising that I learned about "object" from firefox-source-docs.mozilla.org of all places. I guess this is why Chrome has 174% market share and Firefox has -42%.

We could construct our own representation but it might be a challenge to make it look consistent with normal object logging across browsers and light/dark modes. We might have to settle for unstyled text if we attempt this.

];
}

return null;
},

hasBody: () => false
});
}

// the arguments need to include an object, so that we discover
// whether custom formatters are enabled
// eslint-disable-next-line no-console
console.log(`Running Svelte in development mode`, { version: VERSION });

if (is_chrome && !custom_formatters_enabled) {
w.enable_custom_formatters();
}
}
7 changes: 7 additions & 0 deletions packages/svelte/src/internal/client/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { DEV } from 'esm-env';
import { install_custom_formatter } from './dev/log.js';

export { FILENAME, HMR } from '../../constants.js';
export { add_locations } from './dev/elements.js';
export { hmr } from './dev/hmr.js';
Expand Down Expand Up @@ -164,3 +167,7 @@ export {
validate_void_dynamic_element
} from '../shared/validate.js';
export { strict_equals, equals } from './dev/equality.js';

if (DEV) {
install_custom_formatter();
}
12 changes: 12 additions & 0 deletions packages/svelte/src/internal/client/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ export function binding_property_non_reactive(binding, location) {
}
}

/**
* We recommend enabling custom formatters for better results when logging `$state` objects — in your devtools, click the gear icon and check the 'Custom formatters' box
*/
export function enable_custom_formatters() {
if (DEV) {
console.warn(`%c[svelte] enable_custom_formatters\n%cWe recommend enabling custom formatters for better results when logging \`$state\` objects — in your devtools, click the gear icon and check the 'Custom formatters' box`, bold, normal);
} else {
// TODO print a link to the documentation
console.warn("enable_custom_formatters");
}
}

/**
* %handler% should be a function. Did you mean to %suggestion%?
* @param {string} handler
Expand Down
Loading