diff --git a/index.js b/index.js index e20053e..da41b74 100644 --- a/index.js +++ b/index.js @@ -36,35 +36,56 @@ class HtmlWebpackPartialsPlugin { // to index.html if not set return partial.should_inject && ( - partial.template_filename === data.plugin.options.filename || + partial.template_filename === data.plugin.options.filename || Array.isArray(partial.template_filename) ? partial.template_filename.includes(data.plugin.options.filename) : partial.template_filename === '*' ); - }).forEach(partial => { + }).forEach(partial => this.buildTemplate(partial, data)); - // Once we know we're using the partial, read the file and create a template + callback(null, data); + + }); - partial.createTemplate(); + }); - // Finally inject the partial into the HTML stream + } - data.html = Util.injectPartial(data.html, { - options: partial.options, - html: partial.template(partial.options), - priority: partial.priority, - location: partial.location, - }); - }); + /** + * Build template from Partial object and data.html, returning new html. + */ + buildTemplate(partial, data){ + // Once we know we're using the partial, read the file and create a template - callback(null, data); + partial.createTemplate(); + // Get partial HTML + let partial_html = partial.template(partial.options); + + // Iterate through sub-partials and build into partial_html + if(partial.options.subPartials != null && Array.isArray(partial.options.subPartials)){ + + partial.options.subPartials.map(subpartial => { + return new Partial(subpartial); + }).forEach(subpartial => { + partial_html = this.buildTemplate(subpartial, { html: partial_html }); }); + } + + + // Finally inject the partial into the HTML stream + + data.html = Util.injectPartial(data.html, { + options: partial.options, + html: partial_html, + priority: partial.priority, + location: partial.location, }); + return data.html; } }