malaporte / nashorn-commonjs-modules

CommonJS modules support for Nashorn
MIT License
108 stars 31 forks source link

Ability to load modules from Maven webjars? #23

Open deinspanjer opened 7 years ago

deinspanjer commented 7 years ago

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

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

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

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

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

@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