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

Ability to load modules from Maven webjars? #23

Open
deinspanjer opened this issue May 31, 2017 · 5 comments
Open

Ability to load modules from Maven webjars? #23

deinspanjer opened this issue May 31, 2017 · 5 comments

Comments

@deinspanjer
Copy link

I have a couple of simple modules that I'm trying to wrangle into Nashorn. One example is LoDash.

If I include it as a Maven dependency via webjars:

<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>lodash</artifactId>
    <version>4.17.4</version>
</dependency>

I technically have it in my classpath via the jar lodash-4.17.4.jar with an example path inside it of /META-INF/resources/webjars/lodash/4.17.4/index.js.

Is there any chance this is enough to be able to require the module? I'm figuring the version is one problem, and the other problem is not being sure how to reference the path in the ResourceFolder.

@deinspanjer
Copy link
Author

deinspanjer commented May 31, 2017

I've been playing around with it, and here is the closest I've gotten:

final ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(this.getClass().getClassLoader());
final Resource[] resource = resolver.getResources("classpath*:/META-INF/resources/webjars/lodash/*/") ;
log.info("Path: {}; Exists: {}",resource[0].getURL(),resource[0].exists());
ResourceFolder rootFolder = ResourceFolder.create(getClass().getClassLoader(), "", "UTF-8");
Require.enable(jsEngine.getNashornEngineReference(), rootFolder);
jsEngine.eval(String.format("var _ = require('%s%s');",resource[0].getURL(),"lodash"));

I get this log followed by an error:

Path: jar:file:/Users/dre/.m2/repository/org/webjars/npm/lodash/4.17.4/lodash-4.17.4.jar!/META-INF/resources/webjars/lodash/4.17.4/; Exists: true
javax.script.ScriptException: Error: Module not found: jar:file:/Users/dre/.m2/repository/org/webjars/npm/lodash/4.17.4/lodash-4.17.4.jar!/META-INF/resources/webjars/lodash/4.17.4/lodash in <eval> at line number 1

@malaporte
Copy link
Owner

ResourceFolder must point at the root of a folder containing the library in NPM format. Is that the case? I see you are passing an empty string as the path parameter which can't be right...

@deinspanjer
Copy link
Author

Are the webjars in "NPM format"? This folder:
jar:file:/Users/dre/.m2/repository/org/webjars/npm/lodash/4.17.4/lodash-4.17.4.jar!/META-INF/resources/webjars/lodash/4.17.4/
contains all the files that would normally reside in the lodash folder of the node_modules directory.

Or is what I have to do to extract out all those jars and build them into an actual node_modules directory and then pass that to ResourceFolder as the path?

@malaporte
Copy link
Owner

You need to pass the path of the resources at the very least (2nd arg of ResourceFolder constructor). Then it should work it they are in the proper format.

@maybeec
Copy link

maybeec commented Jul 10, 2017

@malaporte the request is ment a little bit different as far as I understand it.
The webjars should be automatically registered by adding them to the classpath. So you might want to dynamically look at the path /webjars/<module> in the classpath.
http://www.baeldung.com/maven-webjars

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

3 participants