Search code examples
javamavenlesswro4j

Using The LessCssProcessor only on a certain group of files


I'm trying to implement a LessCssProcessor in my maven project. However, the project I'm trying to use the processor on has files with CSS errors, causing the css files not to generate. Is it possible to configure the processor or the project so that the Less processor, or any processor, can only be ran on a certain group of files. All help is appreciated!


Solution

  • The simplest way to apply a processor only on a limited set of resources is to use ExtensionsAwareProcessorDecorator, example:

    ExtensionsAwareProcessorDecorator.decorate(new LessCssProcessor()).addExtension("less");
    

    The decorated processor should be used instead of LessCssProcessor and it will process only resources having less extension (example: style.less) and will ignore anything else.

    The same thing can be achieved using a configuration convention, example:

    preProcessors=lessCss.less
    

    Notice that the alias of the processor hass ".less" suffix, meaning that it will be applied only on resources having less extension.

    If you need something more elaborate, there is another decorator available called PathPatternProcessorDecorator. Usage example:

    String[] patterns = new String[] {"/a/**/n?me.css", "*.less", "/less/*.css"}
    PathPatternProcessorDecorator.include(new LessCssProcessor(), patterns);
    

    The above example creates a decorated processor which is applied only when a resource matches to one of the provided patterns. A similar approach can be used if you need to exclude patterns:

    String[] patterns = new String[] {"*.css", "/script/*.js"}
    PathPatternProcessorDecorator.exclude(new LessCssProcessor(), patterns);
    

    In this case the processor will not be applied if the resource matches any of the provided patterns.

    If none of the existing implementations is good enough, you could implement your own processor decorator which applies or not the processing in a custom way.