Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

data-uri function not working #5

Closed
ILadis opened this issue Nov 8, 2016 · 6 comments
Closed

data-uri function not working #5

ILadis opened this issue Nov 8, 2016 · 6 comments
Assignees
Labels
Milestone

Comments

@ILadis
Copy link

ILadis commented Nov 8, 2016

Hey,

I'm using your plugin to compile my Less sources. I tried to make use of the data-uri function (docs see here) that Less provides, but I could not get it working. The plugin keeps on throwing the following error:

[ERROR] Failed to execute goal biz.gabrys.maven.plugins:lesscss-maven-plugin:1.2.0:compile (default) on project ...: RuntimeError: error evaluating function `data-uri`: Cannot find module './fs' in .../gabrys-biz-lesscss-maven-plugin/styles.less-n1871238842.source.code on line 5

The line (5) the error message is refering to is the exact position in my Less file where I used the data-uri function.

I invastigated the issue and came to the following solution:

1. Fixing: Cannot find module './fs'

This issue lies in your lesscss-compiler project, more specifically in less-rhino-1.7.5.js. The fs module is defined, but requiring it via require('./fs') is simply not working. When invoking the stubbed require function with a module containing a / the implementation will not look in less.modules (where the fs module is defined). Therefore changing require('./fs') to require('fs') (see position in code less-rhino-1.7.5.js:2679) will resolve this issue.

2. Fixing: Cannot convert org.mozilla.javascript.NativeArray to byte[]

When (1) was fixed I instead got a similar issue as described here. To fix this issue I had to make changes to the encodeBase64String(String) (see position in code less-rhino-1.7.5.js:143. I changed the encodeBase64String(String) like this:

function encodeBase64Bytes(bytes) {
  var array = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, bytes.length);
  for (var index = 0; index < bytes.length; index++) {
    var oneByte = bytes[index];
    if (oneByte > 127) {
      oneByte -= 256;
    }
    array[index] = oneByte;
  }
  // requires at least a JRE Platform 6 (or JAXB 1.0 on the classpath)
  return javax.xml.bind.DatatypeConverter.printBase64Binary(array);
}

After those two fixes I was able to use the data-uri function. One still has to tell the maven plugin where to find the resources to be data-uri'ed by either using absolute paths or placing the resources inside target/gabrys-biz-lesscss-maven-plugin.

Is there any chance the data-uri function will be fixed in a future version? I could also send a pull request with my fix, if you wish.

@agabrys agabrys added the bug label Nov 8, 2016
@agabrys agabrys added this to the 1.2.1 milestone Nov 8, 2016
@agabrys agabrys self-assigned this Nov 8, 2016
@agabrys
Copy link
Contributor

agabrys commented Nov 8, 2016

Hi,
Thank you for noting that 😄
You can prepare a PR or I will add/copy you changes. Have you got any test project for this issue? I want to test it before release 😉

@ILadis
Copy link
Author

ILadis commented Nov 8, 2016

Thanks for replying!

I attached a ZIP archive (test-project.zip) containing a very simple maven project that is using your lesscss-maven-plugin in the current version 1.2.0. If you run mvn compile on the project root directory, you will get the error I described above.

I also attached my fix (less-rhino-1.7.5.txt, rename it to less-rhino-1.7.5.js) for this issue. Once you built a new version of the lesscss-compiler and lesscss-maven-plugin the example project should compile without any errors.
You can use the index.html in my sample project to check whether the image got data-uri'ed successfully.

@agabrys
Copy link
Contributor

agabrys commented Nov 8, 2016

I tested your solution and it works. The only problem is that resources paths must be relative to working directory... Unfortunately it will be difficult to fix it. I have an idea (a new vision for LessCSS Compiler library) but I need to consult it with one colleague from my work.

For now you can use workaround:

  1. add sonatype-nexus-snapshots repository to pom.xml file:
<repositories>
    <repository>
        <id>sonatype-nexus-snapshots</id>
        <name>Sonatype's SNAPSHOT repository</name>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
  1. add biz.gabrys.lesscss:compiler:1.2.2-SNAPSHOT dependency to lesscss-maven-plugin:
<plugin>
    <groupId>biz.gabrys.maven.plugins</groupId>
    <artifactId>lesscss-maven-plugin</artifactId>
    <version>1.2.0</version>
    <dependencies>
        <dependency>
            <groupId>biz.gabrys.lesscss</groupId>
            <artifactId>compiler</artifactId>
            <version>1.2.2-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            ...
        </execution>
    </executions>
</plugin>
  1. change lesscss-maven-plugin compiler type form full to local:
<executions>
    <execution>
        <goals>
            <goal>compile</goal>
        </goals>
        <configuration>
            <compilerType>local</compilerType>
            ...
        </configuration>
    </execution>
</executions>

@ILadis
Copy link
Author

ILadis commented Nov 9, 2016

Thanks for looking into my solution!

I will give feedback on your workaround as soon as I am able to try it out. It's currently not super urgent to have a fixed and running version of the Less compiler for my project.

But I am still looking forward to use the fixed Less Compiler on it's next official release without the need of any workarounds.

@agabrys
Copy link
Contributor

agabrys commented Nov 12, 2016

Ok. I fixed (with your help 😄) support for data-uri function, but (for now) only for local compiler. If you don't import files located in Internet or classpath then you can use:

<plugin>
    <groupId>biz.gabrys.maven.plugins</groupId>
    <artifactId>lesscss-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <compilerType>local</compilerType>
                ...
            </configuration>
        </execution>
    </executions>
</plugin>

@ILadis
Copy link
Author

ILadis commented Nov 14, 2016

Hey,
thank you so much for fixing the data-uri function. I just gave the new version a try and it works like a charm!

Thanks for fixing it so quickly!

@ILadis ILadis closed this as completed May 12, 2018
agabrys added a commit that referenced this issue Aug 5, 2018
- add information about fix in 2.0.0 changelog
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

2 participants