nashorn-commonjs-modules icon indicating copy to clipboard operation
nashorn-commonjs-modules copied to clipboard

Ability to load modules from Maven webjars?

Open deinspanjer opened this issue 7 years ago • 5 comments

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 avatar May 31 '17 20:05 deinspanjer

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

deinspanjer avatar May 31 '17 21:05 deinspanjer

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...

malaporte avatar Jun 01 '17 10:06 malaporte

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?

deinspanjer avatar Jun 03 '17 21:06 deinspanjer

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.

malaporte avatar Jun 05 '17 15:06 malaporte

@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

maybeec avatar Jul 10 '17 18:07 maybeec