osgirequirejsamdjs-amdeclipse-virgo

Integrating RequireJS into OSGi server


Note: Please don't be intimidated by the word "OSGi."

I am integrating RequireJS on an OSGi-based Virgo server environment (though the details are irrelevant). With an OSGi configuration, I have a root "OSGi bundle" that has JavaScript that needs to set up the main view. Then there are sub-bundles that rely on the root, each with their own JavaScript that needs to be executed.

I have RequireJS running well in the root bundle, with a "main" file that sets up the main view.

Questions: However, I don't know how best to initialize the execution of sub-bundles. Should I add a second <script data-main="main" src="require.js"></script> tag, this time in the sub-bundle to kick off its JavaScript execution? Should I simply import the main JavaScript file as normal in the sub-bundles?


Solution

  • I ended up setting up the framework's main JavaScript resources with the following tag in the root bundle:

    <script data-main="js/main" src="<c:url value="require.js" />"></script>
    

    . . . with "js/main.js" being:

    require(['jquery'], function ($) {
      // main script goes here . . .
    })
    

    With the above in the root bundle, sub-bundles simply invoke their own JavaScript with:

    <script>require(['sub-bundle/js/script.js'])</script>
    

    So the answer to my question is simply: Use the require function in sub-bundles!