|
17 | 17 | import java.io.ByteArrayOutputStream; |
18 | 18 | import java.io.File; |
19 | 19 | import java.io.IOException; |
| 20 | +import java.io.InputStream; |
20 | 21 | import java.io.InputStreamReader; |
21 | 22 | import java.io.OutputStreamWriter; |
22 | 23 | import java.io.PrintStream; |
23 | 24 | import java.io.PrintWriter; |
| 25 | +import java.io.SequenceInputStream; |
24 | 26 | import java.net.URL; |
25 | 27 | import java.util.ArrayList; |
26 | 28 | import java.util.Arrays; |
@@ -68,7 +70,8 @@ public class LessCompiler { |
68 | 70 |
|
69 | 71 | private static final LessLogger logger = LessLoggerFactory.getLogger(LessCompiler.class); |
70 | 72 |
|
71 | | - private URL lessJs = LessCompiler.class.getClassLoader().getResource("META-INF/less-rhino-1.5.1.js"); |
| 73 | + private URL lessJs = LessCompiler.class.getClassLoader().getResource("META-INF/less-rhino-1.6.1.js"); |
| 74 | + private URL lesscJs = LessCompiler.class.getClassLoader().getResource("META-INF/lessc-rhino-1.6.1.js"); |
72 | 75 | private List<URL> customJs = Collections.emptyList(); |
73 | 76 | private List<String> options = Collections.emptyList(); |
74 | 77 | private Boolean compress = null; |
@@ -144,6 +147,28 @@ public synchronized void setLessJs(URL lessJs) { |
144 | 147 | this.lessJs = lessJs; |
145 | 148 | } |
146 | 149 |
|
| 150 | + /** |
| 151 | + * Returns the LESSC JavaScript file used by the compiler. |
| 152 | + * COMPILE_STRING |
| 153 | + * @return The LESSC JavaScript file used by the compiler. |
| 154 | + */ |
| 155 | + public URL getLesscJs() { |
| 156 | + return lesscJs; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Sets the LESSC JavaScript file used by the compiler. |
| 161 | + * Must be set before {@link #init()} is called. |
| 162 | + * |
| 163 | + * @param lesscJs LESSC JavaScript file used by the compiler. |
| 164 | + */ |
| 165 | + public synchronized void setLesscJs(URL lesscJs) { |
| 166 | + if (scope != null) { |
| 167 | + throw new IllegalStateException("This method can only be called before init()"); |
| 168 | + } |
| 169 | + this.lesscJs = lesscJs; |
| 170 | + } |
| 171 | + |
147 | 172 | /** |
148 | 173 | * Returns the custom JavaScript files used by the compiler. |
149 | 174 | * |
@@ -249,21 +274,24 @@ public synchronized void init() { |
249 | 274 | out = new ByteArrayOutputStream(); |
250 | 275 | global.setOut(new PrintStream(out)); |
251 | 276 |
|
252 | | - // Load the compiler into a function we can run |
253 | | - compiler = (Function) cx.compileReader(new InputStreamReader(lessJs.openConnection().getInputStream()), lessJs.toString(), 1, null); |
| 277 | + // Combine all of the streams (less, custom, lessc) into one big stream |
| 278 | + List<InputStream> streams = new ArrayList<InputStream>(); |
254 | 279 |
|
255 | | - List<URL> jsUrls = new ArrayList<URL>(); |
256 | | - jsUrls.addAll( customJs ); |
257 | | - |
258 | | - // load any custom JS |
259 | | - for(URL url : jsUrls) { |
260 | | - InputStreamReader inputStreamReader = new InputStreamReader(url.openConnection().getInputStream()); |
261 | | - try{ |
262 | | - cx.evaluateReader(scope, inputStreamReader, url.toString(), 1, null); |
263 | | - }finally{ |
264 | | - inputStreamReader.close(); |
265 | | - } |
| 280 | + // less should be first |
| 281 | + streams.add(lessJs.openConnection().getInputStream()); |
| 282 | + |
| 283 | + // then the custom js so it has a chance to add any hooks |
| 284 | + for(URL url : customJs) { |
| 285 | + streams.add(url.openConnection().getInputStream()); |
266 | 286 | } |
| 287 | + |
| 288 | + // then the lessc so we can do the compile |
| 289 | + streams.add(lesscJs.openConnection().getInputStream()); |
| 290 | + |
| 291 | + InputStreamReader reader = new InputStreamReader(new SequenceInputStream(Collections.enumeration(streams))); |
| 292 | + |
| 293 | + // Load the streams into a function we can run |
| 294 | + compiler = (Function) cx.compileReader(reader, lessJs.toString(), 1, null); |
267 | 295 |
|
268 | 296 | } |
269 | 297 | catch (Exception e) { |
|
0 commit comments