From daa053a675bf742bd5c581bfad6f229e0a0a6a4e Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Fri, 1 Dec 2023 23:13:09 -0500 Subject: [PATCH] Minor Handlebars plugin touch ups Removed PluginTmpl.#options, which became obsolete in #25 (commit e768bcc984c4a58e21583d62dd51ab41f560eb36). Made PluginImpl.#hasHelpers() and PluginImpl.#partialRegistration() private. Return the source map object from PluginImpl.#adjustSourceMap() instead of a JSON.stringified version. Though Handlebars.precompile() emits the source map as a string, I'm pretty sure Vite+Rollups ends up parsing it. Since #adjustSourceMap() already JSON.parses the string into an object, it seems worth returning the object instead of a string. --- .../rollup-plugin-handlebars-precompiler.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/strcalc/src/main/frontend/rollup-plugin-handlebars-precompiler.js b/strcalc/src/main/frontend/rollup-plugin-handlebars-precompiler.js index 0311c52..a6ff9ad 100644 --- a/strcalc/src/main/frontend/rollup-plugin-handlebars-precompiler.js +++ b/strcalc/src/main/frontend/rollup-plugin-handlebars-precompiler.js @@ -73,7 +73,6 @@ class PartialCollector extends Handlebars.Visitor { } class PluginImpl { - #options #helpers #isTemplate #isPartial @@ -100,8 +99,7 @@ class PluginImpl { () => options.compiler } - hasHelpers() { return this.#helpers.length } - shouldEmitHelpersModule(id) { return id === PLUGIN_ID && this.hasHelpers() } + shouldEmitHelpersModule(id) { return id === PLUGIN_ID && this.#hasHelpers() } isTemplate(id) { return this.#isTemplate(id) } helpersModule() { @@ -123,7 +121,7 @@ class PluginImpl { const preTmpl = [ IMPORT_HANDLEBARS, - ...(this.hasHelpers() ? [ IMPORT_HELPERS ] : []), + ...(this.#hasHelpers() ? [ IMPORT_HELPERS ] : []), ...collector.partials.map(p => `import '${this.#partialPath(p, id)}'`), 'const Template = Handlebars.template(' ] @@ -131,7 +129,7 @@ class PluginImpl { ')', // The trailing ';' prevents source map loss if it's the last line. 'export default Template;', - ...(this.#isPartial(id) ? [this.partialRegistration(id)] : []) + ...(this.#isPartial(id) ? [ this.#partialRegistration(id) ] : []) ] return { code: [ ...preTmpl, tmpl, ...postTmpl ].join('\n'), @@ -139,13 +137,15 @@ class PluginImpl { } } + #hasHelpers() { return this.#helpers.length } + #adjustSourceMap(map, preTmplLen) { const result = JSON.parse(map) result.mappings = `${';'.repeat(preTmplLen)}${result.mappings}` - return JSON.stringify(result) + return result } - partialRegistration(id) { + #partialRegistration(id) { return `Handlebars.registerPartial('${this.#partialName(id)}', Template)` } }