我目前正在使用Grunt-contrib-requirejs优化器,这样我的最终包结构基本上如下所示:
Public/
css
js
-myapp.js
-require.js
我想使用requirejs-handlebars渲染模板(也可以在服务器端使用Express3-handlebars)。我已经使用NPM包requirejs-handlebars了,但是仅当我通过以下行在Express服务器中公开该模块时:
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的脚本错误
我猜这个错误是我最终的构建结构和我的需求优化程序的结果。由于明显的原因,该脚本不存在,因为我的最终构建结构未包含该脚本。我想我想要的是不必使用快速中间件包含handlebars.runtime,也不必将其与最终的myapp.js合并(我不确定这样做的最佳方法是什么)。有任何想法吗?对不起,n00bness ...任何建议将不胜感激!
谢谢!
我的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,
}
},
},
似乎笨拙的requirejs没有内联handlebars.runtime模块,这就是为什么您必须在快速代码中为其添加该远程路由的原因。
我设法通过声明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']
},
}
现在,当我咕unt咕build地编译时,我可以看到handlebars和handlebars.runtime都内联到我的app.js中。那应该使您不必暴露来自express的node_modules目录。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句