webjars / webjars-locator

MIT License
2 stars 7 forks source link

Support for GraalJS #7

Open lburgazzoli opened 4 years ago

lburgazzoli commented 4 years ago

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('foo@bar.com');"
        );

        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 commented 4 years ago

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 commented 4 years ago

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

jamesward commented 4 years ago

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

lburgazzoli commented 4 years ago

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