From 0eba5e46d27bb96f15bb31c83dfcd77a10e60946 Mon Sep 17 00:00:00 2001 From: Vincent Taglia Date: Sat, 28 Nov 2020 14:40:37 -0600 Subject: [PATCH 1/2] Added support for nested partials feature --- index.js | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index e20053e..9f8562c 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 subpartials 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; } } From b3609c11abcb392438ec4a6e2580a45bdce497f9 Mon Sep 17 00:00:00 2001 From: Vincent Taglia Date: Mon, 30 Nov 2020 01:43:57 -0600 Subject: [PATCH 2/2] Style changes --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 9f8562c..da41b74 100644 --- a/index.js +++ b/index.js @@ -53,7 +53,7 @@ class HtmlWebpackPartialsPlugin { } - /* + /** * Build template from Partial object and data.html, returning new html. */ buildTemplate(partial, data){ @@ -64,13 +64,13 @@ class HtmlWebpackPartialsPlugin { // Get partial HTML let partial_html = partial.template(partial.options); - // Iterate through subpartials and build into partial_html - if(partial.options["subPartials"] != null && Array.isArray(partial.options["subPartials"])){ + // Iterate through sub-partials and build into partial_html + if(partial.options.subPartials != null && Array.isArray(partial.options.subPartials)){ - partial.options["subPartials"].map(subpartial => { + partial.options.subPartials.map(subpartial => { return new Partial(subpartial); }).forEach(subpartial => { - partial_html = this.buildTemplate(subpartial, {html: partial_html}); + partial_html = this.buildTemplate(subpartial, { html: partial_html }); }); }