Requirejs is not loading my handlebars dependencie

2019-08-30 11:07发布

问题:

I am trying to load some js files with require. Here is my config file:

define(function() {

  "use strict";

  require.config({
    hbs : {
      templateExtension : 'hbs',
      disableHelpers: true,
      disableI18n : true
    },

    shim: {
      'underscore': {
        exports: '_'
      },
      'backbone': {
        deps: [
          'underscore',
          'jquery'
        ],
        exports: 'Backbone'
      },
      json2 : {
        exports: "JSON"
      },
      'jqueryMockAjax': {
        exports: '$.mockjax',
        deps: ['jquery']
      }
    },

    paths: {

      jquery: 'libs/jquery/jquery',
      jqueryMockAjax: 'libs/jquery-mockjax/jquery.mockjax',
      underscore: 'libs/underscore/underscore',
      backbone: 'libs/backbone/backbone',

      handlebars : 'libs/hbs/handlebars',
      text: 'libs/text/text',
      hbs: 'libs/hbs/hbs',
      i18nprecompile : 'libs/i18nprecompile',
      json2 : 'libs/json2/json2'
    }
  });
});

As you can see my js vendor files are located in my libs directory. My directory structure looks like this:

+project
  +app
    +js
      +libs
    app.js
    main.js

Most of my dependencies are loading like they should, like jquery and underscore, but the handlebars stuff is not loading like it should. For example, I have a path to handlebars.js as libs/handlebars/handlebars and it is removing the libs part of the path. With other files it is not. Here is my network tab showing what gets pulled in and what doesnt.

You can see the libs part of the path being removed. I dont know what that means. Any help will be appreciated.

回答1:

The reason that I was having import issues, was that when bower pulled in the hbs plugin, it pulled in all of the files that I needed for that plugin to work. handlebars, i18nprecompile and json2 were all in a directory in the hbs plugin. When I referred to the copies in that file, then everything worked just like it was supposed to work. I don't understand why the previously defined paths did not work. If anybody has some info on that issue please comment.