Skip to content

Commit

Permalink
feat: support declarations comment
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhdaware committed Aug 7, 2022
1 parent 2b33109 commit 4ccc09c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/abell/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "abell",
"version": "1.0.0-alpha.65",
"version": "1.0.0-alpha.66",
"description": "Abell is a static-site-generator for JavaScript developers. Powered by Vite, It tries to stay close to fundamentals while providing a great DX",
"bin": "./dist/bin.js",
"main": "./dist/index.js",
Expand Down
21 changes: 21 additions & 0 deletions packages/abell/src/vite-plugin-abell/compiler/compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ describe('compile()', () => {
expect(out.declarationsBlock.text).toMatchInlineSnapshot('""');
});

test('should create first block as declaration block with declarations comment', () => {
const abellCode = `
{{
// declarations
const a = 3;
}}
<body>{{ a }}</body>
`;
const out = compile(abellCode, {
filepath: __dirname,
outputType: 'syntax-blocks'
});
expect(out.out.text.trim()).toMatchInlineSnapshot(
'"<body data-abell-heumYD>${e( a )}</body>"'
);
expect(out.declarationsBlock.text.trim()).toMatchInlineSnapshot(`
"// declarations
const a = 3;"
`);
});

test('should successfully compile with declarations', () => {
const abellCode = `
{{
Expand Down
6 changes: 5 additions & 1 deletion packages/abell/src/vite-plugin-abell/compiler/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export const isDeclarationBlock = (
blockCount: number,
blockContent: string
): boolean => {
if (blockCount < 2 && blockContent.includes('import ')) {
if (
blockCount < 2 &&
(blockContent.includes('import ') ||
blockContent.includes('// declaration'))
) {
return true;
}
return false;
Expand Down

0 comments on commit 4ccc09c

Please sign in to comment.