【问题标题】:How can I precompile HandlebarsJS templates from Visual Studio?如何从 Visual Studio 预编译 HandlebarsJS 模板?
【发布时间】:2013-04-27 23:14:13
【问题描述】:

是否可以从 Visual Studio 的构建后事件或 MVC Web 应用的 App_Start 中预编译 Handlebars 模板? 提前非常感谢。 戴尔

【问题讨论】:

    标签: asp.net-mvc visual-studio-2010 visual-studio-2012 handlebars.js


    【解决方案1】:

    当然,你有很多选择:

    1. 你可以为windows和npm安装node.js,并为编译配置一个post-build事件(example here from previous question)
    2. 如果您使用的是 ember.js,here's an implementation 使用捆绑转换来实现预编译。
    3. ember.js 的另一个支持组件here's the implementation,它也使用捆绑转换。
    4. 这是一个使用侏罗纪 JavaScript 编译器的example of compilation on C#

    【讨论】:

    • 谢谢 ahmed - 我昨天搜了又搜,没找到答案(上面的第 1 条)谢谢指出。那么我是否只需为 Windows 安装节点,然后将命令行内容添加到我的构建后事件中?
    • 是的,你基本上安装node,尝试编译方法,如果它工作正常,那么你可以设置一个批处理文件来运行构建后的命令
    • 感谢 Amhed - 非常感谢!
    【解决方案2】:

    一种方法是使用捆绑转换和 jurrasic js 编译器生成一个 js 文件,其中包含所有已编译的视图和部分视图。

    public class BundleConfig
    {
        public static void RegisterHandlBarBundles(BundleCollection bundles, string path)
        {
            HandleBarBundleTransform transform = new HandleBarBundleTransform();
            transform.jsPath = path;
            bundles.Add(new Bundle("~/views.js", transform).IncludeDirectory("~/views", "*.hbs", true));
            BundleTable.EnableOptimizations = true;
        }
    }
    

    这样做的好处是不需要 node.js 或 ember,同时仍然使用简单的 bundletransform 钩子。

    The full source for the HandleBarBundleTransform is here

    这具有 @ahmed 为不使用 ember 的用户发布的 ember 解决方案的便利性。

    【讨论】:

      【解决方案3】:

      我知道这个问题是在 Visual Studio 2010 上提出的,但我在寻找 VS2013 中 Handlebars 重新编译的解决方案时发现了这个问题。我花了一些时间寻找解决方案,直到我发现 Web Essentials for Visual Studio 在 2014 年 11 月添加了 auto-precompile of handlebars templates

      Web Essentials 中的这个功能并没有很好地宣传,我只是在构建解决方案时才发现它,发现在我编辑车把.hbs 文件时自动出现了一个.hbs.js 文件!当它已经存在时,我浪费了很多时间寻找解决方案。

      我希望这个答案对其他人有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-11
        • 1970-01-01
        • 2017-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多