Skip to content
This repository was archived by the owner on Mar 15, 2021. It is now read-only.

Commit e475004

Browse files
committed
#3 Support for Source Maps
- add documentation
1 parent a870fa5 commit e475004

File tree

8 files changed

+268
-53
lines changed

8 files changed

+268
-53
lines changed

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,37 @@ The idea for the `NativeLessCompiler` class was based on the [lesscss-java](http
3232
library by [Marcel Overdijk](https://github.com/marceloverdijk).
3333

3434
# Usage
35-
How to compile a source file:
35+
The `LessCompiler` contains 14 methods. Below is an example of how to use some of them:
3636
```
37+
String cssCode = null;
38+
LessOptions options = null;
39+
3740
// create compiler
3841
LessCompiler compiler = new LessCompiler();
3942
4043
// compile source code
41-
String cssCode1 = compiler.compile(".basic { display: block; }");
44+
cssCode = compiler.compile(".basic { display: block; }");
45+
46+
// compile source code with custom options
47+
options = new LessOptionsBuilder().ieCompatibilityOff().build();
48+
cssCode = compiler.compile(".basic { display: block; }", options);
4249
4350
// compile source file
44-
String cssCode2 = compiler.compile(new File("/source.less"));
51+
cssCode = compiler.compile(new File("source.less"));
4552
4653
// compile source file and save CSS code in an output file
47-
compiler.compile(new File("/source.less"), new File("/output.css"));
54+
compiler.compile(new File("source.less"), new File("output.css"));
4855
4956
// compile source file and compress CSS code
50-
String compressedCssCode = compiler.compileAndCompress(new File("/source.less"));
57+
cssCode = compiler.compileAndCompress(new File("source.less"));
58+
59+
// compile source code and generate inline source map
60+
cssCode = compiler.compileWithInlineSourceMap(".basic { display: block; }", new LessOptions());
61+
62+
// compile source file and generate source map (save it in output.map file)
63+
options = new LessOptionsBuilder().sourceMapBasePath("basePath").build();
64+
compiler.compileWithSourceMap(new File("source.less"), new File("output.css"), new File("output.map"), options);
65+
66+
// compile source file and generate source map (save it in output.css.map file)
67+
compiler.compileWithSourceMap(new File("source.less"), new File("output.css"), options);
5168
```

src/main/java/biz/gabrys/lesscss/compiler2/LessCompiler.java

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,73 @@
2525
* <a href="https://github.com/less/less.js/releases/tag/v1.7.5">Less 1.7.5</a>.
2626
* </p>
2727
* <p>
28-
* How to use:
28+
* This class is a facade for the {@link NativeLessCompiler}. List of methods with a developer-friendly API:
29+
* </p>
30+
* <ul>
31+
* <li>{@link #compile(CharSequence)} - compiles a Less source code to a CSS code</li>
32+
* <li>{@link #compile(CharSequence, LessOptions)} - compiles a Less source code with custom configuration options to a
33+
* CSS code</li>
34+
* <li>{@link #compile(File)} - compiles a Less source file to a CSS code</li>
35+
* <li>{@link #compile(File, LessOptions)} - compiles a Less source file with custom configuration options to a CSS
36+
* code</li>
37+
* <li>{@link #compile(File, File)} - compiles a Less source file to a CSS code and saves it in an output file</li>
38+
* <li>{@link #compile(File, File, LessOptions)} - compiles a Less source file with custom configuration options and
39+
* saves result in an output file</li>
40+
* <li>{@link #compileAndCompress(CharSequence)} - compiles a Less source file to a compressed CSS code</li>
41+
* <li>{@link #compileAndCompress(File)} - compiles a Less source file to a compressed CSS code</li>
42+
* <li>{@link #compileAndCompress(File, File)} - compiles a Less source file to a compressed CSS code and saves it in an
43+
* output file</li>
44+
* <li>{@link #compileWithInlineSourceMap(CharSequence, LessOptions)} - compiles a Less source code with custom
45+
* configuration options to a CSS code with an inline Source Map</li>
46+
* <li>{@link #compileWithInlineSourceMap(File, File, LessOptions)} - compiles a Less source file with custom
47+
* configuration to a CSS code with an inline Source Map and saves it in an output file</li>
48+
* <li>{@link #compileWithInlineSourceMap(File, LessOptions)} - compiles a Less source file with custom configuration
49+
* options to a CSS code with an inline Source Map</li>
50+
* <li>{@link #compileWithSourceMap(File, File, LessOptions)} - compiles a Less source file with custom configuration to
51+
* a CSS code with a Source Map (Source Map file name is equal to the output file name with {@code map} extension)</li>
52+
* <li>{@link #compileWithSourceMap(File, File, File, LessOptions)} - compiles a Less source file with custom
53+
* configuration to a CSS code with a Source Map</li>
54+
* </ul>
55+
* <p>
56+
* Example code:
2957
* </p>
3058
*
3159
* <pre>
60+
* String cssCode = null;
61+
* {@link LessOptions} options = null;
62+
*
3263
* // create compiler
33-
* LessCompiler compiler = new LessCompiler();
34-
*
64+
* LessCompiler compiler = new {@link #LessCompiler() LessCompiler}();
65+
*
3566
* // compile source code
36-
* String cssCode1 = compiler.compile(".basic { display: block; }");
67+
* cssCode = compiler.{@link #compile(CharSequence) compile}(".basic { display: block; }");
68+
*
69+
* // compile source code with custom options
70+
* options = new {@link LessOptionsBuilder#LessOptionsBuilder() LessOptionsBuilder}().{@link LessOptionsBuilder#ieCompatibilityOff() ieCompatibilityOff}().{@link LessOptionsBuilder#build() build}();
71+
* cssCode = compiler.{@link #compile(CharSequence, LessOptions) compile}(".basic { display: block; }", options);
3772
*
3873
* // compile source file
39-
* String cssCode2 = compiler.compile(new File("/source.less"));
74+
* cssCode = compiler.{@link #compile(File) compile}(new File("source.less"));
4075
*
4176
* // compile source file and save CSS code in an output file
42-
* compiler.compile(new File("/source.less"), new File("/output.css"));
77+
* compiler.{@link #compile(File, File) compile}(new File("source.less"), new File("output.css"));
4378
*
4479
* // compile source file and compress CSS code
45-
* String compressedCssCode = compiler.compileAndCompress(new File("/source.less"));
80+
* cssCode = compiler.{@link #compileAndCompress(File) compileAndCompress}(new File("source.less"));
81+
*
82+
* // compile source code and generate inline source map
83+
* cssCode = compiler.{@link #compileWithInlineSourceMap(CharSequence, LessOptions) compileWithInlineSourceMap}(".basic { display: block; }", new {@link LessOptions#LessOptions() LessOptions}());
84+
*
85+
* // compile source file and generate source map (save it in output.map file)
86+
* options = new {@link LessOptionsBuilder#LessOptionsBuilder() LessOptionsBuilder}().{@link LessOptionsBuilder#sourceMapBasePath(CharSequence) sourceMapBasePath}("basePath").{@link LessOptionsBuilder#build() build}();
87+
* compiler.{@link #compileWithSourceMap(File, File, File, LessOptions) compileWithSourceMap}(new File("source.less"), new File("output.css"), new File("output.map"), options);
88+
*
89+
* // compile source file and generate source map (save it in output.css.map file)
90+
* compiler.{@link #compileWithSourceMap(File, File, LessOptions) compileWithSourceMap}(new File("source.less"), new File("output.css"), options);
4691
* </pre>
4792
*
4893
* @since 2.0.0
94+
* @see LessOptionsBuilder
4995
*/
5096
public class LessCompiler {
5197

@@ -365,7 +411,8 @@ public void compileWithInlineSourceMap(final File source, final File output, fin
365411

366412
/**
367413
* Compiles a Less source file with custom configuration to a CSS code with a Source Map. The CSS code will be saved
368-
* in an output file and the Source Map in a file with name equal to the output file name with map extension.
414+
* in an output file and the Source Map in a file with name equal to the output file name with {@code map}
415+
* extension.
369416
* @param source the source file (cannot be {@code null} and must exist).
370417
* @param output the output file (cannot be {@code null}).
371418
* @param options the configuration options (can be {@code null}).

src/main/java/biz/gabrys/lesscss/compiler2/LessOptions.java

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,47 @@
1717
import java.util.List;
1818

1919
/**
20+
* <p>
2021
* Represents <a href="http://lesscss.org/usage/index.html#command-line-usage-options">Less compiler options</a>
2122
* responsible for controlling the {@link LessCompiler} compilation process.
23+
* </p>
24+
* <p>
25+
* Base options:
26+
* </p>
27+
* <ul>
28+
* <li>{@link #isCompress() compress} - whether a CSS code should be compressed (default: {@code false})</li>
29+
* <li>{@link #isIeCompatibility() IE compatibility} - whether a CSS code should be compatible with Internet Explorer
30+
* browser (default: {@code true})</li>
31+
* <li>{@link #getIncludePaths() included paths} - available include paths (default: {@code empty collection})</li>
32+
* <li>{@link #isJavaScript() JavaScript} - whether a compiler should allow usage of JavaScript language (default:
33+
* {@code true})</li>
34+
* <li>{@link #getLineNumbers() line numbers} - whether a compiler should generate inline source-mapping (default:
35+
* {@link LineNumbersValue#OFF})</li>
36+
* <li>{@link #isRelativeUrls() relative URLs} - whether a compiler should rewrite relative URLs (default:
37+
* {@code false})</li>
38+
* <li>{@link #getRootPath() root path} - a path which will be added to every generated import and URL in CSS code
39+
* (default: {@code null})</li>
40+
* <li>{@link #isSilent() silent} - whether a compiler shouldn't log compilation warnings (default: {@code false})</li>
41+
* <li>{@link #isStrictImports() strict imports} - whether a compiler should disallow an @import operation inside of
42+
* either @media blocks or other selector blocks (default: {@code false})</li>
43+
* <li>{@link #isStrictMath() strict math} - whether a compiler should try and process all maths in Less code (default:
44+
* {@code false})</li>
45+
* <li>{@link #isStrictUnits() strict units} - whether a compiler should guess units in Less code when it does maths
46+
* (default: {@code false})</li>
47+
* </ul>
48+
* <p>
49+
* Source Map options:
50+
* </p>
51+
* <ul>
52+
* <li>{@link #getSourceMapBasePath() base path} - a path that will be removed from each of the Less file paths inside
53+
* the Source Map and also from the path to the map file specified in your output CSS (default: {@code null})</li>
54+
* <li>{@link #isSourceMapLessInline() Less inline} - whether a compiler should include all of the Less files in to the
55+
* Source Map (default: {@code false})</li>
56+
* <li>{@link #getSourceMapRootPath() root path} - a path that will be prepended to each of the Less file paths inside
57+
* the Source Map and also to the path to the map file specified in your output CSS (default: {@code null})</li>
58+
* <li>{@link #getSourceMapUrl() URL} - a path which will overwrite the URL in the CSS that points at the Source Map
59+
* file (default: {@code null})</li>
60+
* </ul>
2261
* @since 2.0.0
2362
* @see LessOptionsBuilder
2463
*/
@@ -139,20 +178,21 @@ public void setCompress(final boolean compress) {
139178
}
140179

141180
/**
142-
* Sets whether a CSS code should be compatible with IE browser (default: {@code true}). Used for the
181+
* Sets whether a CSS code should be compatible with Internet Explorer browser (default: {@code true}). Used for the
143182
* {@code data-uri} function to ensure that images aren't created that are too large for the browser to handle.
144-
* @return {@code true} whether the CSS code should be compatible with IE browser, otherwise {@code false}.
183+
* @return {@code true} whether the CSS code should be compatible with Internet Explorer browser, otherwise
184+
* {@code false}.
145185
* @since 2.0.0
146186
*/
147187
public boolean isIeCompatibility() {
148188
return ieCompatibility;
149189
}
150190

151191
/**
152-
* Sets whether a CSS code should be compatible with IE browser (default: {@code true}). Used for the
192+
* Sets whether a CSS code should be compatible with Internet Explorer browser (default: {@code true}). Used for the
153193
* {@code data-uri} function to ensure that images aren't created that are too large for the browser to handle.
154-
* @param ieCompatibility {@code true} whether the CSS code should be compatible with IE browser, otherwise
155-
* {@code false}.
194+
* @param ieCompatibility {@code true} whether the CSS code should be compatible with Internet Explorer browser,
195+
* otherwise {@code false}.
156196
* @since 2.0.0
157197
*/
158198
public void setIeCompatibility(final boolean ieCompatibility) {
@@ -607,7 +647,7 @@ public void setSourceMapLessInline(final boolean sourceMapLessInline) {
607647
}
608648

609649
/**
610-
* Returns a path which will overwrite the URL in the CSS that points at the Source Map map file (default:
650+
* Returns a path which will overwrite the URL in the CSS that points at the Source Map file (default:
611651
* {@code null}). This is for cases when the {@link #getSourceMapRootPath() root path} and the
612652
* {@link #getSourceMapBasePath() base path} options are not producing exactly what you need.
613653
* @return the Source Map URL.
@@ -620,8 +660,8 @@ public String getSourceMapUrl() {
620660
}
621661

622662
/**
623-
* Sets a path which will overwrite the URL in the CSS that points at the Source Map map file (default:
624-
* {@code null}). This is for cases when the {@link #setSourceMapRootPath(String) root path} and the
663+
* Sets a path which will overwrite the URL in the CSS that points at the Source Map file (default: {@code null}).
664+
* This is for cases when the {@link #setSourceMapRootPath(String) root path} and the
625665
* {@link #setSourceMapBasePath(String) base path} options are not producing exactly what you need.
626666
* @param sourceMapUrl the Source Map URL.
627667
* @since 2.0.0

src/main/java/biz/gabrys/lesscss/compiler2/LessOptionsBuilder.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,27 @@
2020
import biz.gabrys.lesscss.compiler2.util.StringUtils;
2121

2222
/**
23+
* <p>
2324
* Responsible for creating new instances of the {@link LessOptions}.
25+
* </p>
26+
* <p>
27+
* Example code:
28+
* </p>
29+
*
30+
* <pre>
31+
* {@link LessOptions} options = null;
32+
*
33+
* // create options with enabled code compression
34+
* options = new {@link #LessOptionsBuilder() LessOptionsBuilder}().{@link #compressOn() compressOn}().{@link #build() build}();
35+
*
36+
* // create options with enabled strict units and disabled IE compatibility
37+
* options = new {@link #LessOptionsBuilder() LessOptionsBuilder}().{@link #strictUnitsOn() strictUnitsOn}().{@link #ieCompatibilityOff() ieCompatibilityOff}().{@link #build() build}();
38+
*
39+
* // use existing options object to create a new one
40+
* boolean compression = false;
41+
* options = new {@link #LessOptionsBuilder(LessOptions) LessOptionsBuilder}(options).{@link #compress(boolean) compress}(compression).{@link #build() build}();
42+
* </pre>
43+
*
2444
* @since 2.0.0
2545
*/
2646
public class LessOptionsBuilder {

src/main/java/biz/gabrys/lesscss/compiler2/NativeLessCompiler.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,24 @@
4949
* <a href="https://github.com/marceloverdijk">Marcel Overdijk</a>.
5050
* </p>
5151
* <p>
52-
* How to use:
52+
* Example code:
5353
* </p>
5454
*
5555
* <pre>
5656
* // create compiler and source file
57-
* NativeLessCompiler compiler = new NativeLessCompiler();
57+
* NativeLessCompiler compiler = new {@link #NativeLessCompiler() NativeLessCompiler}();
5858
* File input = new File("/less/file.less");
5959
*
6060
* // compile source file
61-
* String cssCode = compiler.execute(new NativeLessOptionsBuilder().inputFile(input).create());
61+
* String cssCode = compiler.{@link #execute(Collection) execute}(new {@link NativeLessOptionsBuilder#NativeLessOptionsBuilder() NativeLessOptionsBuilder}().inputFile(input).build());
6262
*
63-
* // compile source file and compress CSS code
64-
* Collection&lt;Object&gt; options = new NativeLessOptionsBuilder().inputFile(input).compress(true).create();
65-
* String cssCompressedCode = compiler.execute(options);
63+
* // compile source file with enabled CSS code compression
64+
* Collection&lt;Object&gt; options = new {@link NativeLessOptionsBuilder#NativeLessOptionsBuilder() NativeLessOptionsBuilder}().inputFile(input).compress(true).build();
65+
* String cssCompressedCode = compiler.{@link #execute(Collection) execute}(options);
6666
* </pre>
6767
*
6868
* @since 2.0.0
69+
* @see NativeLessOptionsBuilder
6970
*/
7071
public class NativeLessCompiler {
7172

@@ -93,9 +94,10 @@ public NativeLessCompiler() {
9394

9495
/**
9596
* Executes the compiler. You can use standard
96-
* <a href="http://lesscss.org/usage/index.html#command-line-usage-options">Less options</a> with some exceptions:
97+
* <a href="http://lesscss.org/usage/index.html#command-line-usage-options">Less command line options</a> with some
98+
* exceptions:
9799
* <ul>
98-
* <li>use {@link #INCLUDE_PATHS_SEPARATOR} to separate paths in {@code --include-path"} option instead of ';'
100+
* <li>use {@link #INCLUDE_PATHS_SEPARATOR} to separate paths in {@code --include-path} option instead of ';'
99101
* character on Windows and ':' character on Unix/Linux machines</li>
100102
* <li>does not support options:
101103
* <ul>

0 commit comments

Comments
 (0)