简体   繁体   English

使用require.js加载非amd模块

[英]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. 目前我正在使用require.js进行一个有趣的项目我正在工作一切正常,除了代码语法higlighting插件名为prism.js。 I can see that the plugin is being pulled via the network tab in chrome, but the plugin isn't initializing. 我可以看到插件是通过chrome中的网络选项卡提取的,但插件没有初始化。

I am not sure if it's a require problem or uf the plugin is the issue and was wondering if anyone could help. 我不确定这是一个需求问题,或者uf插件是问题,并且想知道是否有人可以提供帮助。

Here is a look at my main.js: 这是我的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();
  }
);

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"
  }
}

Handlebars and Prism are not compatible with AMD(Asyncronous Module Definition) so you need to shim it yourself like below; 把手和棱镜与AMD(异步模块定义)不兼容,所以你需要像下面那样自己进行填充 ;

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; 您可以查看require.js shim文档站点; http://requirejs.org/docs/api.html#config-shim http://requirejs.org/docs/api.html#config-shim

Hope this will help 希望这会有所帮助

Prism should be added to shim too. Prism也应该加入shim Just as backbone it is not AMD compliant and therefore must be declared same way. 就像骨干一样,它不符合AMD标准,因此必须以同样的方式声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM