handlebars预编译
handlebars预编译
浮世微凉 发表于2年前
handlebars预编译
  • 发表于 2年前
  • 阅读 37
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】买域名送云解析+SSL证书+建站!>>>   

摘要: handlebars预编译

Using the Handlebars precompiler, you can precompile your Handlebars templates to save time on the client and reduce the required runtime size of the handlebars library.

Getting Started

First, you will need to install node and npm. On OS X:
$ brew install node
This assumes you already have Homebrew installed. If not,  install it first.
Next, install the Handlebars npm package.
$ npm install handlebars -g
Using the -g flag installs the package globally, so it can be used in any project.
Now, you're ready to use the precompiler:
$ handlebars <input> -f <output>
The compiler will insert templates in Handlebars.templates. If your input file is person.handlebars, the compiler will insert it at Handlebars.templates.person. This template will be a function that may be directly executed in the same manner as templates compiled locally. I.e.
Handlebars.templates.person(context, options);
If you're working with precompiled templates, you don't need to ship the compiler with your deployed application. Instead, you can use the smaller "runtime" build.
<script src="/libs/handlebars.runtime.js"></script>
In addition to reducing the download size, eliminating client-side compilation will significantly speed up boot time, as compilation is the most expensive part of Handlebars.

Optimizations

Because you are precompiling templates, you can also apply several optimization to the compiler. The first allows you to specify a list of the known helpers to the compiler
handlebars <input> -f <output> -k each -k if -k unless
The Handlebars compiler will optimize accesses to those helpers for performance.
When all helpers are known at compile time, the --knownOnly option provides the smallest generated code that also provides the fastest execution.

Usage

Usage: node ./bin/handlebars [template|directory]...

Options:
  -f, --output         Output File
  --map                Source Map File                                                    [string]  [default: undefined]
  -a, --amd            Exports amd style (require.js)
  -c, --commonjs       Exports CommonJS style, path to Handlebars module                                 [default: null]
  -h, --handlebarPath  Path to handlebar.js (only valid for amd-style)                                     [default: ""]
  -k, --known          Known helpers
  -o, --knownOnly      Known helpers only
  -m, --min            Minimize output
  -n, --namespace      Template namespace                                              [default: "Handlebars.templates"]
  -s, --simple         Output template function only.
  -N, --name           Name of passed string templates. Optional if running in a simple mode. Required when operating
                       on multiple templates.
  -i, --string         Generates a template from the passed CLI argument.
                       "-" is treated as a special value and causes stdin to be read for the template value.
  -r, --root           Template root. Base value that will be stripped from template names.
  -p, --partial        Compiling a partial template
  -d, --data           Include data when compiling
  -e, --extension      Template extension.                                                       [default: "handlebars"]
  -b, --bom            Removes the BOM (Byte Order Mark) from the beginning of the templates.
  -v, --version        Prints the current compiler version
  --help               Outputs this message
If using the precompiler's normal mode, the resulting templates will be stored to the Handlebars.templatesobject using the relative template name sans the extension. These templates may be executed in the same manner as templates.
If using the simple mode the precompiler will generate a single javascript method. To execute this method it must be passed to the Handlebars.template method and the resulting object may be used as normal.

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 13
码字总数 2789
×
浮世微凉
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: