Sublime Text 2 Plugin to compile less files to css on save
Sublime Text 2 and 3 Plugin to compile less files to css on save. Requires lessc installed on PATH.
NB This plugin requires lessc to be in your execution path
minify=falseand
disableVerbose=truewithin your less2css settings. If you do not do this compiling of less files will not work.
Either clone into your sublime packages directory, or just use Package Control
Less2Css requires lessc to compile less to css.
Install less
npm install less -gd
Optional: To use minification you will need a minifier. Install less-plugin-clean-css or similar.
npm install -g less-plugin-clean-css
Optional: To use autoprefix. Install less-plugin-autoprefix.
npm install -g less-plugin-autoprefix
less2css can be configured on two levels. There are the user settings which you can access through
Preferences\Package Settings\less2css. These are your global settings. Below you will find a description for all the various settings. The second level where you can configure less2css is at the project level. If you have a Sublime Text project file, it has the extension
.sublime-project, you can override your user settings for just that project. This will be described at the end of this chapter.
The allowed values are
trueand
false. When this setting is set to
truethe plugin will compile your LESS file each time you save it (otherwise you can compile from Sublime's "Tools" > "Less > Css" > "Compile this file to css" menu item).
When
truea css source map will be generated.
This option allows for the disabling of the verbose option which is used by default. The only time you might want / need to turn this off is then using the ruby gem less (which does not support --verbose).
This folder is only used when compiling all LESS files at once through Tools \ Less>Css \ Compile all less in less base directory to css. This can be an absolute path or a relative path. A relative path is useful when your projects always use the same structure, like a folder named
lessto keep all your LESS files in. When compiling all files at once it will also process all subfolders under the base folder.
This setting can be used to specify a different compiler. When it is left empty the default compiler, named lessc, will be used.
The allowed values are
trueand
false. When this setting is
truethe plugin will not compile files whose file name start with an underscore (
_) when:
autoCompileset to
true
You can still compile the file through Tools \ Less>Css \ Compile this less file to css or the appropriate shortcut.
When you specify a main file only this file will get compiled when you save any LESS file. This is especially useful if you have one LESS file which imports all your other LESS files. Please note that this setting is only used when compiling a single LESS file and not when compiling all LESS files in the LESS base folder through Tools \ Less>Css \ Compile all less in less base directory to css. Note: If your main file imports other files, the other files must be in the same folder for less2css to automatically compile your main file when editing an imported file.
Default: True The allowed values are
true/
falseor a string which passes a minification option to lessc (e.g.
--clean-css). When this setting is set to
truethe LESS compiler will be instructed to create a minified CSS file. The recommended less minifier is
npm install -g less-plugin-clean-csswhich is a required dependancy when
minify=true.
When the minify setting is set to
truethe LESS compiler will add
.minto the created minified version of the css files:
default.lesswould be compiled to the minified
default.min.css.
Use this setting to specify the folder where the CSS files will be placed. The following values are supported:
Empty string or
./Use an empty string or
./to have the CSS file stored in the same folder as the LESS file.
Absolute path Specify an absolute path to the directory where the CSS file should be stored, eg.
/home/user/projects/site/assets/css
Relative path Specify a partial path to the directory where the CSS should be stored, eg.
./css. This will store the CSS files in a folder CSS in the root of the project.
autosetting
If you set the
outputDirto
auto, the plugin will try to automatically determine the folder where the CSS should be compiled to. It works best when you compile a single css file that imports other CSS files. If you work with multiple CSS files within one project that get compiled seperately, consider using the
shadowsetting instead.
The
autosetting recognizes the following project setups:
When your LESS files are stored directly in a folder called
css\less(and not in any subfolders) the compiled CSS files will be placed in the
cssfolder.
[project] |- [css] |---- [less] |-------- site.less
Will result in the following after compilation:
[project] |- [css] |---- [less] |-------- site.less |---- site.css
When your LESS files are stored in a folder called
lessand its parent folder has a subfolder named
cssthe compiled CSS files will be placed in the
cssfolder.
[project] |- [css] |- [less] |---- site.less
Will result in the following after compilation:
[project] |- [css] |---- site.css |- [less] |---- site.less
shadowsetting
Use this setting to compile a complete
lessfolder into a shadow
cssfolder. It is expected your LESS files are stored in a folder named
less. Within this folder you are free to create any number of subfolder to organise your LESS files. When you compile a single file or all files through the menu command, the string
lesswill be replaced with
cssin the file path. For example, if you have this file structure:
[project] |- [less] |---- [global] |-------- global.less |---- [elements] |-------- header.less |---- site.less
It will generate the same structure, only with css as its root folder like:
[project] |- [css] |---- [global] |-------- global.css |---- [elements] |-------- header.css |---- site.css |- [less] |---- [global] |-------- global.less |---- [elements] |-------- header.less |---- site.less
When you specify an output file, this will be the file name used to compile all LESS files to. The content of the file will be overwritten after each compile. When you build all LESS file in the LESS base folder through Tools \ Less>Css \ Compile all less in less base directory to css you will only have the CSS of the last compiled file! Assign an empty string to have each LESS file compiled to its CSS counterpart, ie: site.less will become site.css.
Set to
trueto see parse errors in a pop up window
Set to
trueto add prefixes to css after conversion from less
Set to
trueto stop any warnings from being shown.
You can use the configuration settings that are described above and apply them to just the project you are working on. In order to do this you need to manually alter the
.sublime-projectfile. A default project file looks like this:
{ "folders": [ { "path": "You can add the less2css settings like this:
{ "folders": [ { "path": "Now the user settings
autoCompileandminifywill be overriden by the project setting.