4
4
5
5
Features:
6
6
7
- * Import Handlebars templates as ES6 modules
8
- * Support for Handlebars [ helpers] ( #helpers ) and partials
9
- * [ Precompiles] ( http://handlebarsjs.com/precompilation.html ) templates so your application only needs the Handlebars runtime
10
- * Handlebars runtime [ included] ( #handlebars )
11
- * Optional rendering to [ jQuery collections] ( #jquery ) vs. raw strings
7
+ - Import Handlebars templates as ES6 modules
8
+ - Support for Handlebars [ helpers] ( #helpers ) and partials
9
+ - [ Precompiles] ( http://handlebarsjs.com/precompilation.html ) templates so your application only needs the Handlebars runtime
10
+ - Handlebars runtime [ included] ( #handlebars )
11
+ - Optional rendering to [ jQuery collections] ( #jquery ) vs. raw strings
12
12
13
13
## Installation
14
14
@@ -31,15 +31,14 @@ var rollup = require('rollup');
31
31
var handlebars = require (' rollup-plugin-handlebars-plus' );
32
32
var rootImport = require (' rollup-plugin-root-import' );
33
33
34
-
35
34
var partialRoots = [` ${ __dirname } /src/client/js/views/` , ` ${ __dirname } /src/common/views/` ];
36
35
37
36
rollup ({
38
37
entry: ' main.js' ,
39
38
plugins: [
40
39
// Required by use of `partialRoot` below.
41
40
rootImport ({
42
- root: partialRoots
41
+ root: partialRoots,
43
42
}),
44
43
handlebars ({
45
44
handlebars: {
@@ -55,14 +54,20 @@ rollup({
55
54
// Options to pass to Handlebars' `parse` and `precompile` methods.
56
55
options: {
57
56
// Whether to generate sourcemaps for the templates
58
- sourceMap: true // Default: true
59
- }
57
+ sourceMap: true , // Default: true
58
+ },
60
59
},
61
60
62
61
// The ID(s) of modules to import before every template, see the "Helpers" section below.
63
62
// Can be a string too.
64
63
helpers: [' /utils/HandlebarsHelpers.js' ], // Default: none
65
64
65
+ // Whether to register the defined helpers at template declaration in a way that would allow
66
+ // the initialization call to be elided if the template is never used. Useful in a library
67
+ // context where the templates might all get tree-shaken away, leaving no need for the
68
+ // helpers. Does nothing if helpers is empty.
69
+ helpersPureInitialize: true , // Default: false
70
+
66
71
// In case you want to compile files with other extensions.
67
72
templateExtension: ' .html' , // Default: '.hbs'
68
73
@@ -74,10 +79,10 @@ rollup({
74
79
partialRoot: partialRoots, // Default: none
75
80
76
81
// The module ID of jQuery, see the "jQuery" section below.
77
- jquery: ' jquery' // Default: none
78
- })
79
- ]
80
- })
82
+ jquery: ' jquery' , // Default: none
83
+ }),
84
+ ],
85
+ });
81
86
```
82
87
83
88
lets you do this:
@@ -114,16 +119,16 @@ rollup({
114
119
entry: ' main.js' ,
115
120
plugins: [
116
121
handlebars ({
117
- helpers: [' /utils/HandlebarsHelpers.js' ]
118
- })
119
- ]
120
- })
122
+ helpers: [' /utils/HandlebarsHelpers.js' ],
123
+ }),
124
+ ],
125
+ });
121
126
```
122
127
123
128
``` js
124
129
// /utils/HandlebarsHelpers.js
125
- export default function (Handlebars ) {
126
- Handlebars .registerHelper (' encodeURIComponent' , function (text ) {
130
+ export default function (Handlebars ) {
131
+ Handlebars .registerHelper (' encodeURIComponent' , function (text ) {
127
132
return new Handlebars.SafeString (encodeURIComponent (text));
128
133
});
129
134
}
@@ -168,9 +173,9 @@ rollup({
168
173
commonjs ({
169
174
include: ' node_modules/**' ,
170
175
}),
171
- handlebars ()
172
- ]
173
- })
176
+ handlebars (),
177
+ ],
178
+ });
174
179
```
175
180
176
181
In case you need the default runtime ID, it's available as ` handlebars.runtimeId ` . This might be
@@ -192,8 +197,8 @@ import Template from './index.html';
192
197
var MyView = Backbone .View .extend ({
193
198
render () {
194
199
this .setElement (Template ());
195
- }
196
- })
200
+ },
201
+ });
197
202
```
198
203
199
204
or by customizing the template using jQuery's APIs:
@@ -206,7 +211,7 @@ var tooltip = TooltipTemplate();
206
211
207
212
tooltip .css ({
208
213
left: 50 ,
209
- top: 100
214
+ top: 100 ,
210
215
});
211
216
212
217
$ (' body' ).append (tooltip);
@@ -223,10 +228,10 @@ rollup({
223
228
entry: ' main.js' ,
224
229
plugins: [
225
230
handlebars ({
226
- jquery: ' jquery'
227
- })
228
- ]
229
- })
231
+ jquery: ' jquery' ,
232
+ }),
233
+ ],
234
+ });
230
235
```
231
236
232
237
Curious about how to ID jQuery when it's a global i.e. you're _ not_ bundling it?
0 commit comments