Skip to content

Commit

Permalink
Merge pull request #26 from mbland/handlebars-touch-ups
Browse files Browse the repository at this point in the history
Minor Handlebars plugin touch ups
  • Loading branch information
mbland authored Dec 2, 2023
2 parents b3dee43 + daa053a commit 0b5108c
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class PartialCollector extends Handlebars.Visitor {
}

class PluginImpl {
#options
#helpers
#isTemplate
#isPartial
Expand All @@ -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() {
Expand All @@ -123,29 +121,31 @@ 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('
]
const postTmpl = [
')',
// 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'),
map: srcMap ? this.#adjustSourceMap(srcMap, preTmpl.length) : null
}
}

#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)`
}
}
Expand Down

0 comments on commit 0b5108c

Please sign in to comment.