Skip to content

Loading Dust via AMD (require.js)

Seth Kinast edited this page Jan 27, 2015 · 9 revisions

Dust supports being loaded as an AMD module as of Dust 2.5.2.

To maintain backwards compatibility, you must enable a config flag to tell Dust to register itself as a named AMD module (in the same way jQuery works).

define.amd.dust = true;

After you've done this, you can use the module names dust or dust-core to load Dust, and dust-full to load Dust plus the compiler.

<script src="r.js"></script>
<script type="text/javascript">
    define.amd.dust = true;
    require(["dust-full"], function(dust) {
        dust.render(...);
    });
</script>

Custom require path

Because Dust registers itself as a named module, you cannot pass a path to require:

// Does not work
require('lib/dust/dust-full', function(dust) {

Instead, you can set a custom path for Dust using require.config:

<script src="r.js"></script>
<script type="text/javascript">
    require.config({
        paths: {
            dust: "lib/dust/2.5.1/dust-core.min"
            "dust-full": "lib/dust/2.5.1/dust-full"
        }
    });
    define.amd.dust = true;
    require(["dust-full"], function(dust) {
        dust.render(...);
    });
</script>

Helpers

If you are using dustjs-helpers 1.5.1 or newer, the helpers also register themselves as an anonymous AMD module.

define.amd.dust = true;
require(["dust", "dust-helpers"], function(dust) {
    // dust helpers are available when you call dust.render()
});