You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 9, 2019. It is now read-only.
Repository navigation
This repository was archived by the owner on Jan 9, 2019. It is now read-only.
@import doesn't work when compiler ran on string #28
When a relative path is specified, I see this error:
org.lesscss.LessException: Couldn't load undefinedvariables.less (0)
That's understandable, obviously the import doesn't know where the path is relative from
However changing '@ import' to the full path still doesn't work:
org.lesscss.LessException: Couldn't load file:///full/path/to/file/css/variables.less (undefined)
I think it comes from envjs returning a different status for file:/// paths than browsers, the xhs.status is undefined even though the file was loaded properly (was able to hack less.js to make it ignore the return status and it worked).
If this bug can be fixed, a great enhancement would be a new function in LessCompiler:
LessCompiler.compile(String data, URL basePath); so imports would more easily work when processing data in a servlet context like the filter does.
So I poked around at the problem a bit more, and after getting frustrated trying to get envjs working 100% in this scenario, I decided to just process the imports directly in the java code instead of trying to handle the imports with xhr() in the javascript code :)
Perhaps this could be inspiration for inclusion in the LessCompiler class or for others who encounter this scenario.
classLessCompilerModextendsLessCompiler {
PatternimportPattern = Pattern.compile("['\"]([^'\"]+)['\"]");
booleanonlyImportOnce = true; // does @import always act like @import-once (like in Less 1.4+)publicStringcompile(Stringinput, URIbaseUri) throwsLessException {
StringBuildermerged = newStringBuilder();
BufferedReaderreader = newBufferedReader(newStringReader(input));
Stringline;
try {
List<String> imported = newArrayList<>();
while ((line = reader.readLine()) != null) {
merged.append(readLineAndProcessImports(line, baseUri, 0, imported));
}
} catch (IOExceptione) {
thrownewRuntimeException(e);
}
returnsuper.compile(merged.toString());
}
StringBuilderreadLineAndProcessImports(Stringline, URIbaseUri, intlevel, List<String> imported) throwsLessException {
if (level >= 10) {
thrownewLessException("Max @import levels exceeded(" + level + "), possible recursion!",
newAssertionError("recursion depth exceeded"));
}
StringBuildermerged = newStringBuilder();
LOGGER.trace(line);
if (line.contains("@import")) {
LOGGER.debug("Found an import: {}", line);
Matchermatch = importPattern.matcher(line);
if (match.find()) {
Stringpath = match.group(1);
if (!path.endsWith(".less") && !path.endsWith(".css")) {
path += ".less";
}
URIfullPath;
fullPath = baseUri.resolve(path);
LOGGER.debug("@import fullPath is '{}'", fullPath);
if ((onlyImportOnce || line.contains("@import-once")) && imported.contains(fullPath.toString())) {
LOGGER.info("Already included: {}", line);
} else {
if (!imported.contains(fullPath.toString())) {
imported.add(fullPath.toString());
}
try (InputStreamstream = fullPath.toURL().openStream()) {
InputStreamReaderimportReader = newInputStreamReader(stream);
BufferedReaderimportBuffered = newBufferedReader(importReader);
StringimportedLine;
while ((importedLine = importBuffered.readLine()) != null) {
LOGGER.trace(importedLine);
if (importedLine.contains("@import")) {
LOGGER.debug("Found a sub import: {}", importedLine);
merged.append(readLineAndProcessImports(importedLine, baseUri, level + 1, imported));
} else {
merged.append(importedLine);
}
}
} catch (IOExceptione) {
thrownewLessException(e);
}
}
} else {
LOGGER.warn("Unable to find a resource path in line: {}", line);
}
} else {
merged.append(line);
}
returnmerged;
}
}
Activity
jamcole commentedon May 10, 2013
So I poked around at the problem a bit more, and after getting frustrated trying to get envjs working 100% in this scenario, I decided to just process the imports directly in the java code instead of trying to handle the imports with xhr() in the javascript code :)
Perhaps this could be inspiration for inclusion in the LessCompiler class or for others who encounter this scenario.
I call it like this: