from : http://handlebarsjs.com/precompilation.html
Handlebars프리컴파일러를 이용하면 핸들바 컴파일이 클라이언트 브라우저에서 컴파일되는 시간을 줄여주며, 필요한 핸들바스 라이브러리를 처리하는 시간을 감소 시켜 준다.
다음으로 npm패키지로 핸들바스를 설치하자.
이제 precompiler를 사용할 준비가 되었다.
첫번째로 알려진 헬퍼 리스트를 지정할 수 있다.
그리고 컴파일 타임에 알려진 헬퍼만 필요하다면 --knownOnly 옵션을 추가하여 최소한의 코드를 생성하도록 할 수 있다. 이는 더 빠르게 수행되도록 해준다.
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
precompiler의 노멀 모드를 이용한다면 결과 템플릿은 Handlebars.templates객체에 저장된다. 이 템플릿들은 동일한 템플릿 호출 방법으로 이용이 가능하다.
만약 단순 모드 프리컴파일을 한다면 이는 단일 자바 스크립트 메소드를 생성해낸다. 이는 Handlebars.template메소드를 거쳐야 하며, 결과 객체는 노멀로 이용될 수 있다.
Handlebars프리컴파일러를 이용하면 핸들바 컴파일이 클라이언트 브라우저에서 컴파일되는 시간을 줄여주며, 필요한 핸들바스 라이브러리를 처리하는 시간을 감소 시켜 준다.
Getting Started
우선 OS X에서 node를 인스톨 하거나, npm을 인스톨 할 필요가 있다.$ brew install node이 가정은 Homebrew가 미리 설치 되어 있다는 것을 가정한다. 설치를 위해서는 다음을 보자.
다음으로 npm패키지로 핸들바스를 설치하자.
$ npm install handlebars -g-g옵션을 이용하면 패키지를 글로벌 속성으로 설치하겠다는 의미이다. 이러게 하면 어떠한 프로젝트에서든지 사용이 가능하다.
이제 precompiler를 사용할 준비가 되었다.
$handlebars <input> -f <output>컴파일러는 Handlebars.template에 템플릿을 인서트 할 것이다. 만약 입력 파일이 person.handlebars라면 컴파일러는 Handlebars.template.person에 추가한다. 이 템플릿은 다음과 같은 장식으로 직접 호출 될 수 있다.
Handlebars.templates.person(context, options);만약 precompile된 template로 작업을 한다면 디플로이된 어플리케이션에 컴파일러를 추가할 필요가 없다. 대신에 더 가벼운 "runtime" 빌드를 이용할 수 있다.
<script src="/libs/handlebars.runtime.js"></script>추가적으로 다운로드 사이즈를 줄여주기 위해서는 클라이언트 사이드의 컴파일을 제거하면 된다. 이러면 부트 타임을 극적으로 줄여준다. 컴파일 타임은 핸들바의 사용에 있어 매우 비싼 작업이다.
Optimizations
precompile를 수행할때 몇가지 최적화를 수행할 수 있다.첫번째로 알려진 헬퍼 리스트를 지정할 수 있다.
handlebars <input> -f <output> - each -k if -k unlesshandlebars 컴파일러는 이러한 헬퍼에 접근하기 위한 최적화를 할 수 있도록 해준다.
그리고 컴파일 타임에 알려진 헬퍼만 필요하다면 --knownOnly 옵션을 추가하여 최소한의 코드를 생성하도록 할 수 있다. 이는 더 빠르게 수행되도록 해준다.
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
precompiler의 노멀 모드를 이용한다면 결과 템플릿은 Handlebars.templates객체에 저장된다. 이 템플릿들은 동일한 템플릿 호출 방법으로 이용이 가능하다.
만약 단순 모드 프리컴파일을 한다면 이는 단일 자바 스크립트 메소드를 생성해낸다. 이는 Handlebars.template메소드를 거쳐야 하며, 결과 객체는 노멀로 이용될 수 있다.