Ember.js,Express.js和Node.js的资产pipe道?

我正在使用Express.js作为后端构build一个Ember.js应用程序。 现在,我单独加载所有的* .js文件,并将我的Handlebars模板存储在我的HTML文件中。 我喜欢用一个类似于Rails中的完整的“资产pipe道”来取而代之。 在一个完美的世界里,这将支持:

  • 将CoffeeScript转换为JavaScript。
  • 使用Ember.js扩展预编译Handlebars模板。
  • 连接并缩小JavaScript和CSS(仅限制作)。

我简单地看了一下Require.js, connect-assets和车队。 前两个似乎没有提供任何简单的方式来预编译Handlebars模板, Ember车队集成是基于一个过时的版本的Ember。

余烬跑者尚未更新一段时间。 grunt-ember-templates看起来像是将Ember模板编译为单个* .js文件的合理方式,所以这可能是一个更大的解决scheme的构build块。

是否有任何Node.js资产编译系统,只要与Ember.js工作? 我很喜欢有一个Node.js相当于呃栏 。

可以使用连接资源 , grunt-ember-templates和Gruntfile来构build一个非常方便的系统。 首先,我们需要将以下configuration添加到Gruntfile.coffee:

grunt.initConfig watch: ember_templates: files: 'assets/templates/**/*.hbr' tasks: 'ember_templates' ember_templates: compile: options: templateName: (sourceFile) -> sourceFile.replace(/assets\/templates\//, '').replace(/\.hbr$/, '') files: 'assets/js/templates.js': 'assets/templates/**/*.hbr' # Plugins. grunt.loadNpmTasks('grunt-contrib-watch') grunt.loadNpmTasks('grunt-ember-templates') # Default task. grunt.registerTask('default', ['ember_templates']) 

然后,在我们的Express.js服务器configuration中:

 app.use require("connect-assets")() 

在我们的index.html文件中,我们需要在适当的位置添加两行。 这些需要通过我们select的Node.js模板引擎进行处理:

 <%- css("application") %> <%- js("application") %> 

然后我们需要创buildassets / css / application.styl(可以使用@import指令)和assets / js / application.coffee(可以使用“#= require foo”注释)。

要使用这个系统,首先运行:

 grunt 

为了使template.js文件永久保持最新,请运行:

 grunt watch 

对于其他所有内容,请参阅connect-assets文档 。 请注意,我使用Stylus的运气比Lessless,在编写本文时,这似乎与连接资产有关。

其他工具正在迅速成熟

自从我写了这个答案之后,我遇到了几个很好的select来处理资产编译的各种方法:

  • ember-tools不支持CoffeeScript或样式表(据我所知),但它处理其他资产编译,似乎很受欢迎。
  • brunch.io处理各种资产编译任务,包括CoffeeScript和各种CSS预处理器。 目前最有前途的插件似乎是brunch-with-ember-reloaded 。

我开始着手使用一个带有一个ember项目的Assetfile的设置,这是基于peepcode教程,并添加了构build工具,请参阅: https : //github.com/pixelhandler/peepcode-ordr

至于编译咖啡脚本这是一个例子… https://github.com/OC-Emberjs/peepcode-ordr-test/blob/assetmanager/Assetfile

 # Assetfile require 'rake-pipeline-web-filters' output "public" input "js/tests" do match "**/*.coffee" do coffee_script concat "tests.js" end end # vim:ft=ruby 

和预编译的Handlebars模板像这样…

 # Assetfile # See Getting Started readme # - https://github.com/livingsocial/rake-pipeline/blob/master/GETTING_STARTED.md # See Assetfile examples: # - https://gist.github.com/dudleyf/1557811 # - https://github.com/ryanto/ryanto.github.com/blob/master/Assetfile require "rake-pipeline-web-filters" output "public" class HandlebarsFilter < Rake::Pipeline::Filter def generate_output(inputs, output) inputs.each do |input| # for sub-templates we can't really use '/' in a filename so using '__' instead, then replacing name = File.basename(input.fullpath, ".handlebars").sub(/__/, "/") output.write "return Ember.TEMPLATES['#{name}'] = Ember.Handlebars.compile(#{input.read.to_json})" end end end input "app/templates" do match "**/*.handlebars" do filter HandlebarsFilter name = proc { |input| File.basename(input.fullpath, ".handlebars").sub(/__/, "/") + "_template" } minispade :module_id_generator => name concat "js/templates.js" end end # vim:ft=ruby 

这是我用来从以下开始的示例文件: https : //github.com/hjr3/dasfd/blob/master/Assetfile