UnoCSS version
66.6.6
Describe the bug
When using @unocss/svelte-scoped/vite, classes in the safelist configuration are still being transformed/hashed if they are defined as shortcuts by presets.
For example, prose from @unocss/preset-typography is a shortcut. Even when added to safelist, it still gets transformed to a hashed class name like uno-xxxx.
Root Cause
In src/_preprocess/transformClasses/sortClassesIntoCategories.ts, the logic checks shortcuts before checking safelist:
for (const token of classes) {
if (!(isShortcut(token, uno.config.shortcuts) || await needsGenerated(token, uno))) {
ignore.push(token);
continue;
}
// ...
}
The needsGenerated function contains the safelist check:
async function needsGenerated(token, uno) {
if (uno.config.safelist.includes(token)) return false;
return !!await uno.parseToken(token);
}
However, since isShortcut() returns true for shortcuts like prose, the needsGenerated() function is never called due to short-circuit evaluation (true || anything = true).
Expected Behavior
Classes in safelist should never be transformed, regardless of whether they are shortcuts or not.
Suggested Fix
Check safelist before checking shortcuts:
for (const token of classes) {
// Check safelist first
if (uno.config.safelist.includes(token)) {
ignore.push(token);
continue;
}
if (!(isShortcut(token, uno.config.shortcuts) || await needsGenerated(token, uno))) {
ignore.push(token);
continue;
}
// ...
}
Reproduction
- Use
@unocss/svelte-scoped/vite with @unocss/preset-typography
- Add
prose to safelist
- Use
class="prose" in a Svelte component
- Observe that
prose is transformed to a hashed class name
Workaround
Currently using presetWind3 instead of presetWind4 seems to avoid this issue in some cases, or manually patching the node_modules file.
Environment
- UnoCSS version: 66.6.6
@unocss/svelte-scoped version: 66.6.6
@unocss/preset-typography version: 66.6.6
System Info
No response
Validations
UnoCSS version
66.6.6
Describe the bug
When using
@unocss/svelte-scoped/vite, classes in thesafelistconfiguration are still being transformed/hashed if they are defined as shortcuts by presets.For example,
prosefrom@unocss/preset-typographyis a shortcut. Even when added tosafelist, it still gets transformed to a hashed class name likeuno-xxxx.Root Cause
In
src/_preprocess/transformClasses/sortClassesIntoCategories.ts, the logic checks shortcuts before checking safelist:The
needsGeneratedfunction contains the safelist check:However, since
isShortcut()returnstruefor shortcuts likeprose, theneedsGenerated()function is never called due to short-circuit evaluation (true || anything = true).Expected Behavior
Classes in
safelistshould never be transformed, regardless of whether they are shortcuts or not.Suggested Fix
Check safelist before checking shortcuts:
Reproduction
@unocss/svelte-scoped/vitewith@unocss/preset-typographyproseto safelistclass="prose"in a Svelte componentproseis transformed to a hashed class nameWorkaround
Currently using
presetWind3instead ofpresetWind4seems to avoid this issue in some cases, or manually patching thenode_modulesfile.Environment
@unocss/svelte-scopedversion: 66.6.6@unocss/preset-typographyversion: 66.6.6System Info
No response
Validations