Skip to content

Commit

Permalink
Merge pull request #23 from mbland/rollup-plugin-handlebars-precompil…
Browse files Browse the repository at this point in the history
…er-rename-refactor

Rename, refactor handlebars-precompiler plugin
  • Loading branch information
mbland authored Nov 30, 2023
2 parents 4264509 + f2ec3d9 commit f164d10
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import { createFilter } from '@rollup/pluginutils'
import Handlebars from 'handlebars'

const PLUGIN_NAME = 'handlebars-precompile'
const PLUGIN_NAME = 'handlebars-precompiler'
const DEFAULT_INCLUDE = ['**/*.hbs', '**/*.handlebars', '**/*.mustache']
const DEFAULT_EXCLUDE = 'node_modules/**'
const DEFAULT_PARTIALS = '**/_*'
Expand All @@ -59,22 +59,17 @@ const IMPORT_HELPERS = `import '${PLUGIN_ID}'`
class PartialCollector extends Handlebars.Visitor {
partials = []

constructor() { super() }

PartialStatement(partial) {
return super.PartialStatement(this.collect(partial))
this.collect(partial.name)
return super.PartialStatement(partial)
}

PartialBlockStatement(partial) {
return super.PartialBlockStatement(this.collect(partial))
this.collect(partial.name)
return super.PartialBlockStatement(partial)
}

collect(partial) {
if (partial.name.type === 'PathExpression') {
this.partials.push(partial.name.original)
}
return partial
}
collect(n) { if (n.type === 'PathExpression') this.partials.push(n.original) }
}

class PluginImpl {
Expand All @@ -97,9 +92,9 @@ class PluginImpl {
this.#partialPath = options.partialPath || DEFAULT_PARTIAL_PATH
}

shouldEmitHelpersModule(id) {
return id === PLUGIN_ID && this.#helpers.length
}
hasHelpers() { return this.#helpers.length }
shouldEmitHelpersModule(id) { return id === PLUGIN_ID && this.hasHelpers() }
isTemplate(id) { return this.#isTemplate(id) }

helpersModule() {
const helpers = this.#helpers
Expand All @@ -110,21 +105,19 @@ class PluginImpl {
].join('\n')
}

isTemplate(id) { return this.#isTemplate(id) }

compiledModule(code, id) {
const compOpts = this.#options.compiler
const ast = Handlebars.parse(code, compOpts)
const tmpl = Handlebars.precompile(ast, compOpts)
compile(code, id) {
const opts = this.#options.compiler
const ast = Handlebars.parse(code, opts)
const tmpl = Handlebars.precompile(ast, opts)
const collector = new PartialCollector()
collector.accept(ast)

return {
code: [
IMPORT_HANDLEBARS,
...(this.#helpers.length ? [ IMPORT_HELPERS ] : []),
...(this.hasHelpers() ? [ IMPORT_HELPERS ] : []),
...collector.partials.map(p => `import '${this.#partialPath(p, id)}'`),
`const Template = Handlebars.template(${tmpl.toString()})`,
`const Template = Handlebars.template(${tmpl})`,
'export default Template',
...(this.#isPartial(id) ? [ this.partialRegistration(id) ] : [])
].join('\n')
Expand All @@ -138,13 +131,10 @@ class PluginImpl {

export default function(options) {
const p = new PluginImpl(options)

return {
name: PLUGIN_NAME,
resolveId(id) { if (p.shouldEmitHelpersModule(id)) return id },
load(id) { if (p.shouldEmitHelpersModule(id)) return p.helpersModule() },
transform(code, id) {
if (p.isTemplate(id)) return p.compiledModule(code, id)
}
transform(code, id) { if (p.isTemplate(id)) return p.compile(code, id) }
}
}
4 changes: 2 additions & 2 deletions strcalc/src/main/frontend/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import handlebarsPrecompiler from './rollup-plugin-handlebars-precompile.js'
import handlebarsPrecompiler from './rollup-plugin-handlebars-precompiler.js'
import { defineConfig } from 'vite'
import { configDefaults } from 'vitest/config'
import path from 'node:path/posix'
Expand All @@ -25,7 +25,7 @@ export default defineConfig({
// into its own repository.
exclude: [
...configDefaults.coverage.exclude,
'rollup-plugin-handlebars-precompile.js'
'rollup-plugin-handlebars-precompiler.js'
]
},
browser: {
Expand Down

0 comments on commit f164d10

Please sign in to comment.