How to use less.js with node.js to watch .less files in a folder?
- thv
- 2012-02-23 21:11
- 7
I am looking for a simple guide to setting up server-side watching and compiling .less files in a folder using less.js and node.js. Bryan wrote about it in this post.
Unfortunately, the instructions to Server-side usage on lesscss.org are of a little avail to somebody who is new to node.js.
I have tried command line usage: $ lessc styles.less > styles.css
.
I have also tried 'watchr' and 'watch-less' and it all works. However, I am looking for a pure node.js + less.js solution. I am sure there is somebody who will be able to plainly explain how to configure node.js and less.js to watch .less files in a folder. Thanks.
7 Answers
Honestly, I have seen a lot of the same threads and answers.
If you are like me and don't want any more code bloat than you already have then just download a LESS app for Win/Mac/Nix and it will auto-compile every time you save your .less files.
I honestly think this would put less of a load on the server because you only need to upload your compiled and minified files at this point.
less-watch-compiler, Dead Simple LESS CSS Watch Compiler A command that watches folders (and subfolders) for file changes and automatically compile the less css files into css. This is a file system watcher and compiler. It also keep track of a dependency tree and recompiles the parent if an imported (child) LESS file is changed. Dead Simple LESS CSS Watch Compiler A command that watches folders (and subfolders) for file changes and automatically compile the less css files into css. This is a file system watcher and compiler. It also keep track of a dependency tree and recompiles the parent if an imported (child) LESS file is changed.
The Nobility
2012-11-24 13:42
I wrote something that could be what you are looking for. The script will look for .less files in the watch folder and compile them into the output folder. All you need to do is specify the folder you want to watch and the output folder.
node less-watch-compiler.js FOLDER_TO_WATCH FOLDER_TO_OUTPUT
https://github.com/jonycheung/Dead-Simple-LESS-Watch-Compiler
It skips watching files with names that starts with underscore or a period.
I wrote this for my development process but it should still work on a server. However, you may want to re-consider watching a folder for changes constantly on a live server. It's an expensive operation.
less compiler windows, LESS.js, the original LESS compiler, by Alexis Sellier; Node.js; Featured Showcase: A popular adult dating social networking platform has recently used LESS to spruce up their log in page. Our website has seen a huge spike in traffic as well because of it. LESS.js, the original LESS compiler, by Alexis Sellier; Node.js; Featured Showcase: A popular adult dating social networking platform has recently used LESS to spruce up their log in page. Our website has seen a huge spike in traffic as well because of it.
jonycheung
2012-09-20 21:10
You can use supervisor with -e argument to provide extensions to watch:
nohup supervisor -e 'js|ejs|less' $APP_DIR/app.js 1>$APP_DIR/log/app.log 2>&1 & echo $! > $APP_DIR/pid/app.pid;
Taken from node-startup script: https://github.com/chovy/node-startup
The simpler version of this command is:
supervisor -e 'js|ejs|less' app.js
how to import (less file in css), If you just want to import a CSS-File as a Reference (e.g. to use the classes in Mixins) but not include the whole CSS file in the result you can use @import (less,reference) "reference.css";: my.less. @import (less,reference) "reference.css"; .my-class{ background-color:black; .reference-class; color:blue; } reference.css If you just want to import a CSS-File as a Reference (e.g. to use the classes in Mixins) but not include the whole CSS file in the result you can use @import (less,reference) "reference.css";: my.less. @import (less,reference) "reference.css"; .my-class{ background-color:black; .reference-class; color:blue; } reference.css
chovy
2012-11-09 07:03
I know this is an old post, but came up a few times when I was recently searching for the same thing.
I have forked and updated the code from jonycheung - and have submitted a merge request.
Update: jonycheung has accepted the merge request so his original code is now updated
In the mean time, the code can be found here:
https://github.com/mikestreety/Dead-Simple-LESS-Watch-Compiler
I have also been working on and developing a pure bash script which uses lessc, but does not require a node script to run:
https://github.com/mikestreety/less-watch
yarn add less, Lion Brand Yarn Vanna's Palettes Iconic 865-206 1 Pack Mini Novelty Yarn. Details. 1. $37.55. 1. Lion Brand Wool Ease Yarn - 10/Pk-Grey Heather - Grey you can buy Lion Brand Yarn Vanna's Palettes Iconic 865-206 1 Pack Mini Novelty Yarn. Details. 1. $37.55. 1. Lion Brand Wool Ease Yarn - 10/Pk-Grey Heather - Grey you can buy
mikestreety
2014-01-02 15:02
Maybe not direct answer. But here is my comment:
Usually we need this watch during the development phase, where we need to change the less file and see the result without the need of re-running the lessc
command by every change in less file.
We do this by pure less.js
in the in the our html. as below:
<link rel="stylesheet/less" href="/path/to/bootstrap.less"> <script src="/path/to/less.js"></script>
After the finalizing the development task. You need to run lessc
only once.
During development your pages may take a liitle more time to load, But if your site is ajax base, and you add this to your template page, it will not be a big deal.
less cdn, Less (which stands for Leaner Style Sheets) is a backwards-compatible language extension for CSS. This is the official documentation for Less, the language and Less.js, the JavaScript tool that converts your Less styles to CSS styles. Because Less looks just like CSS, learning it is a breeze. Less (which stands for Leaner Style Sheets) is a backwards-compatible language extension for CSS. This is the official documentation for Less, the language and Less.js, the JavaScript tool that converts your Less styles to CSS styles. Because Less looks just like CSS, learning it is a breeze.
Alireza Fattahi
2014-01-15 19:59
You can use less-watch-compiler
to watch for .less
files. You don't need a main file to watch just like grunt. Just install this plugin:
npm install -g less-watch-compiler
or
yarn global add less-watch-compiler
Usage:
less-watch-compiler [options] <source_dir> <destination_dir>
Basic example
root └──src │ └── main.less │ └── aux.less └──dist └── main.css
The project can be compiled with the following command:
less-watch-compiler src dist main.less
Basic Example 2
less-watch-compiler.config.json
{ "watchFolder": "src", "outputFolder": "dist", "mainFile": "main.less" }
The project can be compiled with the following command:
less-watch-compiler
less-watch-compiler github, A command that watches folders (and subfolders) for file changes and automatically compile the less css files into css. This is a file system watcher and compiler. It also keep track of a dependency tree and recompiles the parent if an imported (child) LESS file is changed. A command that watches folders (and subfolders) for file changes and automatically compile the less css files into css. This is a file system watcher and compiler. It also keep track of a dependency tree and recompiles the parent if an imported (child) LESS file is changed.
Ilyas karim
2018-06-23 17:33
I have created demo project to show how it works. You can find it here https://github.com/corpix/watcherDemo
After clone enter project directory and execute
npm install
to install less Node.js module. Try runningindex.js
and change.less
file intoless_files/
directory, modified file will be compiled and placed intocss_files/
How to use less.js with node.js to watch .less files in a folder?, Less runs on both the server-side (with Node.js and Rhino) or client-side (modern browsers only). Compile .less files to .css using the command line. Heads up! This will install the latest official version of lessc in your project folder, also adding it to the Then AFTER the less.js file is included, call less.watch() , like this: I have created demo project to show how it works. You can find it here https://github.com/corpix/watcherDemo. After clone enter project directory
Lebovski
2012-02-24 02:16