How to use variables in different less files? - less

Let's say I separate a less files into many less files to be easy to organize.
Here is my repository:
/reset.less
/variables.less
/mixins.less
/main.less
/styles.less
The styles.less is just importing the other files:
#import "reset.less";
#import "mixins.less";
#import "variables.less";
#import "main.less";
However, when I add some codes into main.less and use the #line-color which is defined in the variables.less. It shows Name Error: variable #line-color is undefined and I cannot compile it- I use PHPStorm with less plugin.
Could you pleas suggest me?

You have to import your variables.less to all files which use your variables.
Edit:
You have to compile only your style.less. You cannot compile the main.less because it doesn't know the variables.less but you don't want a main.CSS anyway, do you?
You should get the correct style.css which is (I guess) the only css file you'll need.

I could solve it by doing the following in PHPstorm:
Open Preferences…
In Tools > File Watchers > Less (configure)
Check "track only root files"
Change the "Output paths to refresh" to ../css/$FileNameWithoutExtension$.css (added "../css/" in front - depending on what folder you'd like to write the css files into)
(At this point you may want to check if this already does the job to your liking. If it doesn't, carry on to the next step…)
In Other Settings > LESS Profiles > (Your profile) (configure)
Define "LESS source directory": /Users/macuser/htdocs/MySite/sites/all/themes/mytheme/less (depending on which folder contains the .less files)
Define "Include file by path": /Users/macuser/htdocs/MySite/sites/all/themes/mytheme/less/style.less (file name depending on which .less file compiles the other .less files)
Add "CSS output directory" by selecting the folder you wish to write the (minified) CSS files
Enable Compress CSS output if desired
Notice: If Compress CSS output is enabled, this means that the code will be compressed everytime you right-click a .less-file and hit "Compile to CSS". By default the output will not be compressed with every modification you make to the .less-file. If you do want to compress the CSS straight away,
Install less-plugin-clean-css using the following command in
Terminal: sudo npm install -g less-plugin-clean-css
Next, go to Tools > File Watchers > Less (configure), and change the "Arguments" to --clean-css --no-color $FileName$ (added "--clean-css" in front).
Now it will compile your CSS automatically while you're coding. You'll no longer need to compile manually.

Related

lessc Option --source-map-rootpath seems not to work

I use lessc 2.7.3. I generate css files via a makefile and use following paths
the makefile is in themes/bodensee
the css is generated in themes/bodensee/css
the less files are in themes/bodensee/less
the maps are in the same folder as the css files.
My problem is that css files misses the themes/bodensee path, so it raises a file not found on css.map files.
lessc -s less/wlb.less --clean-css="--s0 --advanced" --source-map-rootpath=themes/bodensee/ --source-map="css/wlb.css.map" css/wlb.css
The CSS file now contains `sourceMappingURL=css/wlb.css.map``The rootpath does not have any effect.
I also tried a fantasy rootpath and searched for it in the file - it does not appear anywhere. But the option is correct. When I try to missspell the option, LESS drops an error.
What am I missing?
Description of the --source-map-rootpath option from here
Specifies a rootpath that should be prepended to each of the less file paths inside the sourcemap and also to the path to the map file specified in your output css.
Because the basepath defaults to the directory of the input less file, the rootpath defaults to the path from the sourcemap output file to the base directory of the input less file.
Use this option if for instance you have a css file generated in the root on your web server but have your source less/css/map files in a different folder. So for the option above you might have
The problem was indeed related to the Clean-CSS plugin.
I now call
lessc --source-map --clean-css="--s0 --advanced" -s less/wlb.less css/wlb.css which is working.
There is a standalone clean-css program, but that does not generate sources for the Less files. It's not clear if the lessc plugin and the standalone tool are the same or different implementations but both use node.
The standalone cleancss tool removes the source map URL generated by lessc be default (did not play around with the dozens of options).
These Node tools develop very fast and manual/tutorials often are outdated. That's why my make file stopped working. Developers of that tools should really consider not to touch working parameters or features and to keep their code compatible.

WebStorm: How to trigger a Less compilation only on the main.less file while watching multiple files?

I have a .less files directory with:
main.less (just import the dependencies)
dep1.less
dep2.less
etc.
I want that modifying a dependency triggers the compilation of the main.less file ONLY.
For now here is my configuration in WebStorm Less watcher. What scope and arguments I need?
Thx to #LazyOne comments I manage to find the right settings.
set the watch scope to the entire folder
put ../$FileNameWithoutExtension$.css at the end of less command arguments
uncheck Create output file from stdout causing empty output file (don't know why)
check Track only root files. Which is not very explicit in my case. Indeed it does not really watch the root file but all the #import dependencies and ONLY COMPILE THE ROOT FILE.
If you prefix your partial files with an underscore
(_dep1.less,_dep2.less)
they wont be compiled. And that doesn't even effect how you call them: #import _dep1.less will import _dep1.less.

.css file not uploading to server after .less compile phpStorm 10

I have a main.less file that contains #import for a number of .less files that I compile into 1 main.css file.
My main.less file:
#import "imports/header";
#import "imports/content";
#import "imports/footer";
......
In phpstorm 10 I set up a file watcher to compile main.less into main.css. My deployment options are to upload changed files on explicit saves (command S)
On save, the main.less file gets compiled properly if I made a change to any .less file. Then all .less files that I made changes to get uploaded to the server. However, main.css does not. I have to manually right click it and upload to server.
I believe this is because phstorm looks for changes before the less is compiled, then compiles less and does not notice the changed main.css
My folder structure for my less is:
/stylesheets/
____/imports/
________/header.less
________/content.less
________/footer.less
________/ .........
____/main.less
____/main.css
My less file watcher settings are:
Name: Less
Description: Compiles .less files into .css files
Uncheck Immediate file synchronization
Check Track only root files
File Type: Less
Scope: Project Files
Program: /usr/local/bin/lessc
Argument: main.less
Working directory: $FileParentDir$/stylesheets/
Environment variables:
i. Name: PATH
ii. Value:
/Applications/MAMP/bin/php/php5.4.30/bin:/Users/xxx/.composer/vendor/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
Output paths to refresh: $FileParentDir$/stylesheets/main.css
How do I make it so my main.css uploads after main.less has been compiled?
You need to configure your deployment settings.
Project Settings | Deployment | Options, setting the Upload changed files automatically to the default server option to Always
And check option Upload external changes
Phpstorm will watch for changed scss and CSS files inside project directory and upload them.
https://confluence.jetbrains.com/display/PhpStorm/Sync+changes+and+automatic+upload+to+a+deployment+server+in+PhpStorm

LESSC to compile to a custom path using environment variable

Bottom line I need lessc to compile my main.less file to $CATALINA_EC_TMP/main.css
I'm working on this project, where I need to generate multiple output css files originating from the same source (LESS file) using LESSC.
So with Jet Brain's (WebStorm or IntelliJ Idea) File Watcher, I don't get much of options to save the output files to a custom path using an environment variable.
The reason why I use an environment variable is because some of the outputted files is in a temporary path (it changes whenever I deploy with ant)
That said ...
This is my Environment Variable:
$CATALINA_EC_TMP = '/foo/bar/'
and it's changing so in the next deployment, it won't be /foo/bar/ anymore.
and this is the command line that's being executed by my IDE to compile less files
/usr/local/bin/lessc --no-color main.less
I need lessc to compile my main.less file to $CATALINA_EC_TMP/main.css
so the resulting file would be in that case /foo/bar/main.css or wherever the $CATALINA_EC_TMP value is.
I hope that there's a solution to this, anyway if it doesn't exist I think I'll use fswatcher to copy my generated css files into my destinations whenever I compile.

yuicompressor and/or uglify-js does not work in PhpStorm

I have installed node package manager (npm) on my windows machine and then i ran the command "npm install less -g" and went on setting up the file watcher in PhpStorm for LESS. This worked very good. I also managed to get it to put the .css files in the css folder instead of in the less folder where I have my .less files.
My next step was to install yuicompressor so I ran the command "npm install yuicompressor -g". And then I set it up in PhpStorm by adding the yuicompressor for CSS, but it only creates empty .min.css files.
In the PhpStorm watcher settings I have set:
Program: "C:\Users\XXX\AppData\Roaming\npm\yuicompressor.cmd"
And ticked the box "Create output file from stdout"
I want to achieve this:
.less (project_root/less) -> .css (project_root/css) -> .min.css (project_root/css)
Is there any one else that has had the same problem, I can't seem to find any solution.
Btw, I also tried to install uglify-js, but with the same result, it only creates empy .min.js files.
Some extra info, my environment variables are as follows:
PATH (User) = C:\Users\XXX\AppData\Roaming\npm
Path (System) = C:\Program Files\nodejs\
I removed the "Create output file from stdout" (from the yui CSS file watcher and left it checked on the LESS file watcher) and it seemed to work. But I had to link to the yuicompressor.cmd and not the .jar file as mentioned in the links from LazyOne. I also had to uncheck the "Immediate file synchronization" on the LESS file watcher but needed it to be checked on the yui CSS file watcher.
Seems like its very tricky to get it to go from "LESS -> CSS -> CSS compressed" with only saving the .less file when editing, but with this setup it finally worked.