lesscss-engine icon indicating copy to clipboard operation
lesscss-engine copied to clipboard

@import resources from the classpath

Open fleaflicker opened this issue 12 years ago • 5 comments

I cannot @import a file from a different folder (but on the classpath).

How does the LessEngine load files for @import?

Example:

@import "test.less"

where test.less isn't in the same folder but its parent is added to the java classpath?

fleaflicker avatar May 30 '12 23:05 fleaflicker

Have you tried @import "../test.less"? For classpath imports we use @import "classpath:META-INF/less/import.less".

asual avatar May 31 '12 06:05 asual

Yes, using the notation @import "classpath:..."; does work

But I cannot use that notation because I'm trying to use the same .less file in production (compiled with asual engine) and in development (using the traditional less.js browser mode).

Is there any way the asual engine to do this automatically behind the scenes? First try to import relative to working directory, then check the classpath if unable to find the file.

fleaflicker avatar May 31 '12 18:05 fleaflicker

It will be great if you can do this with relative paths. I will check if a classpath fallback is possible.

asual avatar Jun 01 '12 11:06 asual

Thanks.

Something as simple as wrapping the call in a try-catch block and then checking the classpath (both with the "classpath:" notation and without) should work.

fleaflicker avatar Jun 03 '12 16:06 fleaflicker

This can be implemented using the changes introduced with https://github.com/asual/lesscss-engine/pull/32

You need to override the default resource loading configuration by adding a variant of ClasspathResourceLoader that will check if the resource is present on the classpath even if no 'classpath:' prefix was given in the @include directive:

import com.asual.lesscss.loader.ClasspathResourceLoader;

class ClasspathFallbackResourceLoader
    extends ClasspathResourceLoader {
    ClasspathFallbackResourceLoader(ClassLoader cl) {
        super(cl);
    }

    @Override
    protected String getSchema() {
        return "";
    }
}

then you can combine it with regular ClasspathResourceLoader using ChainedResourceLoader and pass it to LessEngine.init.

rkrzewski avatar Dec 11 '12 22:12 rkrzewski