質問

I'm looking for a module system for Nashorn. From what I can tell, CommonJS is the way to go concerning modules for JS. I have looked through the list (here and here) and have found little in the way of a CommonJS implementation for Java.

Narwhal is no longer active and it's documentation is no longer hosted on GitHub. Is there an existing CommonJS implementation which supports Java or should I start a new project?

役に立ちましたか?

解決 2

I asked a very similar question on the Nashorn mailing list a little while back, here's Sundar's (Nashorn Engineer) reply:

From: A. Sundararaj​an

To: nashorn-dev@openjdk.java.net

I forgot to add. Nashorn does not contain any builtin module system. But, if a module system is pure JS + Java, it must be possible to run on nashorn.

Nashorn supports "load" (loads scripts from URL, File, resources) and "loadWithNewGlobal" (loads script but into a fresh global scope) primitives in addition to the good old 'eval'. So, it should be possible for any module system to be implemented on top of nashorn in pure JS or perhaps with a bit of Java code.

-Sundar

他のヒント

Have a look at jvm-npm here https://github.com/nodyn/jvm-npm. This project is used by nodyn as the CommonJS module system. It is NPM-aware, meaning you can load modules directly from NPM, but it does not provide any of the Node.js API.

Here is a simple example usage:

$ npm install pegjs
npm http GET https://registry.npmjs.org/pegjs
npm http 200 https://registry.npmjs.org/pegjs
pegjs@0.8.0 node_modules/pegjs
$ jrunscript
nashorn> typeof require
undefined
nashorn> load('./jvm-npm.js')
nashorn> typeof require
function
nashorn> var PEG = require('pegjs');
nashorn> typeof PEG
object

It is primarily all Javascript, but the actual loading of files from the filesystem and such is done using Java.

I've been looking for such an implementation for a while. I've been using a little patched version of Rhino-Require. Although Rhino claimed to be CommonJS compatible, AFAIK, it implemented only modules and not packages (package.json) can't be parsed. RingoJS should be compatible. But Nashorn will never be see.

Laterly, Oracle announced project Avatar which relies on Avatar.js or here. It's the official project of what was unofficially called Node.jar. But as of now, you have to compile it by yourself. The project is very young.

Another very young project is Nodyn which relies on dyn.js.

So, if understood well, CommonJs should work with avatar-js and nodyn, but those two are still pretty young. I don't understand why avatar-js in not fully distributed along with nashorn though.

A kind of solution would be to add a CommonJS compatibility script like the one for Rhino which adds importClass/importPackage (mozilla_compat.js) which would add CommonJS compatibility into nashorn, kind of Rhino-Require shim thoroughly tested.

I had the same need, and I used jvm-npm for a while, but I needed something that would work even without allowing usage of Java packages inside JavaScript, so I wrote my own version here: https://github.com/coveo/nashorn-commonjs-modules

It's implemented entirely in Java and supports loading modules from elsewhere than the filesystem (Java resources, a custom database, etc.)

It's published on Maven Central if someone wants to use it.

There's also nashorn-require, you can get that off of github too. I've used it, I was able to do

        engine.eval(reader("src/main/javascript/nashorn-require.js"),bindings);
        engine.eval("var initRequire = load('src/main/javascript/nashorn-require.js');",bindings);
        engine.eval("initRequire({mainFile : 'src/main/javascript/foo', debug : true})", bindings);
        engine.eval("var babel = require('babel');",bindings);

and then transpile JSX React components to ES5 with

        Buffer input = findTemplateSource(fileLocation,context);
        bindings.put("input",input.toString());
        result = engine.eval("babel.transform(input,{ presets: ['react', 'es2015'] }).code;",bindings);

Then when I pulled react and react-dom into my browser and loaded the resulting js components, things worked fine, so I'm sure Babel was perfectly happy, though I'm not sure about if it will find 3rd-party plugins or not...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top