tailwindcss/integrations/vite
Robin Malfait 4bfacb33a0
Improve error messages when @apply fails (#18059)
This PR improves error messages when `@apply` fails. Right now it gives
you a generic error message that you cannot apply a certain utility.

```css
.foo {
  @apply bg-red-500;
}
```

Would result in:
```
Cannot apply unknown utility class: bg-red-500
```

However, there are some situations where we can give you more context
about what's happening.

### Missing `@import "tailwindcss"` or `@reference`

If you are in a Vue file for example, and you have the following code:
```vue
<template>
  <div class="foo"></div>
</template>

<style>
.foo {
  @apply bg-red-500;
}
</style>
```

Then this will now result in:
```
Cannot apply unknown utility class `bg-white`. Are you using CSS modules or similar and missing `@reference`? https://tailwindcss.com/docs/functions-and-directives#reference-directive
```

We do this by checking if we found a `@tailwind utilities` or
`@reference`. If not, we throw this more specific error.

### Explicitly excluded classes via `@source not inline('…')`

Or via the legacy `blocklist` from a JS config.

If you then have the following file:
```css
@import "tailwindcss";

@source not inline('bg-white');

.foo {
  @apply bg-white;
}
```

Then this will now result in:
```
Cannot apply utility class `bg-white` because it has been explicitly disabled: https://tailwindcss.com/docs/detecting-classes-in-source-files#explicitly-excluding-classes
```

We do this by checking if the class was marked as invalid.

### Applying unprefixed class in prefix mode

If you have the prefix option configured, but you are applying a
non-prefixed class, then we will show the following error:

Given this input:
```css
@import "tailwindcss" prefix(tw);

.foo {
  @apply underline;
}
```


The following error is thrown:
```
Cannot apply unprefixed utility class `underline`. Did you mean `tw:underline`?
```

### Applying known utilities with unknown variants

If you have unknown variants, then we will list them as well if the base
utility does compile correctly.

Given this input:

```css
@import "tailwindcss";

.foo {
  @apply hocus:hover:pocus:bg-red-500;
}
```

The following error is thrown:
```
Cannot apply utility class `hocus:hover:pocus:bg-red-500` because the `hocus` and `pocus` variants do not exist.
```

## Test plan

1. Everything behaves the same, but the error messages give more
details.
2. Updated tests with new error messages
3. Added new unit tests to verify the various scenarios
4. Added a Vue specific integration test with a `<style>…</style>` block
using `@apply`

[ci-all] There are some newlines here and there, let's verify that they
work identically on all platforms.

---------

Co-authored-by: Jonathan Reinink <jonathan@reinink.ca>
2025-05-27 20:19:14 +02:00
..
astro.test.ts Don't use color-mix(…) on currentColor (#17247) 2025-03-17 13:23:02 +01:00
config.test.ts Improve integration tests (stability + performance) (#15125) 2024-12-12 13:48:56 +01:00
css-modules.test.ts Upgrade lightningcss to 1.30.0 (#17979) 2025-05-12 15:49:18 +02:00
html-style-blocks.test.ts Vite: Transform <style> blocks in html files (#16069) 2025-01-31 14:26:45 +00:00
ignored-packages.test.ts Do not scan tailwind-merge sources for candidates (#16005) 2025-01-29 17:32:06 +01:00
index.test.ts Upgrade lightningcss to 1.30.0 (#17979) 2025-05-12 15:49:18 +02:00
multi-root.test.ts Make @reference emit variable fallbacks instead of CSS variable declarations (#16774) 2025-02-25 11:36:43 +01:00
nuxt.test.ts Fix integration tests for Windows (#16693) 2025-02-20 17:50:50 +01:00
other-transforms.test.ts Upgrade lightningcss to 1.30.0 (#17979) 2025-05-12 15:49:18 +02:00
qwik.test.ts Vite: Don't register the current CSS file as a dependency on itself (#17533) 2025-04-03 18:50:16 +02:00
react-router.test.ts Vite: Remove module-graph scanner (#16631) 2025-02-20 15:23:44 +01:00
resolvers.test.ts Upgrade lightningcss to 1.30.0 (#17979) 2025-05-12 15:49:18 +02:00
solidstart.test.ts Fix Vite issues with SolidStart (#16052) 2025-01-30 16:58:20 +01:00
source-maps.test.ts Upgrade lightningcss to 1.30.0 (#17979) 2025-05-12 15:49:18 +02:00
ssr.test.ts Vite: Remove module-graph scanner (#16631) 2025-02-20 15:23:44 +01:00
svelte.test.ts Improve compatibility with Safari 15 (#17435) 2025-04-01 13:33:22 +02:00
url-rewriting.test.ts Upgrade lightningcss to 1.30.0 (#17979) 2025-05-12 15:49:18 +02:00
virtual-modules.test.ts Vite: Don't crash with virtual module dependencies (#16780) 2025-02-25 11:29:58 +01:00
vue.test.ts Improve error messages when @apply fails (#18059) 2025-05-27 20:19:14 +02:00