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

Support for GraalJS #7

Open
lburgazzoli opened this issue Aug 10, 2020 · 4 comments
Open

Support for GraalJS #7

lburgazzoli opened this issue Aug 10, 2020 · 4 comments

Comments

@lburgazzoli
Copy link

I'm trying to use webjars-locator in graaljs and I've the following minimal code:

import java.net.URL;

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
import org.webjars.RequireJS;

import static org.graalvm.polyglot.Source.newBuilder;
import static org.webjars.RequireJS.WEBJARS_MAVEN_PREFIX;
import static org.webjars.RequireJS.getSetupJavaScript;

public class Main {
    private static final String REQUIRE_JS_URL = 
        "https://requirejs.org/docs/release/2.3.6/comments/require.js";

    public static void main(String[] args) throws Exception {
        Context cx = Context.newBuilder("js").build();
        cx.eval(newBuilder("js", new URL(REQUIRE_JS_URL)).buildLiteral());
        cx.eval(newBuilder("js", getSetupJavaScript(WEBJARS_MAVEN_PREFIX), "rjs").buildLiteral());

        Value result = cx.eval(
            "js",
            "var validator = require('validator');\n" +
            "validator.isEmail('[email protected]');"
        );

        System.out.println(result.asBoolean());
    }
}

The pom defines the following dependencies:

    <dependencies>
        <dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js</artifactId>
            <version>20.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator</artifactId>
            <version>0.40</version>
        </dependency>
        <dependency>
            <groupId>org.webjars.npm</groupId>
            <artifactId>validator</artifactId>
            <version>10.9.0</version>
        </dependency>
    </dependencies>

When running the example, I get the following error:

Exception in thread "main" TypeError: {callback: function() {
        // Deprecated WebJars RequireJS plugin loader
        define('webjars', function() {
            return {
                load: function(name, req, onload, config) {
        ...<omitted>...
}} is not a function
	at <js> :program(Unnamed:1:16-35)
	at org.graalvm.polyglot.Context.eval(Context.java:371)
	at com.github.lburgazzoli.graalvm.js.Main.main(Main.java:16)
@jamesward
Copy link
Member

Oh that'd be cool! Do you think the problem is with the syntax used by getSetupJavaScript ? Here is the code for that:
https://github.com/webjars/webjars-locator/blob/master/src/main/java/org/webjars/RequireJS.java#L143-L173

@lburgazzoli
Copy link
Author

I don't know :) my java script knowledge is very very minimal

@jamesward
Copy link
Member

Me too. :) I wonder if we could have a unit test for this. Think that'd be hard?

@lburgazzoli
Copy link
Author

lburgazzoli commented Aug 18, 2020

I can wrap my code in a test, the we can try to understand what's going on

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

2 participants