Skip to content

Commit

Permalink
feat: remove node dependencies from compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhdaware committed Nov 6, 2024
1 parent 29dc343 commit 00fc295
Show file tree
Hide file tree
Showing 8 changed files with 3,393 additions and 2,752 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-beta.4",
"version": "1.0.0-client.1",
"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
1 change: 0 additions & 1 deletion packages/abell/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ export { vitePluginAbell } from './vite-plugin-abell/index.js';
export { defineConfig, makeRoutesFromGlobImport } from './utils/api.js';

export { Route, AbellViteConfig } from './type-utils.js';
export { evaluateAbellBlock } from './utils/internal-utils.js';
7 changes: 2 additions & 5 deletions packages/abell/src/utils/__tests__/internal-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { test, describe, expect } from 'vitest';
import {
getURLFromFilePath,
evaluateAbellBlock,
getFilePathFromURL
} from '../internal-utils';
import { getURLFromFilePath, getFilePathFromURL } from '../internal-utils';
import { evaluateAbellBlock } from '../evaluateAbellBlock';
import { BASE_PATH, prefix } from './test-utils';

describe('getURLFromFilePath()', () => {
Expand Down
22 changes: 22 additions & 0 deletions packages/abell/src/utils/evaluateAbellBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Evaluates the abell block.
*
* Internally used to clean the output and return correct value.
*
*/
export const evaluateAbellBlock = (val: unknown): string | boolean | number => {
if (val === undefined || val === null) return ''; // never print undefined or null
if (typeof val === 'function') return evaluateAbellBlock(val()); // if function, execute the function
if (Array.isArray(val)) return val.join(''); // if array, join the array with ''
if (typeof val === 'object') return JSON.stringify(val); // if object, stringify object
if (
typeof val === 'string' ||
typeof val === 'boolean' || // string, boolean, number can take care of stringifying at the end
typeof val === 'number'
) {
return val;
}

// force stringification on rest
return String(val);
};
23 changes: 0 additions & 23 deletions packages/abell/src/utils/internal-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,26 +408,3 @@ export const addJStoBodyEnd = (

return htmlContent + jsLinks;
};

/**
* Evaluates the abell block.
*
* Internally used to clean the output and return correct value.
*
*/
export const evaluateAbellBlock = (val: unknown): string | boolean | number => {
if (val === undefined || val === null) return ''; // never print undefined or null
if (typeof val === 'function') return evaluateAbellBlock(val()); // if function, execute the function
if (Array.isArray(val)) return val.join(''); // if array, join the array with ''
if (typeof val === 'object') return JSON.stringify(val); // if object, stringify object
if (
typeof val === 'string' ||
typeof val === 'boolean' || // string, boolean, number can take care of stringifying at the end
typeof val === 'number'
) {
return val;
}

// force stringification on rest
return String(val);
};
70 changes: 34 additions & 36 deletions packages/abell/src/vite-plugin-abell/compiler/compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,20 @@ describe('compile()', () => {
});
expect(js.trim().replace(`\\\\test.abell`, '/test.abell'))
.toMatchInlineSnapshot(`
"import { default as _path } from 'path';
import { evaluateAbellBlock as e } from 'abell';
const __filename = \\"/test.abell\\";
const __dirname = _path.dirname(__filename);
const root = _path.relative(__dirname, \\"/\\")
export const html = (props = {}) => {
const Abell = { props, __filename, __dirname };
"import { evaluateAbellBlock as e } from 'abell/dist/utils/evaluateAbellBlock';
return \`
<nav>hello</nav>
\`
};
export default html;"
`);
const __filename = \\"/test.abell\\";
const __dirname = \\"/\\";
const root = \\"\\"
export const html = (props = {}) => {
const Abell = { props, __filename, __dirname };
return \`
<nav>hello</nav>
\`
};
export default html;"
`);
});

test('should create first block as declaration block with declarations comment', () => {
Expand Down Expand Up @@ -94,30 +93,29 @@ describe('compile()', () => {
});
expect(js.trim().replace(`\\\\test.abell`, '/test.abell'))
.toMatchInlineSnapshot(`
"import { default as _path } from 'path';
import { evaluateAbellBlock as e } from 'abell';
import x from './x';
const __filename = \\"/test.abell\\";
const __dirname = _path.dirname(__filename);
const root = _path.relative(__dirname, \\"/\\")
export const html = (props = {}) => {
const Abell = { props, __filename, __dirname };
/** @declarations */
const x = 999;
return \`
"import { evaluateAbellBlock as e } from 'abell/dist/utils/evaluateAbellBlock';
import x from './x';
const __filename = \\"/test.abell\\";
const __dirname = \\"/\\";
const root = \\"\\"
export const html = (props = {}) => {
const Abell = { props, __filename, __dirname };
/** @declarations */
const x = 999;
return \`
<b>\${e( 3 + 4 )}</b>
<nav>\${e( x * 2 )}</nav>
\`
};
export default html;"
`);
<b>\${e( 3 + 4 )}</b>
<nav>\${e( x * 2 )}</nav>
\`
};
export default html;"
`);
});

test('should successfully compile with imports', () => {
Expand Down
13 changes: 8 additions & 5 deletions packages/abell/src/vite-plugin-abell/compiler/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable max-len */
import path from 'path';
import { AbstractSyntaxArrayType } from '../../type-utils.js';
import tokenize from './generic-tokenizer.js';
import { getScopedHTML } from './scope-css/index.js';
Expand Down Expand Up @@ -76,13 +77,15 @@ export function compile(
};
}

const __filename = options.filepath;
const __dirname = path.dirname(options.filepath);

const jsOut = `
import { default as _path } from 'path';
import { evaluateAbellBlock as e } from 'abell';
import { evaluateAbellBlock as e } from 'abell/dist/utils/evaluateAbellBlock';
${importBlock.text}
const __filename = ${JSON.stringify(options.filepath)};
const __dirname = _path.dirname(__filename);
const root = _path.relative(__dirname, ${JSON.stringify(options.cwd)})
const __filename = ${JSON.stringify(__filename)};
const __dirname = ${JSON.stringify(__dirname)};
const root = ${JSON.stringify(path.relative(__dirname, options.cwd ?? ''))}
export const html = (props = {}) => {
const Abell = { props, __filename, __dirname };
${declarationBlocks.text}
Expand Down
Loading

0 comments on commit 00fc295

Please sign in to comment.