Less

Less compilation with functions and mixins

Less

Packem compiles .less files through the Less loader in @visulima/rollup-plugin-css. The loader forwards its options directly to the Less compiler.

Overview

This example demonstrates:

  • Compiling Less with variables, mixins, and functions
  • Registering the Less loader in packem.config.ts
  • Passing Less compiler options

Setup

The Less loader requires the less package.

npm install --save-dev less

Directory Structure

my-package/
├── src/
│   ├── index.ts
│   └── styles.less
├── package.json
└── packem.config.ts

Source Files

src/styles.less

@primary-color: #007bff;
@border-radius: 0.25rem;

.button-style(@bg-color) {
  background-color: @bg-color;
  color: white;
  padding: 0.5rem 1rem;
  border: none;
  border-radius: @border-radius;
  cursor: pointer;

  &:hover {
    background-color: darken(@bg-color, 10%);
  }
}

.btn-primary {
  .button-style(@primary-color);
}

src/index.ts

import './styles.less'

export function createButton() {
  const button = document.createElement('button')
  button.className = 'btn-primary'
  button.textContent = 'Click me'
  return button
}

Configuration

Register the Less loader by importing it and adding it to rollup.css.loaders. The less option accepts the standard Less compiler options (Less.Options).

import { defineConfig } from '@visulima/packem/config'
import transformer from '@visulima/packem/transformer/esbuild'
import lessLoader from '@visulima/packem/css/loader/less'

export default defineConfig({
  transformer,
  rollup: {
    css: {
      mode: 'extract',
      loaders: [lessLoader],
      less: {
        math: 'parens-division',
        paths: ['node_modules', 'src/styles'],
      },
    },
  },
})

The less option maps to Less.Options. See the Less.js options documentation for the full list, including math, paths, globalVars, and plugins.

Building

packem build
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues