8000 feat(lit): make disabling dev mode warnings simpler by maxpatiiuk · Pull Request #4901 · lit/lit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(lit): make disabling dev mode warnings simpler #4901

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

Merged
merged 8 commits into from
Mar 13, 2025

Conversation

maxpatiiuk
Copy link
Contributor

Makes it much easier to work around #4877.
By adding queueMicrotask, the warnings are no longer emitted in the global scope, which makes disabling them much easier (see referenced issue for details).

With this change, I am able to add code for disabling this warning to my runtime code - with that, the warning is be disabled for all consumers of my library without them having to do anything special in their test setup files.

The warning is still enabled by default in vanilla Lit for those who prefer it.

The original discussion was only about the "Lit is in dev mode" warning, but I made this change for other global scope warnings too for consistency.

Testing

Verified that after this change I am able to silence Lit's dev mode warning regardless of the order of imports.

8000
Copy link
changeset-bot bot commented Jan 21, 2025

🦋 Changeset detected

Latest commit: 62f3191

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@lit/reactive-element Minor
lit-element Minor
lit-html Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@maxpatiiuk
Copy link
Contributor Author

cc @sorvell for review

@sorvell sorvell requested review from sorvell and removed request for kevinpschaaf February 3, 2025 14:18
Copy link
Member
@sorvell sorvell left a comment

Choose a reason for hiding this comment

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

We need to add a test for this. To facilitate this, let's consider adding to the issueWarning tracker the code for the warning in addition to the warning itself.

Then I hope you could do something like:

window.litIssuedWarnings ?? = new Set();
window.litIssuedWarnings.add(`dev-mode`);

@maxpatiiuk
Copy link
Contributor Author

Done - though it won't work fully till lit/lit.dev#1350 is in place (which requires Lit team to update the dev website)
Until then, I will add fallback code in issuedWarnings to prevent empty warning code from disabling warnings for disperate empty code warning messages.


Also, I noticed that reactive-element already disables warnings based on a code (which does not match the warning code):

(this.constructor as typeof ReactiveElement).enabledWarnings!.includes(
'migration'
) &&
attrValue === undefined
) {
issueWarning(
'undefined-attribute-value',

@@ -97,15 +97,17 @@ let issueWarning: (code: string, warning: string) => void;
if (DEV_MODE) {
// Ensure warnings are issued only 1x, even if multiple versions of Lit
// are loaded.
const issuedWarnings: Set<string | undefined> =
(globalThis.litIssuedWarnings ??= new Set());
globalThis.litIssuedWarnings ??= new Set();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

some of your tests are re-assigning globalThis.litIssuedWarnings.
to better support that use case, rather than having local issuedWarnings variable, always check what is the current globalThis.litIssuedWarnings value

console.warn(warning);
issuedWarnings.add(warning);
globalThis.litIssuedWarnings!.add(warning);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

While user now has the ability to disable warnings based on warning code, the issueWarning itself still only disables the warnings it emitted using warning content rather than code.
This is because, in case of reactive-element, the same warning message may be emitted once for each tag name (rather than once for entire app) - that seems like a useful behavior.

`The attribute value for the ${name as string} property is ` +
`undefined on element ${this.localName}. The attribute will be ` +
`removed, but in the previous version of \`ReactiveElement\`, ` +
`the attribute would not have changed.`

Comment on lines +15 to +16
globalThis.litIssuedWarnings ??= new Set();
globalThis.litIssuedWarnings!.add('dev-mode');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

dev-mode warning is now delayed by a microtask so we can disable the warning here (even after lit-element is already imported)

However, we can't do so inside of test/suiteSetup as they run later than a microtask.
Not an issue for my use case as disabling warnings in the global scope is now easy (so my library can do so for all consumers).

@maxpatiiuk maxpatiiuk requested a review from sorvell February 3, 2025 18:13
@maxpatiiuk maxpatiiuk requested a review from sorvell February 10, 2025 22:41
@maxpatiiuk
Copy link
Contributor Author

Thanks for the review! Applied all suggested fixes

@maxpatiiuk
Copy link
Contributor Author

Merge conflict resolved

@sorvell sorvell merged commit c916040 into lit:main Mar 13, 2025
7 checks passed
@lit-robot lit-robot mentioned this pull request Apr 10, 2025
jcfranco pushed a commit to Esri/calcite-design-system that referenced this pull request May 21, 2025
**Related Issue:** #10038

## Summary

1. Don't log calcite version message in tests as it adds noise in tests
and can even cause tests to fail (see #10038 for details)
2. Delay emission of a console message until a microtask to give users
more time to disable the console message. This is the exact same change
I implemented in Lit: lit/lit#4901 - see that pr
and associated issue for reasoning
- In case of calcite, the console message can be disabled by running
`(globalThis as { calciteConfig?: { version?: string } }).calciteConfig
??= { version: " " };` before the microtask fires
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0