Skip to content

safelist does not work for shortcuts in @unocss/svelte-scoped #5154

@cheungray123

Description

@cheungray123

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

  1. Use @unocss/svelte-scoped/vite with @unocss/preset-typography
  2. Add prose to safelist
  3. Use class="prose" in a Svelte component
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions