Skip to content

Commit

Permalink
Merge pull request #7 from yoichiro/dynamic-reloading-for-partials
Browse files Browse the repository at this point in the history
Dynamic reloading for partials
  • Loading branch information
yoichiro authored Sep 11, 2024
2 parents e32227f + 4f3b1f3 commit 22f0f43
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
1 change: 1 addition & 0 deletions integration/partials/footer.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>This is footer partial.</p>
2 changes: 2 additions & 0 deletions integration/templates/template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
<h3>Hello, {{ name }}!</h3>
{{> message}}
</section>

{{> footer}}
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.0.2",
"version": "1.1.0",
"author": {
"name": "Yoichiro Tanaka",
"email": "yoichiro6642@gmail.com",
Expand Down
24 changes: 16 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Plugin } from 'vite';
import Handlebars from 'handlebars';
import {
createPartialMap,
decideTemplateFileExtension,
handleHotUpdate,
transform,
} from './internal';

Expand All @@ -17,22 +16,31 @@ export default function handlebarsPlugin(
const templateFileExtension = decideTemplateFileExtension(
options.templateFileExtension
);
const partialMap = createPartialMap(
templateFileExtension,
options.partialsDirectoryPath
);
return {
name: '@yoichiro/vite-plugin-handlebars',
transform(code, id) {
if (!id.endsWith(templateFileExtension)) {
return null;
}
const precompiled = Handlebars.precompile(code);
const transformed = transform(code, id, partialMap);
const transformed = transform(
code,
id,
templateFileExtension,
options.partialsDirectoryPath
);
return {
code: transformed,
map: null,
};
},
handleHotUpdate({ file, server }) {
if (options.partialsDirectoryPath === undefined) {
return;
}
if (!file.startsWith(options.partialsDirectoryPath)) {
return;
}
handleHotUpdate(file, server);
},
};
}
35 changes: 33 additions & 2 deletions src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import path from 'path';
import Handlebars from 'handlebars';
import fs from 'fs';
import { ViteDevServer } from 'vite';

let cachePartialMap: { [p: string]: string } | undefined = undefined;

export const decideTemplateFileExtension = (
templateFileExtension?: string
Expand Down Expand Up @@ -31,11 +34,30 @@ export const createPartialMap = (
);
};

const getPartialMap = (
templateFileExtension: string,
partialsDirectoryPath: string | undefined
): { [p: string]: string } => {
return (
cachePartialMap ??
createPartialMap(templateFileExtension, partialsDirectoryPath)
);
};

const resetPartialMap = (): void => {
cachePartialMap = undefined;
};

export const transform = (
code: string,
id: string,
partialMap: { [p: string]: string }
_id: string,
templateFileExtension: string,
partialsDirectoryPath: string | undefined
): string => {
const partialMap = getPartialMap(
templateFileExtension,
partialsDirectoryPath
);
const precompiled = Handlebars.precompile(code);
return `
import Handlebars from 'handlebars/runtime';
Expand Down Expand Up @@ -104,3 +126,12 @@ export const getAllHandlebarsFiles = (
});
return arrayOfFiles.sort();
};

export const handleHotUpdate = (file: string, server: ViteDevServer): void => {
resetPartialMap();
server.moduleGraph.invalidateAll();
server.ws.send({
type: 'full-reload',
path: '*',
});
};
1 change: 0 additions & 1 deletion test/internal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ describe('createPartialMap', () => {
});

test('should return empty map because directory path not specified', () => {
const partialDirectoryPath = path.resolve(__dirname, 'assets');
const actual = createPartialMap('.hbs', undefined);
expect(Object.keys(actual).length).toEqual(0);
});
Expand Down

0 comments on commit 22f0f43

Please sign in to comment.