【问题标题】:Asset pipeline for Ember.js, Express.js and Node.js?Ember.js、Express.js 和 Node.js 的资产管道?
【发布时间】:2013-03-08 19:55:59
【问题描述】:

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

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

我已经简要了解了 Require.js、connect-assets 和 convoy。前两个似乎没有提供任何简单的方法来预编译 Handlebars 模板,而 Ember convoy integration 是基于 Ember 的过时版本。

ember-runner 有一段时间没有更新了。 grunt-ember-templates 看起来是一种将 Ember 模板编译为单个 *.js 文件的合理方法,因此这可能是更大解决方案的构建块。

是否有任何 Node.js 资产编译系统仅适用于 Ember.js?我很想拥有一个相当于 ember-rails 的 Node.js。

【问题讨论】:

    标签: node.js ember.js express asset-pipeline assets


    【解决方案1】:

    只使用connect-assets、grunt-ember-templates 和 Gruntfile 就可以构建一个非常方便的系统。首先,我们需要在 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 服务器配置中:

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

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

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

    然后我们需要创建 assets/css/application.styl(可以使用 @import 指令)和 assets/js/application.coffee(可以使用 "#= require foo" cmets)。

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

    grunt
    

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

    grunt watch
    

    有关其他所有内容,请参阅connect-assets documentation。请注意,我使用 Stylus 比使用 Less 更幸运,这在撰写本文时似乎存在连接资产问题。

    其他正在迅速成熟的工具

    自从我写下这个答案后,我遇到了其他几个以各种方式处理资产编译的好选择:

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

    【讨论】:

    • (提示:添加您已更新的评论,并有机会获得奖励 +1:s :)
    【解决方案2】:

    【讨论】:

    • 谢谢!这里有一些非常好的想法,可以与 grunt-ember-templates 和上面的其他工具结合使用。但我仍然喜欢更接近 Rails 对 Sprockets 和 ember-rails 所做的事情。
    【解决方案3】:

    我开始着手设置将 Assetfile 与 ember 项目一起使用,这是基于 peepcode 教程并添加了构建工具,请参阅: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

    【讨论】:

    • 这是一个非常酷的方法!它需要使用 Rake,我想在其他全 Node.js 环境中避免使用它,但它肯定很优雅。
    猜你喜欢
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多