-
-
Notifications
You must be signed in to change notification settings - Fork 780
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
feat(inspector): analyzer #2762
Conversation
✅ Deploy Preview for unocss ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
packages/core/src/utils/set.ts
Outdated
@@ -0,0 +1,66 @@ | |||
export class CountableSet<K> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about extending Set
, override the add
function to count, and introduce an additional function to get the counts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean something like this, right?
E.g:
export class CountableSet<K> extends Set {
_map = new Map<K, number>()
add(key: K) {
super.add(key)
this._map.set(key, (this._map.get(key) ?? 0) + 1)
return this
}
getCount(key: K) {
return this._map.get(key) ?? 0
}
}
But in this case Set's values with be duplicated as Map's keys. Not sure if it is a problem 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and we also need delete
method:
delete(key: K) {
super.delete(key)
return this._map.delete(key)
}
as well as clear()
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes :)
There is interesting problem with extending Uncaught TypeError: Cannot read properties of undefined (reading 'set') Reproduction: class CountableSet extends Set {
constructor(...args) {
super(...args);
this._map = new Map()
}
add(key) {
this._map.set(key, 1)
}
}
const mySet = new CountableSet(['foo', 'bar']);
// > Uncaught TypeError: Cannot read properties of undefined (reading 'set') Unfortunately class CountableSet extends Set {
constructor(...args) {
super(...args);
}
add(key) {
if (!this._map) this._map = new Map()
this._map.set(key, 1)
}
} But it is dirty workaround as for me. |
@antfu Maybe lets stick with the initial implementation of export class CountableSet<K> implements Set<K> {
// ... |
@antfu @zyyv Could you please share your thoughts on extending |
The design looks neat! Thanks. I will need some more time to review the changes to the core and how things are categorized |
Thanks! |
Agree, this is a simple but complicated work, (such as |
@antfu Done :) Added the popup which shows the list of modules where target utility/color/shortcut is used. |
@@ -176,7 +176,7 @@ function openEditor(id: string) { | |||
<sup op50 ml-0.5>{{ item.count }}</sup> | |||
</span> | |||
<template #popper> | |||
<ReuseDropdown :matched="item" :show-docs="false" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Linter converts showDocs
to show-docs
, but it doesn't work with ReusableTemplate, so renamed it to docs
as fast fix.
Thanks a lot, great work! Sorry for taking so long |
Thanks! Great news 🥳 |
@@ -4,6 +4,8 @@ export class CountableSet<K> extends Set<K> { | |||
constructor(values?: Iterable<K>) { | |||
super(values) | |||
this._map ??= new Map() | |||
for (const value of values ?? []) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@antfu Why do we need this loop? add()
method will be called twice for each value, because super()
already does in the parent constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because super does not add to _map
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it calls our add()
method which does this, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add some test and remove it if not necessary
I feel suggested shortcuts might not be as useful as I originally implemented in Windi, while adding a bunch of complexity. I guess it's better to define those shortcuts from a Design System rather that from the usages as well. |
Implements #2682
This PR adds utilities usage info to UnoCSS Inspector (under additional
Analyzer
tab), the same way it was done for WindiCSS.Tradeoffs:
Set
as main data structure to add/store tokens (in the core, presets, extractors etc.), newCountableSet
data structure was introduced. The main idea is that besides actual value it stores count number linked to the value, which increments each time the same value is added to theCountableSet
.It is based on the
Map
and has the same API asSet
, but with additionalgetCount
andsetCount
methods. Because of performance reasonsCountableSet
is opt-in and utilised only for Inspector needs.☝️ But currently there is a problem that extractors should also "know" about what data structure to use:
Set
orCountableSet
, which looks like breaking change.Alternative solution I see is to implement separate extractors and
generate
method only for Analyzer needs. But it feels like an overkill.Problems:
parseColor
util from@unocss/preset-mini/utils
- Inspector package should build afterpreset-mini
package, which currently is not the case. The fast workaround was to do this:I know it doesn't look good and for sure there is more correct solution 🙈
Also the design/icons is topic to discuss :)
I would be grateful for a review!