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

@stackstorm/postcss-less-engine

v0.6.2

Published

PostCSS plugin for integrating the popular Less CSS pre-processor into your PostCSS workflow

Downloads

2

Readme

A Complete Compile-LESS-to-CSS PostCSS plugin

Note: this plugin was mostly a proof-of-concept of converting ASTs and not something I'm actively maintaining. The best way to do this is probably just to use a tool like Gulp and pipe through gulp-less and then gulp-postcss.

This is a PostCSS custom parser plugin for integrating the popular Less.js CSS pre-processor into your PostCSS workflow! It integrates the entire Less engine, evaluates your .less, and exports a PostCSS AST that you can use to attach multiple subsequent PostCSS plugins.

Note

  • Because this uses the Less.js parser and not the default PostCSS processor, some parsing will be different. PostCSS accepts "broken" CSS, whereas Less doubles as a de facto CSS linter, and will return errors if your Less / CSS is poorly structured.
  • PostCSS will also sometimes "fix" CSS that uses property hacks, which Less preserves as the property name. As well, PostCSS will remove comments from within values, which are also kept in the value by Less (in most cases).
  • Less.js does not save "raws" when parsing. It also only preserves the start line / column of your source, which is still fine for Source Maps.
  • Important: The less() plugin needs to be the first PostCSS plugin called.

Example

Input example.less file

.add-bg-size(@size) {
	-webkit-background-size+: @size;
	background-size+: @size;
}

@default-size: 20px;

.box {
	.add-bg-size(@default-size (@default-size / 2));
	.add-bg-size(cover);
	width: calc(100% - 50px);
}

JavaScript

var less = require('postcss-less-engine');
var autoprefixer = require('autoprefixer');
var clean = require('postcss-clean');

var exampleLess = fs.readFileSync(path.join(__dirname, 'example.less'), 'utf8');

postcss([
    less({ strictMath: true }), 
    autoprefixer(), 
    clean()
  ])
  .process(exampleLess, { parser: less.parser, from: 'example.less' })
  .then(function (result) {
    console.log(result.css);
  }, function(err) {});

Output

.box{background-size:20px 10px,cover;width:calc(100% - 50px)}

Usage

Follow these simple steps to use postcss-less-engine.

Add postcss-less-engine to your build tool. (You must have Less.js and PostCSS installed as prerequisites.)

npm install postcss-less-engine --save-dev

Node

var less = require('postcss-less-engine');
less({ /* Less.js options */ }).process(YOUR_CSS, { parser: less.parser });

Load postcss-less-engine as a PostCSS plugin:

var less = require('postcss-less-engine');
postcss([
    less({ /* Less.js options */ })
]).process(YOUR_CSS, { parser: less.parser }).then(function (result) {
	// do something with result.css
});

Gulp

Add Gulp PostCSS to your build tool:

npm install gulp-postcss --save-dev

Enable postcss-less-engine within your Gulpfile:

var postcss = require('gulp-postcss');
var less = require('postcss-less-engine');

gulp.task('less', function () {
    return gulp.src('./css/src/style.less').pipe(
        postcss([
            less({ /* Less.js options */ })
        ], { parser: less.parser })
    ).pipe(
        gulp.dest('./css')
    );
});

Grunt

Add Grunt PostCSS to your build tool:

npm install postcss-less-engine --save-dev

Enable postcss-less-engine within your Gruntfile:

grunt.loadNpmTasks('grunt-postcss');

grunt.initConfig({
	postcss: {
		options: {
			parser: require('postcss-less-engine').parser,
			processors: [
				require('postcss-less-engine')({ /* Less.js options */ })
			]
		},
		dist: {
			src: 'css/*.css'
		}
	}
});

Misc

Similarly to postcss-import, the list of imported files can be viewed by assigning a function to the onImport key among the options:

less({
	/* other Less.js options */
	onImport: function(sources){
		console.log(sources)
	}
})

The received sources will be an array of strings, containing the absolute path to the files, which were imported, including the source file.