Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The --include-path option is missing #229

Closed
gdelhumeau opened this issue Sep 16, 2014 · 10 comments
Closed

The --include-path option is missing #229

gdelhumeau opened this issue Sep 16, 2014 · 10 comments
Milestone

Comments

@gdelhumeau
Copy link
Contributor

Hello.

I would be very interested to use Less4j for the integration of LESS inside our application (XWiki: http://extensions.xwiki.org/xwiki/bin/view/Extension/LESS+Module).

But I cannot do it if there is no support for the "--include-path" option, which allows us to specify a directory where LESS can find the files that we would like to import using "@import".

Is there a plan to add this feature in Less4J?

Thanks,

Guillaume from XWiki.

@SomMeri
Copy link
Owner

SomMeri commented Sep 20, 2014

Hi, sorry for later answer, I'm glad you are interested in using less4j in your app. Import dir like functionality can be achieved using custom less source as described here.

What is LessSource
Less4j uses LessSource to fetch imported files. Less4j does not care what is behind the LessSource interface as long as its implementation returns data or throws appropriate exceptions.

Three important methods:

  • relativeSource - get referenced source,
  • String getContent - importing of css and less,
  • byte[] getBytes() - used from data-uri function - it encodes file content into base64.

Then you just have to use CompilationResult compile(LessSource inputFile) method of LessCompiler interface to compile your sources.

How to achieve what you want
You can new LessSource type (maybe from FileSource) which would search for files in both current directory and in custom non-relative paths.

Using custom less source:

//create demo less file and compiler
File inputLessFile = createFile("sampleInput.less", "* { margin: 1 1 1 1; }");
LessCompiler compiler = new ThreadUnsafeLessCompiler();

//use custom less source
CompilationResult compilationResult = compiler.compile(new CustomFileSource(inputLessFile));

//print compiled css
System.out.println(compilationResult.getCss());

Either constructor or relativeSource method of your custom less source will contain something like this:

this.inputFile = new File(parent.inputFile.getParentFile(), filename);
if (!inputFile.exists()) {
  this.inputFile = new File(CUSTOM_PATH, filename);
}

Note: import-once feature uses LessSource.equals to check for multiple imports of the same file. If you are using it, your custom LessSource should implement it equals and hashCode.

@SomMeri SomMeri closed this as completed Sep 20, 2014
@SomMeri SomMeri added this to the 1.8.2 milestone Sep 20, 2014
@SomMeri
Copy link
Owner

SomMeri commented Sep 20, 2014

I updated wiki, because the same request was asked by multiple people.

I added new public FileSource(FileSource parent, File inputFile, String charsetName) constructor to the FileSource class as I was writing that wiki. Therefore, wiki solution is not compatible with latest released version. If it is problem for you, let me know and I will do minor release.

@gdelhumeau
Copy link
Contributor Author

Thanks for the nice answer. I'll check that.

@gdelhumeau
Copy link
Contributor Author

It would be nice if you could release your modifications, indeed.

Thanks :)

@SomMeri
Copy link
Owner

SomMeri commented Oct 4, 2014

@gdelhumeau Done, less4j-1.8.3 should be available in maven central.

@gdelhumeau
Copy link
Contributor Author

Thanks @SomMeri !

@brianhks
Copy link

brianhks commented Jul 2, 2015

You should just add the CustomFileSource to the code base as MultiFileSource or some such. I'm using less4j from a script environment and adding custom classes is a pain in the butt.

@SomMeri
Copy link
Owner

SomMeri commented Jul 7, 2015

@brianhks Good idea, this came up already multiple times. I opened new issue for it #287 . If you have such general classes already written, I would accept pull request.

@gdelhumeau
Copy link
Contributor Author

I've made a pull request for this: #289

@SomMeri
Copy link
Owner

SomMeri commented Jul 8, 2015

@gdelhumeau Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants