Chapter 39. Module Resources

The AMD format allows to define dependencies on a resource loaded by a module based on the loader plug-in.
A module that uses a loader plug-in uses the exclamation mark character (!) to delineate between the loader plug-in module to use and the target resource to load. Loader plug-ins are useful because they can load resources by means of the AMD loading mechanism and benefit from its performance and parallel loading.
There are some useful loader plug-ins:

Procedure 39.1. Configuring a Module Resource

To demonstrate the configuration of an AMD loader plug-in's target resource, we need some HTML code generated by JavaScript. The text AMD loader plug-in can be used for this purpose. The plug-in will load a template as its resource and pass it as a parameter into a module definition callback.
  1. Declare text as a module. It is written as a native module that depends on the predefined RequireJS module called module. The native support mechanism allows this configuration works transparently without modifying the third-party library.
    
    <module>
      <name>text</name>
      <script>
        <path>/requirejs/js/plugins/text.js</path>
      </script>
      <depends>
        <!-- module means RequireJS itself -->
        <module>module</module>
      </depends>
    </module>
    
  2. Define the foo module that requires the text plug-in to load the template by means of the <resource> XML element.
    
    <module>
      <name>foo</name>
      ...
      <depends>
        <module>text</module>
        <as>myTemplate</as>
        <resource>/path/to/template.tmpl</resource>
      </depends>
    </module>
    
  3. The foo module template is loaded by the text plug-in.
    
    (function(myTemplate) {
    //parse the 'myTemplate' then append to DOM
    })(myTemplate);