我結合編譯模板與我的其他腳本的指針(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」文件夾中,這樣它就不會混淆我的項目中可能存在的其他任何實際源代碼。 –