Skip to content

Commit

Permalink
Merge pull request #14 from yoichiro/optimization-of-loading-pertials
Browse files Browse the repository at this point in the history
Add a new option to optimize a partial files registration.
  • Loading branch information
yoichiro authored Sep 23, 2024
2 parents 1e2f7eb + 76157b6 commit 9e5738e
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ This plugin can be configured with the following options:

* `templateFileExtension` (string) - Specifies the extension of Handlebars template files. Defaults to `hbs` if omitted.
* `partialDirectoryPath` (string) - Specifies the path to the directory containing partial template files to be included in Handlebars template files. If omitted, partial template files are not registered.
* `optimizePartialRegistration` (boolean) - Set to true to optimize the partial registration. This option is effective only when `partialsDirectoryPath` is set. If omitted, the plugin does not optimize the partial registration. If true, the plugin does not register the partials that are already registered.
* `compileOptions` (object) - Specifies the options to be passed to the Handlebars compiler. If omitted, the default options are used.

These options can be specified as arguments to the `handlebarsPlugin` function. Below is an example that specifies `handlebars` as the template file extension and `templates/partials` as the directory containing partial template files.
Expand All @@ -85,7 +86,8 @@ export default defineConfig({
plugins: [
handlebarsPlugin({
templateFileExtension: 'handlebars',
partialDirectoryPath: path.resolve(__dirname, 'templates', 'partials')
partialDirectoryPath: path.resolve(__dirname, 'templates', 'partials'),
optimizePartialRegistration: true,
})
]
});
Expand Down
1 change: 1 addition & 0 deletions integration/partials/sub/sub.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<i>This is a partial in the sub directory.</i>
13 changes: 7 additions & 6 deletions integration/templates/template.handlebars
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<section>
<h3>Hello, {{ name }}!</h3>
{{> message}}
</section>

{{> footer}}
<section>
<h3>Hello, {{ name }}!</h3>
<p>{{> message}}</p>
<p>{{> sub/sub }}</p>
</section>

{{> footer}}
1 change: 1 addition & 0 deletions integration/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineConfig({
handlebarsPlugin({
templateFileExtension: '.handlebars',
partialsDirectoryPath: resolve(__dirname, 'partials'),
optimizePartialRegistration: true,
}),
],
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yoichiro/vite-plugin-handlebars",
"version": "1.2.0",
"version": "1.3.0",
"author": {
"name": "Yoichiro Tanaka",
"email": "yoichiro6642@gmail.com",
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ export type HandlebarsPluginOptions = {
* If omitted, the plugin does not load and compile partial Handlebars files.
*/
partialsDirectoryPath?: string;
/**
* Set to true to optimize the partial registration.
* This option is effective only when `partialsDirectoryPath` is set.
* If omitted, the plugin does not optimize the partial registration.
* If true, the plugin does not register the partials that are already registered.
*/
optimizePartialRegistration?: boolean;
/** The compile options for the Handlebars compiler. */
compileOptions?: CompileOptions;
};
Expand All @@ -86,7 +93,8 @@ export default function handlebarsPlugin(
id,
templateFileExtension,
options.partialsDirectoryPath,
options.compileOptions
options.compileOptions,
options.optimizePartialRegistration
);
return {
code: transformed,
Expand Down
12 changes: 8 additions & 4 deletions src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export const transform = (
_id: string,
templateFileExtension: string,
partialsDirectoryPath: string | undefined,
compileOptions: CompileOptions | undefined
compileOptions: CompileOptions | undefined,
optimizePartialRegistration: boolean | undefined
): string => {
const partialMap = getPartialMap(
templateFileExtension,
Expand All @@ -72,9 +73,12 @@ export const transform = (
import Handlebars from 'handlebars/runtime';
${Object.entries(partialMap)
.map(
([name, content]) =>
`Handlebars.registerPartial('${name}', Handlebars.template(${content}));`
.map(([name, content]) =>
optimizePartialRegistration
? `if (Handlebars.partials['${name}'] === undefined) {
Handlebars.registerPartial('${name}', Handlebars.template(${content}));
}`
: `Handlebars.registerPartial('${name}', Handlebars.template(${content}));`
)
.join('\n')}
export default Handlebars.template(${precompiled});`;
Expand Down

0 comments on commit 9e5738e

Please sign in to comment.