如何使用Grunt-contrib-requirejs优化器使用Requirejs-handlebars?

我目前正在使用Grunt-contrib-requirejs优化器,以便我的最终包结构看起来像这样:

Public/ css js -myapp.js -require.js 

我想要使​​用requirejs-handlebars来渲染我的模板(也使用服务器端的Express3-手柄)。 我有NPM包requirejs-handlebars的工作,但只有当我暴露在我的快递服务器模块与下面的行:

 app.use('/node_modules/handlebars/dist/', express.static(path.join(__dirname, './node_modules/handlebars/dist/' ))); 

没有此修复程序,加载我的应用程序时出现以下控制台错误:

GET http:// localhost:3300 / node_modules / handlebars / dist / handlebars.runtime.amd.js require.js:166未捕获错误:脚本错误:handlebars.runtime

我猜这个错误是我的最终构build结构和我的需要优化的结果。由于显而易见的原因,该脚本不存在,因为我的最终构build结构不包括它。 我想我想要的是不必包含使用express中间件的handlebars.runtime,或者将它与我的最终myapp.js(我不知道什么是最好的方式来做到这一点)合并。 有任何想法吗? 对不起nbbness …任何意见将不胜感激!

谢谢!

我的main.js文件如下所示:

  require.config({ shim: { jquery: { exports: '$' }, underscore: { exports: '_' }, backbone: { deps: [ 'jquery', 'underscore' ], exports: 'Backbone' }, marionette: { deps: [ 'jquery', 'underscore', 'backbone' ], exports: 'Marionette' }, bootstrap: { deps: [ 'jquery' ] } }, paths: { backbone: '../../bower_components/backbone/backbone', marionette: '../../bower_components/backbone.marionette/lib/backbone.marionette', jquery: '../../bower_components/jquery/jquery', underscore: '../../bower_components/underscore/underscore', requirejs: '../../bower_components/requirejs/require', text: '../../node_modules/requirejs-text/text', hb: '../../node_modules/requirejs-handlebars/hb', 'handlebars.runtime': '../../node_modules/handlebars/dist/handlebars.runtime.amd', }, packages: [ { name: 'handlebars', location: '../../node_modules/handlebars/dist/amd', main: './handlebars' } ] }); require([ './app', ], function(App){ 'use strict'; var myapp = new App(); myapp.start(); }); 

我的Gruntfile:

  requirejs: { compile: { options: { baseUrl: "client/src", optimize: '', //uglify mainConfigFile:'client/src/main.js', name: "main", out: "build/app.js", removeCombined: true, logLevel: 0, findNestedDependencies: true, fileExclusionRegExp: /^\./, inlineText: true, } }, }, 

看起来grunt requirejs没有内联handlebars.runtime模块,这就是为什么你必须在你的express代码中添加远程路由。

我设法通过声明path为handlebars和handlebars.runtime解决它,我也不得不填充他们。 所以,我的main.js看起来像这样:

 paths: { 'handlebars.runtime': '../bower_components/handlebars/handlebars.runtime', handlebars: '../bower_components/handlebars/handlebars', hbs: '../bower_components/requirejs-handlebars/hb', }, shim: { 'handlebars.runtime': { exports: 'handlebars.runtime' }, handlebars: { deps: ['handlebars.runtime'] }, } 

而现在当我咕噜生成,我可以看到把手和handlebars.runtime内联到我的app.js. 这应该不需要暴露node_modules dirs。