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 |
|
7 | Precompile HTMLBars template strings within the tests of an Ember-CLI project
|
8 | via ES6 tagged template strings:
|
9 |
|
10 | ``` js
|
11 | // ember-cli-project/test/unit/components/my-component-test.js
|
12 | import hbs from 'htmlbars-inline-precompile';
|
13 | import { moduleForComponent, test } from 'ember-qunit';
|
14 |
|
15 | moduleForComponent('my-component');
|
16 |
|
17 | test('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 |
|
32 | If you are using `ember-cli-qunit@0.3.12`, writing component integration tests
|
33 | becomes as readable as:
|
34 |
|
35 | ``` js
|
36 | import hbs from 'htmlbars-inline-precompile';
|
37 | import { moduleForComponent, test } from 'ember-qunit';
|
38 |
|
39 | moduleForComponent('my-component', {
|
40 | integration: true
|
41 | });
|
42 |
|
43 | test('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 |
|
58 | Version `0.2.0` introduced the possibility to use this addon within
|
59 | CoffeeScript, using [`ember-cli-coffeescript`](https://github.com/kimroen/ember-cli-coffeescript). Since the
|
60 | backtick ``` ` ``` is used to embed JavaScript, the `hbs` function can be invoked with the
|
61 | template as a normal string:
|
62 |
|
63 | ``` coffeescript
|
64 | `import hbs from 'htmlbars-inline-precompile';`
|
65 | `import { moduleForComponent, test } from 'ember-qunit';`
|
66 |
|
67 | moduleForComponent "my-component",
|
68 | integration: true
|
69 |
|
70 | test "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 |
|
85 | Install 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 |
|
91 | If you get an error like `Plugin undefined didn't export a default Transformer
|
92 | instance` this likely means that the installed version of `babel-core` is
|
93 | outdated. You can check for the installed version via `npm ls babel-core`
|
94 | within the root of your Ember-CLI application:
|
95 |
|
96 | ```
|
97 | $ npm ls babel-core
|
98 | your-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 |
|
105 | Since this addon relies on a feature implemented in `babel@v5.2.10`, you need
|
106 | to update your installed dependency of `ember-cli-babel` via:
|
107 |
|
108 |
|
109 | ```
|
110 | rm -rf node_modules/ember-cli-babel
|
111 | npm install
|
112 | ```
|
113 |
|
114 | After that the version of `babel-core` should be at least `5.2.10`:
|
115 |
|
116 | ```
|
117 | $ npm ls babel-core
|
118 | your-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 |
|
124 | Starting the development environment via `ember server` or `ember test
|
125 | --server` should start as expected and your inline template strings are
|
126 | compiled.
|
127 |
|
128 | #### JSHint problems with `ember-cli-mocha`: `Expected ')' and instead saw '`
|
129 |
|
130 | If `ember-cli-mocha` complains with a message like `Expected ')' and instead saw '`,
|
131 | you need to upgrade the used `ember-cli-mocha` package in your Ember-CLI app/addon. This
|
132 | has been discussed in [switchfly/ember-cli-mocha#57](https://github.com/switchfly/ember-cli-mocha/pull/57#discussion_r32633195),
|
133 | where the solution is a clean [`npm install`](https://github.com/switchfly/ember-cli-mocha/pull/57#discussion_r32654980).
|
134 |
|
135 |
|
136 | ### Caveats
|
137 |
|
138 | Keep in mind that the source files are transformed, so the inline template
|
139 | definitions are replaced with `Ember.HTMLBars.template(…)` statements. This
|
140 | means that you can't do fancy stuff like string interpolation within the
|
141 | templates:
|
142 |
|
143 | ``` js
|
144 | test('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 |
|
156 | If you need stuff like this, you need to include `ember-template-compiler.js`
|
157 | in 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 |
|
166 | This addon wouldn't exist without the lightning fast response by open source hero [@kittens](https://github.com/kittens),
|
167 | who implemented the [feature to replace a node with a source string](http://git.io/vJSrs) not
|
168 | even an hour after I mentioned it in gitter :heart:.
|