Skip to content

Commit

Permalink
Merge 5b8a415 into 2971cae
Browse files Browse the repository at this point in the history
  • Loading branch information
mbland committed Nov 30, 2023
2 parents 2971cae + 5b8a415 commit 386065a
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions strcalc/src/main/frontend/rollup-plugin-handlebars-precompile.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const DEFAULT_EXCLUDE = 'node_modules/**'
const PLUGIN_ID = `\0${PLUGIN_NAME}`
const HANDLEBARS_PATH = 'handlebars/lib/handlebars.runtime'
const IMPORT_HANDLEBARS = `import Handlebars from '${HANDLEBARS_PATH}'`
const IMPORT_HELPERS = `import '${PLUGIN_ID}'`

class PluginImpl {
#options
Expand Down Expand Up @@ -74,14 +75,16 @@ class PluginImpl {
isTemplate(id) { return this.#isTemplate(id) }

compiledModule(code) {
const precompiled = Handlebars.precompile(code, this.#options)
let imports = this.#helpers.length ? [`import '${PLUGIN_ID}'`] : []
const compOpts = this.#options.compiler
const ast = Handlebars.parse(code, compOpts)
const tmpl = Handlebars.precompile(ast, compOpts)

return {
code: [
IMPORT_HANDLEBARS,
...imports,
`export default Handlebars.template(${precompiled.toString()})`
...(this.#helpers.length ? [ IMPORT_HELPERS ] : []),
`const Template = Handlebars.template(${tmpl.toString()})`,
'export default Template'
].join('\n')
}
}
Expand Down

0 comments on commit 386065a

Please sign in to comment.