@@ -10,6 +10,13 @@ const DEFAULT_HANDLEBARS_ID = path.relative(
10
10
require . resolve ( 'handlebars/runtime' )
11
11
) ;
12
12
13
+ const INTERNAL_INIT_ID = '\0handlebarsPlusHelpersInit' ;
14
+
15
+ const escapePath = ( path ) => path . replace ( / \\ / g, '\\\\' ) ;
16
+
17
+ const nonEmptyOr = ( array , fallback ) => ( array . length ? array : fallback ) ;
18
+ const asArrayOr = ( value , fallback ) => nonEmptyOr ( [ ] . concat ( value || [ ] ) , fallback ) ;
19
+
13
20
/**
14
21
* Constructs a Rollup plugin to compile Handlebars templates.
15
22
*
@@ -74,7 +81,36 @@ function handlebars(options) {
74
81
const Handlebars = options . handlebars . module || require ( 'handlebars' ) ;
75
82
const ImportScanner = require ( './ImportScanner' ) ( Handlebars ) ;
76
83
84
+ const hbsImport = `import Handlebars from '${ escapePath ( options . handlebars . id ) } ';\n` ;
85
+
86
+ const wrapTemplateDefinition = options . helpersPureInitialize
87
+ ? ( defineTemplate , initExpr ) =>
88
+ defineTemplate ( ( expr ) => `(function() {${ initExpr } ;return ${ expr } ;})()` )
89
+ : ( defineTemplate , initExpr ) => `${ initExpr } ;\n${ defineTemplate ( ) } ` ;
90
+
91
+ // Support `helpers` being singular or plural.
92
+ const helpers = asArrayOr ( options . helpers , null ) ;
93
+
77
94
return {
95
+ resolveId : ( id ) => ( helpers && id === INTERNAL_INIT_ID ? id : undefined ) ,
96
+
97
+ load ( id ) {
98
+ if ( ! helpers || id !== INTERNAL_INIT_ID ) return ;
99
+
100
+ let body = hbsImport ;
101
+ body += '' ;
102
+
103
+ const initExpr = helpers . map ( ( helperPath , i ) => {
104
+ const ref = `Helpers${ i } ` ;
105
+ body += `import ${ ref } from '${ escapePath ( helperPath ) } ';\n` ;
106
+ return ` ${ ref } .__initialized || (${ ref } (Handlebars), ${ ref } .__initialized = true);\n` ;
107
+ } ) ;
108
+
109
+ body += `export default function() {\n${ initExpr . join ( '' ) } }\n` ;
110
+
111
+ return { code : body , map : { mappings : '' } } ;
112
+ } ,
113
+
78
114
transform ( code , id ) {
79
115
if ( ! id . endsWith ( options . templateExtension ) ) return ;
80
116
@@ -95,28 +131,21 @@ function handlebars(options) {
95
131
template = template . code ;
96
132
}
97
133
98
- const escapePath = ( path ) => path . replace ( / \\ / g, '\\\\' ) ;
99
-
100
- let body = `import Handlebars from '${ escapePath ( options . handlebars . id ) } ';\n` ;
134
+ let body = hbsImport ;
101
135
if ( options . jquery ) body += `import $ from '${ escapePath ( options . jquery ) } ';\n` ;
102
136
103
- if ( options . helpers ) {
104
- // Support `helpers` being singular or plural.
105
- [ ] . concat ( options . helpers ) . forEach ( ( helpers , i ) => {
106
- body += `import Helpers${ i } from '${ escapePath ( helpers ) } ';\n` ;
107
- body += `if (!Helpers${ i } .__initialized) {\n` ;
108
- body += ` Helpers${ i } (Handlebars);\n` ;
109
- body += ` Helpers${ i } .__initialized = true;\n` ;
110
- body += `}\n` ;
111
- } ) ;
112
- }
137
+ body += `import init from '${ INTERNAL_INIT_ID } ';\n` ;
113
138
114
139
for ( const partial of scanner . partials ) {
115
140
// Register the partial dependencies as partials.
116
141
body += `import '${ escapePath ( partial ) } ${ options . templateExtension } ';\n` ;
117
142
}
118
143
119
- body += `var Template = /*#__PURE__*/Handlebars.template(${ template } );\n` ;
144
+ body += wrapTemplateDefinition (
145
+ ( wrapExpression = ( expr ) => expr ) =>
146
+ `var Template = /*#__PURE__*/${ wrapExpression ( `Handlebars.template(${ template } )` ) } ;\n` ,
147
+ 'init()'
148
+ ) ;
120
149
121
150
if ( options . isPartial ( name ) ) {
122
151
let partialName = id ;
0 commit comments