Home

Awesome

sbt-less

Build Status

Allows less to be used from within sbt. Builds on com.github.sbt:js-engine in order to execute the less compiler along with the scripts to verify. js-engine enables high performance linting given parallelism and native JS engine execution.

To use this plugin use the addSbtPlugin command within your project's plugins.sbt (or as a global setting) i.e.:

addSbtPlugin("com.github.sbt" % "sbt-less" % "2.0.0")

Your project's build file also needs to enable sbt-web plugins. For example with build.sbt:

lazy val root = (project in file(".")).enablePlugins(SbtWeb)

The compiler allows most of the same options to be specified as the lessc CLI itself. Here are the options:

OptionDescription
cleancssCompress output using clean-css.
cleancssOptionsPass an option to clean css, using CLI arguments from https://github.com/GoalSmashers/clean-css .
colorWhether LESS output should be colorised
compressCompress output by removing some whitespaces.
globalVariablesVariables that will be placed at the top of the less file.
ieCompatDo IE compatibility checks.
insecureAllow imports from insecure https hosts.
maxLineLenMaximum line length.
modifyVariablesModifies a variable already declared in the file.
optimizationSet the parser's optimization level.
relativeImportsRe-write import paths relative to the base less file. Default is true.
relativeUrlsRe-write relative urls to the base less file.
rootpathSet rootpath for url rewriting in relative imports and urls.
silentSuppress output of error messages.
sourceMapOutputs a v3 sourcemap.
sourceMapFileInlineWhether the source map should be embedded in the output file
sourceMapLessInlineWhether to embed the less code in the source map
sourceMapRootpathAdds this path onto the sourcemap filename and less file paths.
strictImportsWhether imports should be strict.
strictMathRequires brackets. This option may default to true and be removed in future.
strictUnitsWhether all unit should be strict, or if mixed units are allowed.
urlArgsAdds params into url tokens (e.g. 42, cb=42 or 'a=1&b=2').
verboseBe verbose.

The following sbt code illustrates how compression can be enabled:

Assets / LessKeys.compress := true

By default only main.less is looked for given that the LESS compiler must be explicitly fed the files that are required for compilation. Beyond just main.less, you can use an expression in your build.sbt like the following:

Assets / LessKeys.less / includeFilter := "foo.less" | "bar.less"

...where both foo.less and bar.less will be considered for the LESS compiler.

Alternatively you may want a more general expression to exclude LESS files that are not considered targets for the compiler. Quite commonly, LESS files are divided up into those entry point files and other files, with the latter set intended for importing into the entry point files. These other files tend not to be suitable for the compiler in isolation as they can depend on the global declarations made by other non-imported LESS files. For example, you may have a convention where any LESS file starting with an _ should not be considered for direct compilation. To include all .less files but exclude any beginning with an _ you can use the following declaration:

Assets / LessKeys.less / includeFilter := "*.less"

Assets / LessKeys.less / excludeFilter := "_*.less"