1.5.3 • Published 7 months ago

nuxt-lightningcss v1.5.3

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

nuxt-lightningcss

npm version npm downloads License Nuxt

Use the built-in lightningcss preprocessor of Vite with Nuxt.

Quick Setup

  1. Add nuxt-lightningcss dependency to your project
# Using pnpm
pnpm add -D nuxt-lightningcss

# Using yarn
yarn add --dev nuxt-lightningcss

# Using npm
npm install --save-dev nuxt-lightningcss
  1. Add nuxt-lightningcss to the modules section of nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    'nuxt-lightningcss'
  ]
})
  1. To get the proper syntax highlighting in your components, use lang="postcss" in your style tags:
<style lang="postcss">
  .a {
    color: black;

    .b {
      color: blue;
    }
  }
</style>

That's it! You can now use lightningcss in your Nuxt app ✨

Configuration

To configure lightningcss, add an options object either with the lightningcss key or in the modules array:

export default defineNuxtConfig({
  modules: [
    'nuxt-lightningcss'
  ],

  lightningcss: {
    // Options
  }
})
export default defineNuxtConfig({
  modules: [
    ['nuxt-lightningcss', {
      // Options
    }]
  ]
})

This module takes the same options as those from the lightningcss transform function, except for the following:

  • code
  • filename
  • projectRoot
  • analyzeDependencies
  • sourceMap
  • inputSourceMap
  • cssModules

It also provides these extra options:

  • globals: an array of stylesheet paths to import in all other stylesheets. This is especially useful when you want to transpile custom media queries.

Targets

The lightningcss targets option is automatically set from you project browserslist configuration (either in .browserslistrc, browserslist, package.json or in the BROWSERSLIST environment variable). If there isn't an explicit browserslist configuration in your project, the defaults preset will be used. This can also be overridden from the module options:

import { browserslistToTargets } from 'lightningcss'
import browserslist from 'browserslist'

export default defineNuxtConfig({
  modules: [
    'nuxt-lightningcss'
  ],

  lightningcss: {
    targets: browserslistToTargets(browserslist('> 0.5%, last 2 versions, Firefox ESR, not dead'))
  }
})

Custom media queries

Custom media queries can be transpiled using lightningcss, but since their actual definition is removed from the bundled stylesheets, they need to be imported in all stylesheets that use them. You can do that using the following configuration:

import { Features } from 'lightningcss'

export default defineNuxtConfig({
  modules: [
    'nuxt-lightningcss'
  ],

  lightningcss: {
    globals: [
      // Import your custom media queries in all stylesheets
      '~/assets/stylesheets/media-queries.css'
    ],
    include: Features.CustomMediaQueries,
    drafts: {
      customMedia: true
    }
  }
})

Minify

By default, stylesheets are minified in production using lightningcss. You can disable minification using the following configuration (always disabled in development mode):

import { Features } from 'lightningcss'

export default defineNuxtConfig({
  modules: [
    'nuxt-lightningcss'
  ],

  lightningcss: {
    minify: false
  }
})

Development

# Install dependencies
yarn install

# Generate type stubs
yarn dev:prepare

# Develop with the playground
yarn dev

# Build the playground
yarn dev:build

# Run ESLint
yarn lint

# Run Vitest
yarn test
yarn test:watch

# Release new version
yarn release
1.5.3

7 months ago

1.5.2

7 months ago

1.5.1

8 months ago

1.5.0

8 months ago

1.4.2

9 months ago

1.4.1

9 months ago

1.4.0

9 months ago

1.3.0

9 months ago

1.2.0

9 months ago

1.1.2

10 months ago

1.1.1

10 months ago

1.1.0

10 months ago

1.0.10

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago