1.3.0 • Published 6 years ago

less-vars-to-js v1.3.0

Weekly downloads
73,958
License
-
Repository
github
Last release
6 years ago

Build Status Coverage Status npm semantic-release Commitizen friendly

less-vars-to-js

Read LESS variables from the contents of a file and return them as a javascript object.

$ npm install --save less-vars-to-js

Why?

I wrote this to use in our living style guide where we document our colour palette, typography, grid, components etc. This allows variables to be visualised in the style guide without having to read through the code (useful for non-technical team members).

What does it do?

Takes in the contents of a less file as a string and returns an object containing all the variables it found. It is deliberately naive as it is not intending to be a less parser. Basically it reads anything starting with an @, so it will ignore comments, rule definitions, import statements etc.

Example :

@import (reference) "theme";

// Colour palette
@blue: #0d3880;
@pink: #e60278;

// Elements
@background: @gray;
@favourite: blanchedalmond;

// Grid
@row-height: 9;

.element {
  @foreground: black;
  color: @foreground;
}

Example output:

{
  "@blue": "#0d3880",
  "@pink": "#e60278",
  "@background": "@gray",
  "@favourite": "blanchedalmond",
  "@row-height": 9,
  "@foreground": "black"
}

Note: while it does return variables it finds within rules, it is recommended to use this on files containing only variables, as it's not a parser and is designed to extract design principles for style guides.

Options

OptionEffect
resolveVariablesResolves variables when they are defined in the same file.
dictionaryWhen resolveVariables is true, passes an object to use when the value cannot be resolved in the same file.
stripPrefixRemoves the @ or $ in the returned object keys.

Usage

import lessToJs from 'less-vars-to-js';
import fs from 'fs';

// Read the less file in as string
const paletteLess = fs.readFileSync('palette.less', 'utf8');

// Pass in file contents
const palette = lessToJs(paletteLess, {resolveVariables: true, stripPrefix: true});

// Use the variables (example React component)
export default class Palette extends Component {
  render() {
    return (
      <div>
      {
        Object.keys(palette)
          .forEach(colour => (
            <div style={{ backgroundColor: palette[colour] }}>
              <p>{ colour }</p>
              <p>{ palette[colour] }</p>
            </div>
          ))
      }
      </div>
    );
  }
}

License

MIT

k2.n3ogintonicautoml-templatespreadd-storybook@elasticpath/store-componentsjyswsdssdfdfdsafdtedsssswdswspcs-notif-and-msgued-componentsfpasquet-react-scripts@jackerwang/xzgui@ywjt/mpa-builder@ywjt/spa-builder@ywjt/spa-m-buildernav-styled-component-theme@askme-inc/ant-design-vueseek-asia-style-guidejt-feedback@mrbatman/craco-antdcraco-antd-pluginoa-react-scripts@everything-registry/sub-chunk-2060boxfusion-test-modulequizzlistmodulequizzmodulewolf-uiwdxantdsafetag-nextscii-designschedio-react-scriptstemplate-scriptssparsh-storybookmete-clilatejs-applegao-reactless-vars-loaderless-vars2jsgoodwill_disease_projectliukx_lib_7.3less2css-theme-loadermuappgdjw-components-mobilehzero-cli-ui-themeindependenceaustralia-design-librarygulp-less-variables-to-filegulp-less-vars-to-jsguodimy-component-testlynn-bootlwj-base-frontentlwj-business-frontendlwj-easy-report-frontendlwj-base-frontendlwj-front-core-toolspp-buildpp-deploypubgpublic-poll-storybookoem-libraryng-yunzai-plugin-themeng-stamy-plugin-themenmtsmartformsdkng-alain-plugin-themepcs-message-and-notificationpelago-react-scriptsreact-scripts-antd-custom-abcreact-sotefront-components-testnext-demo-yflnextjs-antdqsqqsq-ui-reactreact-antd-scriptsreact-scripts-pelagostylelint-vars-checkstylelint-vars-check-lesssteer-react-scriptsruxdumi-ui-themeumi-ui-theme-sudoku356-devtheme-color-generatortheme-color-webpack-plugin@amitrahav/antd-scss-theme-plugin@apployees-nx/webserver@atbtech/antd-scss-theme-plugin@ayaleil/components@bixi-react/themes@btri-ui/theme5htp@codewaseem/antd-scss-theme-plugin@brandexponents/ui-componentsyjy-stroeyjy-todoweaver-mobile-pagets_xinputusd-boiler-plate@fle-cli/react-script@infinitebrahmanuniverse/nolb-les@loia5tqd001/antd-scss-theme-plugin@microfe/cli@mgcrea/vite-plugin-ant-design@mypaas/hcm-cli
1.3.0

6 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.2

8 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago