vert-x3 / vertx-lang-js

Nashorn JavaScript implementation for Vert.x
Apache License 2.0
35 stars 23 forks source link

jvm-npm.js: Resolve near module dependencies inside node_modules dir within zip archive #29

Closed mellster2012 closed 9 years ago

mellster2012 commented 9 years ago

It seems that the Require.resolve resolution mechanism only works on the file system. Are there any plans for adding support for resolution of provided node modules within a (self-contained) zip archive? This draft diff will make the npm example at https://github.com/vert-x3/vertx-examples/tree/master/core-examples/src/main/js/npm example work if distributed and launched as zip archive: jvm-npm.js.diff (against version/tag 3.0.0)

78a79
>
248c249,250
<       (root ? resolveAsNodeModule(id, new File(root).getParent()) : false);

---
>       (root ? resolveAsNodeModule(id, new File(root).getParent()) : false) ||
>       resolveAsClasspathNodeModule(id);
290a293,303
>   function resolveAsClasspathNodeModule(name) {
>     name = 'node_modules/' + name;
>     var main = name + '/index.js';
>     var classloader = java.lang.Thread.currentThread().getContextClassLoader();
>     if (classloader.getResource(name + '/package.json')) {
>       var package = JSON.parse(readFile(name + '/package.json', true));
>       if (package.main) main = (name + '/' + package.main).replace(/\.\//g,'');
>     }
>     if (classloader.getResource(main)) return {path: main, core: true};
>   }
>
cinterloper commented 9 years ago

+1 it would be really nice to be able to embed npm modules in the jar as a resource

pmlopes commented 9 years ago

I'll close this issue as the PR #30 has been merged already.