UNPKG

5.52 kBMarkdownView Raw
1# ember-cli-htmlbars-inline-precompile
2
3[![Build Status](https://travis-ci.org/ember-cli/ember-cli-htmlbars-inline-precompile.svg?branch=master)](https://travis-ci.org/ember-cli/ember-cli-htmlbars-inline-precompile)
4[![Ember Observer Score](http://emberobserver.com/badges/ember-cli-htmlbars-inline-precompile.svg)](http://emberobserver.com/addons/ember-cli-htmlbars-inline-precompile)
5[![Dependency Status](https://david-dm.org/ember-cli/ember-cli-htmlbars-inline-precompile.svg)](https://david-dm.org/ember-cli/ember-cli-htmlbars-inline-precompile)
6
7Precompile HTMLBars template strings within the tests of an Ember-CLI project
8via ES6 tagged template strings:
9
10``` js
11// ember-cli-project/test/unit/components/my-component-test.js
12import hbs from 'htmlbars-inline-precompile';
13import { moduleForComponent, test } from 'ember-qunit';
14
15moduleForComponent('my-component');
16
17test('it renders', function(assert) {
18 var component = this.subject({
19 greeting: "hello ember testing",
20 layout: hbs`
21 greeting: <span>{{greeting}}</span>
22 `
23 });
24
25 assert.equal(this.$().html().trim(), "greeting: <span>hello ember testing</span>");
26});
27```
28
29---
30
31
32If you are using `ember-cli-qunit@0.3.12`, writing component integration tests
33becomes as readable as:
34
35``` js
36import hbs from 'htmlbars-inline-precompile';
37import { moduleForComponent, test } from 'ember-qunit';
38
39moduleForComponent('my-component', {
40 integration: true
41});
42
43test('block params work', function(assert) {
44 this.render(hbs`
45 {{#my-component date=theDate as |daysAgo| }}
46 This happened {{daysAgo}} days ago.
47 {{/my-component}}
48 `);
49
50 this.set('theDate', new Date(2015, 2, 11));
51 assert.equal(this.$().text().trim(), "This happened 123 days ago.");
52});
53
54```
55
56### CoffeeScript support
57
58Version `0.2.0` introduced the possibility to use this addon within
59CoffeeScript, using [`ember-cli-coffeescript`](https://github.com/kimroen/ember-cli-coffeescript). Since the
60backtick ``` ` ``` is used to embed JavaScript, the `hbs` function can be invoked with the
61template as a normal string:
62
63``` coffeescript
64`import hbs from 'htmlbars-inline-precompile';`
65`import { moduleForComponent, test } from 'ember-qunit';`
66
67moduleForComponent "my-component",
68 integration: true
69
70test "block params work", (assert) ->
71 @render hbs '''
72 {{#my-component date=theDate as |daysAgo| }}
73 This happened {{daysAgo}} days ago.
74 {{/my-component}}
75 '''
76
77 @set 'theDate', new Date(2015, 2, 11)
78 assert.equal this.$().text().trim(), "This happened 123 days ago."
79```
80
81
82
83### Installation
84
85Install the addon via `ember install ember-cli-htmlbars-inline-precompile`
86
87### Troubleshooting
88
89#### `Plugin undefined didn't export a default Transformer instance`
90
91If you get an error like `Plugin undefined didn't export a default Transformer
92instance` this likely means that the installed version of `babel-core` is
93outdated. You can check for the installed version via `npm ls babel-core`
94within the root of your Ember-CLI application:
95
96```
97$ npm ls babel-core
98your-app@0.0.0 ~/your-app
99└─┬ ember-cli-babel@5.0.0
100 └─┬ broccoli-babel-transpiler@5.0.0
101 └── babel-core@5.1.13
102```
103
104
105Since this addon relies on a feature implemented in `babel@v5.2.10`, you need
106to update your installed dependency of `ember-cli-babel` via:
107
108
109```
110rm -rf node_modules/ember-cli-babel
111npm install
112```
113
114After that the version of `babel-core` should be at least `5.2.10`:
115
116```
117$ npm ls babel-core
118your-app@0.0.0 ~/your-app
119└─┬ ember-cli-babel@5.0.0
120 └─┬ broccoli-babel-transpiler@5.0.0
121 └── babel-core@5.2.10
122```
123
124Starting the development environment via `ember server` or `ember test
125--server` should start as expected and your inline template strings are
126compiled.
127
128#### JSHint problems with `ember-cli-mocha`: `Expected ')' and instead saw '`
129
130If `ember-cli-mocha` complains with a message like `Expected ')' and instead saw '`,
131you need to upgrade the used `ember-cli-mocha` package in your Ember-CLI app/addon. This
132has been discussed in [switchfly/ember-cli-mocha#57](https://github.com/switchfly/ember-cli-mocha/pull/57#discussion_r32633195),
133where the solution is a clean [`npm install`](https://github.com/switchfly/ember-cli-mocha/pull/57#discussion_r32654980).
134
135
136### Caveats
137
138Keep in mind that the source files are transformed, so the inline template
139definitions are replaced with `Ember.HTMLBars.template(…)` statements. This
140means that you can't do fancy stuff like string interpolation within the
141templates:
142
143``` js
144test('string interpolation within templates is NOT supported', function(assert) {
145 var valuePath = 'greeting';
146 var component = this.subject({
147 greeting: "hello ember testing",
148 layout: hbs`
149 ${valuePath}: <span>{{value}}</span>
150 `
151 });
152
153 // the template will be "${valuePath}: <span>{{value}}</span>"
154```
155
156If you need stuff like this, you need to include `ember-template-compiler.js`
157in your test-build and use `Ember.HTMLBars.compile("…")` within your tests.
158
159### Alternatives
160
161- [broccoli-ember-inline-template-compiler](https://github.com/rwjblue/broccoli-ember-inline-template-compiler)
162- Include `ember-handlebars-compiler.js` in your test-build and compile client side via `Ember.HTMLBars.compile(…)`
163
164### Thanks
165
166This addon wouldn't exist without the lightning fast response by open source hero [@kittens](https://github.com/kittens),
167who implemented the [feature to replace a node with a source string](http://git.io/vJSrs) not
168even an hour after I mentioned it in gitter :heart:.