npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ima/cli-plugin-less-constants

v2.1.2

Published

Plugin for @ima/cli to enable sharing constants between LESS and JS source files.

Downloads

8

Readme

@ima/cli-plugin-less-constants

Adds preprocessor which converts theme values defined in the JS file, to their LESS variable counterparts.

Can be used to share theme variables between JS and LESS files or even multiple npm packages to allow for easier overrides.

Installation

npm install @ima/cli-plugin-less-constants -D

Usage

// ./ima.config.js
const { LessConstantsPlugin } = require('@ima/cli-plugin-less-constants');

/**
 * @type import('@ima/cli').ImaConfig
 */
module.exports = {
  plugins: [
    new LessConstantsPlugin({
      entry: './app/config/theme.js'
    })
  ],
};

Create theme.js file with constants definitions

Then export your LESS JS constants from the provided entry file, using the available units helper functions, imported from the CLI plugin:

// ./app/config/theme.js
import { units, media } from '@ima/cli-plugin-less-constants/units';

export default {
  bodyfontSize: units.rem(1),
  headerHeight: units.px(120),
  bodyWidth: units.vw(100),
  greaterThanMobile: media.maxWidthMedia(360, 'screen'),
  zIndexes: units.lessMap({
    header: 100,
    footer: 200,
    body: 1,
  }),
};

This produces the following output:

// ./build/less-constants/constants.less
@bodyfont-size: 1rem;
@header-height: 120px;
@body-width: 100vw;
@greater-than-mobile: ~"screen and (max-width: 360)";
@z-indexes: {
  header: 100;
  footer: 200;
  body: 1;
}

Import generated constants.less in globals

Finally don't forget to import the generated ./build/less-constants/constants.less file in your ./app/less/globals.less to have the variables available in all LESS files automatically without explicit import.

// ./app/less/globals.less
@import "../../build/less-constants/constants.less";

You can verify that your constants are actually used in your Less files. The verify option should include all directories that contain your Less files.

// ./ima.config.js
const { LessConstantsPlugin } = require('@ima/cli-plugin-less-constants');

/**
 * @type import('@ima/cli').ImaConfig
 */
module.exports = {
  plugins: [
    new LessConstantsPlugin({
      entry: './app/config/theme.js'
      verify: ['./app']
    })
  ],
};

Usage in JavaScript

Since every unit returns Unit object, you can always access it's value through the .valueOf() method or use the CSS interpreted value by calling .toString().

import { headerHeight } from 'app/config/theme.js';

export default function ThemeComponent({ children, title, href }) {
  return (
    <div>
      Header height has an absolute value of: {headerHeight.valueOf()} {/* 120 */},
      while it's CSS value is: {headerHeight.toString()} {/* 120px */}
    </div>
  );
}

Info: The constants are generated only in the preProcess which runs just ones before the compilation. So make sure to restart the built manually, when you add any new constants, to allow for the regeneration of the constants.less file.

Options

new LessConstantsPlugin(options: {
  entry: string;
  output?: string;
});

entry

string

Path to the LESS constants JS file.

output

string

Optional custom output path, defaults to ./build/less-constants/constants.less.

Units

The plugin provides unit functions for almost every unit available + some other helpers. Each helper returns Unit object with following interface:

export interface Unit {
  valueOf: () => string;
  toString: () => string;
}
  • Numeric values - em, ex, ch, rem, lh, rlh, vw, vh, vmin, vmax, vb, vi, svw, svh, lvw, lvh, dvw, dvh, cm, mm, Q, inches, pc, pt, px, percent.
  • Color values - hex, rgb, rgba, hsl, hsla.
  • Media queries - maxWidthMedia, minWidthMedia, minAndMaxWidthMedia, maxHeightMedia, minHeightMedia.
  • LESS map helper - lessMap can be used to group together similar values in an "object-like" value.

Custom units

If you're missing any additional helpers, you can always define your own, either custom ones (as long as they adhere to the Unit interface) or you can use the following helper:

import { asUnit } from '@ima/cli-plugin-less-constants/units';

function asUnit(
  unit: string,
  parts: (string | number)[],
  template = '${parts}${unit}'
): Unit {
  return {
    __propertyDeclaration: true,

    valueOf(): string {
      return parts.length === 1 ? parts[0].toString() : this.toString();
    },

    toString(): string {
      return template
        .replace('${parts}', parts.join(','))
        .replace('${unit}', unit);
    },
  };
}

For more information, take a look at the IMA.js documentation.