From f2ec3d9e9bb458f0dc1858513d04d388e7d0cc83 Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Thu, 30 Nov 2023 13:06:46 -0500 Subject: [PATCH] Rename, refactor handlebars-precompiler plugin The rename is a matter of personal preference at the moment. The refactors aim to make the code slightly more concise (10 lines in total). --- ...> rollup-plugin-handlebars-precompiler.js} | 42 +++++++------------ strcalc/src/main/frontend/vite.config.js | 4 +- 2 files changed, 18 insertions(+), 28 deletions(-) rename strcalc/src/main/frontend/{rollup-plugin-handlebars-precompile.js => rollup-plugin-handlebars-precompiler.js} (84%) diff --git a/strcalc/src/main/frontend/rollup-plugin-handlebars-precompile.js b/strcalc/src/main/frontend/rollup-plugin-handlebars-precompiler.js similarity index 84% rename from strcalc/src/main/frontend/rollup-plugin-handlebars-precompile.js rename to strcalc/src/main/frontend/rollup-plugin-handlebars-precompiler.js index b941490..f5f1acf 100644 --- a/strcalc/src/main/frontend/rollup-plugin-handlebars-precompile.js +++ b/strcalc/src/main/frontend/rollup-plugin-handlebars-precompiler.js @@ -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 = '**/_*' @@ -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 { @@ -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 @@ -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') @@ -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) } } } diff --git a/strcalc/src/main/frontend/vite.config.js b/strcalc/src/main/frontend/vite.config.js index c24e730..b5aec28 100644 --- a/strcalc/src/main/frontend/vite.config.js +++ b/strcalc/src/main/frontend/vite.config.js @@ -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' @@ -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: {