Skip to content

Support for GraalJS #7

Description

@lburgazzoli

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions