Resolving modules using require.js and Java/Rhino

后端 未结 1 460
北荒
北荒 2020-12-30 11:59

I\'m trying to get require.js to load modules on the server-side with Java 6 and Rhino.

I\'m able to load require.js itself just fine. Rhino can see the requir

相关标签:
1条回答
  • 2020-12-30 12:41

    require.js works well with rhino. Recently, I used it in a project.

    1. You have to make sure to use r.js (not require.js) , modified version of require.js for rhino.
    2. You have to extend ScritableObject class to implement load and print function. When you call require(["a"]), the load function in this class will be called, you can tweak this function to load the js file from any location. In the below example, I load from classpath.
    3. You have to define the property arguments in the sharedscope as shown below in the sample code
    4. Optionally, you can configure the sub path using require.config, to specify the subdirectory inside classpath where js files are located.

    JsRuntimeSupport

    public class JsRuntimeSupport extends ScriptableObject {
    
        private static final long serialVersionUID = 1L;
        private static Logger logger = Logger.getLogger(JsRuntimeSupport.class);
        private static final boolean silent = false;
    
        @Override
        public String getClassName() {
            return "test";
        }
    
        public static void print(Context cx, Scriptable thisObj, Object[] args,
                Function funObj) {
          if (silent)
            return;
            for (int i = 0; i < args.length; i++)
              logger.info(Context.toString(args[i]));
        }
    
        public static void load(Context cx, Scriptable thisObj, Object[] args,
                Function funObj) throws FileNotFoundException, IOException {
            JsRuntimeSupport shell = (JsRuntimeSupport) getTopLevelScope(thisObj);
            for (int i = 0; i < args.length; i++) {
                logger.info("Loading file " + Context.toString(args[i]));
                shell.processSource(cx, Context.toString(args[i]));
            }
        }
    
        private void processSource(Context cx, String filename)
                throws FileNotFoundException, IOException {
            cx.evaluateReader(this, new InputStreamReader(getInputStream(filename)), filename, 1, null);
        }
    
        private InputStream getInputStream(String file) throws IOException {
            return new ClassPathResource(file).getInputStream();
        }
    }
    

    Sample Code

    public class RJsDemo {
    
        @Test
        public void simpleRhinoTest() throws FileNotFoundException, IOException {
        Context cx = Context.enter();
    
        final JsRuntimeSupport browserSupport = new JsRuntimeSupport();
    
        final ScriptableObject sharedScope = cx.initStandardObjects(browserSupport, true);
    
        String[] names = { "print", "load" };
        sharedScope.defineFunctionProperties(names, sharedScope.getClass(), ScriptableObject.DONTENUM);
    
        Scriptable argsObj = cx.newArray(sharedScope, new Object[] {});
        sharedScope.defineProperty("arguments", argsObj, ScriptableObject.DONTENUM);
    
        cx.evaluateReader(sharedScope, new FileReader("./r.js"), "require", 1, null);
        cx.evaluateReader(sharedScope, new FileReader("./loader.js"), "loader", 1, null);
    
        Context.exit();
    
      }
    
    }
    

    loader.js

    require.config({
        baseUrl: "js/app"
    });
    
    require (["a", "b"], function(a,  b) {
        print('modules loaded');
    });
    

    js/app directory should be in your classpath.

    0 讨论(0)
提交回复
热议问题