Retour
Publié le Sep 6, 2024

Configure Nuxt ESLint with UnoCSS

If you're using UnoCSS with Nuxt, and you're a good person, then you're using the Nuxt ESLint module to maintain clean and consistent code. It can be beneficial to use the 4 ESLint rules provided by UnoCSS:

  • @unocss/order Enforce a specific order for class selectors.
  • @unocss/order-attributify Enforce a specific order for attributify selectors.
  • @unocss/blocklist Disallow specific class selectors. Optional.
  • @unocss/enforce-class-compile Enforce class compile. Optional.

Here's a proposed ESLint configuration using the @nuxt/eslint1 and @unocss/eslint-config2 packages:

// eslint.config.mjs
import unocss from '@unocss/eslint-config/flat'
import withNuxt from './.nuxt/eslint.config.mjs'

export default withNuxt(
  unocss,
  {
    rules: {
      // Your custom rules here, for example:
      'no-console': ['warn'],
      'vue/block-order': ['warn', {
        order: ['script', 'template', 'style'],
      }],
    },
  },
)

To use the .mjs format, make sure you're using Eslint >= v9.0.03 4 and configure your package.json with "type": "module" to enable ECMAScript modules5.

// package.json
{
  "type": "module",
  // [...]
}

If you add the // ts-check comment on the first line of the file, you will get a TypeScript error. This will likely be fixed in a future version (both modules are still in v0.x.x at the moment).

Good to know: The Nuxt Eslint module includes ESLint Config Inspector6 and displays it directly in Nuxt DevTools. Handy for tweaking your configuration!

Footnotes

  1. 1. Nuxt Eslint module
  2. 2. UnoCSS ESlint Config module
  3. 3. Migrate to Eslint v9.x
  4. 4. Migrate to ESLint Flat config
  5. 5. Node.js - Enable ECMAScript modules
  6. 6. ESLint Config Inspector