我结合编译模板与我的其他脚本的指针(jQuery的/ ember/ember-data /我的应用程序代码)使用grunt。然后,在我的index.html中,我只包含单个js脚本(也有助于减少http请求的数量)。
我目前正在使用grunt这个,一个简单的“构建”步骤可能看起来像下面。要使用这个,你需要安装nodejs。接下来,您将NPM安装以下
grunt
grunt-cli
grunt-ember-template-compiler
grunt-contrib-concat
一旦你已经安装了这些,你可以从你的根,如果你把JS到一个名为“Gruntfile.js”文件 - 然后简单地执行“咕噜”的运行和构建它会输出一个deps.min.js(带你的脚本和模板组合)
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-ember-template-compiler');
grunt.initConfig({
concat: {
dist: {
src: [
'website/static/website/script/vendor/handlebars-v1.2.1.js',
'website/static/website/script/vendor/ember.min.js',
'website/static/website/script/my-app.js’,
'website/static/website/script/dist/tmpl.min.js'],
dest: 'website/static/website/script/dist/deps.min.js'
}
},
emberhandlebars: {
compile: {
options: {
templateName: function(sourceFile) {
var newSource = sourceFile.replace('website/static/website/script/app/templates/', '');
return newSource.replace('.handlebars', '');
}
},
files: ['website/static/website/script/app/templates/**/*.handlebars'],
dest: 'website/static/website/script/dist/tmpl.min.js'
}
}
});
grunt.task.registerTask('default', ['emberhandlebars', 'concat:dist']);
};
你确定要将模板编译到模板目录吗?我通常会将汇编后的代码放到一个“dist”文件夹中,这样它就不会混淆我的项目中可能存在的其他任何实际源代码。 –