Lawrence
Lawrence

Reputation: 845

Loading non amd modules with require.js

Currently I am using require.js for a fun side project I am working everything is working fine except a code syntax higlighting plugin called prism.js. I can see that the plugin is being pulled via the network tab in chrome, but the plugin isn't initializing.

I am not sure if it's a require problem or uf the plugin is the issue and was wondering if anyone could help.

Here is a look at my main.js:

require.config({
  // 3rd party script alias names
  paths: {
    // Core Libraries
    modernizr: "libs/modernizr",
    jquery: "libs/jquery",
    underscore: "libs/lodash",
    backbone: "libs/backbone",
    handlebars: "libs/handlebars",

    text: "libs/text",
    prism: "plugins/prism",

    templates: "../templates"
  },
  // Sets the configuration for your third party scripts that are not AMD compatible
  shim: {
    "backbone": {
      "deps": ["underscore", "jquery", "handlebars"],
      "exports": "Backbone"  //attaches "Backbone" to the window object
    }
  }
});

// Include Specific JavaScript
require(['prism', 'modernizr', 'jquery', 'backbone', 'routers/router', 'views/AppVIew' ],
  function(Prism, Modernizr, $, Backbone, Router, App) {
    this.router = new Router();
    this.App = new App();
  }
);

Upvotes: 9

Views: 12588

Answers (3)

Chris Salzberg
Chris Salzberg

Reputation: 27374

Change the shim section to include prism, and make sure it exports "Prism":

shim: {
  "backbone": {
      "deps": ["underscore", "jquery", "handlebars"],
      "exports": "Backbone"  //attaches "Backbone" to the window object
  },
  "prism": {
      "exports": "Prism"
  }
}

Upvotes: 11

Gokhan Tank
Gokhan Tank

Reputation: 3786

Handlebars and Prism are not compatible with AMD(Asyncronous Module Definition) so you need to shim it yourself like below;

requirejs.config({
    shim: {
        'backbone': {
            "deps": ["underscore", "jquery", "handlebars"],
            "exports": "Backbone"  //attaches "Backbone" to the window object
        },
        'handlebars': {
            "exports": 'Handlebars'
        },
        'prism': {
            "exports": "Prism"
        }
    }
});

You may wish to look at the require.js shim documentation site; http://requirejs.org/docs/api.html#config-shim

Hope this will help

Upvotes: 3

yakxxx
yakxxx

Reputation: 2941

Prism should be added to shim too. Just as backbone it is not AMD compliant and therefore must be declared same way.

Upvotes: 1

Related Questions