use of io.bit3.jsass.Output in project mycore by MyCoRe-Org.
the class MCRSassCompilerManager method compile.
/**
* Just compiles a file and fills all maps.
*
* @param name the name of the file (with .min.css or .css ending)
* @return the compiled css
* @throws CompilationException
*/
private String compile(String name, List<Importer> importer) throws CompilationException, IOException {
Options options = new Options();
Collection<Importer> importerList = options.getImporters();
importerList.addAll(importer);
String realFileName = name.replace(".min.css", ".scss").replace(".css", ".scss");
Optional<Import> importedStartStylesheet = importer.stream().map(i -> i.apply(realFileName, null)).filter(Objects::nonNull).map(i -> i.stream().findFirst()).filter(Optional::isPresent).map(Optional::get).findFirst();
if (!importedStartStylesheet.isPresent()) {
return null;
}
Import firstImport = importedStartStylesheet.get();
StringContext context = new StringContext(firstImport.getContents(), firstImport.getAbsoluteUri(), firstImport.getAbsoluteUri(), options);
Compiler compiler = new Compiler();
Output output = compiler.compile(context);
String css = output.getCss();
boolean compress = name.endsWith(".min.css");
if (compress) {
try {
CssCompressor cssCompressor = new CssCompressor(new StringReader(css));
int lineBreaks = MCR_CONFIGURATION.getInt(LINE_BREAK_CONFIG_KEY, DEFAULT_LINE_BREAK_CONFIG_VALUE);
StringWriter out = new StringWriter(css.length());
cssCompressor.compress(out, lineBreaks);
css = out.toString();
} catch (IOException e) {
throw new MCRException("Read from StringReader produces IOException! (this should not happen)", e);
}
}
this.fileCompiledContentMap.put(name, css);
this.fileLastCompileDateMap.put(name, new Date());
try {
this.fileMD5Map.put(name, MCRUtils.asMD5String(1, null, css));
} catch (NoSuchAlgorithmException e) {
throw new MCRException("Error while generating md5 of result css!", e);
}
return css;
}
Aggregations